Bug 767640, part 1 - Put Ci, Cr, Cc, and Cu on chrome contexts. r=bz,jmaher draft
authorAndrew McCreight <continuation@gmail.com>
Thu, 18 Jan 2018 13:04:41 -0800
changeset 750224 8c66dd90fd087c91a9fb83f4af8382edf92227e0
parent 750223 66585dad7efbe74836e790d91f406e2a192edfe0
child 750225 e40f5a8bad5fee6c76f4048af4901b253fa8f989
push id97585
push userbmo:continuation@gmail.com
push dateThu, 01 Feb 2018 19:07:29 +0000
reviewersbz, jmaher
bugs767640
milestone60.0a1
Bug 767640, part 1 - Put Ci, Cr, Cc, and Cu on chrome contexts. r=bz,jmaher Almost every chrome script uses these abbreviations. We can avoid some boilerplate by automatically defining them on chrome contexts where we define Components. The var declarations for Cc and Ci in MozillaFileLogger.js are run before enablePrivilege("UniversalXPConnect"). The latter now attempts to automatically define Cc and Ci, but the non-configurable Cc and Ci prevent that. Work around this by just removing the var declarations. MozReview-Commit-ID: 6FV9ahLeqUb
js/xpconnect/src/XPCJSRuntime.cpp
js/xpconnect/src/XPCWrappedNativeScope.cpp
js/xpconnect/src/xpcprivate.h
testing/talos/talos/scripts/MozillaFileLogger.js
--- a/js/xpconnect/src/XPCJSRuntime.cpp
+++ b/js/xpconnect/src/XPCJSRuntime.cpp
@@ -77,16 +77,20 @@ const char* const XPCJSRuntime::mStrings
     "constructor",          // IDX_CONSTRUCTOR
     "toString",             // IDX_TO_STRING
     "toSource",             // IDX_TO_SOURCE
     "lastResult",           // IDX_LAST_RESULT
     "returnCode",           // IDX_RETURN_CODE
     "value",                // IDX_VALUE
     "QueryInterface",       // IDX_QUERY_INTERFACE
     "Components",           // IDX_COMPONENTS
+    "Cc",                   // IDX_CC
+    "Ci",                   // IDX_CI
+    "Cr",                   // IDX_CR
+    "Cu",                   // IDX_CU
     "wrappedJSObject",      // IDX_WRAPPED_JSOBJECT
     "Object",               // IDX_OBJECT
     "Function",             // IDX_FUNCTION
     "prototype",            // IDX_PROTOTYPE
     "createInstance",       // IDX_CREATE_INSTANCE
     "item",                 // IDX_ITEM
     "__proto__",            // IDX_PROTO
     "eval",                 // IDX_EVAL
--- a/js/xpconnect/src/XPCWrappedNativeScope.cpp
+++ b/js/xpconnect/src/XPCWrappedNativeScope.cpp
@@ -219,16 +219,34 @@ XPCWrappedNativeScope::GetComponentsJSOb
 void
 XPCWrappedNativeScope::ForcePrivilegedComponents()
 {
     nsCOMPtr<nsIXPCComponents> c = do_QueryInterface(mComponents);
     if (!c)
         mComponents = new nsXPCComponents(this);
 }
 
