| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2016 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 "modules/notifications/NotificationManager.h" |
| 6 |
| 7 #include "core/dom/Document.h" |
| 8 #include "public/platform/Platform.h" |
| 9 #include "public/platform/ServiceRegistry.h" |
| 10 #include "public/platform/modules/permissions/permission_status.mojom.h" |
| 11 #include "public/platform/modules/permissions/permission_status.mojom-wtf.h" |
| 12 |
| 13 namespace blink { |
| 14 |
| 15 #define STATIC_ASSERT_ENUM(a, b) \ |
| 16 static_assert(static_cast<int>(a) == static_cast<int>(b), "mismatching enum:
" #a) |
| 17 |
| 18 STATIC_ASSERT_ENUM(mojom::PermissionStatus::GRANTED, mojom::wtf::PermissionStatu
s::GRANTED); |
| 19 STATIC_ASSERT_ENUM(mojom::PermissionStatus::DENIED, mojom::wtf::PermissionStatus
::DENIED); |
| 20 STATIC_ASSERT_ENUM(mojom::PermissionStatus::ASK, mojom::wtf::PermissionStatus::A
SK); |
| 21 STATIC_ASSERT_ENUM(mojom::PermissionStatus::LAST, mojom::wtf::PermissionStatus::
LAST); |
| 22 |
| 23 // static |
| 24 NotificationManager* NotificationManager::from(ExecutionContext* executionContex
t) |
| 25 { |
| 26 DCHECK(executionContext); |
| 27 DCHECK(executionContext->isContextThread()); |
| 28 |
| 29 NotificationManager* manager = nullptr; |
| 30 |
| 31 if (executionContext->isDocument()) { |
| 32 LocalFrame* localFrame = toDocument(executionContext)->frame(); |
| 33 DCHECK(localFrame); |
| 34 |
| 35 manager = static_cast<NotificationManager*>(Supplement<LocalFrame>::from
(localFrame, supplementName())); |
| 36 if (!manager) { |
| 37 manager = new NotificationManager(executionContext); |
| 38 Supplement<LocalFrame>::provideTo(*localFrame, supplementName(), man
ager); |
| 39 } |
| 40 } else { |
| 41 WorkerGlobalScope* workerGlobalScope = toWorkerGlobalScope(executionCont
ext); |
| 42 DCHECK(workerGlobalScope); |
| 43 |
| 44 manager = static_cast<NotificationManager*>(Supplement<WorkerGlobalScope
>::from(workerGlobalScope, supplementName())); |
| 45 if (!manager) { |
| 46 manager = new NotificationManager(executionContext); |
| 47 Supplement<WorkerGlobalScope>::provideTo(*workerGlobalScope, supplem
entName(), manager); |
| 48 } |
| 49 } |
| 50 |
| 51 return manager; |
| 52 } |
| 53 |
| 54 // static |
| 55 const char* NotificationManager::supplementName() |
| 56 { |
| 57 return "NotificationManager"; |
| 58 } |
| 59 |
| 60 NotificationManager::NotificationManager(ExecutionContext* executionContext) |
| 61 : ContextLifecycleObserver(executionContext) |
| 62 { |
| 63 Platform::current()->serviceRegistry()->connectToRemoteService(mojo::GetProx
y(&m_service)); |
| 64 } |
| 65 |
| 66 NotificationManager::~NotificationManager() |
| 67 { |
| 68 } |
| 69 |
| 70 mojom::PermissionStatus NotificationManager::permissionStatus() const |
| 71 { |
| 72 mojom::wtf::PermissionStatus wtfPermissionStatus; |
| 73 |
| 74 const bool result = |
| 75 m_service->GetPermissionStatus(getOriginString(), &wtfPermissionStatus); |
| 76 DCHECK(result); |
| 77 |
| 78 // Both PermissionStatus enums are generated from the same source, but by us
ing the WTF variant |
| 79 // of the NotificationService the same variant of the enum must be used, dif
ferent from other |
| 80 // users. Static assertions for equality are included in this file. |
| 81 return static_cast<mojom::PermissionStatus>(wtfPermissionStatus); |
| 82 } |
| 83 |
| 84 void NotificationManager::show( |
| 85 mojom::wtf::NotificationPtr notification, |
| 86 mojom::wtf::NotificationResourcesPtr notificationResources) |
| 87 { |
| 88 // TODO: Implement this method. |
| 89 } |
| 90 |
| 91 void NotificationManager::showPersistent( |
| 92 mojom::wtf::NotificationPtr notification, |
| 93 mojom::wtf::NotificationResourcesPtr notificationResources, |
| 94 const mojom::wtf::NotificationService::ShowPersistentCallback& callback) |
| 95 { |
| 96 m_service->ShowPersistent(getOriginString(), std::move(notification), std::m
ove(notificationResources), callback); |
| 97 } |
| 98 |
| 99 void NotificationManager::contextDestroyed() |
| 100 { |
| 101 m_service.reset(); |
| 102 } |
| 103 |
| 104 String NotificationManager::getOriginString() const |
| 105 { |
| 106 return getExecutionContext()->getSecurityOrigin()->toString(); |
| 107 } |
| 108 |
| 109 DEFINE_TRACE(NotificationManager) |
| 110 { |
| 111 ContextLifecycleObserver::trace(visitor); |
| 112 Supplement<LocalFrame>::trace(visitor); |
| 113 Supplement<WorkerGlobalScope>::trace(visitor); |
| 114 } |
| 115 |
| 116 } // namespace blink |
| OLD | NEW |