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

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

Issue 2344983003: Merge the code paths for closing different kinds of notifications. (Closed)
Patch Set: rebase Created 4 years, 2 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 119 matching lines...) Expand 10 before | Expand all | Expand 10 after
130 return notification; 130 return notification;
131 } 131 }
132 132
133 Notification::Notification(ExecutionContext* context, 133 Notification::Notification(ExecutionContext* context,
134 Type type, 134 Type type,
135 const WebNotificationData& data) 135 const WebNotificationData& data)
136 : ActiveScriptWrappable(this), 136 : ActiveScriptWrappable(this),
137 ActiveDOMObject(context), 137 ActiveDOMObject(context),
138 m_type(type), 138 m_type(type),
139 m_state(State::Loading), 139 m_state(State::Loading),
140 m_data(data) { 140 m_data(data),
141 m_requestedClose(false) {
141 DCHECK(notificationManager()); 142 DCHECK(notificationManager());
142 } 143 }
143 144
144 Notification::~Notification() {} 145 Notification::~Notification() {}
145 146
146 void Notification::schedulePrepareShow() { 147 void Notification::schedulePrepareShow() {
147 DCHECK_EQ(m_state, State::Loading); 148 DCHECK_EQ(m_state, State::Loading);
148 DCHECK(!m_prepareShowMethodRunner); 149 DCHECK(!m_prepareShowMethodRunner);
149 150
150 m_prepareShowMethodRunner = 151 m_prepareShowMethodRunner =
151 AsyncMethodRunner<Notification>::create(this, &Notification::prepareShow); 152 AsyncMethodRunner<Notification>::create(this, &Notification::prepareShow);
152 m_prepareShowMethodRunner->runAsync(); 153 m_prepareShowMethodRunner->runAsync();
153 } 154 }
154 155
155 void Notification::prepareShow() { 156 void Notification::prepareShow() {
156 DCHECK_EQ(m_state, State::Loading); 157 DCHECK_EQ(m_state, State::Loading);
157 if (NotificationManager::from(getExecutionContext())->permissionStatus() != 158 if (NotificationManager::from(getExecutionContext())->permissionStatus() !=
158 mojom::blink::PermissionStatus::GRANTED) { 159 mojom::blink::PermissionStatus::GRANTED) {
159 dispatchErrorEvent(); 160 dispatchEvent(Event::create(EventTypeNames::error));
160 return; 161 return;
161 } 162 }
162 163
163 m_loader = new NotificationResourcesLoader( 164 m_loader = new NotificationResourcesLoader(
164 WTF::bind(&Notification::didLoadResources, wrapWeakPersistent(this))); 165 WTF::bind(&Notification::didLoadResources, wrapWeakPersistent(this)));
165 m_loader->start(getExecutionContext(), m_data); 166 m_loader->start(getExecutionContext(), m_data);
166 } 167 }
167 168
168 void Notification::didLoadResources(NotificationResourcesLoader* loader) { 169 void Notification::didLoadResources(NotificationResourcesLoader* loader) {
169 DCHECK_EQ(loader, m_loader.get()); 170 DCHECK_EQ(loader, m_loader.get());
170 171
171 SecurityOrigin* origin = getExecutionContext()->getSecurityOrigin(); 172 SecurityOrigin* origin = getExecutionContext()->getSecurityOrigin();
172 DCHECK(origin); 173 DCHECK(origin);
173 174
174 notificationManager()->show(WebSecurityOrigin(origin), m_data, 175 notificationManager()->show(WebSecurityOrigin(origin), m_data,
175 loader->getResources(), this); 176 loader->getResources(), this);
176 m_loader.clear(); 177 m_loader.clear();
177 178
178 m_state = State::Showing; 179 m_state = State::Showing;
179 } 180 }
180 181
181 void Notification::close() { 182 void Notification::close() {
182 if (m_state != State::Showing) 183 if (m_state != State::Showing) {
184 // TODO(peter): Abort the load instead of closing the notification after
185 // it's completed.
186 if (m_state == State::Loading || m_notificationId.isEmpty())
187 m_requestedClose = true;
188
183 return; 189 return;
190 }
184 191
185 // Schedule the "close" event to be fired for non-persistent notifications. 192 // Schedule the "close" event to be fired for non-persistent notifications.
186 // Persistent notifications won't get such events for programmatic closes. 193 // Persistent notifications won't get such events for programmatic closes.
187 if (m_type == Type::NonPersistent) { 194 if (m_type == Type::NonPersistent) {
188 getExecutionContext()->postTask( 195 getExecutionContext()->postTask(
189 BLINK_FROM_HERE, createSameThreadTask(&Notification::dispatchCloseEvent, 196 BLINK_FROM_HERE,
190 wrapPersistent(this))); 197 createSameThreadTask(&Notification::didCloseNotification,
198 wrapPersistent(this)));
191 m_state = State::Closing; 199 m_state = State::Closing;
192 200 } else {
193 notificationManager()->close(this); 201 m_state = State::Closed;
194 return;
195 } 202 }
196 203
197 m_state = State::Closed;
198
199 SecurityOrigin* origin = getExecutionContext()->getSecurityOrigin(); 204 SecurityOrigin* origin = getExecutionContext()->getSecurityOrigin();
200 DCHECK(origin); 205 DCHECK(origin);
201 206
202 notificationManager()->closePersistent(WebSecurityOrigin(origin), m_data.tag, 207 notificationManager()->close(WebSecurityOrigin(origin), m_data.tag,
203 m_notificationId); 208 m_notificationId);
204 } 209 }
205 210
206 void Notification::dispatchShowEvent() { 211 void Notification::didShowNotification(const WebString& notificationId) {
212 DCHECK(m_notificationId.isEmpty());
213 m_notificationId = notificationId;
214
207 dispatchEvent(Event::create(EventTypeNames::show)); 215 dispatchEvent(Event::create(EventTypeNames::show));
216
217 if (m_requestedClose)
218 close();
208 } 219 }
209 220
210 void Notification::dispatchClickEvent() { 221 void Notification::didClickNotification() {
211 UserGestureIndicator gestureIndicator( 222 UserGestureIndicator gestureIndicator(
212 UserGestureToken::create(UserGestureToken::NewGesture)); 223 UserGestureToken::create(UserGestureToken::NewGesture));
224
213 ScopedWindowFocusAllowedIndicator windowFocusAllowed(getExecutionContext()); 225 ScopedWindowFocusAllowedIndicator windowFocusAllowed(getExecutionContext());
214 dispatchEvent(Event::create(EventTypeNames::click)); 226 dispatchEvent(Event::create(EventTypeNames::click));
215 } 227 }
216 228
217 void Notification::dispatchErrorEvent() { 229 void Notification::didCloseNotification() {
218 dispatchEvent(Event::create(EventTypeNames::error)); 230 // The notification will be showing when the user initiated the close, or it
219 } 231 // will be closing if the developer initiated the close.
220
221 void Notification::dispatchCloseEvent() {
222 // The notification should be Showing if the user initiated the close, or it
223 // should be Closing if the developer initiated the close.
224 if (m_state != State::Showing && m_state != State::Closing) 232 if (m_state != State::Showing && m_state != State::Closing)
225 return; 233 return;
226 234
227 m_state = State::Closed; 235 m_state = State::Closed;
228 dispatchEvent(Event::create(EventTypeNames::close)); 236 dispatchEvent(Event::create(EventTypeNames::close));
229 } 237 }
230 238
231 String Notification::title() const { 239 String Notification::title() const {
232 return m_data.title; 240 return m_data.title;
233 } 241 }
(...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after
397 } 405 }
398 406
399 DEFINE_TRACE(Notification) { 407 DEFINE_TRACE(Notification) {
400 visitor->trace(m_prepareShowMethodRunner); 408 visitor->trace(m_prepareShowMethodRunner);
401 visitor->trace(m_loader); 409 visitor->trace(m_loader);
402 EventTargetWithInlineData::trace(visitor); 410 EventTargetWithInlineData::trace(visitor);
403 ActiveDOMObject::trace(visitor); 411 ActiveDOMObject::trace(visitor);
404 } 412 }
405 413
406 } // namespace blink 414 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698