blob: f6ed0fca7466d471d9aead7f66cdf57801687ef9 [file] [log] [blame]
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "content/child/service_worker/web_service_worker_provider_impl.h"
#include "base/atomic_sequence_num.h"
#include "base/logging.h"
#include "content/child/child_thread.h"
#include "content/child/service_worker/service_worker_dispatcher.h"
#include "content/child/service_worker/service_worker_handle_reference.h"
#include "content/child/service_worker/service_worker_provider_context.h"
#include "content/child/service_worker/web_service_worker_impl.h"
#include "content/child/thread_safe_sender.h"
#include "content/common/service_worker/service_worker_messages.h"
#include "third_party/WebKit/public/platform/WebServiceWorkerProviderClient.h"
#include "third_party/WebKit/public/platform/WebURL.h"
using blink::WebURL;
namespace content {
WebServiceWorkerProviderImpl::WebServiceWorkerProviderImpl(
ThreadSafeSender* thread_safe_sender,
ServiceWorkerProviderContext* context)
: thread_safe_sender_(thread_safe_sender),
context_(context),
provider_id_(context->provider_id()) {
}
WebServiceWorkerProviderImpl::~WebServiceWorkerProviderImpl() {
// Make sure the script client is removed.
RemoveScriptClient();
}
void WebServiceWorkerProviderImpl::setClient(
blink::WebServiceWorkerProviderClient* client) {
if (!client) {
RemoveScriptClient();
return;
}
// TODO(kinuko): Here we could also register the current thread ID
// on the provider context so that multiple WebServiceWorkerProviderImpl
// (e.g. on document and on dedicated workers) can properly share
// the single provider context across threads. (http://crbug.com/366538
// for more context)
GetDispatcher()->AddScriptClient(provider_id_, client);
if (context_->waiting_handle_id() != kInvalidServiceWorkerHandleId) {
client->setWaiting(
GetDispatcher()->GetServiceWorker(context_->waiting()->info(), false));
}
if (context_->current_handle_id() != kInvalidServiceWorkerHandleId) {
client->setController(
GetDispatcher()->GetServiceWorker(context_->current()->info(), false));
}
}
void WebServiceWorkerProviderImpl::registerServiceWorker(
const WebURL& pattern,
const WebURL& script_url,
WebServiceWorkerCallbacks* callbacks) {
GetDispatcher()->RegisterServiceWorker(
provider_id_, pattern, script_url, callbacks);
}
void WebServiceWorkerProviderImpl::unregisterServiceWorker(
const WebURL& pattern,
WebServiceWorkerCallbacks* callbacks) {
GetDispatcher()->UnregisterServiceWorker(
provider_id_, pattern, callbacks);
}
void WebServiceWorkerProviderImpl::RemoveScriptClient() {
// Remove the script client, but only if the dispatcher is still there.
// (For cleanup path we don't need to bother creating a new dispatcher)
ServiceWorkerDispatcher* dispatcher =
ServiceWorkerDispatcher::GetThreadSpecificInstance();
if (dispatcher)
dispatcher->RemoveScriptClient(provider_id_);
}
ServiceWorkerDispatcher* WebServiceWorkerProviderImpl::GetDispatcher() {
return ServiceWorkerDispatcher::GetOrCreateThreadSpecificInstance(
thread_safe_sender_);
}
} // namespace content