+static bool
+DefineSubcomponentProperty(JSContext* aCx,
+                           HandleObject aGlobal,
+                           nsISupports* aSubcomponent,
+                           const nsID* aIID,
+                           unsigned int aStringIndex)
+{
+    RootedValue subcompVal(aCx);
+    xpcObjectHelper helper(aSubcomponent);
+    if (!XPCConvert::NativeInterface2JSObject(&subcompVal, helper,
+                                              aIID, false, nullptr))
+        return false;
+    if (NS_WARN_IF(!subcompVal.isObject()))
+        return false;
+    RootedId id(aCx, XPCJSContext::Get()->GetStringID(aStringIndex));
+    return JS_DefinePropertyById(aCx, aGlobal, id, subcompVal, 0);
+}
+
 bool
 XPCWrappedNativeScope::AttachComponentsObject(JSContext* aCx)
 {
     RootedObject components(aCx);
     if (!GetComponentsJSObject(&components))
         return false;
 
     RootedObject global(aCx, GetGlobalJSObject());
@@ -238,17 +256,40 @@ XPCWrappedNativeScope::AttachComponentsO
     // nsXPCComponents object. That way, if it's an nsXPCComponentsBase,
     // enableUniversalXPConnect can upgrade it later.
     unsigned attrs = JSPROP_READONLY | JSPROP_RESOLVING;
     nsCOMPtr<nsIXPCComponents> c = do_QueryInterface(mComponents);
     if (c)
         attrs |= JSPROP_PERMANENT;
 
     RootedId id(aCx, XPCJSContext::Get()->GetStringID(XPCJSContext::IDX_COMPONENTS));
-    return JS_DefinePropertyById(aCx, global, id, components, attrs);
+    if (!JS_DefinePropertyById(aCx, global, id, components, attrs))
+        return false;
+
+// _iid can be nullptr if the object implements classinfo.
+#define DEFINE_SUBCOMPONENT_PROPERTY(_comp, _type, _iid, _id)                     \
+    nsCOMPtr<nsIXPCComponents_ ## _type> obj ## _type;                            \
+    if (NS_FAILED(_comp->Get ## _type(getter_AddRefs( obj ## _type ))))           \
+        return false;                                                             \
+    if (!DefineSubcomponentProperty(aCx, global, obj ## _type, _iid,              \
+                                    XPCJSContext::IDX_ ## _id))                   \
+        return false;
+
+    DEFINE_SUBCOMPONENT_PROPERTY(mComponents, Interfaces, nullptr, CI)
+    DEFINE_SUBCOMPONENT_PROPERTY(mComponents, Results, nullptr, CR)
+
+    if (!c)
+        return true;
+
+    DEFINE_SUBCOMPONENT_PROPERTY(c, Classes, nullptr, CC)
+    DEFINE_SUBCOMPONENT_PROPERTY(c, Utils, &NS_GET_IID(nsIXPCComponents_Utils), CU)
+
+#undef DEFINE_SUBCOMPONENT_PROPERTY
+
+    return true;
 }
 
 static bool
 CompartmentPerAddon()
 {
     static bool initialized = false;
     static bool pref = false;
 
--- a/js/xpconnect/src/xpcprivate.h
+++ b/js/xpconnect/src/xpcprivate.h
@@ -430,16 +430,20 @@ public:
         IDX_CONSTRUCTOR             = 0 ,
         IDX_TO_STRING               ,
         IDX_TO_SOURCE               ,
         IDX_LAST_RESULT             ,
         IDX_RETURN_CODE             ,
         IDX_VALUE                   ,
         IDX_QUERY_INTERFACE         ,
         IDX_COMPONENTS              ,
+        IDX_CC                      ,
+        IDX_CI                      ,
+        IDX_CR                      ,
+        IDX_CU                      ,
         IDX_WRAPPED_JSOBJECT        ,
         IDX_OBJECT                  ,
         IDX_FUNCTION                ,
         IDX_PROTOTYPE               ,
         IDX_CREATE_INSTANCE         ,
         IDX_ITEM                    ,
         IDX_PROTO                   ,
         IDX_EVAL                    ,
--- a/testing/talos/talos/scripts/MozillaFileLogger.js
+++ b/testing/talos/talos/scripts/MozillaFileLogger.js
@@ -29,21 +29,16 @@ function contentAsyncEvent(type, data) {
 function dumpLog(msg) {
   dump(msg);
   MozFileLogger.log(msg);
 }
 
 
 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
 
-if (Cc === undefined) {
-  var Cc = Components.classes;
-  var Ci = Components.interfaces;
-}
-
 const FOSTREAM_CID = "@mozilla.org/network/file-output-stream;1";
 const LF_CID = "@mozilla.org/file/local;1";
 
 // File status flags. It is a bitwise OR of the following bit flags.
 // Only one of the first three flags below may be used.
 const PR_READ_ONLY    = 0x01; // Open for reading only.
 const PR_WRITE_ONLY   = 0x02; // Open for writing only.
 const PR_READ_WRITE   = 0x04; // Open for reading and writing.