OLD | NEW |
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 18 matching lines...) Expand all Loading... |
29 */ | 29 */ |
30 | 30 |
31 #include "config.h" | 31 #include "config.h" |
32 #include "modules/notifications/Notification.h" | 32 #include "modules/notifications/Notification.h" |
33 | 33 |
34 #include "bindings/core/v8/ScriptWrappable.h" | 34 #include "bindings/core/v8/ScriptWrappable.h" |
35 #include "core/dom/Document.h" | 35 #include "core/dom/Document.h" |
36 #include "core/events/Event.h" | 36 #include "core/events/Event.h" |
37 #include "core/frame/UseCounter.h" | 37 #include "core/frame/UseCounter.h" |
38 #include "core/page/WindowFocusAllowedIndicator.h" | 38 #include "core/page/WindowFocusAllowedIndicator.h" |
39 #include "modules/notifications/NotificationClient.h" | |
40 #include "modules/notifications/NotificationController.h" | |
41 #include "modules/notifications/NotificationOptions.h" | 39 #include "modules/notifications/NotificationOptions.h" |
42 #include "modules/notifications/NotificationPermissionClient.h" | 40 #include "modules/notifications/NotificationPermissionClient.h" |
| 41 #include "public/platform/Platform.h" |
| 42 #include "public/platform/WebNotificationData.h" |
| 43 #include "public/platform/WebNotificationManager.h" |
| 44 #include "public/platform/WebSerializedOrigin.h" |
43 | 45 |
44 namespace blink { | 46 namespace blink { |
45 | 47 |
| 48 namespace { |
| 49 |
| 50 WebNotificationManager* notificationManager() |
| 51 { |
| 52 return Platform::current()->notificationManager(); |
| 53 } |
| 54 |
| 55 } // namespace |
| 56 |
46 Notification* Notification::create(ExecutionContext* context, const String& titl
e, const NotificationOptions& options) | 57 Notification* Notification::create(ExecutionContext* context, const String& titl
e, const NotificationOptions& options) |
47 { | 58 { |
48 NotificationClient& client = NotificationController::clientFrom(context); | 59 Notification* notification = new Notification(title, context); |
49 Notification* notification = new Notification(title, context, &client); | |
50 | 60 |
51 notification->setBody(options.body()); | 61 notification->setBody(options.body()); |
52 notification->setTag(options.tag()); | 62 notification->setTag(options.tag()); |
53 notification->setLang(options.lang()); | 63 notification->setLang(options.lang()); |
54 notification->setDir(options.dir()); | 64 notification->setDir(options.dir()); |
55 if (options.hasIcon()) { | 65 if (options.hasIcon()) { |
56 KURL iconUrl = options.icon().isEmpty() ? KURL() : context->completeURL(
options.icon()); | 66 KURL iconUrl = options.icon().isEmpty() ? KURL() : context->completeURL(
options.icon()); |
57 if (!iconUrl.isEmpty() && iconUrl.isValid()) | 67 if (!iconUrl.isEmpty() && iconUrl.isValid()) |
58 notification->setIconUrl(iconUrl); | 68 notification->setIconUrl(iconUrl); |
59 } | 69 } |
60 | 70 |
61 String insecureOriginMessage; | 71 String insecureOriginMessage; |
62 UseCounter::Feature feature = context->securityOrigin()->canAccessFeatureReq
uiringSecureOrigin(insecureOriginMessage) | 72 UseCounter::Feature feature = context->securityOrigin()->canAccessFeatureReq
uiringSecureOrigin(insecureOriginMessage) |
63 ? UseCounter::NotificationSecureOrigin : UseCounter::NotificationInsecur
eOrigin; | 73 ? UseCounter::NotificationSecureOrigin : UseCounter::NotificationInsecur
eOrigin; |
64 UseCounter::count(context, feature); | 74 UseCounter::count(context, feature); |
65 | 75 |
66 notification->suspendIfNeeded(); | 76 notification->suspendIfNeeded(); |
67 return notification; | 77 return notification; |
68 } | 78 } |
69 | 79 |
70 Notification::Notification(const String& title, ExecutionContext* context, Notif
icationClient* client) | 80 Notification::Notification(const String& title, ExecutionContext* context) |
71 : ActiveDOMObject(context) | 81 : ActiveDOMObject(context) |
72 , m_title(title) | 82 , m_title(title) |
73 , m_dir("auto") | 83 , m_dir("auto") |
74 , m_state(NotificationStateIdle) | 84 , m_state(NotificationStateIdle) |
75 , m_client(client) | |
76 , m_asyncRunner(this, &Notification::show) | 85 , m_asyncRunner(this, &Notification::show) |
77 { | 86 { |
78 ASSERT(m_client); | 87 ASSERT(notificationManager()); |
79 | 88 |
80 m_asyncRunner.runAsync(); | 89 m_asyncRunner.runAsync(); |
81 } | 90 } |
82 | 91 |
83 Notification::~Notification() | 92 Notification::~Notification() |
84 { | 93 { |
85 } | 94 } |
86 | 95 |
87 void Notification::show() | 96 void Notification::show() |
88 { | 97 { |
89 ASSERT(m_state == NotificationStateIdle); | 98 ASSERT(m_state == NotificationStateIdle); |
90 if (!toDocument(executionContext())->page()) | 99 if (Notification::checkPermission(executionContext()) != WebNotificationPerm
issionAllowed) { |
91 return; | |
92 | |
93 if (m_client->checkPermission(executionContext()) != NotificationClient::Per
missionAllowed) { | |
94 dispatchErrorEvent(); | 100 dispatchErrorEvent(); |
95 return; | 101 return; |
96 } | 102 } |
97 | 103 |
98 if (m_client->show(this)) | 104 SecurityOrigin* origin = executionContext()->securityOrigin(); |
| 105 ASSERT(origin); |
| 106 |
| 107 // TODO(peter): We should set the correct direction as part of this call. |
| 108 WebNotificationData notificationData(m_title, WebNotificationData::Direction
LeftToRight, m_lang, m_body, m_tag, m_iconUrl); |
| 109 if (notificationManager()->show(WebSerializedOrigin(*origin), notificationDa
ta, this)) |
99 m_state = NotificationStateShowing; | 110 m_state = NotificationStateShowing; |
100 } | 111 } |
101 | 112 |
102 void Notification::close() | 113 void Notification::close() |
103 { | 114 { |
104 switch (m_state) { | 115 if (m_state != NotificationStateShowing) |
105 case NotificationStateIdle: | 116 return; |
106 break; | 117 |
107 case NotificationStateShowing: | 118 m_state = NotificationStateClosed; |
108 m_client->close(this); | 119 notificationManager()->close(this); |
109 break; | |
110 case NotificationStateClosed: | |
111 break; | |
112 } | |
113 } | 120 } |
114 | 121 |
115 void Notification::dispatchShowEvent() | 122 void Notification::dispatchShowEvent() |
116 { | 123 { |
117 dispatchEvent(Event::create(EventTypeNames::show)); | 124 dispatchEvent(Event::create(EventTypeNames::show)); |
118 } | 125 } |
119 | 126 |
120 void Notification::dispatchClickEvent() | 127 void Notification::dispatchClickEvent() |
121 { | 128 { |
122 UserGestureIndicator gestureIndicator(DefinitelyProcessingNewUserGesture); | 129 UserGestureIndicator gestureIndicator(DefinitelyProcessingNewUserGesture); |
123 WindowFocusAllowedIndicator windowFocusAllowed; | 130 WindowFocusAllowedIndicator windowFocusAllowed; |
| 131 |
124 dispatchEvent(Event::create(EventTypeNames::click)); | 132 dispatchEvent(Event::create(EventTypeNames::click)); |
125 } | 133 } |
126 | 134 |
127 void Notification::dispatchErrorEvent() | 135 void Notification::dispatchErrorEvent() |
128 { | 136 { |
129 dispatchEvent(Event::create(EventTypeNames::error)); | 137 dispatchEvent(Event::create(EventTypeNames::error)); |
130 } | 138 } |
131 | 139 |
132 void Notification::dispatchCloseEvent() | 140 void Notification::dispatchCloseEvent() |
133 { | 141 { |
134 dispatchEvent(Event::create(EventTypeNames::close)); | 142 dispatchEvent(Event::create(EventTypeNames::close)); |
135 m_state = NotificationStateClosed; | |
136 } | 143 } |
137 | 144 |
138 TextDirection Notification::direction() const | 145 TextDirection Notification::direction() const |
139 { | 146 { |
140 // FIXME: Resolve dir()=="auto" against the document. | 147 // FIXME: Resolve dir()=="auto" against the document. |
141 return dir() == "rtl" ? RTL : LTR; | 148 return dir() == "rtl" ? RTL : LTR; |
142 } | 149 } |
143 | 150 |
144 const String& Notification::permissionString(NotificationClient::Permission perm
ission) | 151 const String& Notification::permissionString(WebNotificationPermission permissio
n) |
145 { | 152 { |
146 DEFINE_STATIC_LOCAL(const String, allowedPermission, ("granted")); | 153 DEFINE_STATIC_LOCAL(const String, allowedPermission, ("granted")); |
147 DEFINE_STATIC_LOCAL(const String, deniedPermission, ("denied")); | 154 DEFINE_STATIC_LOCAL(const String, deniedPermission, ("denied")); |
148 DEFINE_STATIC_LOCAL(const String, defaultPermission, ("default")); | 155 DEFINE_STATIC_LOCAL(const String, defaultPermission, ("default")); |
149 | 156 |
150 switch (permission) { | 157 switch (permission) { |
151 case NotificationClient::PermissionAllowed: | 158 case WebNotificationPermissionAllowed: |
152 return allowedPermission; | 159 return allowedPermission; |
153 case NotificationClient::PermissionDenied: | 160 case WebNotificationPermissionDenied: |
154 return deniedPermission; | 161 return deniedPermission; |
155 case NotificationClient::PermissionNotAllowed: | 162 case WebNotificationPermissionDefault: |
156 return defaultPermission; | 163 return defaultPermission; |
157 } | 164 } |
158 | 165 |
159 ASSERT_NOT_REACHED(); | 166 ASSERT_NOT_REACHED(); |
160 return deniedPermission; | 167 return deniedPermission; |
161 } | 168 } |
162 | 169 |
163 const String& Notification::permission(ExecutionContext* context) | 170 const String& Notification::permission(ExecutionContext* context) |
164 { | 171 { |
165 return permissionString(NotificationController::clientFrom(context).checkPer
mission(context)); | 172 return permissionString(checkPermission(context)); |
| 173 } |
| 174 |
| 175 WebNotificationPermission Notification::checkPermission(ExecutionContext* contex
t) |
| 176 { |
| 177 SecurityOrigin* origin = context->securityOrigin(); |
| 178 ASSERT(origin); |
| 179 |
| 180 return notificationManager()->checkPermission(WebSerializedOrigin(*origin)); |
166 } | 181 } |
167 | 182 |
168 void Notification::requestPermission(ExecutionContext* context, NotificationPerm
issionCallback* callback) | 183 void Notification::requestPermission(ExecutionContext* context, NotificationPerm
issionCallback* callback) |
169 { | 184 { |
170 // FIXME: Assert that this code-path will only be reached for Document envir
onments | 185 ASSERT(context->isDocument()); |
171 // when Blink supports [Exposed] annotations on class members in IDL definit
ions. | 186 if (NotificationPermissionClient* permissionClient = NotificationPermissionC
lient::from(context)) |
172 if (NotificationPermissionClient* permissionClient = NotificationPermissionC
lient::from(context)) { | |
173 permissionClient->requestPermission(context, callback); | 187 permissionClient->requestPermission(context, callback); |
174 return; | |
175 } | |
176 } | 188 } |
177 | 189 |
178 bool Notification::dispatchEvent(PassRefPtrWillBeRawPtr<Event> event) | 190 bool Notification::dispatchEvent(PassRefPtrWillBeRawPtr<Event> event) |
179 { | 191 { |
180 ASSERT(m_state != NotificationStateClosed); | 192 ASSERT(executionContext()->isContextThread()); |
181 | |
182 return EventTarget::dispatchEvent(event); | 193 return EventTarget::dispatchEvent(event); |
183 } | 194 } |
184 | 195 |
185 const AtomicString& Notification::interfaceName() const | 196 const AtomicString& Notification::interfaceName() const |
186 { | 197 { |
187 return EventTargetNames::Notification; | 198 return EventTargetNames::Notification; |
188 } | 199 } |
189 | 200 |
190 void Notification::stop() | 201 void Notification::stop() |
191 { | 202 { |
| 203 notificationManager()->notifyDelegateDestroyed(this); |
| 204 |
192 m_state = NotificationStateClosed; | 205 m_state = NotificationStateClosed; |
193 | |
194 if (m_client) { | |
195 m_client->notificationObjectDestroyed(this); | |
196 m_client = 0; | |
197 } | |
198 | |
199 m_asyncRunner.stop(); | 206 m_asyncRunner.stop(); |
200 } | 207 } |
201 | 208 |
202 bool Notification::hasPendingActivity() const | 209 bool Notification::hasPendingActivity() const |
203 { | 210 { |
204 return m_state == NotificationStateShowing || m_asyncRunner.isActive(); | 211 return m_state == NotificationStateShowing || m_asyncRunner.isActive(); |
205 } | 212 } |
206 | 213 |
207 } // namespace blink | 214 } // namespace blink |
OLD | NEW |