1 // Copyright 2014 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.
6 #include "ServiceWorkerContainerClient.h"
8 #include "core/dom/Document.h"
9 #include "core/dom/ExecutionContext.h"
10 #include "core/frame/LocalFrame.h"
11 #include "core/loader/FrameLoaderClient.h"
12 #include "core/workers/WorkerGlobalScope.h"
13 #include "public/platform/WebServiceWorkerProvider.h"
17 PassOwnPtrWillBeRawPtr<ServiceWorkerContainerClient> ServiceWorkerContainerClient::create(PassOwnPtr<WebServiceWorkerProvider> provider)
19 return adoptPtrWillBeNoop(new ServiceWorkerContainerClient(provider));
22 ServiceWorkerContainerClient::~ServiceWorkerContainerClient()
26 const char* ServiceWorkerContainerClient::supplementName()
28 return "ServiceWorkerContainerClient";
31 ServiceWorkerContainerClient* ServiceWorkerContainerClient::from(ExecutionContext* context)
33 if (context->isDocument()) {
34 Document* document = toDocument(context);
35 if (!document->frame())
38 ServiceWorkerContainerClient* client = static_cast<ServiceWorkerContainerClient*>(DocumentSupplement::from(document, supplementName()));
42 // If it's not provided yet, create it lazily.
43 document->DocumentSupplementable::provideSupplement(ServiceWorkerContainerClient::supplementName(), ServiceWorkerContainerClient::create(document->frame()->loader().client()->createServiceWorkerProvider()));
44 return static_cast<ServiceWorkerContainerClient*>(DocumentSupplement::from(document, supplementName()));
47 ASSERT(context->isWorkerGlobalScope());
48 return static_cast<ServiceWorkerContainerClient*>(WillBeHeapSupplement<WorkerClients>::from(toWorkerGlobalScope(context)->clients(), supplementName()));
51 ServiceWorkerContainerClient::ServiceWorkerContainerClient(PassOwnPtr<WebServiceWorkerProvider> provider)
52 : m_provider(provider)
56 void provideServiceWorkerContainerClientToWorker(WorkerClients* clients, PassOwnPtr<WebServiceWorkerProvider> provider)
58 clients->provideSupplement(ServiceWorkerContainerClient::supplementName(), ServiceWorkerContainerClient::create(provider));