1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
|
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "third_party/blink/renderer/modules/notifications/service_worker_registration_notifications.h"
#include <utility>
#include "base/memory/scoped_refptr.h"
#include "base/metrics/histogram_functions.h"
#include "third_party/blink/public/platform/platform.h"
#include "third_party/blink/public/platform/web_security_origin.h"
#include "third_party/blink/renderer/bindings/core/v8/script_promise_resolver.h"
#include "third_party/blink/renderer/bindings/modules/v8/v8_get_notification_options.h"
#include "third_party/blink/renderer/bindings/modules/v8/v8_notification_options.h"
#include "third_party/blink/renderer/core/execution_context/execution_context.h"
#include "third_party/blink/renderer/modules/notifications/notification.h"
#include "third_party/blink/renderer/modules/notifications/notification_data.h"
#include "third_party/blink/renderer/modules/notifications/notification_manager.h"
#include "third_party/blink/renderer/modules/notifications/notification_metrics.h"
#include "third_party/blink/renderer/modules/notifications/notification_resources_loader.h"
#include "third_party/blink/renderer/modules/service_worker/service_worker_registration.h"
#include "third_party/blink/renderer/platform/bindings/exception_state.h"
#include "third_party/blink/renderer/platform/heap/garbage_collected.h"
namespace blink {
ServiceWorkerRegistrationNotifications::ServiceWorkerRegistrationNotifications(
ExecutionContext* context,
ServiceWorkerRegistration* registration)
: Supplement(*registration), ExecutionContextLifecycleObserver(context) {}
ScriptPromise<IDLUndefined>
ServiceWorkerRegistrationNotifications::showNotification(
ScriptState* script_state,
ServiceWorkerRegistration& registration,
const String& title,
const NotificationOptions* options,
ExceptionState& exception_state) {
ExecutionContext* execution_context = ExecutionContext::From(script_state);
if (execution_context->IsInFencedFrame()) {
exception_state.ThrowDOMException(
DOMExceptionCode::kNotAllowedError,
"showNotification() is not allowed in fenced frames.");
return EmptyPromise();
}
// If context object's active worker is null, reject the promise with a
// TypeError exception.
if (!registration.active()) {
RecordPersistentNotificationDisplayResult(
PersistentNotificationDisplayResult::kRegistrationNotActive);
exception_state.ThrowTypeError(
"No active registration available on "
"the ServiceWorkerRegistration.");
return EmptyPromise();
}
// If permission for notification's origin is not "granted", reject the
// promise with a TypeError exception, and terminate these substeps.
if (NotificationManager::From(execution_context)->GetPermissionStatus() !=
mojom::blink::PermissionStatus::GRANTED) {
RecordPersistentNotificationDisplayResult(
PersistentNotificationDisplayResult::kPermissionNotGranted);
exception_state.ThrowTypeError(
"No notification permission has been granted for this origin.");
return EmptyPromise();
}
// Validate the developer-provided options to get the NotificationData.
mojom::blink::NotificationDataPtr data = CreateNotificationData(
execution_context, title, options, exception_state);
if (exception_state.HadException())
return EmptyPromise();
// Log number of actions developer provided in linear histogram:
// 0 -> underflow bucket,
// 1-16 -> distinct buckets,
// 17+ -> overflow bucket.
base::UmaHistogramExactLinear(
"Notifications.PersistentNotificationActionCount",
options->actions().size(), 17);
auto* resolver = MakeGarbageCollected<ScriptPromiseResolver<IDLUndefined>>(
script_state, exception_state.GetContext());
auto promise = resolver->Promise();
ServiceWorkerRegistrationNotifications::From(execution_context, registration)
.PrepareShow(std::move(data), resolver);
return promise;
}
ScriptPromise<IDLSequence<Notification>>
ServiceWorkerRegistrationNotifications::getNotifications(
ScriptState* script_state,
ServiceWorkerRegistration& registration,
const GetNotificationOptions* options) {
auto* resolver =
MakeGarbageCollected<ScriptPromiseResolver<IDLSequence<Notification>>>(
script_state);
auto promise = resolver->Promise();
ExecutionContext* execution_context = ExecutionContext::From(script_state);
NotificationManager::From(execution_context)
->GetNotifications(registration.RegistrationId(), options->tag(),
options->includeTriggered(), WrapPersistent(resolver));
return promise;
}
void ServiceWorkerRegistrationNotifications::ContextDestroyed() {
for (auto loader : loaders_)
loader->Stop();
}
void ServiceWorkerRegistrationNotifications::Trace(Visitor* visitor) const {
visitor->Trace(loaders_);
Supplement<ServiceWorkerRegistration>::Trace(visitor);
ExecutionContextLifecycleObserver::Trace(visitor);
}
const char ServiceWorkerRegistrationNotifications::kSupplementName[] =
"ServiceWorkerRegistrationNotifications";
ServiceWorkerRegistrationNotifications&
ServiceWorkerRegistrationNotifications::From(
ExecutionContext* execution_context,
ServiceWorkerRegistration& registration) {
ServiceWorkerRegistrationNotifications* supplement =
Supplement<ServiceWorkerRegistration>::From<
ServiceWorkerRegistrationNotifications>(registration);
if (!supplement) {
supplement = MakeGarbageCollected<ServiceWorkerRegistrationNotifications>(
execution_context, ®istration);
ProvideTo(registration, supplement);
}
return *supplement;
}
void ServiceWorkerRegistrationNotifications::PrepareShow(
mojom::blink::NotificationDataPtr data,
ScriptPromiseResolver<IDLUndefined>* resolver) {
scoped_refptr<const SecurityOrigin> origin =
GetExecutionContext()->GetSecurityOrigin();
NotificationResourcesLoader* loader =
MakeGarbageCollected<NotificationResourcesLoader>(WTF::BindOnce(
&ServiceWorkerRegistrationNotifications::DidLoadResources,
WrapWeakPersistent(this), std::move(origin), data->Clone(),
WrapPersistent(resolver)));
loaders_.insert(loader);
loader->Start(GetExecutionContext(), *data);
}
void ServiceWorkerRegistrationNotifications::DidLoadResources(
scoped_refptr<const SecurityOrigin> origin,
mojom::blink::NotificationDataPtr data,
ScriptPromiseResolver<IDLUndefined>* resolver,
NotificationResourcesLoader* loader) {
DCHECK(loaders_.Contains(loader));
NotificationManager::From(GetExecutionContext())
->DisplayPersistentNotification(GetSupplementable()->RegistrationId(),
std::move(data), loader->GetResources(),
WrapPersistent(resolver));
loaders_.erase(loader);
}
} // namespace blink
|