Chrome side of passing on the service worker registration with geofencing API calls.

BUG=383125

Review URL: https://codereview.chromium.org/623823002

Cr-Commit-Position: refs/heads/master@{#301010}
diff --git a/content/browser/geofencing/geofencing_dispatcher_host.cc b/content/browser/geofencing/geofencing_dispatcher_host.cc
index f2fe54a..5cae113 100644
--- a/content/browser/geofencing/geofencing_dispatcher_host.cc
+++ b/content/browser/geofencing/geofencing_dispatcher_host.cc
@@ -5,6 +5,9 @@
 #include "content/browser/geofencing/geofencing_dispatcher_host.h"
 
 #include "content/browser/geofencing/geofencing_manager.h"
+#include "content/browser/service_worker/service_worker_context_core.h"
+#include "content/browser/service_worker/service_worker_context_wrapper.h"
+#include "content/browser/service_worker/service_worker_registration.h"
 #include "content/common/geofencing_messages.h"
 #include "third_party/WebKit/public/platform/WebCircularGeofencingRegion.h"
 
@@ -38,16 +41,17 @@
     int thread_id,
     int request_id,
     const std::string& region_id,
-    const blink::WebCircularGeofencingRegion& region) {
+    const blink::WebCircularGeofencingRegion& region,
+    int64 service_worker_registration_id) {
   // Sanity check on region_id
   if (region_id.length() > kMaxRegionIdLength) {
     Send(new GeofencingMsg_RegisterRegionComplete(
         thread_id, request_id, GeofencingStatus::GEOFENCING_STATUS_ERROR));
     return;
   }
-  // TODO(mek): Actually pass service worker information to manager.
+
   manager_->RegisterRegion(
-      0, /* service_worker_registration_id */
+      service_worker_registration_id,
       region_id,
       region,
       base::Bind(&GeofencingDispatcherHost::RegisterRegionCompleted,
@@ -59,16 +63,17 @@
 void GeofencingDispatcherHost::OnUnregisterRegion(
     int thread_id,
     int request_id,
-    const std::string& region_id) {
+    const std::string& region_id,
+    int64 service_worker_registration_id) {
   // Sanity check on region_id
   if (region_id.length() > kMaxRegionIdLength) {
     Send(new GeofencingMsg_UnregisterRegionComplete(
         thread_id, request_id, GeofencingStatus::GEOFENCING_STATUS_ERROR));
     return;
   }
-  // TODO(mek): Actually pass service worker information to manager.
+
   manager_->UnregisterRegion(
-      0, /* service_worker_registration_id */
+      service_worker_registration_id,
       region_id,
       base::Bind(&GeofencingDispatcherHost::UnregisterRegionCompleted,
                  weak_factory_.GetWeakPtr(),
@@ -76,13 +81,14 @@
                  request_id));
 }
 
-void GeofencingDispatcherHost::OnGetRegisteredRegions(int thread_id,
-                                                      int request_id) {
+void GeofencingDispatcherHost::OnGetRegisteredRegions(
+    int thread_id,
+    int request_id,
+    int64 service_worker_registration_id) {
   GeofencingRegistrations result;
-  // TODO(mek): Actually pass service worker information to manager.
+
   GeofencingStatus status =
-      manager_->GetRegisteredRegions(0, /* service_worker_registration_id */
-                                     &result);
+      manager_->GetRegisteredRegions(service_worker_registration_id, &result);
   Send(new GeofencingMsg_GetRegisteredRegionsComplete(
       thread_id, request_id, status, result));
 }
diff --git a/content/browser/geofencing/geofencing_dispatcher_host.h b/content/browser/geofencing/geofencing_dispatcher_host.h
index 16bc2b4..40ed3a848 100644
--- a/content/browser/geofencing/geofencing_dispatcher_host.h
+++ b/content/browser/geofencing/geofencing_dispatcher_host.h
@@ -15,6 +15,7 @@
 namespace content {
 
 class GeofencingManager;
+class ServiceWorkerContextWrapper;
 
 class GeofencingDispatcherHost : public BrowserMessageFilter {
  public:
@@ -29,11 +30,15 @@
   void OnRegisterRegion(int thread_id,
                         int request_id,
                         const std::string& region_id,
-                        const blink::WebCircularGeofencingRegion& region);
+                        const blink::WebCircularGeofencingRegion& region,
+                        int64 service_worker_registration_id);
   void OnUnregisterRegion(int thread_id,
                           int request_id,
-                          const std::string& region_id);
-  void OnGetRegisteredRegions(int thread_id, int request_id);
+                          const std::string& region_id,
+                          int64 service_worker_registration_id);
+  void OnGetRegisteredRegions(int thread_id,
+                              int request_id,
+                              int64 service_worker_registration_id);
 
   void RegisterRegionCompleted(int thread_id,
                                int request_id,
diff --git a/content/browser/geofencing/geofencing_manager.cc b/content/browser/geofencing/geofencing_manager.cc
index 33afae5..1147d7d 100644
--- a/content/browser/geofencing/geofencing_manager.cc
+++ b/content/browser/geofencing/geofencing_manager.cc
@@ -16,12 +16,14 @@
 
 struct GeofencingManager::Registration {
   Registration(int64 service_worker_registration_id,
+               const GURL& service_worker_origin,
                const std::string& region_id,
                const blink::WebCircularGeofencingRegion& region,
                const StatusCallback& callback,
                int64 geofencing_registration_id);
 
   int64 service_worker_registration_id;
+  GURL service_worker_origin;
   std::string region_id;
   blink::WebCircularGeofencingRegion region;
 
@@ -39,6 +41,7 @@
 
 GeofencingManager::Registration::Registration(
     int64 service_worker_registration_id,
+    const GURL& service_worker_origin,
     const std::string& region_id,
     const blink::WebCircularGeofencingRegion& region,
     const GeofencingManager::StatusCallback& callback,
@@ -98,6 +101,21 @@
 
   // TODO(mek): Validate region_id and region.
 
+  // Look up service worker. In unit tests |service_worker_context_| might not
+  // be set.
+  GURL service_worker_origin;
+  if (service_worker_context_.get()) {
+    ServiceWorkerRegistration* service_worker_registration =
+        service_worker_context_->context()->GetLiveRegistration(
+            service_worker_registration_id);
+    if (!service_worker_registration) {
+      callback.Run(GEOFENCING_STATUS_OPERATION_FAILED_NO_SERVICE_WORKER);
+      return;
+    }
+
+    service_worker_origin = service_worker_registration->pattern().GetOrigin();
+  }
+
   if (!service_->IsServiceAvailable()) {
     callback.Run(GEOFENCING_STATUS_OPERATION_FAILED_SERVICE_NOT_AVAILABLE);
     return;
@@ -111,6 +129,7 @@
   }
 
   AddRegistration(service_worker_registration_id,
+                  service_worker_origin,
                   region_id,
                   region,
                   callback,
@@ -124,6 +143,18 @@
 
   // TODO(mek): Validate region_id.
 
+  // Look up service worker. In unit tests |service_worker_context_| might not
+  // be set.
+  if (service_worker_context_.get()) {
+    ServiceWorkerRegistration* service_worker_registration =
+        service_worker_context_->context()->GetLiveRegistration(
+            service_worker_registration_id);
+    if (!service_worker_registration) {
+      callback.Run(GEOFENCING_STATUS_OPERATION_FAILED_NO_SERVICE_WORKER);
+      return;
+    }
+  }
+
   if (!service_->IsServiceAvailable()) {
     callback.Run(GEOFENCING_STATUS_OPERATION_FAILED_SERVICE_NOT_AVAILABLE);
     return;
@@ -154,6 +185,17 @@
   DCHECK_CURRENTLY_ON(BrowserThread::IO);
   CHECK(result);
 
+  // Look up service worker. In unit tests |service_worker_context_| might not
+  // be set.
+  if (service_worker_context_.get()) {
+    ServiceWorkerRegistration* service_worker_registration =
+        service_worker_context_->context()->GetLiveRegistration(
+            service_worker_registration_id);
+    if (!service_worker_registration) {
+      return GEOFENCING_STATUS_OPERATION_FAILED_NO_SERVICE_WORKER;
+    }
+  }
+
   if (!service_->IsServiceAvailable()) {
     return GEOFENCING_STATUS_OPERATION_FAILED_SERVICE_NOT_AVAILABLE;
   }
@@ -212,6 +254,7 @@
 
 GeofencingManager::Registration& GeofencingManager::AddRegistration(
     int64 service_worker_registration_id,
+    const GURL& service_worker_origin,
     const std::string& region_id,
     const blink::WebCircularGeofencingRegion& region,
     const StatusCallback& callback,
@@ -222,6 +265,7 @@
       registrations_[service_worker_registration_id]
           .insert(std::make_pair(region_id,
                                  Registration(service_worker_registration_id,
+                                              service_worker_origin,
                                               region_id,
                                               region,
                                               callback,
diff --git a/content/browser/geofencing/geofencing_manager.h b/content/browser/geofencing/geofencing_manager.h
index d5842b33..7cd7b17 100644
--- a/content/browser/geofencing/geofencing_manager.h
+++ b/content/browser/geofencing/geofencing_manager.h
@@ -116,6 +116,7 @@
   // object. Assumes no registration with the same IDs currently exists.
   Registration& AddRegistration(
       int64 service_worker_registration_id,
+      const GURL& service_worker_origin,
       const std::string& region_id,
       const blink::WebCircularGeofencingRegion& region,
       const StatusCallback& callback,
diff --git a/content/browser/service_worker/service_worker_registration_handle.cc b/content/browser/service_worker/service_worker_registration_handle.cc
index 21aa31ef..195a33a 100644
--- a/content/browser/service_worker/service_worker_registration_handle.cc
+++ b/content/browser/service_worker/service_worker_registration_handle.cc
@@ -42,6 +42,7 @@
   ServiceWorkerRegistrationObjectInfo info;
   info.handle_id = handle_id_;
   info.scope = registration_->pattern();
+  info.registration_id = registration_->id();
   return info;
 }
 
diff --git a/content/child/geofencing/geofencing_dispatcher.cc b/content/child/geofencing/geofencing_dispatcher.cc
index 1b0f849..eec721f 100644
--- a/content/child/geofencing/geofencing_dispatcher.cc
+++ b/content/child/geofencing/geofencing_dispatcher.cc
@@ -8,9 +8,11 @@
 #include "base/memory/scoped_ptr.h"
 #include "base/message_loop/message_loop.h"
 #include "base/thread_task_runner_handle.h"
+#include "content/child/service_worker/web_service_worker_registration_impl.h"
 #include "content/child/thread_safe_sender.h"
 #include "content/child/worker_thread_task_runner.h"
 #include "content/common/geofencing_messages.h"
+#include "content/common/service_worker/service_worker_types.h"
 #include "third_party/WebKit/public/platform/WebCircularGeofencingRegion.h"
 #include "third_party/WebKit/public/platform/WebGeofencingError.h"
 #include "third_party/WebKit/public/platform/WebGeofencingRegistration.h"
@@ -63,28 +65,60 @@
 void GeofencingDispatcher::RegisterRegion(
     const blink::WebString& region_id,
     const blink::WebCircularGeofencingRegion& region,
+    blink::WebServiceWorkerRegistration* service_worker_registration,
     blink::WebGeofencingCallbacks* callbacks) {
   DCHECK(callbacks);
   int request_id = region_registration_requests_.Add(callbacks);
-  Send(new GeofencingHostMsg_RegisterRegion(
-      CurrentWorkerId(), request_id, region_id.utf8(), region));
+  // TODO(mek): Immediately reject requests lacking a service worker
+  // registration, without bouncing through browser process.
+  int64 serviceworker_registration_id = kInvalidServiceWorkerRegistrationId;
+  if (service_worker_registration) {
+    serviceworker_registration_id =
+        static_cast<WebServiceWorkerRegistrationImpl*>(
+            service_worker_registration)->registration_id();
+  }
+  Send(new GeofencingHostMsg_RegisterRegion(CurrentWorkerId(),
+                                            request_id,
+                                            region_id.utf8(),
+                                            region,
+                                            serviceworker_registration_id));
 }
 
 void GeofencingDispatcher::UnregisterRegion(
     const blink::WebString& region_id,
+    blink::WebServiceWorkerRegistration* service_worker_registration,
     blink::WebGeofencingCallbacks* callbacks) {
   DCHECK(callbacks);
   int request_id = region_unregistration_requests_.Add(callbacks);
-  Send(new GeofencingHostMsg_UnregisterRegion(
-      CurrentWorkerId(), request_id, region_id.utf8()));
+  // TODO(mek): Immediately reject requests lacking a service worker
+  // registration, without bouncing through browser process.
+  int64 serviceworker_registration_id = kInvalidServiceWorkerRegistrationId;
+  if (service_worker_registration) {
+    serviceworker_registration_id =
+        static_cast<WebServiceWorkerRegistrationImpl*>(
+            service_worker_registration)->registration_id();
+  }
+  Send(new GeofencingHostMsg_UnregisterRegion(CurrentWorkerId(),
+                                              request_id,
+                                              region_id.utf8(),
+                                              serviceworker_registration_id));
 }
 
 void GeofencingDispatcher::GetRegisteredRegions(
+    blink::WebServiceWorkerRegistration* service_worker_registration,
     blink::WebGeofencingRegionsCallbacks* callbacks) {
   DCHECK(callbacks);
   int request_id = get_registered_regions_requests_.Add(callbacks);
-  Send(new GeofencingHostMsg_GetRegisteredRegions(CurrentWorkerId(),
-                                                  request_id));
+  // TODO(mek): Immediately reject requests lacking a service worker
+  // registration, without bouncing through browser process.
+  int64 serviceworker_registration_id = kInvalidServiceWorkerRegistrationId;
+  if (service_worker_registration) {
+    serviceworker_registration_id =
+        static_cast<WebServiceWorkerRegistrationImpl*>(
+            service_worker_registration)->registration_id();
+  }
+  Send(new GeofencingHostMsg_GetRegisteredRegions(
+      CurrentWorkerId(), request_id, serviceworker_registration_id));
 }
 
 GeofencingDispatcher* GeofencingDispatcher::GetOrCreateThreadSpecificInstance(
diff --git a/content/child/geofencing/geofencing_dispatcher.h b/content/child/geofencing/geofencing_dispatcher.h
index fc3988e..57a2a43 100644
--- a/content/child/geofencing/geofencing_dispatcher.h
+++ b/content/child/geofencing/geofencing_dispatcher.h
@@ -33,12 +33,18 @@
   void OnMessageReceived(const IPC::Message& msg);
 
   // Corresponding to WebGeofencingProvider methods.
-  void RegisterRegion(const blink::WebString& region_id,
-                      const blink::WebCircularGeofencingRegion& region,
-                      blink::WebGeofencingCallbacks* callbacks);
-  void UnregisterRegion(const blink::WebString& region_id,
-                        blink::WebGeofencingCallbacks* callbacks);
-  void GetRegisteredRegions(blink::WebGeofencingRegionsCallbacks* callbacks);
+  void RegisterRegion(
+      const blink::WebString& region_id,
+      const blink::WebCircularGeofencingRegion& region,
+      blink::WebServiceWorkerRegistration* service_worker_registration,
+      blink::WebGeofencingCallbacks* callbacks);
+  void UnregisterRegion(
+      const blink::WebString& region_id,
+      blink::WebServiceWorkerRegistration* service_worker_registration,
+      blink::WebGeofencingCallbacks* callbacks);
+  void GetRegisteredRegions(
+      blink::WebServiceWorkerRegistration* service_worker_registration,
+      blink::WebGeofencingRegionsCallbacks* callbacks);
 
   // |thread_safe_sender| needs to be passed in because if the call leads to
   // construction it will be needed.
diff --git a/content/child/geofencing/web_geofencing_provider_impl.cc b/content/child/geofencing/web_geofencing_provider_impl.cc
index d058ffe..5d78b98 100644
--- a/content/child/geofencing/web_geofencing_provider_impl.cc
+++ b/content/child/geofencing/web_geofencing_provider_impl.cc
@@ -20,19 +20,24 @@
 void WebGeofencingProviderImpl::registerRegion(
     const blink::WebString& regionId,
     const blink::WebCircularGeofencingRegion& region,
+    blink::WebServiceWorkerRegistration* service_worker_registration,
     blink::WebGeofencingCallbacks* callbacks) {
-  GetDispatcher()->RegisterRegion(regionId, region, callbacks);
+  GetDispatcher()->RegisterRegion(
+      regionId, region, service_worker_registration, callbacks);
 }
 
 void WebGeofencingProviderImpl::unregisterRegion(
     const blink::WebString& regionId,
+    blink::WebServiceWorkerRegistration* service_worker_registration,
     blink::WebGeofencingCallbacks* callbacks) {
-  GetDispatcher()->UnregisterRegion(regionId, callbacks);
+  GetDispatcher()->UnregisterRegion(
+      regionId, service_worker_registration, callbacks);
 }
 
 void WebGeofencingProviderImpl::getRegisteredRegions(
+    blink::WebServiceWorkerRegistration* service_worker_registration,
     blink::WebGeofencingRegionsCallbacks* callbacks) {
-  GetDispatcher()->GetRegisteredRegions(callbacks);
+  GetDispatcher()->GetRegisteredRegions(service_worker_registration, callbacks);
 }
 
 GeofencingDispatcher* WebGeofencingProviderImpl::GetDispatcher() {
diff --git a/content/child/geofencing/web_geofencing_provider_impl.h b/content/child/geofencing/web_geofencing_provider_impl.h
index a1dd744..9aac795 100644
--- a/content/child/geofencing/web_geofencing_provider_impl.h
+++ b/content/child/geofencing/web_geofencing_provider_impl.h
@@ -21,12 +21,17 @@
 
  private:
   // WebGeofencingProvider implementation.
-  virtual void registerRegion(const blink::WebString& regionId,
-                              const blink::WebCircularGeofencingRegion& region,
-                              blink::WebGeofencingCallbacks* callbacks);
-  virtual void unregisterRegion(const blink::WebString& regionId,
-                                blink::WebGeofencingCallbacks* callbacks);
+  virtual void registerRegion(
+      const blink::WebString& regionId,
+      const blink::WebCircularGeofencingRegion& region,
+      blink::WebServiceWorkerRegistration* service_worker_registration,
+      blink::WebGeofencingCallbacks* callbacks);
+  virtual void unregisterRegion(
+      const blink::WebString& regionId,
+      blink::WebServiceWorkerRegistration* service_worker_registration,
+      blink::WebGeofencingCallbacks* callbacks);
   virtual void getRegisteredRegions(
+      blink::WebServiceWorkerRegistration* service_worker_registration,
       blink::WebGeofencingRegionsCallbacks* callbacks);
 
   GeofencingDispatcher* GetDispatcher();
diff --git a/content/child/service_worker/service_worker_registration_handle_reference.h b/content/child/service_worker/service_worker_registration_handle_reference.h
index 605094d..c7138e62 100644
--- a/content/child/service_worker/service_worker_registration_handle_reference.h
+++ b/content/child/service_worker/service_worker_registration_handle_reference.h
@@ -33,6 +33,7 @@
   const ServiceWorkerRegistrationObjectInfo& info() const { return info_; }
   int handle_id() const { return info_.handle_id; }
   GURL scope() const { return info_.scope; }
+  int64 registration_id() const { return info_.registration_id; }
 
  private:
   ServiceWorkerRegistrationHandleReference(
diff --git a/content/child/service_worker/web_service_worker_registration_impl.cc b/content/child/service_worker/web_service_worker_registration_impl.cc
index 833d22b..12db531 100644
--- a/content/child/service_worker/web_service_worker_registration_impl.cc
+++ b/content/child/service_worker/web_service_worker_registration_impl.cc
@@ -112,4 +112,8 @@
   return handle_ref_->scope();
 }
 
+int64 WebServiceWorkerRegistrationImpl::registration_id() const {
+  return handle_ref_->registration_id();
+}
+
 }  // namespace content
diff --git a/content/child/service_worker/web_service_worker_registration_impl.h b/content/child/service_worker/web_service_worker_registration_impl.h
index e143a5c..52ee1029 100644
--- a/content/child/service_worker/web_service_worker_registration_impl.h
+++ b/content/child/service_worker/web_service_worker_registration_impl.h
@@ -39,6 +39,8 @@
   virtual blink::WebServiceWorkerRegistrationProxy* proxy();
   virtual blink::WebURL scope() const;
 
+  int64 registration_id() const;
+
  private:
   enum QueuedTaskType {
     INSTALLING,
diff --git a/content/common/geofencing_messages.h b/content/common/geofencing_messages.h
index 5012be34..152b108e 100644
--- a/content/common/geofencing_messages.h
+++ b/content/common/geofencing_messages.h
@@ -30,20 +30,23 @@
 IPC_STRUCT_TRAITS_END()
 
 // Messages sent from the child process to the browser.
-IPC_MESSAGE_CONTROL4(GeofencingHostMsg_RegisterRegion,
+IPC_MESSAGE_CONTROL5(GeofencingHostMsg_RegisterRegion,
                      int /* thread_id */,
                      int /* request_id */,
                      std::string /* region_id */,
-                     blink::WebCircularGeofencingRegion /* region */)
+                     blink::WebCircularGeofencingRegion /* region */,
+                     int64 /* serviceworker_registration_id */)
 
-IPC_MESSAGE_CONTROL3(GeofencingHostMsg_UnregisterRegion,
+IPC_MESSAGE_CONTROL4(GeofencingHostMsg_UnregisterRegion,
                      int /* thread_id */,
                      int /* request_id */,
-                     std::string /* region_id */)
+                     std::string /* region_id */,
+                     int64 /* serviceworker_registration_id */)
 
-IPC_MESSAGE_CONTROL2(GeofencingHostMsg_GetRegisteredRegions,
+IPC_MESSAGE_CONTROL3(GeofencingHostMsg_GetRegisteredRegions,
                      int /* thread_id */,
-                     int /* request_id */)
+                     int /* request_id */,
+                     int64 /* serviceworker_registration_id */)
 
 // Messages sent from the browser to the child process.
 
diff --git a/content/common/service_worker/service_worker_messages.h b/content/common/service_worker/service_worker_messages.h
index 3857592..6b7b090 100644
--- a/content/common/service_worker/service_worker_messages.h
+++ b/content/common/service_worker/service_worker_messages.h
@@ -88,6 +88,7 @@
 IPC_STRUCT_TRAITS_BEGIN(content::ServiceWorkerRegistrationObjectInfo)
   IPC_STRUCT_TRAITS_MEMBER(handle_id)
   IPC_STRUCT_TRAITS_MEMBER(scope)
+  IPC_STRUCT_TRAITS_MEMBER(registration_id)
 IPC_STRUCT_TRAITS_END()
 
 IPC_STRUCT_TRAITS_BEGIN(content::ServiceWorkerVersionAttributes)
diff --git a/content/common/service_worker/service_worker_types.h b/content/common/service_worker/service_worker_types.h
index 36ad372..9fa5bc0 100644
--- a/content/common/service_worker/service_worker_types.h
+++ b/content/common/service_worker/service_worker_types.h
@@ -156,6 +156,7 @@
   ServiceWorkerRegistrationObjectInfo();
   int handle_id;
   GURL scope;
+  int64 registration_id;
 };
 
 struct ServiceWorkerVersionAttributes {