File: Notification.cpp

package info (click to toggle)
chromium-browser 57.0.2987.98-1~deb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 2,637,852 kB
  • ctags: 2,544,394
  • sloc: cpp: 12,815,961; ansic: 3,676,222; python: 1,147,112; asm: 526,608; java: 523,212; xml: 286,794; perl: 92,654; sh: 86,408; objc: 73,271; makefile: 27,698; cs: 18,487; yacc: 13,031; tcl: 12,957; pascal: 4,875; ml: 4,716; lex: 3,904; sql: 3,862; ruby: 1,982; lisp: 1,508; php: 1,368; exp: 404; awk: 325; csh: 117; jsp: 39; sed: 37
file content (406 lines) | stat: -rw-r--r-- 13,233 bytes parent folder | download
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
/*
 * Copyright (C) 2013 Google Inc. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 *     * Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above
 * copyright notice, this list of conditions and the following disclaimer
 * in the documentation and/or other materials provided with the
 * distribution.
 *     * Neither the name of Google Inc. nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include "modules/notifications/Notification.h"

#include "bindings/core/v8/ExceptionState.h"
#include "bindings/core/v8/ScriptState.h"
#include "bindings/core/v8/SerializedScriptValueFactory.h"
#include "bindings/modules/v8/V8NotificationAction.h"
#include "core/dom/Document.h"
#include "core/dom/DocumentUserGestureToken.h"
#include "core/dom/ExecutionContext.h"
#include "core/dom/ExecutionContextTask.h"
#include "core/dom/ScopedWindowFocusAllowedIndicator.h"
#include "core/dom/TaskRunnerHelper.h"
#include "core/events/Event.h"
#include "core/frame/UseCounter.h"
#include "modules/notifications/NotificationAction.h"
#include "modules/notifications/NotificationData.h"
#include "modules/notifications/NotificationManager.h"
#include "modules/notifications/NotificationOptions.h"
#include "modules/notifications/NotificationResourcesLoader.h"
#include "platform/RuntimeEnabledFeatures.h"
#include "platform/UserGestureIndicator.h"
#include "public/platform/Platform.h"
#include "public/platform/WebSecurityOrigin.h"
#include "public/platform/modules/notifications/WebNotificationAction.h"
#include "public/platform/modules/notifications/WebNotificationConstants.h"
#include "public/platform/modules/notifications/WebNotificationManager.h"
#include "wtf/Assertions.h"
#include "wtf/Functional.h"

namespace blink {
namespace {

WebNotificationManager* notificationManager() {
  return Platform::current()->notificationManager();
}

}  // namespace

Notification* Notification::create(ExecutionContext* context,
                                   const String& title,
                                   const NotificationOptions& options,
                                   ExceptionState& exceptionState) {
  // The Notification constructor may be disabled through a runtime feature when
  // the platform does not support non-persistent notifications.
  if (!RuntimeEnabledFeatures::notificationConstructorEnabled()) {
    exceptionState.throwTypeError(
        "Illegal constructor. Use ServiceWorkerRegistration.showNotification() "
        "instead.");
    return nullptr;
  }

  // The Notification constructor may not be used in Service Worker contexts.
  if (context->isServiceWorkerGlobalScope()) {
    exceptionState.throwTypeError("Illegal constructor.");
    return nullptr;
  }

  if (!options.actions().isEmpty()) {
    exceptionState.throwTypeError(
        "Actions are only supported for persistent notifications shown using "
        "ServiceWorkerRegistration.showNotification().");
    return nullptr;
  }

  if (context->isSecureContext()) {
    UseCounter::count(context, UseCounter::NotificationSecureOrigin);
    if (context->isDocument())
      UseCounter::countCrossOriginIframe(
          *toDocument(context), UseCounter::NotificationAPISecureOriginIframe);
  } else {
    UseCounter::count(context, UseCounter::NotificationInsecureOrigin);
    if (context->isDocument())
      UseCounter::countCrossOriginIframe(
          *toDocument(context),
          UseCounter::NotificationAPIInsecureOriginIframe);
  }

  WebNotificationData data =
      createWebNotificationData(context, title, options, exceptionState);
  if (exceptionState.hadException())
    return nullptr;

  Notification* notification =
      new Notification(context, Type::NonPersistent, data);
  notification->schedulePrepareShow();
  return notification;
}

Notification* Notification::create(ExecutionContext* context,
                                   const String& notificationId,
                                   const WebNotificationData& data,
                                   bool showing) {
  Notification* notification =
      new Notification(context, Type::Persistent, data);
  notification->setState(showing ? State::Showing : State::Closed);
  notification->setNotificationId(notificationId);
  return notification;
}

Notification::Notification(ExecutionContext* context,
                           Type type,
                           const WebNotificationData& data)
    : ContextLifecycleObserver(context),
      m_type(type),
      m_state(State::Loading),
      m_data(data) {
  DCHECK(notificationManager());
}

Notification::~Notification() {}

void Notification::schedulePrepareShow() {
  DCHECK_EQ(m_state, State::Loading);
  DCHECK(!m_prepareShowMethodRunner);

  m_prepareShowMethodRunner =
      AsyncMethodRunner<Notification>::create(this, &Notification::prepareShow);
  m_prepareShowMethodRunner->runAsync();
}

void Notification::prepareShow() {
  DCHECK_EQ(m_state, State::Loading);
  if (NotificationManager::from(getExecutionContext())
          ->permissionStatus(getExecutionContext()) !=
      mojom::blink::PermissionStatus::GRANTED) {
    dispatchErrorEvent();
    return;
  }

  m_loader = new NotificationResourcesLoader(
      WTF::bind(&Notification::didLoadResources, wrapWeakPersistent(this)));
  m_loader->start(getExecutionContext(), m_data);
}

void Notification::didLoadResources(NotificationResourcesLoader* loader) {
  DCHECK_EQ(loader, m_loader.get());

  SecurityOrigin* origin = getExecutionContext()->getSecurityOrigin();
  DCHECK(origin);

  notificationManager()->show(WebSecurityOrigin(origin), m_data,
                              loader->getResources(), this);
  m_loader.clear();

  m_state = State::Showing;
}

void Notification::close() {
  if (m_state != State::Showing)
    return;

  // Schedule the "close" event to be fired for non-persistent notifications.
  // Persistent notifications won't get such events for programmatic closes.
  if (m_type == Type::NonPersistent) {
    getExecutionContext()->postTask(
        TaskType::UserInteraction, BLINK_FROM_HERE,
        createSameThreadTask(&Notification::dispatchCloseEvent,
                             wrapPersistent(this)));
    m_state = State::Closing;

    notificationManager()->close(this);
    return;
  }

  m_state = State::Closed;

  SecurityOrigin* origin = getExecutionContext()->getSecurityOrigin();
  DCHECK(origin);

  notificationManager()->closePersistent(WebSecurityOrigin(origin), m_data.tag,
                                         m_notificationId);
}

void Notification::dispatchShowEvent() {
  dispatchEvent(Event::create(EventTypeNames::show));
}

void Notification::dispatchClickEvent() {
  ExecutionContext* context = getExecutionContext();
  UserGestureIndicator gestureIndicator(DocumentUserGestureToken::create(
      context->isDocument() ? toDocument(context) : nullptr,
      UserGestureToken::NewGesture));
  ScopedWindowFocusAllowedIndicator windowFocusAllowed(getExecutionContext());
  dispatchEvent(Event::create(EventTypeNames::click));
}

void Notification::dispatchErrorEvent() {
  dispatchEvent(Event::create(EventTypeNames::error));
}

void Notification::dispatchCloseEvent() {
  // The notification should be Showing if the user initiated the close, or it
  // should be Closing if the developer initiated the close.
  if (m_state != State::Showing && m_state != State::Closing)
    return;

  m_state = State::Closed;
  dispatchEvent(Event::create(EventTypeNames::close));
}

String Notification::title() const {
  return m_data.title;
}

String Notification::dir() const {
  switch (m_data.direction) {
    case WebNotificationData::DirectionLeftToRight:
      return "ltr";
    case WebNotificationData::DirectionRightToLeft:
      return "rtl";
    case WebNotificationData::DirectionAuto:
      return "auto";
  }

  NOTREACHED();
  return String();
}

String Notification::lang() const {
  return m_data.lang;
}

String Notification::body() const {
  return m_data.body;
}

String Notification::tag() const {
  return m_data.tag;
}

String Notification::image() const {
  return m_data.image.string();
}

String Notification::icon() const {
  return m_data.icon.string();
}

String Notification::badge() const {
  return m_data.badge.string();
}

NavigatorVibration::VibrationPattern Notification::vibrate() const {
  NavigatorVibration::VibrationPattern pattern;
  pattern.appendRange(m_data.vibrate.begin(), m_data.vibrate.end());

  return pattern;
}

DOMTimeStamp Notification::timestamp() const {
  return m_data.timestamp;
}

bool Notification::renotify() const {
  return m_data.renotify;
}

bool Notification::silent() const {
  return m_data.silent;
}

bool Notification::requireInteraction() const {
  return m_data.requireInteraction;
}

ScriptValue Notification::data(ScriptState* scriptState) {
  const WebVector<char>& serializedData = m_data.data;
  RefPtr<SerializedScriptValue> serializedValue = SerializedScriptValue::create(
      serializedData.data(), serializedData.size());

  return ScriptValue(scriptState,
                     serializedValue->deserialize(scriptState->isolate()));
}

Vector<v8::Local<v8::Value>> Notification::actions(
    ScriptState* scriptState) const {
  Vector<v8::Local<v8::Value>> actions;
  actions.grow(m_data.actions.size());

  for (size_t i = 0; i < m_data.actions.size(); ++i) {
    NotificationAction action;

    switch (m_data.actions[i].type) {
      case WebNotificationAction::Button:
        action.setType("button");
        break;
      case WebNotificationAction::Text:
        action.setType("text");
        break;
      default:
        NOTREACHED() << "Unknown action type: " << m_data.actions[i].type;
    }

    action.setAction(m_data.actions[i].action);
    action.setTitle(m_data.actions[i].title);
    action.setIcon(m_data.actions[i].icon.string());
    action.setPlaceholder(m_data.actions[i].placeholder);

    // Both the Action dictionaries themselves and the sequence they'll be
    // returned in are expected to the frozen. This cannot be done with WebIDL.
    actions[i] =
        freezeV8Object(ToV8(action, scriptState), scriptState->isolate());
  }

  return actions;
}

String Notification::permissionString(
    mojom::blink::PermissionStatus permission) {
  switch (permission) {
    case mojom::blink::PermissionStatus::GRANTED:
      return "granted";
    case mojom::blink::PermissionStatus::DENIED:
      return "denied";
    case mojom::blink::PermissionStatus::ASK:
      return "default";
  }

  NOTREACHED();
  return "denied";
}

String Notification::permission(ExecutionContext* context) {
  return permissionString(
      NotificationManager::from(context)->permissionStatus(context));
}

ScriptPromise Notification::requestPermission(
    ScriptState* scriptState,
    NotificationPermissionCallback* deprecatedCallback) {
  return NotificationManager::from(scriptState->getExecutionContext())
      ->requestPermission(scriptState, deprecatedCallback);
}

size_t Notification::maxActions() {
  return kWebNotificationMaxActions;
}

DispatchEventResult Notification::dispatchEventInternal(Event* event) {
  DCHECK(getExecutionContext()->isContextThread());
  return EventTarget::dispatchEventInternal(event);
}

const AtomicString& Notification::interfaceName() const {
  return EventTargetNames::Notification;
}

void Notification::contextDestroyed(ExecutionContext*) {
  notificationManager()->notifyDelegateDestroyed(this);

  m_state = State::Closed;

  if (m_prepareShowMethodRunner)
    m_prepareShowMethodRunner->stop();

  if (m_loader)
    m_loader->stop();
}

bool Notification::hasPendingActivity() const {
  // Non-persistent notification can receive events until they've been closed.
  // Persistent notifications should be subject to regular garbage collection.
  if (m_type == Type::NonPersistent)
    return m_state != State::Closed;

  return false;
}

DEFINE_TRACE(Notification) {
  visitor->trace(m_prepareShowMethodRunner);
  visitor->trace(m_loader);
  EventTargetWithInlineData::trace(visitor);
  ContextLifecycleObserver::trace(visitor);
}

}  // namespace blink