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 "modules/push_messaging/PushManager.h"
8 #include "bindings/core/v8/CallbackPromiseAdapter.h"
9 #include "bindings/core/v8/ScriptPromise.h"
10 #include "bindings/core/v8/ScriptPromiseResolver.h"
11 #include "bindings/core/v8/ScriptState.h"
12 #include "core/dom/DOMException.h"
13 #include "core/dom/Document.h"
14 #include "core/dom/ExceptionCode.h"
15 #include "core/dom/ExecutionContext.h"
16 #include "core/frame/LocalDOMWindow.h"
17 #include "modules/push_messaging/PushController.h"
18 #include "modules/push_messaging/PushError.h"
19 #include "modules/push_messaging/PushRegistration.h"
20 #include "modules/serviceworkers/NavigatorServiceWorker.h"
21 #include "modules/serviceworkers/ServiceWorkerContainer.h"
22 #include "public/platform/WebPushClient.h"
23 #include "wtf/RefPtr.h"
27 PushManager::PushManager()
31 ScriptPromise PushManager::registerPushMessaging(ScriptState* scriptState, const String& senderId)
33 ASSERT(scriptState->executionContext()->isDocument());
35 Document* document = toDocument(scriptState->executionContext());
36 if (!document->domWindow() || !document->page())
37 return ScriptPromise::rejectWithDOMException(scriptState, DOMException::create(AbortError, "Document is detached from window."));
39 WebServiceWorkerProvider* serviceWorkerProvider = NavigatorServiceWorker::serviceWorker(document->domWindow()->navigator())->provider();
40 if (!serviceWorkerProvider)
41 return ScriptPromise::rejectWithDOMException(scriptState, DOMException::create(AbortError, "No Service Worker installed for this document."));
43 WebPushClient* client = PushController::clientFrom(document->page());
46 RefPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::create(scriptState);
47 ScriptPromise promise = resolver->promise();
48 client->registerPushMessaging(senderId, new CallbackPromiseAdapter<PushRegistration, PushError>(resolver), serviceWorkerProvider);