Bug 1363482: Part 3 - Preload string bundles off-thread during startup. r?erahm draft
authorKris Maglione <maglione.k@gmail.com>
Wed, 10 May 2017 17:08:38 -0700
changeset 656257 27051af8e2abb40723fb768618a0551e1850c891
parent 656256 e2e35c9a6fe57882516545bb83e67146102bbe33
child 656258 c6e5e1f24ee3be5569092b8cb107ef83744d12fe
push id77137
push usermaglione.k@gmail.com
push dateWed, 30 Aug 2017 23:02:44 +0000
reviewerserahm
bugs1363482
milestone57.0a1
Bug 1363482: Part 3 - Preload string bundles off-thread during startup. r?erahm MozReview-Commit-ID: 3Fig5BS9xMz
intl/strres/nsStringBundle.cpp
--- a/intl/strres/nsStringBundle.cpp
+++ b/intl/strres/nsStringBundle.cpp
@@ -20,16 +20,18 @@
 #include "nsIInputStream.h"
 #include "nsIURI.h"
 #include "nsIObserverService.h"
 #include "nsCOMArray.h"
 #include "nsTextFormatter.h"
 #include "nsIErrorService.h"
 #include "nsICategoryManager.h"
 #include "nsContentUtils.h"
+#include "nsStringStream.h"
+#include "mozilla/URLPreloader.h"
 
 // for async loading
 #ifdef ASYNC_LOADING
 #include "nsIBinaryInputStream.h"
 #include "nsIStringStream.h"
 #endif
 
 using namespace mozilla;
@@ -88,31 +90,38 @@ nsStringBundle::LoadProperties()
   nsCString scheme;
   uri->GetScheme(scheme);
   if (!scheme.EqualsLiteral("chrome") && !scheme.EqualsLiteral("jar") &&
       !scheme.EqualsLiteral("resource") && !scheme.EqualsLiteral("file") &&
       !scheme.EqualsLiteral("data")) {
     return NS_ERROR_ABORT;
   }
 
-  nsCOMPtr<nsIChannel> channel;
-  rv = NS_NewChannel(getter_AddRefs(channel),
-                     uri,
-                     nsContentUtils::GetSystemPrincipal(),
-                     nsILoadInfo::SEC_ALLOW_CROSS_ORIGIN_DATA_IS_NULL,
-                     nsIContentPolicy::TYPE_OTHER);
+  nsCOMPtr<nsIInputStream> in;
 
-  if (NS_FAILED(rv)) return rv;
+  auto result = URLPreloader::ReadURI(uri);
+  if (result.isOk()) {
+    rv = NS_NewCStringInputStream(getter_AddRefs(in), result.unwrap());
+    if (NS_FAILED(rv)) return rv;
+  } else {
+    nsCOMPtr<nsIChannel> channel;
+    rv = NS_NewChannel(getter_AddRefs(channel),
+                       uri,
+                       nsContentUtils::GetSystemPrincipal(),
+                       nsILoadInfo::SEC_ALLOW_CROSS_ORIGIN_DATA_IS_NULL,
+                       nsIContentPolicy::TYPE_OTHER);
 
-  // It's a string bundle.  We expect a text/plain type, so set that as hint
-  channel->SetContentType(NS_LITERAL_CSTRING("text/plain"));
+    if (NS_FAILED(rv)) return rv;
 
-  nsCOMPtr<nsIInputStream> in;
-  rv = channel->Open2(getter_AddRefs(in));
-  if (NS_FAILED(rv)) return rv;
+    // It's a string bundle.  We expect a text/plain type, so set that as hint
+    channel->SetContentType(NS_LITERAL_CSTRING("text/plain"));
+
+    rv = channel->Open2(getter_AddRefs(in));
+    if (NS_FAILED(rv)) return rv;
+  }
 
   NS_ASSERTION(NS_SUCCEEDED(rv) && in, "Error in OpenBlockingStream");
   NS_ENSURE_TRUE(NS_SUCCEEDED(rv) && in, NS_ERROR_FAILURE);
 
   static NS_DEFINE_CID(kPersistentPropertiesCID, NS_IPERSISTENTPROPERTIES_CID);
   mProps = do_CreateInstance(kPersistentPropertiesCID, &rv);
   NS_ENSURE_SUCCESS(rv, rv);