Bug 1416066 - Enable caching for scripts with codebase URLs of about:home, about:newtab, and about:welcome. draft
authorimjching <jlim@mozilla.com>
Tue, 03 Jul 2018 21:24:52 -0400
changeset 816196 8e9355a75e5d68a0d9ab0f49de0859b5f57e2692
parent 816195 f47da8c5ba63084eb0fc01a75c253fa217264ab9
child 816197 58650e44e30df89de10b2a61cfff3f0797b4068f
push id115771
push userbmo:jlim@mozilla.com
push dateTue, 10 Jul 2018 17:44:26 +0000
bugs1416066
milestone63.0a1
Bug 1416066 - Enable caching for scripts with codebase URLs of about:home, about:newtab, and about:welcome. MozReview-Commit-ID: HC3cNVxWLe6
caps/moz.build
js/xpconnect/loader/mozJSSubScriptLoader.cpp
--- a/caps/moz.build
+++ b/caps/moz.build
@@ -19,19 +19,22 @@ XPIDL_SOURCES += [
     'nsIDomainPolicy.idl',
     'nsIPrincipal.idl',
     'nsIScriptSecurityManager.idl',
 ]
 
 XPIDL_MODULE = 'caps'
 
 EXPORTS += [
+    'ContentPrincipal.h',
     'nsJSPrincipals.h',
+    'nsScriptSecurityManager.h',
     'NullPrincipal.h',
     'NullPrincipalURI.h',
+    'SystemPrincipal.h',
 ]
 
 EXPORTS.mozilla = [
     'BasePrincipal.h',
     'OriginAttributes.h',
 ]
 
 SOURCES += [
--- a/js/xpconnect/loader/mozJSSubScriptLoader.cpp
+++ b/js/xpconnect/loader/mozJSSubScriptLoader.cpp
@@ -10,16 +10,18 @@
 
 #include "nsIURI.h"
 #include "nsIIOService.h"
 #include "nsIChannel.h"
 #include "nsIInputStream.h"
 #include "nsNetCID.h"
 #include "nsNetUtil.h"
 #include "nsIFileURL.h"
+#include "ContentPrincipal.h"
+#include "SystemPrincipal.h"
 
 #include "jsapi.h"
 #include "jsfriendapi.h"
 #include "xpcprivate.h" // For xpc::OptionsBase
 #include "js/Wrapper.h"
 
 #include "mozilla/dom/Promise.h"
 #include "mozilla/dom/ToJSValue.h"
@@ -685,19 +687,37 @@ mozJSSubScriptLoader::DoLoadSubScriptWit
         tmp.AppendLiteral(" -> ");
         tmp.Append(uriStr);
 
         uriStr = tmp;
     }
 
     // Suppress caching if we're compiling as content or if we're loading a
     // blob: URI.
-    bool ignoreCache = options.ignoreCache
-        || !GetObjectPrincipal(targetObj)->GetIsSystemPrincipal()
-        || scheme.EqualsLiteral("blob");
+    auto* principal = BasePrincipal::Cast(GetObjectPrincipal(targetObj));
+    bool isSystem = principal->Is<SystemPrincipal>();
+    if (!isSystem && principal->Is<ContentPrincipal>()) {
+        auto* content = principal->As<ContentPrincipal>();
+
+        nsAutoCString scheme;
+        content->mCodebase->GetScheme(scheme);
+
+        // We want to enable caching for scripts with Activity Stream's
+        // codebase URLs.
+        if (scheme.EqualsLiteral("about")) {
+            nsAutoCString filePath;
+            content->mCodebase->GetFilePath(filePath);
+
+            isSystem = filePath.EqualsLiteral("home") ||
+                       filePath.EqualsLiteral("newtab") ||
+                       filePath.EqualsLiteral("welcome");
+        }
+    }
+    bool ignoreCache = options.ignoreCache || !isSystem || scheme.EqualsLiteral("blob");
+
     StartupCache* cache = ignoreCache ? nullptr : StartupCache::GetSingleton();
 
     nsAutoCString cachePath;
     SubscriptCachePath(cx, uri, targetObj, cachePath);
 
     RootedScript script(cx);
     if (!options.ignoreCache) {
         if (!options.wantReturnValue)