DELETE: 1 patches - 984048-swutil-fetcher
authorNikhil Marathe <nsm.nikhil@gmail.com>
Wed, 23 Jul 2014 17:34:05 -0700
changeset 1117 813230c769c67f7310d8cc5b41c222879820bc8e
parent 1116 af55728bd88e3fafcda4aea08268fa5a25294656
child 1118 67e92f92ff03262ed112f4f16ea226beef84ad64
push id32
push usernsm.nikhil@gmail.com
push dateThu, 24 Jul 2014 00:32:46 +0000
bugs984048
DELETE: 1 patches - 984048-swutil-fetcher DELETE: 984048-swutil-fetcher qparent: 666e5e6b3abf qtip: 666e5e6b3abf top: (none)
984048-swutil-fetcher
series
deleted file mode 100644
--- a/984048-swutil-fetcher
+++ /dev/null
@@ -1,285 +0,0 @@
-# HG changeset patch
-# Parent 6a984e21c2ca17cc65aac6077e45cf0c99ea51fe
-# User Nikhil Marathe <nsm.nikhil@gmail.com>
-Bug 984048: Part 2 - Fetch ServiceWorker script without spinning a worker.
-
-diff --git a/dom/workers/ServiceWorkerUtil.cpp b/dom/workers/ServiceWorkerUtil.cpp
-new file mode 100644
---- /dev/null
-+++ b/dom/workers/ServiceWorkerUtil.cpp
-@@ -0,0 +1,175 @@
-+/* This Source Code Form is subject to the terms of the Mozilla Public
-+ * License, v. 2.0. If a copy of the MPL was not distributed with this
-+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
-+
-+#include "ServiceWorkerUtil.h"
-+
-+#include "nsIChannel.h"
-+#include "nsIContentPolicy.h"
-+#include "nsIDocument.h"
-+#include "nsIIOService.h"
-+#include "nsIURI.h"
-+
-+#include "nsContentPolicyUtils.h"
-+#include "nsNetUtil.h"
-+#include "nsNetCID.h"
-+#include "ServiceWorkerManager.h"
-+
-+BEGIN_WORKERS_NAMESPACE
-+
-+NS_IMETHODIMP
-+ServiceWorkerFetcher::asyncFetch(ServiceWorkerRegistration* aRegistration,
-+                                 nsPIDOMWindow* aWindow)
-+{
-+  NS_ENSURE_ARG_POINTER(aRegistration);
-+  NS_ENSURE_ARG_POINTER(aWindow);
-+
-+#ifdef DEBUG
-+  // Strictly a one time use class!
-+  MOZ_ASSERT(!mUsed);
-+  mUsed = true;
-+#endif
-+
-+  mRegistration = aRegistration;
-+  mWindow = aWindow;
-+
-+  nsCOMPtr<nsIDocument> doc = aWindow->GetExtantDoc();
-+  if (!doc) {
-+    NS_WARNING("No document!");
-+    return NS_ERROR_FAILURE;
-+  }
-+
-+  nsCOMPtr<nsILoadGroup> loadGroup = doc->GetDocumentLoadGroup();
-+
-+  nsCOMPtr<nsIIOService> ios(do_GetIOService());
-+
-+  nsresult rv;
-+  nsCOMPtr<nsIURI> uri;
-+  rv =
-+    nsContentUtils::NewURIWithDocumentCharset(getter_AddRefs(uri),
-+                                              NS_ConvertUTF8toUTF16(aRegistration->mScriptSpec),
-+                                              doc, nullptr);
-+  if (NS_FAILED(rv)) {
-+    return NS_ERROR_DOM_SYNTAX_ERR;
-+  }
-+
-+  {
-+    nsIScriptSecurityManager* secMan = nsContentUtils::GetSecurityManager();
-+    int16_t shouldLoad = nsIContentPolicy::ACCEPT;
-+    rv = NS_CheckContentLoadPolicy(nsIContentPolicy::TYPE_SCRIPT, uri,
-+                                   doc->NodePrincipal(), doc,
-+                                   NS_LITERAL_CSTRING("text/javascript"),
-+                                   nullptr, &shouldLoad,
-+                                   nsContentUtils::GetContentPolicy(),
-+                                   secMan);
-+
-+    if (NS_FAILED(rv) || NS_CP_REJECTED(shouldLoad)) {
-+      if (NS_FAILED(rv) || shouldLoad != nsIContentPolicy::REJECT_TYPE) {
-+        return rv = NS_ERROR_CONTENT_BLOCKED;
-+      }
-+      return rv = NS_ERROR_CONTENT_BLOCKED_SHOW_ALT;
-+    }
-+  }
-+
-+  nsCString scheme;
-+  rv = uri->GetScheme(scheme);
-+  if (NS_WARN_IF(NS_FAILED(rv))) {
-+    return rv;
-+  }
-+
-+  // We pass true as the 3rd argument to checkMayLoad here.
-+  // This allows workers in sandboxed documents to load data URLs
-+  // (and other URLs that inherit their principal from their
-+  // creator.)
-+  rv = doc->NodePrincipal()->CheckMayLoad(uri, false, true);
-+  if (NS_WARN_IF(NS_FAILED(rv))) {
-+    return NS_ERROR_DOM_SECURITY_ERR;
-+  }
-+
-+  // FIXME(nsm): Verify CSP.
-+
-+  uint32_t flags = nsIRequest::INHIBIT_CACHING | nsIRequest::LOAD_BYPASS_CACHE;
-+
-+  nsCOMPtr<nsIChannel> channel;
-+  rv = NS_NewChannel(getter_AddRefs(channel), uri, ios, loadGroup, nullptr,
-+                     flags, nullptr);
-+  if (NS_WARN_IF(NS_FAILED(rv))) {
-+    return rv;
-+  }
-+
-+  nsCOMPtr<nsIHttpChannel> httpChannel = do_QueryInterface(channel);
-+  if (!httpChannel) {
-+    return NS_ERROR_FAILURE;
-+  }
-+
-+  // Prevent redirects.
-+  httpChannel->SetRedirectionLimit(0);
-+
-+  nsCOMPtr<nsIStreamLoader> loader;
-+  rv = NS_NewStreamLoader(getter_AddRefs(loader), this);
-+  if (NS_WARN_IF(NS_FAILED(rv))) {
-+    return rv;
-+  }
-+
-+  return channel->AsyncOpen(loader, nullptr);
-+}
-+
-+NS_IMETHODIMP
-+ServiceWorkerFetcher::OnStreamComplete(nsIStreamLoader* aLoader,
-+                                       nsISupports* aContext, nsresult aStatus,
-+                                       uint32_t aStringLen,
-+                                       const uint8_t* aString)
-+{
-+  if (mAborted) {
-+    NS_WARNING("Aborted");
-+    return NS_OK;
-+  }
-+
-+  // If we get a 404 or something, indicate failure to SWM.
-+  nsCOMPtr<nsIRequest> request;
-+  nsresult rv = aLoader->GetRequest(getter_AddRefs(request));
-+  if (NS_WARN_IF(NS_FAILED(rv))) {
-+    // ServiceWorkerManager::GetInstance()->FinishFetch(this, rv, 0, nullptr);
-+    return NS_OK;
-+  }
-+
-+  nsCOMPtr<nsIHttpChannel> httpChannel = do_QueryInterface(request);
-+  if (httpChannel) {
-+    bool requestSucceeded;
-+    rv = httpChannel->GetRequestSucceeded(&requestSucceeded);
-+    if (NS_WARN_IF(NS_FAILED(rv))) {
-+      // ServiceWorkerManager::GetInstance()->FinishFetch(this, rv, 0, nullptr);
-+      return NS_OK;
-+    }
-+
-+    if (requestSucceeded) {
-+      // ServiceWorkerManager::GetInstance()->FinishFetch(this, aStatus,
-+      //                                                  aStringLen, aString);
-+    } else {
-+      // ServiceWorkerManager::GetInstance()->FinishFetch(this,
-+      //                                                  NS_ERROR_NOT_AVAILABLE,
-+      //                                                  aStringLen, aString);
-+    }
-+  }
-+
-+  return NS_OK;
-+}
-+
-+NS_IMETHODIMP
-+ServiceWorkerFetcher::OnCacheEntryCheck(nsICacheEntry *aEntry, nsIApplicationCache *aApplicationCache, uint32_t* aEntryState)
-+{
-+  fprintf(stderr, "NSM OnCacheEntryAvailable %p %p\n", aEntry, aApplicationCache);
-+  *aEntryState = nsICacheEntryOpenCallback::ENTRY_WANTED;
-+  return NS_OK;
-+}
-+
-+NS_IMETHODIMP
-+ServiceWorkerFetcher::OnCacheEntryAvailable(nsICacheEntry *aEntry, bool aNew, nsIApplicationCache *aApplicationCache, nsresult aResult)
-+{
-+  fprintf(stderr, "NSM OnCacheEntryAvailable %p %d %p %d\n", aEntry, aNew, aApplicationCache, aResult);
-+  return NS_OK;
-+}
-+
-+NS_IMPL_ISUPPORTS(ServiceWorkerFetcher, nsIStreamLoaderObserver, nsICacheEntryOpenCallback);
-+
-+END_WORKERS_NAMESPACE
-diff --git a/dom/workers/ServiceWorkerUtil.h b/dom/workers/ServiceWorkerUtil.h
-new file mode 100644
---- /dev/null
-+++ b/dom/workers/ServiceWorkerUtil.h
-@@ -0,0 +1,74 @@
-+/* This Source Code Form is subject to the terms of the Mozilla Public
-+ * License, v. 2.0. If a copy of the MPL was not distributed with this
-+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
-+
-+#ifndef mozilla_dom_workers_serviceworkerutil_h
-+#define mozilla_dom_workers_serviceworkerutil_h
-+
-+#include "nsIStreamLoader.h"
-+#include "nsICacheEntryOpenCallback.h"
-+
-+#include "nsCOMPtr.h"
-+
-+#include "Workers.h"
-+
-+class nsIDocument;
-+
-+BEGIN_WORKERS_NAMESPACE
-+
-+class ServiceWorkerManager;
-+class ServiceWorkerRegistration;
-+
-+/*
-+ * FIXME(nsm)
-+ * This class pretends to implement the ServiceWorker update logic for now; stuff like
-+ * "byte-wise equal" and "more than 24 hours".
-+ *
-+ * The intention is that it will actually do those things a little later once
-+ * the other registration bits have landed.
-+ */
-+class ServiceWorkerFetcher MOZ_FINAL : public nsIStreamLoaderObserver,
-+                                       public nsICacheEntryOpenCallback
-+{
-+  // Owned by SWM's hashtable. The registration will maintain a refptr to this,
-+  // so we don't have to worry about mRegistration being freed.
-+  ServiceWorkerRegistration* mRegistration;
-+  // The window from where a register() call triggered the current fetch.
-+  // This may be null on SoftUpdate().
-+  nsCOMPtr<nsPIDOMWindow> mWindow;
-+
-+  // Don't inform the SWM of a finished transaction if it doesn't care anymore.
-+  bool mAborted;
-+
-+  // The fetcher is a one-time use class.
-+  DebugOnly<bool> mUsed;
-+public:
-+  NS_DECL_ISUPPORTS
-+  NS_DECL_NSISTREAMLOADEROBSERVER
-+  NS_DECL_NSICACHEENTRYOPENCALLBACK
-+
-+  friend class ServiceWorkerManager;
-+
-+  ServiceWorkerFetcher()
-+    : mRegistration(nullptr), mAborted(false)
-+#ifdef DEBUG
-+    , mUsed(false)
-+#endif
-+  { }
-+
-+  virtual ~ServiceWorkerFetcher()
-+  { }
-+
-+  void
-+  Abort()
-+  {
-+    mAborted = true;
-+  }
-+
-+  NS_IMETHOD
-+  asyncFetch(ServiceWorkerRegistration* aRegistration, nsPIDOMWindow* aWindow);
-+};
-+
-+END_WORKERS_NAMESPACE
-+
-+#endif // mozilla_dom_workers_serviceworkerutil_h
-diff --git a/dom/workers/moz.build b/dom/workers/moz.build
---- a/dom/workers/moz.build
-+++ b/dom/workers/moz.build
-@@ -47,16 +47,17 @@ SOURCES += [
-     'Principal.cpp',
-     'RegisterBindings.cpp',
-     'RuntimeService.cpp',
-     'ScriptLoader.cpp',
-     'ServiceWorker.cpp',
-     'ServiceWorkerContainer.cpp',
-     'ServiceWorkerEvents.cpp',
-     'ServiceWorkerManager.cpp',
-+    'ServiceWorkerUtil.cpp',
-     'SharedWorker.cpp',
-     'URL.cpp',
-     'WorkerPrivate.cpp',
-     'WorkerRunnable.cpp',
-     'WorkerScope.cpp',
-     'XMLHttpRequest.cpp',
-     'XMLHttpRequestUpload.cpp',
- ]
--- a/series
+++ b/series
@@ -12,10 +12,9 @@ 903441-navigation-event
 903441-setup-request
 903441-intercepts
 903441-sw-nointercept
 foo
 898524-cache
 sw-install-tests2
 sw-more-install-tests
 943704-importscripts
-984048-swutil-fetcher
 984048-swm-update+