Home | History | Annotate | Download | only in push_messaging
      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/push_messaging/PushManager.h"
      7 
      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"
     24 
     25 namespace blink {
     26 
     27 PushManager::PushManager()
     28 {
     29 }
     30 
     31 ScriptPromise PushManager::registerPushMessaging(ScriptState* scriptState, const String& senderId)
     32 {
     33     ASSERT(scriptState->executionContext()->isDocument());
     34 
     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."));
     38 
     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."));
     42 
     43     WebPushClient* client = PushController::clientFrom(document->page());
     44     ASSERT(client);
     45 
     46     RefPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::create(scriptState);
     47     ScriptPromise promise = resolver->promise();
     48     client->registerPushMessaging(senderId, new CallbackPromiseAdapter<PushRegistration, PushError>(resolver), serviceWorkerProvider);
     49     return promise;
     50 }
     51 
     52 } // namespace blink
     53