Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / Source / web / NotificationPermissionClientImpl.cpp
blob2881968e0ca62e0fc9806a1ed1fc04e4577ac140
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.
5 #include "config.h"
6 #include "web/NotificationPermissionClientImpl.h"
8 #include "bindings/core/v8/ScriptPromiseResolver.h"
9 #include "bindings/core/v8/ScriptState.h"
10 #include "core/dom/Document.h"
11 #include "core/dom/ExecutionContext.h"
12 #include "modules/notifications/Notification.h"
13 #include "modules/notifications/NotificationPermissionCallback.h"
14 #include "public/web/WebFrameClient.h"
15 #include "public/web/modules/notifications/WebNotificationPermissionCallback.h"
16 #include "web/WebLocalFrameImpl.h"
18 namespace blink {
20 namespace {
22 class WebNotificationPermissionCallbackImpl : public WebNotificationPermissionCallback {
23 public:
24 WebNotificationPermissionCallbackImpl(ScriptPromiseResolver* resolver, NotificationPermissionCallback* deprecatedCallback)
25 : m_resolver(resolver)
26 , m_deprecatedCallback(deprecatedCallback)
30 ~WebNotificationPermissionCallbackImpl() override { }
32 void permissionRequestComplete(WebNotificationPermission permission) override
34 String permissionString = Notification::permissionString(permission);
35 if (m_deprecatedCallback)
36 m_deprecatedCallback->handleEvent(permissionString);
38 m_resolver->resolve(permissionString);
41 private:
42 Persistent<ScriptPromiseResolver> m_resolver;
43 Persistent<NotificationPermissionCallback> m_deprecatedCallback;
46 } // namespace
48 PassOwnPtrWillBeRawPtr<NotificationPermissionClientImpl> NotificationPermissionClientImpl::create()
50 return adoptPtrWillBeNoop(new NotificationPermissionClientImpl());
53 NotificationPermissionClientImpl::NotificationPermissionClientImpl()
57 NotificationPermissionClientImpl::~NotificationPermissionClientImpl()
61 ScriptPromise NotificationPermissionClientImpl::requestPermission(ScriptState* scriptState, NotificationPermissionCallback* deprecatedCallback)
63 ASSERT(scriptState);
65 ExecutionContext* context = scriptState->executionContext();
66 ASSERT(context && context->isDocument());
68 Document* document = toDocument(context);
69 WebLocalFrameImpl* webFrame = WebLocalFrameImpl::fromFrame(document->frame());
71 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState);
72 ScriptPromise promise = resolver->promise();
74 webFrame->client()->requestNotificationPermission(WebSecurityOrigin(context->securityOrigin()), new WebNotificationPermissionCallbackImpl(resolver, deprecatedCallback));
76 return promise;
79 } // namespace blink