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

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

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

Powered by Google App Engine
This is Rietveld 408576698