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.
4
5#include "config.h"
6#include "ServiceWorkerContainerClient.h"
7
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"
14
15namespace blink {
16
17PassOwnPtrWillBeRawPtr<ServiceWorkerContainerClient> ServiceWorkerContainerClient::create(PassOwnPtr<WebServiceWorkerProvider> provider)
18{
19    return adoptPtrWillBeNoop(new ServiceWorkerContainerClient(provider));
20}
21
22ServiceWorkerContainerClient::~ServiceWorkerContainerClient()
23{
24}
25
26const char* ServiceWorkerContainerClient::supplementName()
27{
28    return "ServiceWorkerContainerClient";
29}
30
31ServiceWorkerContainerClient* ServiceWorkerContainerClient::from(ExecutionContext* context)
32{
33    if (context->isDocument()) {
34        Document* document = toDocument(context);
35        if (!document->frame())
36            return 0;
37
38        ServiceWorkerContainerClient* client = static_cast<ServiceWorkerContainerClient*>(DocumentSupplement::from(document, supplementName()));
39        if (client)
40            return client;
41
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()));
45    }
46
47    ASSERT(context->isWorkerGlobalScope());
48    return static_cast<ServiceWorkerContainerClient*>(WillBeHeapSupplement<WorkerClients>::from(toWorkerGlobalScope(context)->clients(), supplementName()));
49}
50
51ServiceWorkerContainerClient::ServiceWorkerContainerClient(PassOwnPtr<WebServiceWorkerProvider> provider)
52    : m_provider(provider)
53{
54}
55
56void provideServiceWorkerContainerClientToWorker(WorkerClients* clients, PassOwnPtr<WebServiceWorkerProvider> provider)
57{
58    clients->provideSupplement(ServiceWorkerContainerClient::supplementName(), ServiceWorkerContainerClient::create(provider));
59}
60
61} // namespace blink
62