Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(10)

Side by Side Diff: third_party/WebKit/Source/modules/notifications/Notification.cpp

Issue 1904163002: Move Web Notifications to use Mojo Base URL: https://chromium.googlesource.com/chromium/src.git@skbitmap-blink
Patch Set: resolves the promise Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2013 Google Inc. All rights reserved. 2 * Copyright (C) 2013 Google Inc. All rights reserved.
3 * 3 *
4 * Redistribution and use in source and binary forms, with or without 4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are 5 * modification, are permitted provided that the following conditions are
6 * met: 6 * met:
7 * 7 *
8 * * Redistributions of source code must retain the above copyright 8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer. 9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above 10 * * Redistributions in binary form must reproduce the above
(...skipping 23 matching lines...) Expand all
34 #include "bindings/core/v8/ScriptState.h" 34 #include "bindings/core/v8/ScriptState.h"
35 #include "bindings/core/v8/SerializedScriptValueFactory.h" 35 #include "bindings/core/v8/SerializedScriptValueFactory.h"
36 #include "core/dom/Document.h" 36 #include "core/dom/Document.h"
37 #include "core/dom/ExecutionContext.h" 37 #include "core/dom/ExecutionContext.h"
38 #include "core/dom/ExecutionContextTask.h" 38 #include "core/dom/ExecutionContextTask.h"
39 #include "core/dom/ScopedWindowFocusAllowedIndicator.h" 39 #include "core/dom/ScopedWindowFocusAllowedIndicator.h"
40 #include "core/events/Event.h" 40 #include "core/events/Event.h"
41 #include "core/frame/UseCounter.h" 41 #include "core/frame/UseCounter.h"
42 #include "modules/notifications/NotificationAction.h" 42 #include "modules/notifications/NotificationAction.h"
43 #include "modules/notifications/NotificationData.h" 43 #include "modules/notifications/NotificationData.h"
44 #include "modules/notifications/NotificationManager.h"
44 #include "modules/notifications/NotificationOptions.h" 45 #include "modules/notifications/NotificationOptions.h"
45 #include "modules/notifications/NotificationPermissionClient.h" 46 #include "modules/notifications/NotificationPermissionClient.h"
46 #include "modules/notifications/NotificationResourcesLoader.h" 47 #include "modules/notifications/NotificationResourcesLoader.h"
47 #include "platform/RuntimeEnabledFeatures.h" 48 #include "platform/RuntimeEnabledFeatures.h"
48 #include "platform/UserGestureIndicator.h" 49 #include "platform/UserGestureIndicator.h"
49 #include "public/platform/Platform.h" 50 #include "public/platform/Platform.h"
50 #include "public/platform/WebSecurityOrigin.h" 51 #include "public/platform/WebSecurityOrigin.h"
51 #include "public/platform/WebString.h" 52 #include "public/platform/WebString.h"
52 #include "public/platform/modules/notifications/WebNotificationAction.h"
53 #include "public/platform/modules/notifications/WebNotificationConstants.h" 53 #include "public/platform/modules/notifications/WebNotificationConstants.h"
54 #include "public/platform/modules/notifications/WebNotificationManager.h" 54 #include "public/platform/modules/permissions/permission_status.mojom.h"
55 #include "wtf/Functional.h" 55 #include "wtf/Functional.h"
56 56
57 namespace blink { 57 namespace blink {
58 namespace { 58 namespace {
59 59
60 const int64_t kInvalidPersistentId = -1; 60 const int64_t kInvalidPersistentId = -1;
61 61
62 WebNotificationManager* notificationManager()
63 {
64 return Platform::current()->notificationManager();
65 }
66
67 } // namespace 62 } // namespace
68 63
69 Notification* Notification::create(ExecutionContext* context, const String& titl e, const NotificationOptions& options, ExceptionState& exceptionState) 64 Notification* Notification::create(ExecutionContext* context, const String& titl e, const NotificationOptions& options, ExceptionState& exceptionState)
70 { 65 {
71 // The Web Notification constructor may be disabled through a runtime featur e. The 66 // The Web Notification constructor may be disabled through a runtime featur e. The
72 // behavior of the constructor is changing, but not completely agreed upon y et. 67 // behavior of the constructor is changing, but not completely agreed upon y et.
73 if (!RuntimeEnabledFeatures::notificationConstructorEnabled()) { 68 if (!RuntimeEnabledFeatures::notificationConstructorEnabled()) {
74 exceptionState.throwTypeError("Illegal constructor. Use ServiceWorkerReg istration.showNotification() instead."); 69 exceptionState.throwTypeError("Illegal constructor. Use ServiceWorkerReg istration.showNotification() instead.");
75 return nullptr; 70 return nullptr;
76 } 71 }
(...skipping 13 matching lines...) Expand all
90 if (context->isSecureContext(insecureOriginMessage)) { 85 if (context->isSecureContext(insecureOriginMessage)) {
91 UseCounter::count(context, UseCounter::NotificationSecureOrigin); 86 UseCounter::count(context, UseCounter::NotificationSecureOrigin);
92 if (context->isDocument()) 87 if (context->isDocument())
93 UseCounter::countCrossOriginIframe(*toDocument(context), UseCounter ::NotificationAPISecureOriginIframe); 88 UseCounter::countCrossOriginIframe(*toDocument(context), UseCounter ::NotificationAPISecureOriginIframe);
94 } else { 89 } else {
95 UseCounter::count(context, UseCounter::NotificationInsecureOrigin); 90 UseCounter::count(context, UseCounter::NotificationInsecureOrigin);
96 if (context->isDocument()) 91 if (context->isDocument())
97 UseCounter::countCrossOriginIframe(*toDocument(context), UseCounter ::NotificationAPIInsecureOriginIframe); 92 UseCounter::countCrossOriginIframe(*toDocument(context), UseCounter ::NotificationAPIInsecureOriginIframe);
98 } 93 }
99 94
100 WebNotificationData data = createWebNotificationData(context, title, options , exceptionState); 95 mojom::wtf::NotificationPtr data = createNotificationData(context, title, op tions, exceptionState);
101 if (exceptionState.hadException()) 96 if (exceptionState.hadException())
102 return nullptr; 97 return nullptr;
103 98
104 Notification* notification = new Notification(context, data); 99 Notification* notification = new Notification(context, std::move(data));
105 notification->schedulePrepareShow(); 100 notification->schedulePrepareShow();
106 notification->suspendIfNeeded(); 101 notification->suspendIfNeeded();
107 102
108 return notification; 103 return notification;
109 } 104 }
110 105
111 Notification* Notification::create(ExecutionContext* context, int64_t persistent Id, const WebNotificationData& data, bool showing) 106 Notification* Notification::create(ExecutionContext* context, int64_t persistent Id, mojom::wtf::NotificationPtr data, bool showing)
112 { 107 {
113 Notification* notification = new Notification(context, data); 108 Notification* notification = new Notification(context, std::move(data));
114 notification->setPersistentId(persistentId); 109 notification->setPersistentId(persistentId);
115 notification->setState(showing ? NotificationStateShowing : NotificationStat eClosed); 110 notification->setState(showing ? NotificationStateShowing : NotificationStat eClosed);
116 notification->suspendIfNeeded(); 111 notification->suspendIfNeeded();
117 112
118 return notification; 113 return notification;
119 } 114 }
120 115
121 Notification::Notification(ExecutionContext* context, const WebNotificationData& data) 116 Notification::Notification(ExecutionContext* context, mojom::wtf::NotificationPt r data)
122 : ActiveScriptWrappable(this) 117 : ActiveScriptWrappable(this)
123 , ActiveDOMObject(context) 118 , ActiveDOMObject(context)
124 , m_data(data) 119 , m_data(std::move(data))
125 , m_persistentId(kInvalidPersistentId) 120 , m_persistentId(kInvalidPersistentId)
126 , m_state(NotificationStateIdle) 121 , m_state(NotificationStateIdle)
127 , m_prepareShowMethodRunner(AsyncMethodRunner<Notification>::create(this, &N otification::prepareShow)) 122 , m_prepareShowMethodRunner(AsyncMethodRunner<Notification>::create(this, &N otification::prepareShow))
128 { 123 {
129 ASSERT(notificationManager());
130 } 124 }
131 125
132 Notification::~Notification() 126 Notification::~Notification()
133 { 127 {
134 } 128 }
135 129
130 NotificationManager* Notification::manager() const
131 {
132 return NotificationManager::from(getExecutionContext());
133 }
134
136 void Notification::schedulePrepareShow() 135 void Notification::schedulePrepareShow()
137 { 136 {
138 ASSERT(m_state == NotificationStateIdle); 137 ASSERT(m_state == NotificationStateIdle);
139 ASSERT(!m_prepareShowMethodRunner->isActive()); 138 ASSERT(!m_prepareShowMethodRunner->isActive());
140 139
141 m_prepareShowMethodRunner->runAsync(); 140 m_prepareShowMethodRunner->runAsync();
142 } 141 }
143 142
144 void Notification::prepareShow() 143 void Notification::prepareShow()
145 { 144 {
146 ASSERT(m_state == NotificationStateIdle); 145 ASSERT(m_state == NotificationStateIdle);
147 if (Notification::checkPermission(getExecutionContext()) != mojom::Permissio nStatus::GRANTED) { 146 if (manager()->permissionStatus() != mojom::PermissionStatus::GRANTED) {
148 dispatchErrorEvent(); 147 dispatchErrorEvent();
149 return; 148 return;
150 } 149 }
151 150
152 m_loader = new NotificationResourcesLoader(bind<NotificationResourcesLoader* >(&Notification::didLoadResources, WeakPersistentThisPointer<Notification>(this) )); 151 // TODO(peter): Make the NotificationManager responsible for loading resourc es.
153 m_loader->start(getExecutionContext(), m_data); 152 m_loader = new NotificationResourcesLoader(bind<NotificationResourcesLoader* , mojom::wtf::NotificationPtr>(&Notification::didLoadResources, WeakPersistentTh isPointer<Notification>(this)));
153 m_loader->start(getExecutionContext(), m_data.Clone());
154 } 154 }
155 155
156 void Notification::didLoadResources(NotificationResourcesLoader* loader) 156 void Notification::didLoadResources(NotificationResourcesLoader* loader, mojom:: wtf::NotificationPtr notification)
157 { 157 {
158 DCHECK_EQ(loader, m_loader.get()); 158 DCHECK_EQ(loader, m_loader.get());
159 159
160 SecurityOrigin* origin = getExecutionContext()->getSecurityOrigin(); 160 manager()->show(std::move(notification), loader->getResources());
161 ASSERT(origin);
162 161
163 notificationManager()->show(WebSecurityOrigin(origin), m_data, loader->getRe sources(), this);
164 m_loader.clear(); 162 m_loader.clear();
165 163
166 m_state = NotificationStateShowing; 164 m_state = NotificationStateShowing;
167 } 165 }
168 166
169 void Notification::close() 167 void Notification::close()
170 { 168 {
171 if (m_state != NotificationStateShowing) 169 if (m_state != NotificationStateShowing)
172 return; 170 return;
173 171
174 if (m_persistentId == kInvalidPersistentId) { 172 if (m_persistentId == kInvalidPersistentId) {
175 // Fire the close event asynchronously. 173 // Fire the close event asynchronously.
176 getExecutionContext()->postTask(BLINK_FROM_HERE, createSameThreadTask(&N otification::dispatchCloseEvent, this)); 174 getExecutionContext()->postTask(BLINK_FROM_HERE, createSameThreadTask(&N otification::dispatchCloseEvent, this));
177 175
178 m_state = NotificationStateClosing; 176 m_state = NotificationStateClosing;
179 notificationManager()->close(this); 177 //notificationManager()->close(this);
180 } else { 178 } else {
181 m_state = NotificationStateClosed; 179 m_state = NotificationStateClosed;
182 180
183 SecurityOrigin* origin = getExecutionContext()->getSecurityOrigin(); 181 SecurityOrigin* origin = getExecutionContext()->getSecurityOrigin();
184 ASSERT(origin); 182 ASSERT(origin);
185 183
186 notificationManager()->closePersistent(WebSecurityOrigin(origin), m_pers istentId); 184 //notificationManager()->closePersistent(WebSecurityOrigin(origin), m_pe rsistentId);
187 } 185 }
188 } 186 }
189 187
190 void Notification::dispatchShowEvent() 188 void Notification::dispatchShowEvent()
191 { 189 {
192 dispatchEvent(Event::create(EventTypeNames::show)); 190 dispatchEvent(Event::create(EventTypeNames::show));
193 } 191 }
194 192
195 void Notification::dispatchClickEvent() 193 void Notification::dispatchClickEvent()
196 { 194 {
(...skipping 13 matching lines...) Expand all
210 // closing if the developer initiated the close. 208 // closing if the developer initiated the close.
211 if (m_state != NotificationStateShowing && m_state != NotificationStateClosi ng) 209 if (m_state != NotificationStateShowing && m_state != NotificationStateClosi ng)
212 return; 210 return;
213 211
214 m_state = NotificationStateClosed; 212 m_state = NotificationStateClosed;
215 dispatchEvent(Event::create(EventTypeNames::close)); 213 dispatchEvent(Event::create(EventTypeNames::close));
216 } 214 }
217 215
218 String Notification::title() const 216 String Notification::title() const
219 { 217 {
220 return m_data.title; 218 return m_data->title;
221 } 219 }
222 220
223 String Notification::dir() const 221 String Notification::dir() const
224 { 222 {
225 switch (m_data.direction) { 223 switch (m_data->direction) {
226 case WebNotificationData::DirectionLeftToRight: 224 case mojom::wtf::NotificationDirection::LEFT_TO_RIGHT:
227 return "ltr"; 225 return "ltr";
228 case WebNotificationData::DirectionRightToLeft: 226 case mojom::wtf::NotificationDirection::RIGHT_TO_LEFT:
229 return "rtl"; 227 return "rtl";
230 case WebNotificationData::DirectionAuto: 228 case mojom::wtf::NotificationDirection::AUTO:
231 return "auto"; 229 return "auto";
232 } 230 }
233 231
234 ASSERT_NOT_REACHED(); 232 ASSERT_NOT_REACHED();
235 return String(); 233 return String();
236 } 234 }
237 235
238 String Notification::lang() const 236 String Notification::lang() const
239 { 237 {
240 return m_data.lang; 238 return m_data->lang;
241 } 239 }
242 240
243 String Notification::body() const 241 String Notification::body() const
244 { 242 {
245 return m_data.body; 243 return m_data->body;
246 } 244 }
247 245
248 String Notification::tag() const 246 String Notification::tag() const
249 { 247 {
250 return m_data.tag; 248 return m_data->tag;
251 } 249 }
252 250
253 String Notification::icon() const 251 String Notification::icon() const
254 { 252 {
255 return m_data.icon.string(); 253 return m_data->icon;
256 } 254 }
257 255
258 String Notification::badge() const 256 String Notification::badge() const
259 { 257 {
260 return m_data.badge.string(); 258 return m_data->badge;
261 } 259 }
262 260
263 NavigatorVibration::VibrationPattern Notification::vibrate(bool& isNull) const 261 NavigatorVibration::VibrationPattern Notification::vibrate(bool& isNull) const
264 { 262 {
265 NavigatorVibration::VibrationPattern pattern; 263 NavigatorVibration::VibrationPattern pattern;
266 pattern.appendRange(m_data.vibrate.begin(), m_data.vibrate.end()); 264 pattern.appendRange(m_data->vibration_pattern.storage().begin(), m_data->vib ration_pattern.storage().end());
267 265
268 if (!pattern.size()) 266 if (!pattern.size())
269 isNull = true; 267 isNull = true;
270 268
271 return pattern; 269 return pattern;
272 } 270 }
273 271
274 DOMTimeStamp Notification::timestamp() const 272 DOMTimeStamp Notification::timestamp() const
275 { 273 {
276 return m_data.timestamp; 274 return m_data->timestamp;
277 } 275 }
278 276
279 bool Notification::renotify() const 277 bool Notification::renotify() const
280 { 278 {
281 return m_data.renotify; 279 return m_data->renotify;
282 } 280 }
283 281
284 bool Notification::silent() const 282 bool Notification::silent() const
285 { 283 {
286 return m_data.silent; 284 return m_data->silent;
287 } 285 }
288 286
289 bool Notification::requireInteraction() const 287 bool Notification::requireInteraction() const
290 { 288 {
291 return m_data.requireInteraction; 289 return m_data->require_interaction;
292 } 290 }
293 291
294 ScriptValue Notification::data(ScriptState* scriptState) 292 ScriptValue Notification::data(ScriptState* scriptState)
295 { 293 {
296 if (m_developerData.isEmpty()) { 294 if (m_developerData.isEmpty()) {
297 RefPtr<SerializedScriptValue> serializedValue; 295 RefPtr<SerializedScriptValue> serializedValue;
298 296
299 const WebVector<char>& serializedData = m_data.data; 297 const WebVector<char>& serializedData = m_data->data.storage();
300 if (serializedData.size()) 298 if (serializedData.size())
301 serializedValue = SerializedScriptValueFactory::instance().createFro mWireBytes(serializedData.data(), serializedData.size()); 299 serializedValue = SerializedScriptValueFactory::instance().createFro mWireBytes(serializedData.data(), serializedData.size());
302 else 300 else
303 serializedValue = SerializedScriptValueFactory::instance().create(); 301 serializedValue = SerializedScriptValueFactory::instance().create();
304 302
305 m_developerData = ScriptValue(scriptState, serializedValue->deserialize( scriptState->isolate())); 303 m_developerData = ScriptValue(scriptState, serializedValue->deserialize( scriptState->isolate()));
306 } 304 }
307 305
308 return m_developerData; 306 return m_developerData;
309 } 307 }
310 308
311 HeapVector<NotificationAction> Notification::actions() const 309 HeapVector<NotificationAction> Notification::actions() const
312 { 310 {
313 HeapVector<NotificationAction> actions; 311 HeapVector<NotificationAction> actions;
314 actions.grow(m_data.actions.size()); 312 actions.grow(m_data->actions.size());
315 313
316 for (size_t i = 0; i < m_data.actions.size(); ++i) { 314 for (size_t i = 0; i < m_data->actions.size(); ++i) {
317 switch (m_data.actions[i].type) { 315 const auto& action = m_data->actions[i];
318 case WebNotificationAction::Button: 316
317 switch (action->type) {
318 case mojom::wtf::NotificationActionType::BUTTON:
319 actions[i].setType("button"); 319 actions[i].setType("button");
320 break; 320 break;
321 case WebNotificationAction::Text: 321 case mojom::wtf::NotificationActionType::TEXT:
322 actions[i].setType("text"); 322 actions[i].setType("text");
323 break; 323 break;
324 default: 324 default:
325 NOTREACHED() << "Unknown action type: " << m_data.actions[i].type; 325 NOTREACHED() << "Unknown action type: " << action->type;
326 } 326 }
327 actions[i].setAction(m_data.actions[i].action); 327
328 actions[i].setTitle(m_data.actions[i].title); 328 actions[i].setAction(action->action);
329 actions[i].setIcon(m_data.actions[i].icon.string()); 329 actions[i].setTitle(action->title);
330 actions[i].setPlaceholder(m_data.actions[i].placeholder); 330 actions[i].setIcon(action->icon);
331 actions[i].setPlaceholder(action->placeholder);
331 } 332 }
332 333
333 return actions; 334 return actions;
334 } 335 }
335 336
336 String Notification::permissionString(mojom::PermissionStatus permission) 337 String Notification::permissionString(mojom::PermissionStatus permission)
337 { 338 {
338 switch (permission) { 339 switch (permission) {
339 case mojom::PermissionStatus::GRANTED: 340 case mojom::PermissionStatus::GRANTED:
340 return "granted"; 341 return "granted";
341 case mojom::PermissionStatus::DENIED: 342 case mojom::PermissionStatus::DENIED:
342 return "denied"; 343 return "denied";
343 case mojom::PermissionStatus::ASK: 344 case mojom::PermissionStatus::ASK:
344 return "default"; 345 return "default";
345 } 346 }
346 347
347 ASSERT_NOT_REACHED(); 348 ASSERT_NOT_REACHED();
348 return "denied"; 349 return "denied";
349 } 350 }
350 351
351 String Notification::permission(ExecutionContext* context) 352 String Notification::permission(ExecutionContext* context)
352 { 353 {
353 return permissionString(checkPermission(context)); 354 return permissionString(NotificationManager::from(context)->permissionStatus ());
354 }
355
356 mojom::PermissionStatus Notification::checkPermission(ExecutionContext* context)
357 {
358 SecurityOrigin* origin = context->getSecurityOrigin();
359 ASSERT(origin);
360
361 return notificationManager()->checkPermission(WebSecurityOrigin(origin));
362 } 355 }
363 356
364 ScriptPromise Notification::requestPermission(ScriptState* scriptState, Notifica tionPermissionCallback* deprecatedCallback) 357 ScriptPromise Notification::requestPermission(ScriptState* scriptState, Notifica tionPermissionCallback* deprecatedCallback)
365 { 358 {
366 ExecutionContext* context = scriptState->getExecutionContext(); 359 ExecutionContext* context = scriptState->getExecutionContext();
367 if (NotificationPermissionClient* permissionClient = NotificationPermissionC lient::from(context)) 360 if (NotificationPermissionClient* permissionClient = NotificationPermissionC lient::from(context))
368 return permissionClient->requestPermission(scriptState, deprecatedCallba ck); 361 return permissionClient->requestPermission(scriptState, deprecatedCallba ck);
369 362
370 // The context has been detached. Return a promise that will never settle. 363 // The context has been detached. Return a promise that will never settle.
371 ASSERT(context->activeDOMObjectsAreStopped()); 364 ASSERT(context->activeDOMObjectsAreStopped());
(...skipping 11 matching lines...) Expand all
383 return EventTarget::dispatchEventInternal(event); 376 return EventTarget::dispatchEventInternal(event);
384 } 377 }
385 378
386 const AtomicString& Notification::interfaceName() const 379 const AtomicString& Notification::interfaceName() const
387 { 380 {
388 return EventTargetNames::Notification; 381 return EventTargetNames::Notification;
389 } 382 }
390 383
391 void Notification::stop() 384 void Notification::stop()
392 { 385 {
393 notificationManager()->notifyDelegateDestroyed(this); 386 //notificationManager()->notifyDelegateDestroyed(this);
394 387
395 m_state = NotificationStateClosed; 388 m_state = NotificationStateClosed;
396 389
397 m_prepareShowMethodRunner->stop(); 390 m_prepareShowMethodRunner->stop();
398 391
399 if (m_loader) 392 if (m_loader)
400 m_loader->stop(); 393 m_loader->stop();
401 } 394 }
402 395
403 bool Notification::hasPendingActivity() const 396 bool Notification::hasPendingActivity() const
404 { 397 {
405 return m_state == NotificationStateShowing || m_prepareShowMethodRunner->isA ctive() || m_loader; 398 return m_state == NotificationStateShowing || m_prepareShowMethodRunner->isA ctive() || m_loader;
406 } 399 }
407 400
408 DEFINE_TRACE(Notification) 401 DEFINE_TRACE(Notification)
409 { 402 {
410 visitor->trace(m_prepareShowMethodRunner); 403 visitor->trace(m_prepareShowMethodRunner);
411 visitor->trace(m_loader); 404 visitor->trace(m_loader);
412 EventTargetWithInlineData::trace(visitor); 405 EventTargetWithInlineData::trace(visitor);
413 ActiveDOMObject::trace(visitor); 406 ActiveDOMObject::trace(visitor);
414 } 407 }
415 408
416 } // namespace blink 409 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698