Bug 1353636 - Part 2: Expose ServieworkerRegistration.updateViaCache. r=bkelly
authorHo-Pang Hsu <hopang.hsu@gmail.com>
Wed, 16 Aug 2017 14:21:29 +0800
changeset 648497 128194809ab37dc0dce47c5a27ff6e73cfe7c8de
parent 648496 9865180c7e5972d6bf0f490121a2426046624575
child 648498 cbba741c30615c9c0dfd81e71ba6a6ec71cd291a
push id74775
push userbmo:ted@mielczarek.org
push dateThu, 17 Aug 2017 21:15:41 +0000
reviewersbkelly
bugs1353636
milestone57.0a1
Bug 1353636 - Part 2: Expose ServieworkerRegistration.updateViaCache. r=bkelly
dom/interfaces/base/nsIServiceWorkerManager.idl
dom/webidl/ServiceWorkerRegistration.webidl
dom/workers/ServiceWorkerRegistration.cpp
dom/workers/ServiceWorkerRegistration.h
dom/workers/ServiceWorkerRegistrationInfo.cpp
--- a/dom/interfaces/base/nsIServiceWorkerManager.idl
+++ b/dom/interfaces/base/nsIServiceWorkerManager.idl
@@ -69,16 +69,17 @@ interface nsIServiceWorkerRegistrationIn
   const unsigned short UPDATE_VIA_CACHE_IMPORTS = 0;
   const unsigned short UPDATE_VIA_CACHE_ALL = 1;
   const unsigned short UPDATE_VIA_CACHE_NONE = 2;
 
   readonly attribute nsIPrincipal principal;
 
   readonly attribute DOMString scope;
   readonly attribute DOMString scriptSpec;
+  readonly attribute unsigned short updateViaCache;
 
   readonly attribute PRTime lastUpdateTime;
 
   readonly attribute nsIServiceWorkerInfo installingWorker;
   readonly attribute nsIServiceWorkerInfo waitingWorker;
   readonly attribute nsIServiceWorkerInfo activeWorker;
 
   // Allows to get the related nsIServiceWorkerInfo for a given
