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

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

Issue 1750083004: Add badge to web notifications. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 9 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 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "modules/notifications/NotificationData.h" 5 #include "modules/notifications/NotificationData.h"
6 6
7 #include "bindings/core/v8/ExceptionState.h" 7 #include "bindings/core/v8/ExceptionState.h"
8 #include "bindings/core/v8/SerializedScriptValue.h" 8 #include "bindings/core/v8/SerializedScriptValue.h"
9 #include "bindings/core/v8/SerializedScriptValueFactory.h" 9 #include "bindings/core/v8/SerializedScriptValueFactory.h"
10 #include "core/dom/ExecutionContext.h" 10 #include "core/dom/ExecutionContext.h"
11 #include "modules/notifications/Notification.h" 11 #include "modules/notifications/Notification.h"
12 #include "modules/notifications/NotificationOptions.h" 12 #include "modules/notifications/NotificationOptions.h"
13 #include "modules/vibration/NavigatorVibration.h" 13 #include "modules/vibration/NavigatorVibration.h"
14 #include "platform/weborigin/KURL.h" 14 #include "platform/weborigin/KURL.h"
15 #include "wtf/CurrentTime.h" 15 #include "wtf/CurrentTime.h"
16 16
17 namespace blink { 17 namespace blink {
18 namespace { 18 namespace {
19 19
20 WebNotificationData::Direction toDirectionEnumValue(const String& direction) 20 WebNotificationData::Direction toDirectionEnumValue(const String& direction)
21 { 21 {
22 if (direction == "ltr") 22 if (direction == "ltr")
23 return WebNotificationData::DirectionLeftToRight; 23 return WebNotificationData::DirectionLeftToRight;
24 if (direction == "rtl") 24 if (direction == "rtl")
25 return WebNotificationData::DirectionRightToLeft; 25 return WebNotificationData::DirectionRightToLeft;
26 26
27 return WebNotificationData::DirectionAuto; 27 return WebNotificationData::DirectionAuto;
28 } 28 }
29 29
30 KURL completeURL(ExecutionContext* executionContext, const String& url)
Peter Beverloo 2016/03/03 17:55:39 nit: Perhaps be explicit and make this return a We
Michael van Ouwerkerk 2016/03/09 18:28:25 Done.
31 {
32 KURL kurl = executionContext->completeURL(url);
33 if (kurl.isValid())
34 return kurl;
35 return KURL();
36 }
37
30 } // namespace 38 } // namespace
31 39
32 WebNotificationData createWebNotificationData(ExecutionContext* executionContext , const String& title, const NotificationOptions& options, ExceptionState& excep tionState) 40 WebNotificationData createWebNotificationData(ExecutionContext* executionContext , const String& title, const NotificationOptions& options, ExceptionState& excep tionState)
33 { 41 {
34 // If silent is true, the notification must not have a vibration pattern. 42 // If silent is true, the notification must not have a vibration pattern.
35 if (options.hasVibrate() && options.silent()) { 43 if (options.hasVibrate() && options.silent()) {
36 exceptionState.throwTypeError("Silent notifications must not specify vib ration patterns."); 44 exceptionState.throwTypeError("Silent notifications must not specify vib ration patterns.");
37 return WebNotificationData(); 45 return WebNotificationData();
38 } 46 }
39 47
40 // If renotify is true, the notification must have a tag. 48 // If renotify is true, the notification must have a tag.
41 if (options.renotify() && options.tag().isEmpty()) { 49 if (options.renotify() && options.tag().isEmpty()) {
42 exceptionState.throwTypeError("Notifications which set the renotify flag must specify a non-empty tag."); 50 exceptionState.throwTypeError("Notifications which set the renotify flag must specify a non-empty tag.");
43 return WebNotificationData(); 51 return WebNotificationData();
44 } 52 }
45 53
46 WebNotificationData webData; 54 WebNotificationData webData;
47 55
48 webData.title = title; 56 webData.title = title;
49 webData.direction = toDirectionEnumValue(options.dir()); 57 webData.direction = toDirectionEnumValue(options.dir());
50 webData.lang = options.lang(); 58 webData.lang = options.lang();
51 webData.body = options.body(); 59 webData.body = options.body();
52 webData.tag = options.tag(); 60 webData.tag = options.tag();
53 61
54 KURL iconUrl; 62 if (options.hasIcon() && !options.icon().isEmpty())
63 webData.icon = completeURL(executionContext, options.icon());
55 64
56 if (options.hasIcon() && !options.icon().isEmpty()) { 65 if (options.hasSmallIcon() && !options.smallIcon().isEmpty())
57 iconUrl = executionContext->completeURL(options.icon()); 66 webData.smallIcon = completeURL(executionContext, options.smallIcon());
58 if (!iconUrl.isValid())
59 iconUrl = KURL();
60 }
61 67
62 webData.icon = iconUrl;
63 webData.vibrate = NavigatorVibration::sanitizeVibrationPattern(options.vibra te()); 68 webData.vibrate = NavigatorVibration::sanitizeVibrationPattern(options.vibra te());
64 webData.timestamp = options.hasTimestamp() ? static_cast<double>(options.tim estamp()) : WTF::currentTimeMS(); 69 webData.timestamp = options.hasTimestamp() ? static_cast<double>(options.tim estamp()) : WTF::currentTimeMS();
65 webData.renotify = options.renotify(); 70 webData.renotify = options.renotify();
66 webData.silent = options.silent(); 71 webData.silent = options.silent();
67 webData.requireInteraction = options.requireInteraction(); 72 webData.requireInteraction = options.requireInteraction();
68 73
69 if (options.hasData()) { 74 if (options.hasData()) {
70 RefPtr<SerializedScriptValue> serializedScriptValue = SerializedScriptVa lueFactory::instance().create(options.data().isolate(), options.data(), nullptr, exceptionState); 75 RefPtr<SerializedScriptValue> serializedScriptValue = SerializedScriptVa lueFactory::instance().create(options.data().isolate(), options.data(), nullptr, exceptionState);
71 if (exceptionState.hadException()) 76 if (exceptionState.hadException())
72 return WebNotificationData(); 77 return WebNotificationData();
73 78
74 Vector<char> serializedData; 79 Vector<char> serializedData;
75 serializedScriptValue->toWireBytes(serializedData); 80 serializedScriptValue->toWireBytes(serializedData);
76 81
77 webData.data = serializedData; 82 webData.data = serializedData;
78 } 83 }
79 84
80 Vector<WebNotificationAction> actions; 85 Vector<WebNotificationAction> actions;
81 86
82 const size_t maxActions = Notification::maxActions(); 87 const size_t maxActions = Notification::maxActions();
83 for (const NotificationAction& action : options.actions()) { 88 for (const NotificationAction& action : options.actions()) {
84 if (actions.size() >= maxActions) 89 if (actions.size() >= maxActions)
85 break; 90 break;
86 91
87 WebNotificationAction webAction; 92 WebNotificationAction webAction;
88 webAction.action = action.action(); 93 webAction.action = action.action();
89 webAction.title = action.title(); 94 webAction.title = action.title();
90 95
91 KURL iconUrl; 96 if (action.hasIcon() && !action.icon().isEmpty())
92 if (action.hasIcon() && !action.icon().isEmpty()) { 97 webAction.icon = completeURL(executionContext, action.icon());
93 iconUrl = executionContext->completeURL(action.icon());
94 if (!iconUrl.isValid())
95 iconUrl = KURL();
96 }
97 webAction.icon = iconUrl;
98 98
99 actions.append(webAction); 99 actions.append(webAction);
100 } 100 }
101 101
102 webData.actions = actions; 102 webData.actions = actions;
103 103
104 return webData; 104 return webData;
105 } 105 }
106 106
107 } // namespace blink 107 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698