| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "config.h" | 5 #include "config.h" |
| 6 #include "ServiceWorkerContainerClient.h" | 6 #include "ServiceWorkerContainerClient.h" |
| 7 | 7 |
| 8 #include "core/dom/Document.h" | 8 #include "core/dom/Document.h" |
| 9 #include "core/dom/ExecutionContext.h" | 9 #include "core/dom/ExecutionContext.h" |
| 10 #include "core/frame/LocalFrame.h" | 10 #include "core/frame/LocalFrame.h" |
| 11 #include "core/loader/FrameLoaderClient.h" | 11 #include "core/loader/FrameLoaderClient.h" |
| 12 #include "core/workers/WorkerGlobalScope.h" | 12 #include "core/workers/WorkerGlobalScope.h" |
| 13 #include "public/platform/modules/serviceworker/WebServiceWorkerProvider.h" | 13 #include "public/platform/modules/serviceworker/WebServiceWorkerProvider.h" |
| 14 | 14 |
| 15 namespace blink { | 15 namespace blink { |
| 16 | 16 |
| 17 PassOwnPtrWillBeRawPtr<ServiceWorkerContainerClient> ServiceWorkerContainerClien
t::create(PassOwnPtr<WebServiceWorkerProvider> provider) | 17 PassOwnPtrWillBeRawPtr<ServiceWorkerContainerClient> ServiceWorkerContainerClien
t::create(PassOwnPtr<WebServiceWorkerProvider> provider) |
| 18 { | 18 { |
| 19 return adoptPtrWillBeNoop(new ServiceWorkerContainerClient(provider)); | 19 return adoptPtrWillBeNoop(new ServiceWorkerContainerClient(provider)); |
| 20 } | 20 } |
| 21 | 21 |
| 22 ServiceWorkerContainerClient::ServiceWorkerContainerClient(PassOwnPtr<WebService
WorkerProvider> provider) |
| 23 : m_provider(provider) |
| 24 { |
| 25 } |
| 26 |
| 22 ServiceWorkerContainerClient::~ServiceWorkerContainerClient() | 27 ServiceWorkerContainerClient::~ServiceWorkerContainerClient() |
| 23 { | 28 { |
| 24 } | 29 } |
| 25 | 30 |
| 26 const char* ServiceWorkerContainerClient::supplementName() | 31 const char* ServiceWorkerContainerClient::supplementName() |
| 27 { | 32 { |
| 28 return "ServiceWorkerContainerClient"; | 33 return "ServiceWorkerContainerClient"; |
| 29 } | 34 } |
| 30 | 35 |
| 31 ServiceWorkerContainerClient* ServiceWorkerContainerClient::from(ExecutionContex
t* context) | 36 ServiceWorkerContainerClient* ServiceWorkerContainerClient::from(ExecutionContex
t* context) |
| 32 { | 37 { |
| 33 if (context->isDocument()) { | 38 if (context->isWorkerGlobalScope()) { |
| 34 Document* document = toDocument(context); | 39 WorkerClients* clients = toWorkerGlobalScope(context)->clients(); |
| 35 if (!document->frame()) | 40 ASSERT(clients); |
| 36 return 0; | 41 return static_cast<ServiceWorkerContainerClient*>(WillBeHeapSupplement<W
orkerClients>::from(clients, supplementName())); |
| 42 } |
| 43 Document* document = toDocument(context); |
| 44 if (!document->frame()) |
| 45 return nullptr; |
| 37 | 46 |
| 38 ServiceWorkerContainerClient* client = static_cast<ServiceWorkerContaine
rClient*>(WillBeHeapSupplement<Document>::from(document, supplementName())); | 47 ServiceWorkerContainerClient* client = static_cast<ServiceWorkerContainerCli
ent*>(WillBeHeapSupplement<Document>::from(document, supplementName())); |
| 39 if (client) | 48 if (!client) { |
| 40 return client; | 49 client = new ServiceWorkerContainerClient(document->frame()->loader().cl
ient()->createServiceWorkerProvider()); |
| 41 | 50 WillBeHeapSupplement<Document>::provideTo(*document, supplementName(), a
doptPtrWillBeNoop(client)); |
| 42 // If it's not provided yet, create it lazily. | |
| 43 document->WillBeHeapSupplementable<Document>::provideSupplement(ServiceW
orkerContainerClient::supplementName(), ServiceWorkerContainerClient::create(doc
ument->frame()->loader().client()->createServiceWorkerProvider())); | |
| 44 return static_cast<ServiceWorkerContainerClient*>(WillBeHeapSupplement<D
ocument>::from(document, supplementName())); | |
| 45 } | 51 } |
| 46 | 52 return client; |
| 47 WorkerClients* clients = toWorkerGlobalScope(context)->clients(); | |
| 48 ASSERT(clients); | |
| 49 return static_cast<ServiceWorkerContainerClient*>(WillBeHeapSupplement<Worke
rClients>::from(clients, supplementName())); | |
| 50 } | |
| 51 | |
| 52 ServiceWorkerContainerClient::ServiceWorkerContainerClient(PassOwnPtr<WebService
WorkerProvider> provider) | |
| 53 : m_provider(provider) | |
| 54 { | |
| 55 } | 53 } |
| 56 | 54 |
| 57 void provideServiceWorkerContainerClientToWorker(WorkerClients* clients, PassOwn
Ptr<WebServiceWorkerProvider> provider) | 55 void provideServiceWorkerContainerClientToWorker(WorkerClients* clients, PassOwn
Ptr<WebServiceWorkerProvider> provider) |
| 58 { | 56 { |
| 59 clients->provideSupplement(ServiceWorkerContainerClient::supplementName(), S
erviceWorkerContainerClient::create(provider)); | 57 clients->provideSupplement(ServiceWorkerContainerClient::supplementName(), S
erviceWorkerContainerClient::create(provider)); |
| 60 } | 58 } |
| 61 | 59 |
| 62 } // namespace blink | 60 } // namespace blink |
| OLD | NEW |