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

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

Issue 22909013: Notification should not leak document. (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: add test expectation Created 7 years, 4 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) 2009 Google Inc. All rights reserved. 2 * Copyright (C) 2009 Google Inc. All rights reserved.
3 * Copyright (C) 2009, 2011, 2012 Apple Inc. All rights reserved. 3 * Copyright (C) 2009, 2011, 2012 Apple Inc. All rights reserved.
4 * 4 *
5 * Redistribution and use in source and binary forms, with or without 5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are 6 * modification, are permitted provided that the following conditions are
7 * met: 7 * met:
8 * 8 *
9 * * Redistributions of source code must retain the above copyright 9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer. 10 * notice, this list of conditions and the following disclaimer.
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
48 #include "core/workers/WorkerGlobalScope.h" 48 #include "core/workers/WorkerGlobalScope.h"
49 #include "modules/notifications/DOMWindowNotifications.h" 49 #include "modules/notifications/DOMWindowNotifications.h"
50 #include "modules/notifications/NotificationCenter.h" 50 #include "modules/notifications/NotificationCenter.h"
51 #include "modules/notifications/NotificationClient.h" 51 #include "modules/notifications/NotificationClient.h"
52 #include "modules/notifications/NotificationController.h" 52 #include "modules/notifications/NotificationController.h"
53 #include "modules/notifications/NotificationPermissionCallback.h" 53 #include "modules/notifications/NotificationPermissionCallback.h"
54 54
55 namespace WebCore { 55 namespace WebCore {
56 56
57 Notification::Notification() 57 Notification::Notification()
58 : ActiveDOMObject(0) 58 : ContextLifecycleObserver(0)
59 { 59 {
60 ScriptWrappable::init(this); 60 ScriptWrappable::init(this);
61 } 61 }
62 62
63 #if ENABLE(LEGACY_NOTIFICATIONS) 63 #if ENABLE(LEGACY_NOTIFICATIONS)
64 Notification::Notification(const String& title, const String& body, const String & iconURI, ScriptExecutionContext* context, ExceptionState& es, PassRefPtr<Notif icationCenter> provider) 64 Notification::Notification(const String& title, const String& body, const String & iconURI, ScriptExecutionContext* context, ExceptionState& es, PassRefPtr<Notif icationCenter> provider)
65 : ActiveDOMObject(context) 65 : ContextLifecycleObserver(context)
66 , m_title(title) 66 , m_title(title)
67 , m_body(body) 67 , m_body(body)
68 , m_state(Idle) 68 , m_state(Idle)
69 , m_notificationCenter(provider) 69 , m_notificationCenter(provider)
70 { 70 {
71 ScriptWrappable::init(this); 71 ScriptWrappable::init(this);
72 if (m_notificationCenter->checkPermission() != NotificationClient::Permissio nAllowed) { 72 if (m_notificationCenter->checkPermission() != NotificationClient::Permissio nAllowed) {
73 es.throwDOMException(SecurityError); 73 es.throwDOMException(SecurityError);
74 return; 74 return;
75 } 75 }
76 76
77 m_icon = iconURI.isEmpty() ? KURL() : scriptExecutionContext()->completeURL( iconURI); 77 m_icon = iconURI.isEmpty() ? KURL() : scriptExecutionContext()->completeURL( iconURI);
78 if (!m_icon.isEmpty() && !m_icon.isValid()) { 78 if (!m_icon.isEmpty() && !m_icon.isValid()) {
79 es.throwDOMException(SyntaxError); 79 es.throwDOMException(SyntaxError);
80 return; 80 return;
81 } 81 }
82 } 82 }
83 #endif 83 #endif
84 84
85 #if ENABLE(NOTIFICATIONS) 85 #if ENABLE(NOTIFICATIONS)
86 Notification::Notification(ScriptExecutionContext* context, const String& title) 86 Notification::Notification(ScriptExecutionContext* context, const String& title)
87 : ActiveDOMObject(context) 87 : ContextLifecycleObserver(context)
88 , m_title(title) 88 , m_title(title)
89 , m_state(Idle) 89 , m_state(Idle)
90 , m_taskTimer(adoptPtr(new Timer<Notification>(this, &Notification::taskTime rFired))) 90 , m_taskTimer(adoptPtr(new Timer<Notification>(this, &Notification::taskTime rFired)))
91 { 91 {
92 ScriptWrappable::init(this); 92 ScriptWrappable::init(this);
93 m_notificationCenter = DOMWindowNotifications::webkitNotifications(toDocumen t(context)->domWindow()); 93 m_notificationCenter = DOMWindowNotifications::webkitNotifications(toDocumen t(context)->domWindow());
94 94
95 ASSERT(m_notificationCenter->client()); 95 ASSERT(m_notificationCenter->client());
96 m_taskTimer->startOneShot(0); 96 m_taskTimer->startOneShot(0);
97 } 97 }
98 #endif 98 #endif
99 99
100 Notification::~Notification() 100 Notification::~Notification()
101 { 101 {
102 } 102 }
103 103
104 #if ENABLE(LEGACY_NOTIFICATIONS) 104 #if ENABLE(LEGACY_NOTIFICATIONS)
105 PassRefPtr<Notification> Notification::create(const String& title, const String& body, const String& iconURI, ScriptExecutionContext* context, ExceptionState& e s, PassRefPtr<NotificationCenter> provider) 105 PassRefPtr<Notification> Notification::create(const String& title, const String& body, const String& iconURI, ScriptExecutionContext* context, ExceptionState& e s, PassRefPtr<NotificationCenter> provider)
106 { 106 {
107 RefPtr<Notification> notification(adoptRef(new Notification(title, body, ico nURI, context, es, provider))); 107 RefPtr<Notification> notification(adoptRef(new Notification(title, body, ico nURI, context, es, provider)));
108 notification->suspendIfNeeded();
109 return notification.release(); 108 return notification.release();
110 } 109 }
111 #endif 110 #endif
112 111
113 #if ENABLE(NOTIFICATIONS) 112 #if ENABLE(NOTIFICATIONS)
114 PassRefPtr<Notification> Notification::create(ScriptExecutionContext* context, c onst String& title, const Dictionary& options) 113 PassRefPtr<Notification> Notification::create(ScriptExecutionContext* context, c onst String& title, const Dictionary& options)
115 { 114 {
116 RefPtr<Notification> notification(adoptRef(new Notification(context, title)) ); 115 RefPtr<Notification> notification(adoptRef(new Notification(context, title)) );
117 String argument; 116 String argument;
118 if (options.get("body", argument)) 117 if (options.get("body", argument))
119 notification->setBody(argument); 118 notification->setBody(argument);
120 if (options.get("tag", argument)) 119 if (options.get("tag", argument))
121 notification->setTag(argument); 120 notification->setTag(argument);
122 if (options.get("lang", argument)) 121 if (options.get("lang", argument))
123 notification->setLang(argument); 122 notification->setLang(argument);
124 if (options.get("dir", argument)) 123 if (options.get("dir", argument))
125 notification->setDir(argument); 124 notification->setDir(argument);
126 if (options.get("icon", argument)) { 125 if (options.get("icon", argument)) {
127 KURL iconURI = argument.isEmpty() ? KURL() : context->completeURL(argume nt); 126 KURL iconURI = argument.isEmpty() ? KURL() : context->completeURL(argume nt);
128 if (!iconURI.isEmpty() && iconURI.isValid()) 127 if (!iconURI.isEmpty() && iconURI.isValid())
129 notification->setIconURL(iconURI); 128 notification->setIconURL(iconURI);
130 } 129 }
131 130
132 notification->suspendIfNeeded();
133 return notification.release(); 131 return notification.release();
134 } 132 }
135 #endif 133 #endif
136 134
137 const AtomicString& Notification::interfaceName() const 135 const AtomicString& Notification::interfaceName() const
138 { 136 {
139 return eventNames().interfaceForNotification; 137 return eventNames().interfaceForNotification;
140 } 138 }
141 139
142 void Notification::show() 140 void Notification::show()
143 { 141 {
144 // prevent double-showing 142 // prevent double-showing
145 if (m_state == Idle && m_notificationCenter->client()) { 143 if (m_state == Idle && m_notificationCenter->client()) {
146 #if ENABLE(NOTIFICATIONS) 144 #if ENABLE(NOTIFICATIONS)
147 if (!toDocument(scriptExecutionContext())->page()) 145 if (!toDocument(scriptExecutionContext())->page())
148 return; 146 return;
149 if (NotificationController::from(toDocument(scriptExecutionContext())->p age())->client()->checkPermission(scriptExecutionContext()) != NotificationClien t::PermissionAllowed) { 147 if (NotificationController::from(toDocument(scriptExecutionContext())->p age())->client()->checkPermission(scriptExecutionContext()) != NotificationClien t::PermissionAllowed) {
150 dispatchErrorEvent(); 148 dispatchErrorEvent();
151 return; 149 return;
152 } 150 }
153 #endif 151 #endif
154 if (m_notificationCenter->client()->show(this)) { 152 if (m_notificationCenter->client()->show(this)) {
155 m_state = Showing; 153 m_state = Showing;
156 setPendingActivity(this);
157 } 154 }
158 } 155 }
159 } 156 }
160 157
161 void Notification::close() 158 void Notification::close()
162 { 159 {
163 switch (m_state) { 160 switch (m_state) {
164 case Idle: 161 case Idle:
165 break; 162 break;
166 case Showing: 163 case Showing:
(...skipping 10 matching lines...) Expand all
177 return &m_eventTargetData; 174 return &m_eventTargetData;
178 } 175 }
179 176
180 EventTargetData* Notification::ensureEventTargetData() 177 EventTargetData* Notification::ensureEventTargetData()
181 { 178 {
182 return &m_eventTargetData; 179 return &m_eventTargetData;
183 } 180 }
184 181
185 void Notification::contextDestroyed() 182 void Notification::contextDestroyed()
186 { 183 {
187 ActiveDOMObject::contextDestroyed(); 184 ContextLifecycleObserver::contextDestroyed();
188 if (m_notificationCenter->client()) 185 if (m_notificationCenter->client())
189 m_notificationCenter->client()->notificationObjectDestroyed(this); 186 m_notificationCenter->client()->notificationObjectDestroyed(this);
190 } 187 }
191 188
192 void Notification::finalize()
193 {
194 if (m_state == Closed)
195 return;
196 m_state = Closed;
197 unsetPendingActivity(this);
198 }
199
200 void Notification::dispatchShowEvent() 189 void Notification::dispatchShowEvent()
201 { 190 {
191 #if ENABLE(LEGACY_NOTIFICATIONS)
192 dispatchEvent(Event::create(eventNames().displayEvent, false, false));
193 #endif
202 dispatchEvent(Event::create(eventNames().showEvent, false, false)); 194 dispatchEvent(Event::create(eventNames().showEvent, false, false));
203 } 195 }
204 196
205 void Notification::dispatchClickEvent() 197 void Notification::dispatchClickEvent()
206 { 198 {
199 UserGestureIndicator gestureIndicator(DefinitelyProcessingNewUserGesture);
207 WindowFocusAllowedIndicator windowFocusAllowed; 200 WindowFocusAllowedIndicator windowFocusAllowed;
208 dispatchEvent(Event::create(eventNames().clickEvent, false, false)); 201 dispatchEvent(Event::create(eventNames().clickEvent, false, false));
209 } 202 }
210 203
211 void Notification::dispatchCloseEvent() 204 void Notification::dispatchCloseEvent()
212 { 205 {
213 dispatchEvent(Event::create(eventNames().closeEvent, false, false)); 206 dispatchEvent(Event::create(eventNames().closeEvent, false, false));
214 finalize(); 207 m_state = Closed;
215 } 208 }
216 209
217 void Notification::dispatchErrorEvent() 210 void Notification::dispatchErrorEvent()
218 { 211 {
219 dispatchEvent(Event::create(eventNames().errorEvent, false, false)); 212 dispatchEvent(Event::create(eventNames().errorEvent, false, false));
220 } 213 }
221 214
222 #if ENABLE(NOTIFICATIONS) 215 #if ENABLE(NOTIFICATIONS)
223 void Notification::taskTimerFired(Timer<Notification>* timer) 216 void Notification::taskTimerFired(Timer<Notification>* timer)
224 { 217 {
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
258 void Notification::requestPermission(ScriptExecutionContext* context, PassRefPtr <NotificationPermissionCallback> callback) 251 void Notification::requestPermission(ScriptExecutionContext* context, PassRefPtr <NotificationPermissionCallback> callback)
259 { 252 {
260 ASSERT(toDocument(context)->page()); 253 ASSERT(toDocument(context)->page());
261 NotificationController::from(toDocument(context)->page())->client()->request Permission(context, callback); 254 NotificationController::from(toDocument(context)->page())->client()->request Permission(context, callback);
262 } 255 }
263 #endif 256 #endif
264 257
265 } // namespace WebCore 258 } // namespace WebCore
266 259
267 #endif // ENABLE(NOTIFICATIONS) || ENABLE(LEGACY_NOTIFICATIONS) 260 #endif // ENABLE(NOTIFICATIONS) || ENABLE(LEGACY_NOTIFICATIONS)
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698