Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / modules / serviceworkers / ServiceWorkerClients.cpp
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 "modules/serviceworkers/ServiceWorkerClients.h"
7
8 #include "bindings/v8/CallbackPromiseAdapter.h"
9 #include "bindings/v8/ScriptPromiseResolver.h"
10 #include "bindings/v8/ScriptPromiseResolverWithContext.h"
11 #include "modules/serviceworkers/Client.h"
12 #include "modules/serviceworkers/ServiceWorkerError.h"
13 #include "modules/serviceworkers/ServiceWorkerGlobalScopeClient.h"
14 #include "public/platform/WebServiceWorkerClientsInfo.h"
15 #include "wtf/RefPtr.h"
16 #include "wtf/Vector.h"
17
18 namespace WebCore {
19
20 namespace {
21
22     class ClientArray {
23     public:
24         typedef blink::WebServiceWorkerClientsInfo WebType;
25         static Vector<RefPtr<Client> > from(ScriptPromiseResolverWithContext*, WebType* webClientsRaw)
26         {
27             OwnPtr<WebType> webClients = adoptPtr(webClientsRaw);
28             Vector<RefPtr<Client> > clients;
29             for (size_t i = 0; i < webClients->clientIDs.size(); ++i) {
30                 clients.append(Client::create(webClients->clientIDs[i]));
31             }
32             return clients;
33         }
34
35     private:
36         WTF_MAKE_NONCOPYABLE(ClientArray);
37         ClientArray() WTF_DELETED_FUNCTION;
38     };
39
40 } // namespace
41
42 PassRefPtr<ServiceWorkerClients> ServiceWorkerClients::create()
43 {
44     return adoptRef(new ServiceWorkerClients());
45 }
46
47 ServiceWorkerClients::ServiceWorkerClients()
48 {
49     ScriptWrappable::init(this);
50 }
51
52 ServiceWorkerClients::~ServiceWorkerClients()
53 {
54 }
55
56 ScriptPromise ServiceWorkerClients::getServiced(ExecutionContext* context)
57 {
58     RefPtr<ScriptPromiseResolverWithContext> resolver = ScriptPromiseResolverWithContext::create(ScriptState::current(toIsolate(context)));
59     ServiceWorkerGlobalScopeClient::from(context)->getClients(new CallbackPromiseAdapter<ClientArray, ServiceWorkerError>(resolver));
60     return resolver->promise();
61 }
62
63 } // namespace WebCore