Bug 1220757 - Add report to console when service worker register fails due to mismatching scope path.r=bkelly
authorJohn Dai <jdai@mozilla.com>
Thu, 31 Mar 2016 23:08:45 -0700
changeset 291191 1d2d0e61c4d1d045fc41c7393f8c583e88d8549c
parent 291190 9d39b91bba9c192fe3cca3c772e3e83e96b9a298
child 291192 906bb507cee3daac0120c0fa75ade9b2033b0108
push id19656
push usergwagner@mozilla.com
push dateMon, 04 Apr 2016 13:43:23 +0000
treeherderb2g-inbound@e99061fde28a [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbkelly
bugs1220757
milestone48.0a1
Bug 1220757 - Add report to console when service worker register fails due to mismatching scope path.r=bkelly MozReview-Commit-ID: 9hRBzWtSAd2
dom/locales/en-US/chrome/dom/dom.properties
dom/workers/ServiceWorkerManager.cpp
--- a/dom/locales/en-US/chrome/dom/dom.properties
+++ b/dom/locales/en-US/chrome/dom/dom.properties
@@ -182,16 +182,18 @@ InterceptedUsedResponseWithURL=Failed to
 # LOCALIZATION NOTE: Do not translate "ServiceWorker", "opaqueredirect", "Response", "FetchEvent.respondWith()", or "FetchEvent". %s is a URL.
 BadOpaqueRedirectInterceptionWithURL=Failed to load '%S'. A ServiceWorker passed an opaqueredirect Response to FetchEvent.respondWith() while handling a non-navigation FetchEvent.
 # LOCALIZATION NOTE: Do not translate "ServiceWorker" or "FetchEvent.preventDefault()". %S is a URL.
 InterceptionCanceledWithURL=Failed to load '%S'. A ServiceWorker canceled the load by calling FetchEvent.preventDefault().
 # LOCALIZATION NOTE: Do not translate "ServiceWorker", "promise", or "FetchEvent.respondWith()". %1$S is a URL. %2$S is an error string.
 InterceptionRejectedResponseWithURL=Failed to load '%1$S'. A ServiceWorker passed a promise to FetchEvent.respondWith() that rejected with '%2$S'.
 # LOCALIZATION NOTE: Do not translate "ServiceWorker", "promise", "FetchEvent.respondWith()", or "Response". %1$S is a URL. %2$S is an error string.
 InterceptedNonResponseWithURL=Failed to load '%1$S'. A ServiceWorker passed a promise to FetchEvent.respondWith() that resolved with non-Response value '%2$S'.
+# LOCALIZATION NOTE: Do not translate "ServiceWorker", "Service-Worker-Allowed" or "HTTP". %1$S and %2$S are URLs.
+ServiceWorkerScopePathMismatch=Failed to register a ServiceWorker: The path of the provided scope '%1$S' is not under the max scope allowed '%2$S'. Adjust the scope, move the Service Worker script, or use the Service-Worker-Allowed HTTP header to allow the scope.
 ExecCommandCutCopyDeniedNotInputDriven=document.execCommand('cut'/'copy') was denied because it was not called from inside a short running user-generated event handler.
 ManifestShouldBeObject=Manifest should be an object.
 ManifestScopeURLInvalid=The scope URL is invalid.
 ManifestScopeNotSameOrigin=The scope URL must be same origin as document.
 ManifestStartURLOutsideScope=The start URL is outside the scope, so the scope is invalid.
 ManifestStartURLInvalid=The start URL is invalid.
 ManifestStartURLShouldBeSameOrigin=The start URL must be same origin as document.
 # LOCALIZATION NOTE: %1$S is the name of the object whose property is invalid. %2$S is the name of the invalid property. %3$S is the expected type of the property value. E.g. "Expected the manifest's start_url member to be a string."
--- a/dom/workers/ServiceWorkerManager.cpp
+++ b/dom/workers/ServiceWorkerManager.cpp
@@ -1452,17 +1452,30 @@ public:
       rv = GetRequiredScopeStringPrefix(maxScopeURI, maxPrefix, eUsePath);
       if (NS_WARN_IF(NS_FAILED(rv))) {
         Fail(NS_ERROR_DOM_SECURITY_ERR);
         return;
       }
     }
 
     if (!StringBeginsWith(mRegistration->mScope, maxPrefix)) {
-      NS_WARNING("By default a service worker's scope is restricted to at or below it's script's location.");
+      nsXPIDLString message;
+      NS_ConvertUTF8toUTF16 reportScope(mRegistration->mScope);
+      NS_ConvertUTF8toUTF16 reportMaxPrefix(maxPrefix);
+      const char16_t* params[] = { reportScope.get(), reportMaxPrefix.get() };
+
+      rv = nsContentUtils::FormatLocalizedString(nsContentUtils::eDOM_PROPERTIES,
+                                                 "ServiceWorkerScopePathMismatch",
+                                                 params, message);
+      NS_WARN_IF_FALSE(NS_SUCCEEDED(rv), "Failed to format localized string");
+      swm->ReportToAllClients(mScope,
+                              message,
+                              EmptyString(),
+                              EmptyString(), 0, 0,
+                              nsIScriptError::errorFlag);
       Fail(NS_ERROR_DOM_SECURITY_ERR);
       return;
     }
 
     nsAutoCString scopeKey;
     rv = swm->PrincipalToScopeKey(mRegistration->mPrincipal, scopeKey);
     if (NS_WARN_IF(NS_FAILED(rv))) {
       return Fail(NS_ERROR_FAILURE);
@@ -2706,19 +2719,23 @@ ServiceWorkerManager::ReportToAllClients
                                          const nsString& aMessage,
                                          const nsString& aFilename,
                                          const nsString& aLine,
                                          uint32_t aLineNumber,
                                          uint32_t aColumnNumber,
                                          uint32_t aFlags)
 {
   nsCOMPtr<nsIURI> uri;
-  nsresult rv = NS_NewURI(getter_AddRefs(uri), aFilename);
-  if (NS_WARN_IF(NS_FAILED(rv))) {
-    return;
+  nsresult rv;
+
+  if (!aFilename.IsEmpty()) {
+    rv = NS_NewURI(getter_AddRefs(uri), aFilename);
+    if (NS_WARN_IF(NS_FAILED(rv))) {
+      return;
+    }
   }
 
   AutoTArray<uint64_t, 16> windows;
 
   // Report errors to every controlled document.
   for (auto iter = mControlledDocuments.Iter(); !iter.Done(); iter.Next()) {
     ServiceWorkerRegistrationInfo* reg = iter.UserData();
     MOZ_ASSERT(reg);