From b74f90eeacc5341e39f30d431650c6f18c33ac29 Mon Sep 17 00:00:00 2001 From: "ddrinan%netscape.com" Date: Wed, 12 Apr 2000 00:15:15 +0000 Subject: [PATCH] Initial checkin of the PSM server --- security/psm/server/macglue.cp | 97 +++++++++ security/psm/server/macshell.cp | 278 +++++++++++++++++++++++++ security/psm/server/nlslayer.cpp | 337 +++++++++++++++++++++++++++++++ 3 files changed, 712 insertions(+) create mode 100644 security/psm/server/macglue.cp create mode 100644 security/psm/server/macshell.cp create mode 100644 security/psm/server/nlslayer.cpp diff --git a/security/psm/server/macglue.cp b/security/psm/server/macglue.cp new file mode 100644 index 00000000000..c7a210da5dc --- /dev/null +++ b/security/psm/server/macglue.cp @@ -0,0 +1,97 @@ +/* + * The contents of this file are subject to the Mozilla Public + * License Version 1.1 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS + * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or + * implied. See the License for the specific language governing + * rights and limitations under the License. + * + * The Original Code is the Netscape security libraries. + * + * The Initial Developer of the Original Code is Netscape + * Communications Corporation. Portions created by Netscape are + * Copyright (C) 1994-2000 Netscape Communications Corporation. All + * Rights Reserved. + * + * Contributor(s): + * + * Alternatively, the contents of this file may be used under the + * terms of the GNU General Public License Version 2 or later (the + * "GPL"), in which case the provisions of the GPL are applicable + * instead of those above. If you wish to allow use of your + * version of this file only under the terms of the GPL and not to + * allow others to use your version of this file under the MPL, + * indicate your decision by deleting the provisions above and + * replace them with the notice and other provisions required by + * the GPL. If you do not delete the provisions above, a recipient + * may use your version of this file under either the MPL or the + * GPL. + */ +#include "TArray.h" +#include "TArrayIterator.h" +#include "prthread.h" +#include "prlog.h" +#include "prmem.h" + + +typedef struct SSMThreadPrivateData +{ + PRThread *thd; + PRUintn indx; +} SSMThreadPrivateData; + +TArray* myThreads = NULL; +PRUintn thdIndex = 0; + +void +SSM_MacDelistThread(void *priv) +{ + // remove this lump from the list of active threads + myThreads->Remove((PRThread *) priv); +} + +void +SSM_KillAllThreads(void) +{ + int i; + SSMThreadPrivateData *priv = NULL; + PRStatus rv; + + if (myThreads != nil) { + TArrayIterator iterator(*myThreads); + PRThread *thd; + while (iterator.Next(thd)) { + rv = PR_Interrupt(thd); // thread data dtor will deallocate (*priv) + PR_ASSERT(rv == PR_SUCCESS); + } + } +} + +PRThread * +SSM_CreateAndRegisterThread(PRThreadType type, + void (*start)(void *arg), + void *arg, + PRThreadPriority priority, + PRThreadScope scope, + PRThreadState state, + PRUint32 stackSize) +{ + PRThread *thd; + + if (!myThreads) + { + PR_NewThreadPrivateIndex(&thdIndex, SSM_MacDelistThread); + myThreads = new TArray; + } + + thd = PR_CreateThread(type, start, arg, priority, scope, state, stackSize); + if (thd) + { + /* Add this thread to our list of threads */ + myThreads->AddItem(thd); + } +} + diff --git a/security/psm/server/macshell.cp b/security/psm/server/macshell.cp new file mode 100644 index 00000000000..7e3ec458ea1 --- /dev/null +++ b/security/psm/server/macshell.cp @@ -0,0 +1,278 @@ +/* + * The contents of this file are subject to the Mozilla Public + * License Version 1.1 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS + * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or + * implied. See the License for the specific language governing + * rights and limitations under the License. + * + * The Original Code is the Netscape security libraries. + * + * The Initial Developer of the Original Code is Netscape + * Communications Corporation. Portions created by Netscape are + * Copyright (C) 1994-2000 Netscape Communications Corporation. All + * Rights Reserved. + * + * Contributor(s): + * + * Alternatively, the contents of this file may be used under the + * terms of the GNU General Public License Version 2 or later (the + * "GPL"), in which case the provisions of the GPL are applicable + * instead of those above. If you wish to allow use of your + * version of this file only under the terms of the GPL and not to + * allow others to use your version of this file under the MPL, + * indicate your decision by deleting the provisions above and + * replace them with the notice and other provisions required by + * the GPL. If you do not delete the provisions above, a recipient + * may use your version of this file under either the MPL or the + * GPL. + */ +// =========================================================================== +// CCompleteApp.cp ©1994-1998 Metrowerks Inc. All rights reserved. +// =========================================================================== +// This file contains the starter code for a complete PowerPlant project that +// includes precompiled headers and both debug and release targets. + +#include "macshell.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include "macstdlibextras.h" + +#include "prthread.h" +#include "prinit.h" +#include "prlog.h" + +#include "serv.h" + +// put declarations for resource ids (ResIDTs) here + +const PP_PowerPlant::ResIDT wind_SampleWindow = 128; // EXAMPLE, create a new window + +// +// NSPR repeater +// + +class CNSPRRepeater : public LPeriodical +{ +public: + CNSPRRepeater(); + virtual void SpendTime(const EventRecord& inEvent); +}; + +CNSPRRepeater::CNSPRRepeater(void) + : LPeriodical() +{ +} + +CNSPRRepeater *gNSPRRepeater; + +PRBool gShouldQuit = PR_FALSE; + +void +CNSPRRepeater::SpendTime(const EventRecord& inEvent) +{ + /* + Can't just exit on the Mac, because otherwise the NSPR threads + will keep spinning forever. We have to send the app a + Quit command so that all the threads will shut down in an orderly + way. + + On top of that, NSPR threads can't send a Quit Apple Event, because + that causes the threads to close improperly. So, we keep a flag and allow + threads to raise it. We check for that flag here. + */ + + if (gShouldQuit) + { + if (gNSPRRepeater) + gNSPRRepeater->StopIdling(); + // PR_Interrupt all threads (other than the primordial one) + SSM_KillAllThreads(); + gTheApp->SendAEQuit(); // will cause this repeater to stop repeating + } + else + PR_Sleep(PR_INTERVAL_NO_WAIT); // give time to NSPR threads +} + +// =========================================================================== +// ¥ Main Program +// =========================================================================== + +PRIntn MacPSMMain(PRIntn argc, char **argv); + +int main() +{ + char *fakeArgv[] = { NULL, NULL }; +#ifndef NDEBUG + SetDebugThrow_(PP_PowerPlant::debugAction_Alert); // Set Debugging options + SetDebugSignal_(PP_PowerPlant::debugAction_Alert); +#endif + + PP_PowerPlant::InitializeHeap(3); // Initialize Memory Manager + // Parameter is number of Master Pointer + // blocks to allocate + +#ifdef DEBUG + InitializeSIOUX(false); // prevent us from getting double menus, etc. +#endif + PP_PowerPlant::UQDGlobals::InitializeToolbox(&qd); // Initialize standard Toolbox managers + + new PP_PowerPlant::LGrowZone(20000); // Install a GrowZone function to catch + // low memory situations. + PR_Initialize(MacPSMMain, 0, fakeArgv, 0); + return 0; +} + +CBasicApp *gTheApp; + +PRIntn MacPSMMain(PRIntn argc, char **argv) +{ + gNSPRRepeater = new CNSPRRepeater; + PR_ASSERT(gNSPRRepeater != NULL); + gNSPRRepeater->StartIdling(); + + CBasicApp theApp; // create instance of your application + gTheApp = &theApp; + + theApp.Run(); + return 0; +} + +// --------------------------------------------------------------------------- +// ¥ CBasicApp +// --------------------------------------------------------------------------- +// Constructor + +CBasicApp::CBasicApp() +{ +#ifndef NDEBUG + PP_PowerPlant::RegisterAllPPClasses(); // Register functions to create core +#else // PowerPlant classes + RegisterClass_(PP_PowerPlant::LWindow); + RegisterClass_(PP_PowerPlant::LCaption); +#endif +} + + +// --------------------------------------------------------------------------- +// ¥ ~CBasicApp +// --------------------------------------------------------------------------- +// Destructor + +CBasicApp::~CBasicApp() +{ +} + +// --------------------------------------------------------------------------- +// ¥ StartUp +// --------------------------------------------------------------------------- +// This method lets you do something when the application starts up +// without a document. For example, you could issue your own new command. + +extern void RunMacPSM(void *); + +void +CBasicApp::StartUp() +{ + // ObeyCommand(PP_PowerPlant::cmd_New, nil); // EXAMPLE, create a new window + + /* + The Unix/Win32 main function (which we call RunMacPSM) blocks on + PR_Accept(), listening for control connections. Since we cannot + block from the main thread, we have to spin a separate NSPR thread + for RunMacPSM. + */ + SSM_CreateAndRegisterThread(PR_USER_THREAD, + RunMacPSM, + NULL, + PR_PRIORITY_NORMAL, + PR_LOCAL_THREAD, + PR_UNJOINABLE_THREAD, 0); +} + +// --------------------------------------------------------------------------- +// ¥ ObeyCommand +// --------------------------------------------------------------------------- +// This method lets the application respond to commands like Menu commands + +Boolean +CBasicApp::ObeyCommand( + PP_PowerPlant::CommandT inCommand, + void *ioParam) +{ + Boolean cmdHandled = true; + + switch (inCommand) { + + // Handle command messages (defined in PP_Messages.h). + case PP_PowerPlant::cmd_New: +#if 0 + PP_PowerPlant::LWindow *theWindow = + PP_PowerPlant::LWindow::CreateWindow(wind_SampleWindow, this); + ThrowIfNil_(theWindow); + + // LWindow is not initially visible in PPob resource + theWindow->Show(); + break; +#endif + + case PP_PowerPlant::cmd_Quit: + if (!gShouldQuit) // do this only if repeater hasn't done it for us + { + if (gNSPRRepeater) + gNSPRRepeater->StopIdling(); + // PR_Interrupt all threads (other than the primordial one) + SSM_KillAllThreads(); + } + // fall through to default Quit behavior + + // Any that you don't handle, such as cmd_About and cmd_Quit, + // will be passed up to LApplication + default: + cmdHandled = PP_PowerPlant::LApplication::ObeyCommand(inCommand, ioParam); + break; + } + + return cmdHandled; +} + +// --------------------------------------------------------------------------- +// ¥ FindCommandStatus +// --------------------------------------------------------------------------- +// This method enables menu items. + +void +CBasicApp::FindCommandStatus( + PP_PowerPlant::CommandT inCommand, + Boolean &outEnabled, + Boolean &outUsesMark, + PP_PowerPlant::Char16 &outMark, + Str255 outName) +{ + + switch (inCommand) { + + // Return menu item status according to command messages. + case PP_PowerPlant::cmd_New: + case PP_PowerPlant::cmd_Quit: + outEnabled = true; + break; + + // Any that you don't handle, such as cmd_About and cmd_Quit, + // will be passed up to LApplication + default: + PP_PowerPlant::LApplication::FindCommandStatus(inCommand, outEnabled, + outUsesMark, outMark, outName); + break; + } +} diff --git a/security/psm/server/nlslayer.cpp b/security/psm/server/nlslayer.cpp new file mode 100644 index 00000000000..7568b863ebe --- /dev/null +++ b/security/psm/server/nlslayer.cpp @@ -0,0 +1,337 @@ +/* + * The contents of this file are subject to the Mozilla Public + * License Version 1.1 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS + * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or + * implied. See the License for the specific language governing + * rights and limitations under the License. + * + * The Original Code is the Netscape security libraries. + * + * The Initial Developer of the Original Code is Netscape + * Communications Corporation. Portions created by Netscape are + * Copyright (C) 1994-2000 Netscape Communications Corporation. All + * Rights Reserved. + * + * Contributor(s): + * + * Alternatively, the contents of this file may be used under the + * terms of the GNU General Public License Version 2 or later (the + * "GPL"), in which case the provisions of the GPL are applicable + * instead of those above. If you wish to allow use of your + * version of this file only under the terms of the GPL and not to + * allow others to use your version of this file under the MPL, + * indicate your decision by deleting the provisions above and + * replace them with the notice and other provisions required by + * the GPL. If you do not delete the provisions above, a recipient + * may use your version of this file under either the MPL or the + * GPL. + */ + +#include "nspr.h" +#include "nscore.h" +#include "nsString.h" +#include "nsIServiceManager.h" +#include "nsIStringBundle.h" +#include "nsIDateTimeFormat.h" +#include "nsDateTimeFormatCID.h" +#include "nsICharsetConverterManager.h" +extern "C" { +#include "nlslayer.h" +} + +static NS_DEFINE_IID(kStringBundleServiceCID, NS_STRINGBUNDLESERVICE_CID); +static NS_DEFINE_IID(kIStringBundleServiceIID, NS_ISTRINGBUNDLESERVICE_IID); +static NS_DEFINE_CID(kDateTimeCID, NS_DATETIMEFORMAT_CID); +static NS_DEFINE_CID(kCharsetConverterManagerCID, NS_ICHARSETCONVERTERMANAGER_CID); + +static nsIUnicodeEncoder *encoderUTF8 = nsnull; +static nsIUnicodeDecoder *decoderUTF8 = nsnull; + +static nsIUnicodeEncoder *encoderASCII = nsnull; +static nsIUnicodeDecoder *decoderASCII = nsnull; + +#define TEXT_BUNDLE "resource:/ui/psm_text.properties" +#define UI_BUNDLE "resource:/ui/psm_ui.properties" +#define BIN_BUNDLE "resource:/ui/psm_bin.properties" +#define DOC_BUNDLE "resource:/ui/psm_doc.properties" + +extern "C" { +static nsIStringBundle* nlsCreateBundle(char* bundleURL); +static char* nlsGetUTF8StringFromBundle(nsIStringBundle *bundle, const char *name); +static nsIStringBundle * bundles[4] = {NULL, NULL, NULL, NULL}; +} + + +extern "C" PRBool nlsInit() +{ + nsICharsetConverterManager *ccm = nsnull; + nsAutoString charsetUTF8("UTF-8"); + nsAutoString charsetASCII("ISO-8859-1"); + PRBool ret = PR_FALSE; + nsresult res; + + res = NS_InitXPCOM(NULL, NULL); + NS_ASSERTION( NS_SUCCEEDED(res), "NS_InitXPCOM failed" ); + + // Register components + res = nsComponentManager::AutoRegister(nsIComponentManager::NS_Startup, + NULL /* default */); + if(NS_FAILED(res)) { + goto loser; + } + + // Create the bundles + bundles[0] = nlsCreateBundle(TEXT_BUNDLE); + bundles[1] = nlsCreateBundle(UI_BUNDLE); + bundles[2] = nlsCreateBundle(BIN_BUNDLE); + bundles[3] = nlsCreateBundle(DOC_BUNDLE); + + // Create a unicode encoder and decoder + res = nsServiceManager::GetService(kCharsetConverterManagerCID, + NS_GET_IID(nsICharsetConverterManager), + (nsISupports**)&ccm); + + if (NS_FAILED(res) || (nsnull == ccm)) { + goto loser; + } + + res = ccm->GetUnicodeEncoder(&charsetUTF8, &encoderUTF8); + if (NS_FAILED(res) || (nsnull == encoderUTF8)) { + goto loser; + } + + res = ccm->GetUnicodeDecoder(&charsetUTF8, &decoderUTF8); + if (NS_FAILED(res) || (nsnull == decoderUTF8)) { + goto loser; + } + + res = ccm->GetUnicodeEncoder(&charsetASCII, &encoderASCII); + if (NS_FAILED(res) || (nsnull == encoderASCII)) { + goto loser; + } + + res = ccm->GetUnicodeDecoder(&charsetASCII, &decoderASCII); + if (NS_FAILED(res) || (nsnull == decoderASCII)) { + goto loser; + } + + ret = PR_TRUE; + goto done; +loser: + NS_IF_RELEASE(bundles[0]); + NS_IF_RELEASE(bundles[1]); + NS_IF_RELEASE(bundles[2]); + NS_IF_RELEASE(bundles[3]); + NS_IF_RELEASE(encoderUTF8); + NS_IF_RELEASE(decoderUTF8); + NS_IF_RELEASE(encoderASCII); + NS_IF_RELEASE(decoderASCII); +done: + return ret; +} + +extern "C" nsIStringBundle* nlsCreateBundle(char* bundleURL) +{ + nsresult ret; + nsIStringBundleService *service = nsnull; + nsIStringBundle* bundle = nsnull; + nsILocale *locale = nsnull; + + // Get the string bundle service + ret = nsServiceManager::GetService(kStringBundleServiceCID, + kIStringBundleServiceIID, + (nsISupports**)&service); + if (NS_FAILED(ret)) { + return NULL; + } + + // Create the bundle + ret = service->CreateBundle(bundleURL, locale, &bundle); + if (NS_FAILED(ret)) { + return NULL; + } + + NS_IF_RELEASE(service); + + return bundle; +} + +extern "C" char* nlsGetUTF8StringFromBundle(nsIStringBundle *bundle, const char *name) +{ + nsresult ret; + + nsString key(name); + nsString value; + PRUnichar * p = NULL; + ret = bundle->GetStringFromName(key.GetUnicode(), &p); + if (NS_FAILED(ret)) { + return NULL; + } + value = p; + + // XXX This is a hack to get cr and lf chars in string. + // See bug# 21418 + value.ReplaceSubstring("", "\r"); + value.ReplaceSubstring("", "\n"); + return value.ToNewUTF8String(); +} + +extern "C" char* nlsGetUTF8String(const char *name) +{ + int i; + char *value = NULL; + + for (i=0;i<4;i++) { + value = nlsGetUTF8StringFromBundle(bundles[i], name); + if (value) { + break; + } + } + return value; +} + +extern "C" void * nlsNewDateFormat() +{ + nsIComponentManager *comMgr; + nsIDateTimeFormat *dateTimeFormat = nsnull; + nsresult rv; + + rv = NS_GetGlobalComponentManager(&comMgr); + if (NS_FAILED(rv)) { + return NULL; + } + rv = comMgr->CreateInstance(kDateTimeCID, nsnull, NS_GET_IID(nsIDateTimeFormat), (void**)&dateTimeFormat); + if (NS_FAILED(rv)) { + return NULL; + } + return dateTimeFormat; +} + +extern "C" void nlsFreeDateFormat(void * p) +{ + nsIDateTimeFormat *dateTimeFormat = (nsIDateTimeFormat*)p; + + NS_IF_RELEASE(dateTimeFormat); +} + +extern "C" char * nslPRTimeToUTF8String(void* p, PRInt64 t) +{ + nsIDateTimeFormat *dateTimeFormat = (nsIDateTimeFormat*)p; + nsString dateTime; + nsresult rv; + + rv = dateTimeFormat->FormatPRTime(nsnull, kDateFormatShort, kTimeFormatNoSeconds, PRTime(t), dateTime); + if (NS_FAILED(rv)) { + return nsnull; + } + + return dateTime.ToNewUTF8String(); +} + +extern "C" PRBool nlsUnicodeToUTF8(unsigned char * inBuf, unsigned int inBufBytes, + unsigned char * outBuf, unsigned int maxOutBufLen, + unsigned int * outBufLen) +{ + PRBool ret = PR_FALSE; + nsIUnicodeEncoder *enc = encoderUTF8; + PRInt32 dstLength; + nsresult res; + + res = enc->GetMaxLength((const PRUnichar *)inBuf, inBufBytes, &dstLength); + if (NS_FAILED(res) || (dstLength > maxOutBufLen)) { + goto loser; + } + + res = enc->Convert((const PRUnichar *)inBuf, (PRInt32*)&inBufBytes, (char*)outBuf, &dstLength); + if (NS_FAILED(res)) { + goto loser; + } + + /* outBuf[dstLength] = '\0';*/ + *outBufLen = dstLength; + ret = PR_TRUE; + +loser: + return ret; +} + +extern "C" PRBool nlsUTF8ToUnicode(unsigned char * inBuf, unsigned int inBufBytes, + unsigned char * outBuf, unsigned int maxOutBufLen, + unsigned int * outBufLen) +{ + PRBool ret = PR_FALSE; + PRInt32 dstLength; + nsIUnicodeDecoder *dec = decoderUTF8; + nsresult res; + + res = dec->GetMaxLength((const char*)inBuf, inBufBytes, &dstLength); + if (NS_FAILED(res) || (dstLength > maxOutBufLen)) { + goto loser; + } + + res = dec->Convert((const char *)inBuf, (PRInt32*)&inBufBytes, (PRUnichar*)outBuf, &dstLength); + if (NS_FAILED(res)) { + goto loser; + } + + ret = PR_TRUE; + +loser: + return ret; +} + +extern "C" PRBool nlsUnicodeToASCII(unsigned char * inBuf, unsigned int inBufBytes, + unsigned char * outBuf, unsigned int maxOutBufLen, + unsigned int * outBufLen) +{ + PRBool ret = PR_FALSE; + nsIUnicodeEncoder *enc = encoderASCII; + PRInt32 dstLength; + nsresult res; + + res = enc->GetMaxLength((const PRUnichar *)inBuf, inBufBytes, &dstLength); + if (NS_FAILED(res) || (dstLength > maxOutBufLen)) { + goto loser; + } + + res = enc->Convert((const PRUnichar *)inBuf, (PRInt32*)&inBufBytes, (char*)outBuf, &dstLength); + if (NS_FAILED(res)) { + goto loser; + } + + /* outBuf[dstLength] = '\0';*/ + *outBufLen = dstLength; + ret = PR_TRUE; + +loser: + return ret; +} + +extern "C" PRBool nlsASCIIToUnicode(unsigned char * inBuf, unsigned int inBufBytes, + unsigned char * outBuf, unsigned int maxOutBufLen, + unsigned int * outBufLen) +{ + PRBool ret = PR_FALSE; + PRInt32 dstLength; + nsIUnicodeDecoder *dec = decoderASCII; + nsresult res; + + res = dec->GetMaxLength((const char*)inBuf, inBufBytes, &dstLength); + if (NS_FAILED(res) || (dstLength > maxOutBufLen)) { + goto loser; + } + + res = dec->Convert((const char *)inBuf, (PRInt32*)&inBufBytes, (PRUnichar*)outBuf, &dstLength); + if (NS_FAILED(res)) { + goto loser; + } + + ret = PR_TRUE; + +loser: + return ret; +}