--- a/dom/webidl/ServiceWorkerRegistration.webidl
+++ b/dom/webidl/ServiceWorkerRegistration.webidl
@@ -12,16 +12,17 @@
 [Func="mozilla::dom::ServiceWorkerRegistration::Visible",
  Exposed=(Window,Worker)]
 interface ServiceWorkerRegistration : EventTarget {
   [Unforgeable] readonly attribute ServiceWorker? installing;
   [Unforgeable] readonly attribute ServiceWorker? waiting;
   [Unforgeable] readonly attribute ServiceWorker? active;
 
   readonly attribute USVString scope;
+  readonly attribute ServiceWorkerUpdateViaCache updateViaCache;
 
   [Throws, NewObject]
   Promise<void> update();
 
   [Throws, NewObject]
   Promise<boolean> unregister();
 
   // event
--- a/dom/workers/ServiceWorkerRegistration.cpp
+++ b/dom/workers/ServiceWorkerRegistration.cpp
@@ -134,16 +134,43 @@ public:
   RegistrationRemoved() override;
 
   void
   GetScope(nsAString& aScope) const override
   {
     aScope = mScope;
   }
 
+  ServiceWorkerUpdateViaCache
+  UpdateViaCache() const override
+  {
+    RefPtr<ServiceWorkerManager> swm = ServiceWorkerManager::GetInstance();
+    MOZ_ASSERT(swm);
+
+    nsCOMPtr<nsPIDOMWindowInner> window = GetOwner();
+    MOZ_ASSERT(window);
+
+    nsCOMPtr<nsIDocument> doc = window->GetExtantDoc();
+    MOZ_ASSERT(doc);
+
+    nsCOMPtr<nsIServiceWorkerRegistrationInfo> registration;
+    nsresult rv = swm->GetRegistrationByPrincipal(doc->NodePrincipal(), mScope,
+                                                  getter_AddRefs(registration));
+    MOZ_ASSERT(NS_SUCCEEDED(rv) && registration);
+
+    uint16_t updateViaCache;
+    rv = registration->GetUpdateViaCache(&updateViaCache);
+    MOZ_ASSERT(NS_SUCCEEDED(rv));
+
+    // Silence possible compiler warnings.
+    Unused << rv;
+
+    return static_cast<ServiceWorkerUpdateViaCache>(updateViaCache);
+  }
+
 private:
   ~ServiceWorkerRegistrationMainThread();
 
   already_AddRefed<ServiceWorker>
   GetWorkerReference(WhichServiceWorker aWhichOne);
 
   void
   StartListeningForEvents();
@@ -911,16 +938,23 @@ public:
   GetActive() override;
 
   void
   GetScope(nsAString& aScope) const override
   {
     aScope = mScope;
   }
 
+  ServiceWorkerUpdateViaCache
+  UpdateViaCache() const override
+  {
+    // FIXME(hopang): Will be implemented after Bug 1113522.
+    return ServiceWorkerUpdateViaCache::Imports;
+  }
+
   bool
   Notify(Status aStatus) override;
 
   already_AddRefed<PushManager>
   GetPushManager(JSContext* aCx, ErrorResult& aRv) override;
 
 private:
   ~ServiceWorkerRegistrationWorkerThread();
--- a/dom/workers/ServiceWorkerRegistration.h
+++ b/dom/workers/ServiceWorkerRegistration.h
@@ -5,16 +5,17 @@
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #ifndef mozilla_dom_ServiceWorkerRegistration_h
 #define mozilla_dom_ServiceWorkerRegistration_h
 
 #include "mozilla/DOMEventTargetHelper.h"
 #include "mozilla/dom/ServiceWorkerBinding.h"
 #include "mozilla/dom/ServiceWorkerCommon.h"
+#include "mozilla/dom/ServiceWorkerRegistrationBinding.h"
 #include "mozilla/dom/workers/bindings/WorkerHolder.h"
 #include "nsContentUtils.h" // Required for nsContentUtils::PushEnabled
 
 // Support for Notification API extension.
 #include "mozilla/dom/NotificationBinding.h"
 
 class nsPIDOMWindowInner;
 
@@ -85,16 +86,19 @@ public:
   GetWaiting() = 0;
 
   virtual already_AddRefed<workers::ServiceWorker>
   GetActive() = 0;
 
   virtual void
   GetScope(nsAString& aScope) const = 0;
 
+  virtual ServiceWorkerUpdateViaCache
+  UpdateViaCache() const = 0;
+
   virtual already_AddRefed<Promise>
   Update(ErrorResult& aRv) = 0;
 
   virtual already_AddRefed<Promise>
   Unregister(ErrorResult& aRv) = 0;
 
   virtual already_AddRefed<PushManager>
   GetPushManager(JSContext* aCx, ErrorResult& aRv) = 0;
--- a/dom/workers/ServiceWorkerRegistrationInfo.cpp
+++ b/dom/workers/ServiceWorkerRegistrationInfo.cpp
@@ -124,16 +124,23 @@ ServiceWorkerRegistrationInfo::GetScript
   RefPtr<ServiceWorkerInfo> newest = Newest();
   if (newest) {
     CopyUTF8toUTF16(newest->ScriptSpec(), aScriptSpec);
   }
   return NS_OK;
 }
 
 NS_IMETHODIMP
+ServiceWorkerRegistrationInfo::GetUpdateViaCache(uint16_t* aUpdateViaCache)
+{
+    *aUpdateViaCache = static_cast<uint16_t>(GetUpdateViaCache());
+    return NS_OK;
+}
+
+NS_IMETHODIMP
 ServiceWorkerRegistrationInfo::GetLastUpdateTime(PRTime* _retval)
 {
   AssertIsOnMainThread();
   MOZ_ASSERT(_retval);
   *_retval = mLastUpdateTime;
   return NS_OK;
 }