From 7ff8664132f73d3bd52fda5bb17b7b7224440e8d Mon Sep 17 00:00:00 2001 From: "darin%netscape.com" Date: Thu, 7 Nov 2002 07:34:54 +0000 Subject: [PATCH] 1) cleanup ipcService/ipcTransport platform factoring 2) add ipc-startup-category 3) add ipc-startup and ipc-shutdown observer topics --- modules/ipc/daemon/ipcModuleUtil.h | 37 ++++++++++++++ modules/ipc/public/ipcIService.idl | 12 +++-- modules/ipc/src/ipcService.cpp | 74 ++++++++++++---------------- modules/ipc/src/ipcService.h | 1 - modules/ipc/src/ipcTransport.cpp | 10 ++-- modules/ipc/src/ipcTransport.h | 4 +- modules/ipc/src/ipcTransportUnix.cpp | 33 ++++++++++++- 7 files changed, 114 insertions(+), 57 deletions(-) diff --git a/modules/ipc/daemon/ipcModuleUtil.h b/modules/ipc/daemon/ipcModuleUtil.h index 6e8eed4e0d71..b5d1c006f886 100644 --- a/modules/ipc/daemon/ipcModuleUtil.h +++ b/modules/ipc/daemon/ipcModuleUtil.h @@ -1,3 +1,40 @@ +/* ***** BEGIN LICENSE BLOCK ***** + * Version: MPL 1.1/GPL 2.0/LGPL 2.1 + * + * 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 Mozilla IPC. + * + * The Initial Developer of the Original Code is + * Netscape Communications Corporation. + * Portions created by the Initial Developer are Copyright (C) 2002 + * the Initial Developer. All Rights Reserved. + * + * Contributor(s): + * Darin Fisher + * + * Alternatively, the contents of this file may be used under the terms of + * either the GNU General Public License Version 2 or later (the "GPL"), or + * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), + * in which case the provisions of the GPL or the LGPL are applicable instead + * of those above. If you wish to allow use of your version of this file only + * under the terms of either the GPL or the LGPL, and not to allow others to + * use your version of this file under the terms of the MPL, indicate your + * decision by deleting the provisions above and replace them with the notice + * and other provisions required by the GPL or the LGPL. If you do not delete + * the provisions above, a recipient may use your version of this file under + * the terms of any one of the MPL, the GPL or the LGPL. + * + * ***** END LICENSE BLOCK ***** */ + #ifndef ipcModuleUtil_h__ #define ipcModuleUtil_h__ diff --git a/modules/ipc/public/ipcIService.idl b/modules/ipc/public/ipcIService.idl index b23fc7346406..594037f16bfc 100644 --- a/modules/ipc/public/ipcIService.idl +++ b/modules/ipc/public/ipcIService.idl @@ -41,6 +41,12 @@ interface nsISimpleEnumerator; interface ipcIMessageObserver; interface ipcIClientObserver; +%{C++ +#define IPC_SERVICE_STARTUP_CATEGORY "ipc-startup-category" +#define IPC_SERVICE_STARTUP_TOPIC "ipc-startup" +#define IPC_SERVICE_SHUTDOWN_TOPIC "ipc-shutdown" +%} + /** * the IPC service provides the means to communicate with an external IPC * daemon and/or other mozilla-based applications on the same physical system. @@ -50,10 +56,8 @@ interface ipcIClientObserver; * at application startup, the IPC service will attempt to establish a * connection with the IPC daemon. the IPC daemon will be automatically * started if necessary. when a connection has been established, the IPC - * service will broadcast an "ipc-startup" notification using the observer - * service. - * - * XXX startup category + * service will enumerate the "ipc-startup-category" and broadcast an + * "ipc-startup" notification using the observer service. * * when the connection to the IPC daemon is closed, an "ipc-shutdown" * notification will be broadcast. diff --git a/modules/ipc/src/ipcService.cpp b/modules/ipc/src/ipcService.cpp index 75a2050c40fa..1c1d4fafec2f 100644 --- a/modules/ipc/src/ipcService.cpp +++ b/modules/ipc/src/ipcService.cpp @@ -35,20 +35,14 @@ * * ***** END LICENSE BLOCK ***** */ -#ifdef XP_UNIX -#include -#include -#include -#endif - #include "plstr.h" -#include "nsIFile.h" #include "nsIServiceManager.h" -#include "nsDirectoryServiceUtils.h" -#include "nsDirectoryServiceDefs.h" #include "nsIPrefService.h" #include "nsIPrefBranch.h" +#include "nsIObserverService.h" +#include "nsICategoryManager.h" +#include "nsCategoryManagerUtils.h" #include "ipcConfig.h" #include "ipcLog.h" @@ -153,14 +147,7 @@ ipcService::Init() if (appName.IsEmpty()) appName = NS_LITERAL_CSTRING("test-app"); - // get socket path from directory service - nsCAutoString socketPath; -#ifdef XP_UNIX - if (NS_FAILED(GetSocketPath(socketPath))) - socketPath = NS_LITERAL_CSTRING(IPC_DEFAULT_SOCKET_PATH); -#endif - - rv = mTransport->Init(appName, socketPath, this); + rv = mTransport->Init(appName, this); if (NS_FAILED(rv)) return rv; return NS_OK; @@ -199,28 +186,6 @@ ipcService::HandleQueryResult(const ipcMessage *rawMsg, PRBool succeeded) mQueryQ.DeleteFirst(); } -#ifdef XP_UNIX -nsresult -ipcService::GetSocketPath(nsACString &socketPath) -{ - nsCOMPtr file; - NS_GetSpecialDirectory(NS_OS_TEMP_DIR, getter_AddRefs(file)); - if (!file) - return NS_ERROR_FAILURE; - // XXX may want to use getpwuid_r when available - struct passwd *pw = getpwuid(geteuid()); - if (!pw) - return NS_ERROR_UNEXPECTED; - nsCAutoString leaf; - leaf = NS_LITERAL_CSTRING(".mozilla-ipc-") - + nsDependentCString(pw->pw_name); - file->AppendNative(leaf); - file->AppendNative(NS_LITERAL_CSTRING("ipcd")); - file->GetNativePath(socketPath); - return NS_OK; -} -#endif - //----------------------------------------------------------------------------- // interface impl //----------------------------------------------------------------------------- @@ -373,16 +338,39 @@ void ipcService::OnConnectionEstablished(PRUint32 clientID) { mClientID = clientID; + + // + // enumerate ipc startup category... + // + NS_CreateServicesFromCategory(IPC_SERVICE_STARTUP_CATEGORY, + NS_STATIC_CAST(nsISupports *, this), + IPC_SERVICE_STARTUP_TOPIC); } void ipcService::OnConnectionLost() { - // - // XXX error out any pending queries - // - mClientID = 0; + + // + // error out any pending queries + // + while (mQueryQ.First()) { + ipcClientQuery *query = mQueryQ.First(); + query->mObserver->OnClientStatus(query->mReqToken, + ipcIClientObserver::CLIENT_DOWN, + nsnull); + mQueryQ.DeleteFirst(); + } + + // + // broadcast ipc shutdown... + // + nsCOMPtr observ( + do_GetService("@mozilla.org/observer-service;1")); + if (observ) + observ->NotifyObservers(NS_STATIC_CAST(nsISupports *, this), + IPC_SERVICE_SHUTDOWN_TOPIC, nsnull); } void diff --git a/modules/ipc/src/ipcService.h b/modules/ipc/src/ipcService.h index 46b35e61e42d..84150aac5a8a 100644 --- a/modules/ipc/src/ipcService.h +++ b/modules/ipc/src/ipcService.h @@ -112,7 +112,6 @@ public: private: nsresult ErrorAccordingToIPCM(PRUint32 err); void HandleQueryResult(const ipcMessage *, PRBool succeeded); - nsresult GetSocketPath(nsACString &); // ipcTransportObserver: void OnConnectionEstablished(PRUint32 clientID); diff --git a/modules/ipc/src/ipcTransport.cpp b/modules/ipc/src/ipcTransport.cpp index c4ba52eed3b3..e0f7d2b6c635 100644 --- a/modules/ipc/src/ipcTransport.cpp +++ b/modules/ipc/src/ipcTransport.cpp @@ -60,18 +60,18 @@ nsresult ipcTransport::Init(const nsACString &appName, - const nsACString &socketPath, ipcTransportObserver *obs) { mAppName = appName; mObserver = obs; -#ifdef XP_UNIX - InitUnix(socketPath); -#endif - LOG(("ipcTransport::Init [app-name=%s]\n", mAppName.get())); +#ifdef XP_UNIX + nsresult rv = InitUnix(); + if (NS_FAILED(rv)) return rv; +#endif + // XXX service should be the observer nsCOMPtr observ(do_GetService("@mozilla.org/observer-service;1")); if (observ) { diff --git a/modules/ipc/src/ipcTransport.h b/modules/ipc/src/ipcTransport.h index 745ac062e9ed..1110af5f1f2f 100644 --- a/modules/ipc/src/ipcTransport.h +++ b/modules/ipc/src/ipcTransport.h @@ -91,7 +91,6 @@ public: virtual ~ipcTransport() {} nsresult Init(const nsACString &appName, - const nsACString &socketPath, // ignored if not UNIX ipcTransportObserver *observer); nsresult Shutdown(); @@ -140,8 +139,9 @@ private: // // unix specific helpers // - nsresult InitUnix(const nsACString &socketPath); + nsresult InitUnix(); nsresult CreateTransport(); + nsresult GetSocketPath(nsACString &); public: // diff --git a/modules/ipc/src/ipcTransportUnix.cpp b/modules/ipc/src/ipcTransportUnix.cpp index 82460d022619..b8a60dc3a4a5 100644 --- a/modules/ipc/src/ipcTransportUnix.cpp +++ b/modules/ipc/src/ipcTransportUnix.cpp @@ -35,7 +35,14 @@ * * ***** END LICENSE BLOCK ***** */ +#include +#include +#include + +#include "nsIFile.h" #include "nsIServiceManager.h" +#include "nsDirectoryServiceUtils.h" +#include "nsDirectoryServiceDefs.h" #include "ipcSocketProviderUnix.h" #include "nsISocketTransportService.h" #include "nsIInputStream.h" @@ -56,9 +63,11 @@ static NS_DEFINE_CID(kSocketTransportServiceCID, NS_SOCKETTRANSPORTSERVICE_CID); //----------------------------------------------------------------------------- nsresult -ipcTransport::InitUnix(const nsACString &socketPath) +ipcTransport::InitUnix() { - mSocketPath = socketPath; + nsresult rv = GetSocketPath(mSocketPath); + if (NS_FAILED(rv)) return rv; + ipcSocketProviderUnix::SetSocketPath(socketPath); return NS_OK; } @@ -184,6 +193,26 @@ ipcTransport::CreateTransport() return rv; } +nsresult +ipcTransport::GetSocketPath(nsACString &socketPath) +{ + nsCOMPtr file; + NS_GetSpecialDirectory(NS_OS_TEMP_DIR, getter_AddRefs(file)); + if (!file) + return NS_ERROR_FAILURE; + // XXX may want to use getpwuid_r when available + struct passwd *pw = getpwuid(geteuid()); + if (!pw) + return NS_ERROR_UNEXPECTED; + nsCAutoString leaf; + leaf = NS_LITERAL_CSTRING(".mozilla-ipc-") + + nsDependentCString(pw->pw_name); + file->AppendNative(leaf); + file->AppendNative(NS_LITERAL_CSTRING("ipcd")); + file->GetNativePath(socketPath); + return NS_OK; +} + //----------------------------------------------------------------------------- // ipcSendQueue //-----------------------------------------------------------------------------