Home | History | Annotate | Download | only in service_worker
      1 // Copyright 2013 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #include "content/child/service_worker/web_service_worker_provider_impl.h"
      6 
      7 #include "base/atomic_sequence_num.h"
      8 #include "base/logging.h"
      9 #include "content/child/child_thread.h"
     10 #include "content/child/service_worker/service_worker_dispatcher.h"
     11 #include "content/child/service_worker/service_worker_handle_reference.h"
     12 #include "content/child/service_worker/service_worker_provider_context.h"
     13 #include "content/child/service_worker/web_service_worker_impl.h"
     14 #include "content/child/thread_safe_sender.h"
     15 #include "content/common/service_worker/service_worker_messages.h"
     16 #include "third_party/WebKit/public/platform/WebServiceWorkerProviderClient.h"
     17 #include "third_party/WebKit/public/platform/WebURL.h"
     18 
     19 using blink::WebURL;
     20 
     21 namespace content {
     22 
     23 WebServiceWorkerProviderImpl::WebServiceWorkerProviderImpl(
     24     ThreadSafeSender* thread_safe_sender,
     25     ServiceWorkerProviderContext* context)
     26     : thread_safe_sender_(thread_safe_sender),
     27       context_(context),
     28       provider_id_(context->provider_id()) {
     29 }
     30 
     31 WebServiceWorkerProviderImpl::~WebServiceWorkerProviderImpl() {
     32   // Make sure the script client is removed.
     33   RemoveScriptClient();
     34 }
     35 
     36 void WebServiceWorkerProviderImpl::setClient(
     37     blink::WebServiceWorkerProviderClient* client) {
     38   if (!client) {
     39     RemoveScriptClient();
     40     return;
     41   }
     42 
     43   // TODO(kinuko): Here we could also register the current thread ID
     44   // on the provider context so that multiple WebServiceWorkerProviderImpl
     45   // (e.g. on document and on dedicated workers) can properly share
     46   // the single provider context across threads. (http://crbug.com/366538
     47   // for more context)
     48   GetDispatcher()->AddScriptClient(provider_id_, client);
     49 
     50   if (context_->waiting_handle_id() != kInvalidServiceWorkerHandleId) {
     51     client->setWaiting(
     52         GetDispatcher()->GetServiceWorker(context_->waiting()->info(), false));
     53   }
     54 
     55   if (context_->current_handle_id() != kInvalidServiceWorkerHandleId) {
     56     client->setController(
     57         GetDispatcher()->GetServiceWorker(context_->current()->info(), false));
     58   }
     59 }
     60 
     61 void WebServiceWorkerProviderImpl::registerServiceWorker(
     62     const WebURL& pattern,
     63     const WebURL& script_url,
     64     WebServiceWorkerCallbacks* callbacks) {
     65   GetDispatcher()->RegisterServiceWorker(
     66       provider_id_, pattern, script_url, callbacks);
     67 }
     68 
     69 void WebServiceWorkerProviderImpl::unregisterServiceWorker(
     70     const WebURL& pattern,
     71     WebServiceWorkerCallbacks* callbacks) {
     72   GetDispatcher()->UnregisterServiceWorker(
     73       provider_id_, pattern, callbacks);
     74 }
     75 
     76 void WebServiceWorkerProviderImpl::RemoveScriptClient() {
     77   // Remove the script client, but only if the dispatcher is still there.
     78   // (For cleanup path we don't need to bother creating a new dispatcher)
     79   ServiceWorkerDispatcher* dispatcher =
     80       ServiceWorkerDispatcher::GetThreadSpecificInstance();
     81   if (dispatcher)
     82     dispatcher->RemoveScriptClient(provider_id_);
     83 }
     84 
     85 ServiceWorkerDispatcher* WebServiceWorkerProviderImpl::GetDispatcher() {
     86   return ServiceWorkerDispatcher::GetOrCreateThreadSpecificInstance(
     87       thread_safe_sender_);
     88 }
     89 
     90 }  // namespace content
     91