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

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 it' s completed.
185 if (m_state == State::Loading || m_notificationId.isEmpty())
186 m_requestedClose = true;
187
183 return; 188 return;
189 }
184 190
185 // Schedule the "close" event to be fired for non-persistent notifications. 191 // Schedule the "close" event to be fired for non-persistent notifications.
186 // Persistent notifications won't get such events for programmatic closes. 192 // Persistent notifications won't get such events for programmatic closes.
187 if (m_type == Type::NonPersistent) { 193 if (m_type == Type::NonPersistent) {
188 getExecutionContext()->postTask( 194 getExecutionContext()->postTask(
189 BLINK_FROM_HERE, createSameThreadTask(&Notification::dispatchCloseEvent, 195 BLINK_FROM_HERE,
190 wrapPersistent(this))); 196 createSameThreadTask(&Notification::didCloseNotification,
197 wrapPersistent(this)));
191 m_state = State::Closing; 198 m_state = State::Closing;
192 199 } else {
193 notificationManager()->close(this); 200 m_state = State::Closed;
194 return;
195 } 201 }
196 202
197 m_state = State::Closed;
198
199 SecurityOrigin* origin = getExecutionContext()->getSecurityOrigin(); 203 SecurityOrigin* origin = getExecutionContext()->getSecurityOrigin();
200 DCHECK(origin); 204 DCHECK(origin);
201 205
202 notificationManager()->closePersistent(WebSecurityOrigin(origin), m_data.tag, 206 notificationManager()->close(WebSecurityOrigin(origin), m_data.tag,
203 m_notificationId); 207 m_notificationId);
204 } 208 }
205 209
206 void Notification::dispatchShowEvent() { 210 void Notification::didShowNotification(const WebString& notificationId) {
211 DCHECK(m_notificationId.isEmpty());
212 m_notificationId = notificationId;
213
207 dispatchEvent(Event::create(EventTypeNames::show)); 214 dispatchEvent(Event::create(EventTypeNames::show));
215
216 if (m_requestedClose)
217 close();
208 } 218 }
209 219
210 void Notification::dispatchClickEvent() { 220 void Notification::didClickNotification() {
211 UserGestureIndicator gestureIndicator(DefinitelyProcessingNewUserGesture); 221 UserGestureIndicator gestureIndicator(DefinitelyProcessingNewUserGesture);
212 ScopedWindowFocusAllowedIndicator windowFocusAllowed(getExecutionContext()); 222 ScopedWindowFocusAllowedIndicator windowFocusAllowed(getExecutionContext());
213 dispatchEvent(Event::create(EventTypeNames::click)); 223 dispatchEvent(Event::create(EventTypeNames::click));
214 } 224 }
215 225
216 void Notification::dispatchErrorEvent() { 226 void Notification::didCloseNotification() {
217 dispatchEvent(Event::create(EventTypeNames::error));
218 }
219
220 void Notification::dispatchCloseEvent() {
221 // The notification will be showing when the user initiated the close, or it w ill be 227 // The notification will be showing when the user initiated the close, or it w ill be
222 // closing if the developer initiated the close. 228 // closing if the developer initiated the close.
223 if (m_state != State::Showing && m_state != State::Closing) 229 if (m_state != State::Showing && m_state != State::Closing)
224 return; 230 return;
225 231
226 m_state = State::Closed; 232 m_state = State::Closed;
227 dispatchEvent(Event::create(EventTypeNames::close)); 233 dispatchEvent(Event::create(EventTypeNames::close));
228 } 234 }
229 235
230 String Notification::title() const { 236 String Notification::title() const {
(...skipping 165 matching lines...) Expand 10 before | Expand all | Expand 10 after
396 } 402 }
397 403
398 DEFINE_TRACE(Notification) { 404 DEFINE_TRACE(Notification) {
399 visitor->trace(m_prepareShowMethodRunner); 405 visitor->trace(m_prepareShowMethodRunner);
400 visitor->trace(m_loader); 406 visitor->trace(m_loader);
401 EventTargetWithInlineData::trace(visitor); 407 EventTargetWithInlineData::trace(visitor);
402 ActiveDOMObject::trace(visitor); 408 ActiveDOMObject::trace(visitor);
403 } 409 }
404 410
405 } // namespace blink 411 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698