Bug 507218 - Expose the process type (chrome/content) to client code via XRE_GetProcessType and nsIXULRuntime, r=bent
authorRobin Bate Beorop <moz@shorestreet.com>
Wed, 19 Aug 2009 13:09:51 -0400
changeset 35860 d906997755b8574a0bd97bb864918977490cc1fc
parent 35859 2d5c46725f895823d6066be414387e9668cba3ec
child 35861 4820aee8bc0265a24fa50ccd10eaf3dc61cdcd9a
push idunknown
push userunknown
push dateunknown
reviewersbent
bugs507218
milestone1.9.2a1pre
Bug 507218 - Expose the process type (chrome/content) to client code via XRE_GetProcessType and nsIXULRuntime, r=bent
dom/ipc/ContentProcessParent.cpp
dom/plugins/PluginProcessParent.cpp
ipc/app/MozillaRuntimeMain.cpp
ipc/glue/GeckoChildProcessHost.cpp
ipc/glue/GeckoChildProcessHost.h
ipc/test-harness/TestProcessParent.cpp
toolkit/library/dlldeps-xul.cpp
toolkit/xre/nsAppRunner.cpp
toolkit/xre/nsEmbedFunctions.cpp
xpcom/build/nsXULAppAPI.h
xpcom/system/nsIXULRuntime.idl
--- a/dom/ipc/ContentProcessParent.cpp
+++ b/dom/ipc/ContentProcessParent.cpp
@@ -29,17 +29,17 @@ ContentProcessParent::CreateTab(const Ma
 
 TestShellParent*
 ContentProcessParent::CreateTestShell()
 {
   return static_cast<TestShellParent*>(SendTestShellConstructor());
 }
 
 ContentProcessParent::ContentProcessParent()
-    : mSubprocess(GeckoChildProcess_Tab)
+    : mSubprocess(GeckoProcessType_Content)
 {
     // TODO: async launching!
     mSubprocess.SyncLaunch();
     Open(mSubprocess.GetChannel());
 }
 
 ContentProcessParent::~ContentProcessParent()
 {
--- a/dom/plugins/PluginProcessParent.cpp
+++ b/dom/plugins/PluginProcessParent.cpp
@@ -43,17 +43,17 @@
 
 using mozilla::ipc::GeckoChildProcessHost;
 
 namespace mozilla {
 namespace plugins {
 
 
 PluginProcessParent::PluginProcessParent(const std::string& aPluginFilePath) :
-    GeckoChildProcessHost(GeckoChildProcess_Plugin),
+    GeckoChildProcessHost(GeckoProcessType_Plugin),
     mPluginFilePath(aPluginFilePath)
 {
 }
 
 PluginProcessParent::~PluginProcessParent()
 {
 }
 
--- a/ipc/app/MozillaRuntimeMain.cpp
+++ b/ipc/app/MozillaRuntimeMain.cpp
@@ -54,16 +54,16 @@
 
 int
 main(int argc, char* argv[])
 {
 #if 0
     MessageBox(NULL, L"Hi", L"Hi", MB_OK);
 #endif
 
-    GeckoChildProcessType proctype =
+    GeckoProcessType proctype =
         XRE_StringToChildProcessType(argv[argc - 1]);
 
     nsresult rv = XRE_InitChildProcess(argc - 1, argv, proctype);
     NS_ENSURE_SUCCESS(rv, 1);
 
     return 0;
 }
--- a/ipc/glue/GeckoChildProcessHost.cpp
+++ b/ipc/glue/GeckoChildProcessHost.cpp
@@ -48,17 +48,17 @@ using mozilla::ipc::GeckoChildProcessHos
 
 template<>
 struct RunnableMethodTraits<GeckoChildProcessHost>
 {
     static void RetainCallee(GeckoChildProcessHost* obj) { }
     static void ReleaseCallee(GeckoChildProcessHost* obj) { }
 };
 
-GeckoChildProcessHost::GeckoChildProcessHost(GeckoChildProcessType aProcessType)
+GeckoChildProcessHost::GeckoChildProcessHost(GeckoProcessType aProcessType)
   : ChildProcessHost(RENDER_PROCESS), // FIXME/cjones: we should own this enum
     mProcessType(aProcessType),
     mMonitor("mozilla.ipc.GeckChildProcessHost.mMonitor"),
     mLaunched(false)
 {
 }
 
 bool
--- a/ipc/glue/GeckoChildProcessHost.h
+++ b/ipc/glue/GeckoChildProcessHost.h
@@ -40,28 +40,28 @@
 #include "base/file_path.h"
 #include "base/process_util.h"
 #include "base/scoped_ptr.h"
 #include "base/waitable_event.h"
 #include "chrome/common/child_process_host.h"
 
 #include "mozilla/Monitor.h"
 
-#include "nsXULAppAPI.h"        // for GeckoChildProcessType
+#include "nsXULAppAPI.h"        // for GeckoProcessType
 
 namespace mozilla {
 namespace ipc {
 
 class GeckoChildProcessHost : public ChildProcessHost
 {
 protected:
   typedef mozilla::Monitor Monitor;
 
 public:
-  GeckoChildProcessHost(GeckoChildProcessType aProcessType=GeckoChildProcess_Default);
+  GeckoChildProcessHost(GeckoProcessType aProcessType=GeckoProcessType_Default);
 
   bool SyncLaunch(std::vector<std::wstring> aExtraOpts=std::vector<std::wstring>());
   bool AsyncLaunch(std::vector<std::wstring> aExtraOpts=std::vector<std::wstring>());
 
   virtual void OnChannelConnected(int32 peer_pid);
   virtual void OnMessageReceived(const IPC::Message& aMsg);
   virtual void OnChannelError();
 
@@ -71,17 +71,17 @@ public:
     return channelp();
   }
 
   base::WaitableEvent* GetShutDownEvent() {
     return GetProcessEvent();
   }
 
 protected:
-  GeckoChildProcessType mProcessType;
+  GeckoProcessType mProcessType;
   Monitor mMonitor;
   bool mLaunched;
   FilePath mProcessPath;
 
 #if defined(OS_POSIX)
   base::file_handle_mapping_vector mFileMap;
 #endif
 
--- a/ipc/test-harness/TestProcessParent.cpp
+++ b/ipc/test-harness/TestProcessParent.cpp
@@ -1,17 +1,17 @@
 #include "mozilla/test/TestProcessParent.h"
 using mozilla::ipc::GeckoChildProcessHost;
 
 namespace mozilla {
 namespace test {
 
 
 TestProcessParent::TestProcessParent() :
-    GeckoChildProcessHost(GeckoChildProcess_TestHarness)
+    GeckoChildProcessHost(GeckoProcessType_TestHarness)
 {
 }
 
 TestProcessParent::~TestProcessParent()
 {
 }
 
 
--- a/toolkit/library/dlldeps-xul.cpp
+++ b/toolkit/library/dlldeps-xul.cpp
@@ -44,14 +44,15 @@ void xxxNeverCalledXUL()
   XRE_GetStaticComponents(nsnull, nsnull);
   XRE_LockProfileDirectory(nsnull, nsnull);
   XRE_InitEmbedding(nsnull, nsnull, nsnull, nsnull, 0);
   XRE_NotifyProfile();
   XRE_TermEmbedding();
   XRE_CreateAppData(nsnull, nsnull);
   XRE_ParseAppData(nsnull, nsnull);
   XRE_FreeAppData(nsnull);
-  XRE_ChildProcessTypeToString(GeckoChildProcess_Default);
+  XRE_ChildProcessTypeToString(GeckoProcessType_Default);
   XRE_StringToChildProcessType("");
-  XRE_InitChildProcess(0, nsnull, GeckoChildProcess_Default);
+  XRE_InitChildProcess(0, nsnull, GeckoProcessType_Default);
   XRE_InitParentProcess(0, nsnull, nsnull, nsnull);
   XRE_RunTestShell(0, nsnull);
+  XRE_GetProcessType();
 }
--- a/toolkit/xre/nsAppRunner.cpp
+++ b/toolkit/xre/nsAppRunner.cpp
@@ -748,16 +748,39 @@ nsXULAppInfo::GetXPCOMABI(nsACString& aR
 
 NS_IMETHODIMP
 nsXULAppInfo::GetWidgetToolkit(nsACString& aResult)
 {
   aResult.AssignLiteral(MOZ_WIDGET_TOOLKIT);
   return NS_OK;
 }
 
+// Ensure that the GeckoProcessType enum, defined in xpcom/build/nsXULAppAPI.h,
+// is synchronized with the const unsigned longs defined in
+// xpcom/system/nsIXULRuntime.idl.
+#define SYNC_ENUMS(a,b) \
+  PR_STATIC_ASSERT(nsIXULRuntime::PROCESS_TYPE_ ## a == \
+                   static_cast<int>(GeckoProcessType_ ## b));
+
+SYNC_ENUMS(DEFAULT, Default)
+SYNC_ENUMS(PLUGIN, Plugin)
+SYNC_ENUMS(CONTENT, Content)
+SYNC_ENUMS(TESTHARNESS, TestHarness)
+
+// .. and ensure that that is all of them:
+PR_STATIC_ASSERT(GeckoProcessType_TestHarness + 1 == GeckoProcessType_End);
+
+NS_IMETHODIMP
+nsXULAppInfo::GetProcessType(PRUint32* aResult)
+{
+  NS_ENSURE_ARG_POINTER(aResult);
+  *aResult = XRE_GetProcessType();
+  return NS_OK;
+}
+
 #ifdef XP_WIN
 // Matches the enum in WinNT.h for the Vista SDK but renamed so that we can
 // safely build with the Vista SDK and without it.
 typedef enum 
 {
   VistaTokenElevationTypeDefault = 1,
   VistaTokenElevationTypeFull,
   VistaTokenElevationTypeLimited
--- a/toolkit/xre/nsEmbedFunctions.cpp
+++ b/toolkit/xre/nsEmbedFunctions.cpp
@@ -207,40 +207,42 @@ XRE_TermEmbedding()
 
   gDirServiceProvider->DoShutdown();
   NS_ShutdownXPCOM(nsnull);
   delete [] sCombined;
   delete gDirServiceProvider;
 }
 
 const char*
-XRE_ChildProcessTypeToString(GeckoChildProcessType aProcessType)
+XRE_ChildProcessTypeToString(GeckoProcessType aProcessType)
 {
-  return (aProcessType < GeckoChildProcess_End) ?
-    kGeckoChildProcessTypeString[aProcessType] : nsnull;
+  return (aProcessType < GeckoProcessType_End) ?
+    kGeckoProcessTypeString[aProcessType] : nsnull;
 }
 
-GeckoChildProcessType
+GeckoProcessType
 XRE_StringToChildProcessType(const char* aProcessTypeString)
 {
   for (int i = 0;
-       i < (int) NS_ARRAY_LENGTH(kGeckoChildProcessTypeString);
+       i < (int) NS_ARRAY_LENGTH(kGeckoProcessTypeString);
        ++i) {
-    if (!strcmp(kGeckoChildProcessTypeString[i], aProcessTypeString)) {
-      return static_cast<GeckoChildProcessType>(i);
+    if (!strcmp(kGeckoProcessTypeString[i], aProcessTypeString)) {
+      return static_cast<GeckoProcessType>(i);
     }
   }
-  return GeckoChildProcess_Invalid;
+  return GeckoProcessType_Invalid;
 }
 
 #ifdef MOZ_IPC
+static GeckoProcessType sChildProcessType = GeckoProcessType_Default;
+
 nsresult
 XRE_InitChildProcess(int aArgc,
                      char* aArgv[],
-                     GeckoChildProcessType aProcess)
+                     GeckoProcessType aProcess)
 {
   NS_ENSURE_ARG_MIN(aArgc, 1);
   NS_ENSURE_ARG_POINTER(aArgv);
   NS_ENSURE_ARG_POINTER(aArgv[0]);
 
   if (PR_GetEnv("MOZ_DEBUG_CHILD_PROCESS")) {
 #ifdef OS_POSIX
       printf("\n\nCHILDCHILDCHILDCHILD\n  debug me @%d\n\n", getpid());
@@ -253,45 +255,52 @@ XRE_InitChildProcess(int aArgc,
   base::AtExitManager exitManager;
   CommandLine::Init(aArgc, aArgv);
   MessageLoopForIO mainMessageLoop;
 
   {
     ChildThread* mainThread;
 
     switch (aProcess) {
-    case GeckoChildProcess_Default:
+    case GeckoProcessType_Default:
       mainThread = new GeckoThread();
       break;
 
-    case GeckoChildProcess_Plugin:
+    case GeckoProcessType_Plugin:
       mainThread = new PluginThreadChild();
       break;
 
-    case GeckoChildProcess_Tab:
+    case GeckoProcessType_Content:
       mainThread = new ContentProcessThread();
       break;
 
-    case GeckoChildProcess_TestHarness:
+    case GeckoProcessType_TestHarness:
       mainThread = new TestThreadChild();
       break;
 
     default:
       NS_RUNTIMEABORT("Unknown main thread class");
     }
 
+    sChildProcessType = aProcess;
     ChildProcess process(mainThread);
 
     // Do IPC event loop
     MessageLoop::current()->Run();
   }
 
   return NS_OK;
 }
 
+GeckoProcessType
+XRE_GetProcessType()
+{
+  return sChildProcessType;
+}
+
 namespace {
 
 class MainFunctionRunnable : public nsRunnable
 {
 public:
   NS_DECL_NSIRUNNABLE
 
   MainFunctionRunnable(MainFunction aFunction,
--- a/xpcom/build/nsXULAppAPI.h
+++ b/xpcom/build/nsXULAppAPI.h
@@ -414,50 +414,53 @@ XRE_API(nsresult,
                            nsXREAppData *aAppData))
 
 /**
  * Free a nsXREAppData structure that was allocated with XRE_CreateAppData.
  */
 XRE_API(void,
         XRE_FreeAppData, (nsXREAppData *aAppData))
 
-enum GeckoChildProcessType {
-  GeckoChildProcess_Default = 0,
+enum GeckoProcessType {
+  GeckoProcessType_Default = 0,
 
-  GeckoChildProcess_Plugin,
-  GeckoChildProcess_Tab,
+  GeckoProcessType_Plugin,
+  GeckoProcessType_Content,
 
-  GeckoChildProcess_TestHarness,
+  GeckoProcessType_TestHarness,
 
-  GeckoChildProcess_End,
-  GeckoChildProcess_Invalid = GeckoChildProcess_End
+  GeckoProcessType_End,
+  GeckoProcessType_Invalid = GeckoProcessType_End
 };
 
-static const char* const kGeckoChildProcessTypeString[] = {
+static const char* const kGeckoProcessTypeString[] = {
   "default",
   "plugin",
   "tab",
   "testharness",
 };
 
-PR_STATIC_ASSERT(sizeof(kGeckoChildProcessTypeString) /
-                 sizeof(kGeckoChildProcessTypeString[0]) ==
-                 GeckoChildProcess_End);
+PR_STATIC_ASSERT(sizeof(kGeckoProcessTypeString) /
+                 sizeof(kGeckoProcessTypeString[0]) ==
+                 GeckoProcessType_End);
 
 
 XRE_API(const char*,
-        XRE_ChildProcessTypeToString, (GeckoChildProcessType aProcessType))
+        XRE_ChildProcessTypeToString, (GeckoProcessType aProcessType))
 
-XRE_API(GeckoChildProcessType,
+XRE_API(GeckoProcessType,
         XRE_StringToChildProcessType, (const char* aProcessTypeString))
 
 XRE_API(nsresult,
         XRE_InitChildProcess, (int aArgc,
                                char* aArgv[],
-                               GeckoChildProcessType aProcess))
+                               GeckoProcessType aProcess))
+
+XRE_API(GeckoProcessType,
+        XRE_GetProcessType, ())
 
 typedef void (*MainFunction)(void* aData);
 
 XRE_API(nsresult,
         XRE_InitParentProcess, (int aArgc,
                                 char* aArgv[],
                                 MainFunction aMainFunction,
                                 void* aMainFunctionExtraData))
--- a/xpcom/system/nsIXULRuntime.idl
+++ b/xpcom/system/nsIXULRuntime.idl
@@ -81,9 +81,22 @@ interface nsIXULRuntime : nsISupports
    */
   readonly attribute AUTF8String XPCOMABI;
 
   /**
    * A string tag identifying the target widget toolkit in use.
    * This is taken from the MOZ_WIDGET_TOOLKIT configure variable.
    */
   readonly attribute AUTF8String widgetToolkit;
+
+  /**
+   * The legal values of processType.
+   */
+  const unsigned long PROCESS_TYPE_DEFAULT = 0;
+  const unsigned long PROCESS_TYPE_PLUGIN = 1;
+  const unsigned long PROCESS_TYPE_CONTENT = 2;
+  const unsigned long PROCESS_TYPE_TESTHARNESS = 3;
+
+  /**
+   * The type of the caller's process.  Returns one of the values above.
+   */
+  readonly attribute unsigned long processType;
 };