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

Side by Side Diff: chrome/browser/notifications/desktop_notification_service.cc

Issue 8550003: Defer construction of NotificationUIManager to fix notification initialization. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 9 years, 1 month 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
« no previous file with comments | « chrome/browser/notifications/desktop_notification_service.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/notifications/desktop_notification_service.h" 5 #include "chrome/browser/notifications/desktop_notification_service.h"
6 6
7 #include "base/metrics/histogram.h" 7 #include "base/metrics/histogram.h"
8 #include "base/threading/thread.h" 8 #include "base/threading/thread.h"
9 #include "base/utf_string_conversions.h" 9 #include "base/utf_string_conversions.h"
10 #include "chrome/browser/browser_process.h"
10 #include "chrome/browser/content_settings/content_settings_details.h" 11 #include "chrome/browser/content_settings/content_settings_details.h"
11 #include "chrome/browser/content_settings/content_settings_provider.h" 12 #include "chrome/browser/content_settings/content_settings_provider.h"
12 #include "chrome/browser/content_settings/host_content_settings_map.h" 13 #include "chrome/browser/content_settings/host_content_settings_map.h"
13 #include "chrome/browser/extensions/extension_service.h" 14 #include "chrome/browser/extensions/extension_service.h"
14 #include "chrome/browser/infobars/infobar_tab_helper.h" 15 #include "chrome/browser/infobars/infobar_tab_helper.h"
15 #include "chrome/browser/notifications/desktop_notification_service_factory.h" 16 #include "chrome/browser/notifications/desktop_notification_service_factory.h"
16 #include "chrome/browser/notifications/notification.h" 17 #include "chrome/browser/notifications/notification.h"
17 #include "chrome/browser/notifications/notification_object_proxy.h" 18 #include "chrome/browser/notifications/notification_object_proxy.h"
18 #include "chrome/browser/notifications/notification_ui_manager.h" 19 #include "chrome/browser/notifications/notification_ui_manager.h"
19 #include "chrome/browser/prefs/pref_service.h" 20 #include "chrome/browser/prefs/pref_service.h"
(...skipping 186 matching lines...) Expand 10 before | Expand all | Expand 10 after
206 if (template_html.empty()) { 207 if (template_html.empty()) {
207 NOTREACHED() << "unable to load template. ID: " << resource; 208 NOTREACHED() << "unable to load template. ID: " << resource;
208 return string16(); 209 return string16();
209 } 210 }
210 211
211 std::string data = ReplaceStringPlaceholders(template_html, subst, NULL); 212 std::string data = ReplaceStringPlaceholders(template_html, subst, NULL);
212 return UTF8ToUTF16("data:text/html;charset=utf-8," + 213 return UTF8ToUTF16("data:text/html;charset=utf-8," +
213 net::EscapeQueryParamValue(data, false)); 214 net::EscapeQueryParamValue(data, false));
214 } 215 }
215 216
216 DesktopNotificationService::DesktopNotificationService(Profile* profile, 217 DesktopNotificationService::DesktopNotificationService(Profile* profile,
jam 2011/11/19 01:18:32 can we make this take a single argument now?
stevenjb 2011/11/19 01:24:20 Unfortunately no. We do construct locally a Notifi
217 NotificationUIManager* ui_manager) 218 NotificationUIManager* ui_manager)
218 : profile_(profile), 219 : profile_(profile),
219 ui_manager_(ui_manager) { 220 ui_manager_(ui_manager) {
220 StartObserving(); 221 StartObserving();
221 } 222 }
222 223
223 DesktopNotificationService::~DesktopNotificationService() { 224 DesktopNotificationService::~DesktopNotificationService() {
224 StopObserving(); 225 StopObserving();
225 } 226 }
226 227
227 void DesktopNotificationService::SetUIManager(
228 NotificationUIManager* ui_manager) {
229 DCHECK(!ui_manager_);
230 ui_manager_ = ui_manager;
231 }
232
233 void DesktopNotificationService::StartObserving() { 228 void DesktopNotificationService::StartObserving() {
234 if (!profile_->IsOffTheRecord()) { 229 if (!profile_->IsOffTheRecord()) {
235 notification_registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED, 230 notification_registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED,
236 content::Source<Profile>(profile_)); 231 content::Source<Profile>(profile_));
237 } 232 }
238 notification_registrar_.Add(this, chrome::NOTIFICATION_PROFILE_DESTROYED, 233 notification_registrar_.Add(this, chrome::NOTIFICATION_PROFILE_DESTROYED,
239 content::Source<Profile>(profile_)); 234 content::Source<Profile>(profile_));
240 } 235 }
241 236
242 void DesktopNotificationService::StopObserving() { 237 void DesktopNotificationService::StopObserving() {
(...skipping 24 matching lines...) Expand all
267 262
268 void DesktopNotificationService::Observe( 263 void DesktopNotificationService::Observe(
269 int type, 264 int type,
270 const content::NotificationSource& source, 265 const content::NotificationSource& source,
271 const content::NotificationDetails& details) { 266 const content::NotificationDetails& details) {
272 if (type == chrome::NOTIFICATION_EXTENSION_UNLOADED) { 267 if (type == chrome::NOTIFICATION_EXTENSION_UNLOADED) {
273 // Remove all notifications currently shown or queued by the extension 268 // Remove all notifications currently shown or queued by the extension
274 // which was unloaded. 269 // which was unloaded.
275 const Extension* extension = 270 const Extension* extension =
276 content::Details<UnloadedExtensionInfo>(details)->extension; 271 content::Details<UnloadedExtensionInfo>(details)->extension;
277 if (extension && ui_manager_) 272 if (extension)
278 ui_manager_->CancelAllBySourceOrigin(extension->url()); 273 GetUIManager()->CancelAllBySourceOrigin(extension->url());
279 } else if (type == chrome::NOTIFICATION_PROFILE_DESTROYED) { 274 } else if (type == chrome::NOTIFICATION_PROFILE_DESTROYED) {
280 StopObserving(); 275 StopObserving();
281 } 276 }
282 } 277 }
283 278
284 ContentSetting DesktopNotificationService::GetDefaultContentSetting( 279 ContentSetting DesktopNotificationService::GetDefaultContentSetting(
285 std::string* provider_id) { 280 std::string* provider_id) {
286 return profile_->GetHostContentSettingsMap()->GetDefaultContentSetting( 281 return profile_->GetHostContentSettingsMap()->GetDefaultContentSetting(
287 CONTENT_SETTINGS_TYPE_NOTIFICATIONS, provider_id); 282 CONTENT_SETTINGS_TYPE_NOTIFICATIONS, provider_id);
288 } 283 }
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
364 } else { 359 } else {
365 // Notify renderer immediately. 360 // Notify renderer immediately.
366 RenderViewHost* host = RenderViewHost::FromID(process_id, route_id); 361 RenderViewHost* host = RenderViewHost::FromID(process_id, route_id);
367 if (host) 362 if (host)
368 host->DesktopNotificationPermissionRequestDone(callback_context); 363 host->DesktopNotificationPermissionRequestDone(callback_context);
369 } 364 }
370 } 365 }
371 366
372 void DesktopNotificationService::ShowNotification( 367 void DesktopNotificationService::ShowNotification(
373 const Notification& notification) { 368 const Notification& notification) {
374 // ui_manager_ should never be NULL here. 369 GetUIManager()->Add(notification, profile_);
375 ui_manager_->Add(notification, profile_);
376 } 370 }
377 371
378 bool DesktopNotificationService::CancelDesktopNotification( 372 bool DesktopNotificationService::CancelDesktopNotification(
379 int process_id, int route_id, int notification_id) { 373 int process_id, int route_id, int notification_id) {
380 scoped_refptr<NotificationObjectProxy> proxy( 374 scoped_refptr<NotificationObjectProxy> proxy(
381 new NotificationObjectProxy(process_id, route_id, notification_id, 375 new NotificationObjectProxy(process_id, route_id, notification_id,
382 false)); 376 false));
383 if (ui_manager_) 377 return GetUIManager()->CancelById(proxy->id());
384 return ui_manager_->CancelById(proxy->id());
385 return false;
386 } 378 }
387 379
388 bool DesktopNotificationService::ShowDesktopNotification( 380 bool DesktopNotificationService::ShowDesktopNotification(
389 const content::ShowDesktopNotificationHostMsgParams& params, 381 const content::ShowDesktopNotificationHostMsgParams& params,
390 int process_id, int route_id, DesktopNotificationSource source) { 382 int process_id, int route_id, DesktopNotificationSource source) {
391 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 383 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
392 const GURL& origin = params.origin; 384 const GURL& origin = params.origin;
393 NotificationObjectProxy* proxy = 385 NotificationObjectProxy* proxy =
394 new NotificationObjectProxy(process_id, route_id, 386 new NotificationObjectProxy(process_id, route_id,
395 params.notification_id, 387 params.notification_id,
(...skipping 28 matching lines...) Expand all
424 return UTF8ToUTF16(origin.host()); 416 return UTF8ToUTF16(origin.host());
425 } 417 }
426 418
427 void DesktopNotificationService::NotifySettingsChange() { 419 void DesktopNotificationService::NotifySettingsChange() {
428 content::NotificationService::current()->Notify( 420 content::NotificationService::current()->Notify(
429 chrome::NOTIFICATION_DESKTOP_NOTIFICATION_SETTINGS_CHANGED, 421 chrome::NOTIFICATION_DESKTOP_NOTIFICATION_SETTINGS_CHANGED,
430 content::Source<DesktopNotificationService>(this), 422 content::Source<DesktopNotificationService>(this),
431 content::NotificationService::NoDetails()); 423 content::NotificationService::NoDetails());
432 } 424 }
433 425
426 NotificationUIManager* DesktopNotificationService::GetUIManager() {
427 // We defer setting ui_manager_ to the global singleton until we need it
428 // in order to avoid UI dependent construction during startup.
429 if (!ui_manager_)
jam 2011/11/19 01:18:32 this member variable, and even this function, seem
stevenjb 2011/11/19 01:24:20 Tests might set this in which case we do not want
430 ui_manager_ = g_browser_process->notification_ui_manager();
431 return ui_manager_;
432 }
433
434 WebKit::WebNotificationPresenter::Permission 434 WebKit::WebNotificationPresenter::Permission
435 DesktopNotificationService::HasPermission(const GURL& origin) { 435 DesktopNotificationService::HasPermission(const GURL& origin) {
436 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 436 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
437 HostContentSettingsMap* host_content_settings_map = 437 HostContentSettingsMap* host_content_settings_map =
438 profile_->GetHostContentSettingsMap(); 438 profile_->GetHostContentSettingsMap();
439 ContentSetting setting = host_content_settings_map->GetContentSetting( 439 ContentSetting setting = host_content_settings_map->GetContentSetting(
440 origin, 440 origin,
441 origin, 441 origin,
442 CONTENT_SETTINGS_TYPE_NOTIFICATIONS, 442 CONTENT_SETTINGS_TYPE_NOTIFICATIONS,
443 NO_RESOURCE_IDENTIFIER); 443 NO_RESOURCE_IDENTIFIER);
444 444
445 if (setting == CONTENT_SETTING_ALLOW) 445 if (setting == CONTENT_SETTING_ALLOW)
446 return WebKit::WebNotificationPresenter::PermissionAllowed; 446 return WebKit::WebNotificationPresenter::PermissionAllowed;
447 if (setting == CONTENT_SETTING_BLOCK) 447 if (setting == CONTENT_SETTING_BLOCK)
448 return WebKit::WebNotificationPresenter::PermissionDenied; 448 return WebKit::WebNotificationPresenter::PermissionDenied;
449 if (setting == CONTENT_SETTING_ASK) 449 if (setting == CONTENT_SETTING_ASK)
450 return WebKit::WebNotificationPresenter::PermissionNotAllowed; 450 return WebKit::WebNotificationPresenter::PermissionNotAllowed;
451 NOTREACHED() << "Invalid notifications settings value: " << setting; 451 NOTREACHED() << "Invalid notifications settings value: " << setting;
452 return WebKit::WebNotificationPresenter::PermissionNotAllowed; 452 return WebKit::WebNotificationPresenter::PermissionNotAllowed;
453 } 453 }
OLDNEW
« no previous file with comments | « chrome/browser/notifications/desktop_notification_service.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698