OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/browser_process.h" |
(...skipping 25 matching lines...) Expand all Loading... |
36 #include "content/public/common/show_desktop_notification_params.h" | 36 #include "content/public/common/show_desktop_notification_params.h" |
37 #include "extensions/common/constants.h" | 37 #include "extensions/common/constants.h" |
38 #include "grit/browser_resources.h" | 38 #include "grit/browser_resources.h" |
39 #include "grit/chromium_strings.h" | 39 #include "grit/chromium_strings.h" |
40 #include "grit/generated_resources.h" | 40 #include "grit/generated_resources.h" |
41 #include "grit/theme_resources.h" | 41 #include "grit/theme_resources.h" |
42 #include "net/base/escape.h" | 42 #include "net/base/escape.h" |
43 #include "third_party/WebKit/Source/WebKit/chromium/public/WebSecurityOrigin.h" | 43 #include "third_party/WebKit/Source/WebKit/chromium/public/WebSecurityOrigin.h" |
44 #include "ui/base/l10n/l10n_util.h" | 44 #include "ui/base/l10n/l10n_util.h" |
45 #include "ui/base/resource/resource_bundle.h" | 45 #include "ui/base/resource/resource_bundle.h" |
46 #include "ui/message_center/message_center_util.h" | |
47 #include "ui/message_center/notifier_settings.h" | 46 #include "ui/message_center/notifier_settings.h" |
48 #include "ui/webui/web_ui_util.h" | 47 #include "ui/webui/web_ui_util.h" |
49 | 48 |
50 using content::BrowserThread; | 49 using content::BrowserThread; |
51 using content::RenderViewHost; | 50 using content::RenderViewHost; |
52 using content::WebContents; | 51 using content::WebContents; |
53 using WebKit::WebNotificationPresenter; | 52 using WebKit::WebNotificationPresenter; |
54 using WebKit::WebTextDirection; | 53 using WebKit::WebTextDirection; |
55 using WebKit::WebSecurityOrigin; | 54 using WebKit::WebSecurityOrigin; |
56 | 55 |
57 const ContentSetting kDefaultSetting = CONTENT_SETTING_ASK; | 56 const ContentSetting kDefaultSetting = CONTENT_SETTING_ASK; |
58 | 57 |
59 namespace { | 58 namespace { |
60 | 59 |
| 60 bool UsesTextNotifications() { |
| 61 #if defined(USE_ASH) |
| 62 return true; |
| 63 #else |
| 64 return |
| 65 g_browser_process->notification_ui_manager()->DelegatesToMessageCenter(); |
| 66 #endif |
| 67 } |
| 68 |
61 void ToggleListPrefItem(PrefService* prefs, const char* key, | 69 void ToggleListPrefItem(PrefService* prefs, const char* key, |
62 const std::string& item, bool flag) { | 70 const std::string& item, bool flag) { |
63 ListPrefUpdate update(prefs, key); | 71 ListPrefUpdate update(prefs, key); |
64 base::ListValue* const list = update.Get(); | 72 base::ListValue* const list = update.Get(); |
65 if (flag) { | 73 if (flag) { |
66 // AppendIfNotPresent will delete |adding_value| when the same value | 74 // AppendIfNotPresent will delete |adding_value| when the same value |
67 // already exists. | 75 // already exists. |
68 base::StringValue* const adding_value = new base::StringValue(item); | 76 base::StringValue* const adding_value = new base::StringValue(item); |
69 list->AppendIfNotPresent(adding_value); | 77 list->AppendIfNotPresent(adding_value); |
70 } else { | 78 } else { |
(...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
224 action_taken_ = true; | 232 action_taken_ = true; |
225 return true; | 233 return true; |
226 } | 234 } |
227 | 235 |
228 | 236 |
229 // DesktopNotificationService ------------------------------------------------- | 237 // DesktopNotificationService ------------------------------------------------- |
230 | 238 |
231 // static | 239 // static |
232 void DesktopNotificationService::RegisterUserPrefs( | 240 void DesktopNotificationService::RegisterUserPrefs( |
233 user_prefs::PrefRegistrySyncable* registry) { | 241 user_prefs::PrefRegistrySyncable* registry) { |
| 242 #if defined(OS_CHROMEOS) || defined(ENABLE_MESSAGE_CENTER) |
234 registry->RegisterListPref(prefs::kMessageCenterDisabledExtensionIds, | 243 registry->RegisterListPref(prefs::kMessageCenterDisabledExtensionIds, |
235 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF); | 244 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF); |
236 registry->RegisterListPref(prefs::kMessageCenterDisabledSystemComponentIds, | 245 registry->RegisterListPref(prefs::kMessageCenterDisabledSystemComponentIds, |
237 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF); | 246 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF); |
| 247 #endif |
238 } | 248 } |
239 | 249 |
240 // static | 250 // static |
241 string16 DesktopNotificationService::CreateDataUrl( | 251 string16 DesktopNotificationService::CreateDataUrl( |
242 const GURL& icon_url, const string16& title, const string16& body, | 252 const GURL& icon_url, const string16& title, const string16& body, |
243 WebTextDirection dir) { | 253 WebTextDirection dir) { |
244 int resource; | 254 int resource; |
245 std::vector<std::string> subst; | 255 std::vector<std::string> subst; |
246 if (icon_url.is_valid()) { | 256 if (icon_url.is_valid()) { |
247 resource = IDR_NOTIFICATION_ICON_HTML; | 257 resource = IDR_NOTIFICATION_ICON_HTML; |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
290 | 300 |
291 // static | 301 // static |
292 std::string DesktopNotificationService::AddNotification( | 302 std::string DesktopNotificationService::AddNotification( |
293 const GURL& origin_url, | 303 const GURL& origin_url, |
294 const string16& title, | 304 const string16& title, |
295 const string16& message, | 305 const string16& message, |
296 const GURL& icon_url, | 306 const GURL& icon_url, |
297 const string16& replace_id, | 307 const string16& replace_id, |
298 NotificationDelegate* delegate, | 308 NotificationDelegate* delegate, |
299 Profile* profile) { | 309 Profile* profile) { |
300 if (message_center::IsRichNotificationEnabled()) { | 310 if (UsesTextNotifications()) { |
301 // For message center create a non-HTML notification with |icon_url|. | 311 // For message center create a non-HTML notification with |icon_url|. |
302 Notification notification(origin_url, icon_url, title, message, | 312 Notification notification(origin_url, icon_url, title, message, |
303 WebKit::WebTextDirectionDefault, | 313 WebKit::WebTextDirectionDefault, |
304 string16(), replace_id, delegate); | 314 string16(), replace_id, delegate); |
305 g_browser_process->notification_ui_manager()->Add(notification, profile); | 315 g_browser_process->notification_ui_manager()->Add(notification, profile); |
306 return notification.notification_id(); | 316 return notification.notification_id(); |
307 } | 317 } |
308 | 318 |
309 // Generate a data URL embedding the icon URL, title, and message. | 319 // Generate a data URL embedding the icon URL, title, and message. |
310 GURL content_url(CreateDataUrl( | 320 GURL content_url(CreateDataUrl( |
311 icon_url, title, message, WebKit::WebTextDirectionDefault)); | 321 icon_url, title, message, WebKit::WebTextDirectionDefault)); |
312 Notification notification( | 322 Notification notification( |
313 GURL(), content_url, string16(), replace_id, delegate); | 323 GURL(), content_url, string16(), replace_id, delegate); |
314 g_browser_process->notification_ui_manager()->Add(notification, profile); | 324 g_browser_process->notification_ui_manager()->Add(notification, profile); |
315 return notification.notification_id(); | 325 return notification.notification_id(); |
316 } | 326 } |
317 | 327 |
318 // static | 328 // static |
319 std::string DesktopNotificationService::AddIconNotification( | 329 std::string DesktopNotificationService::AddIconNotification( |
320 const GURL& origin_url, | 330 const GURL& origin_url, |
321 const string16& title, | 331 const string16& title, |
322 const string16& message, | 332 const string16& message, |
323 const gfx::Image& icon, | 333 const gfx::Image& icon, |
324 const string16& replace_id, | 334 const string16& replace_id, |
325 NotificationDelegate* delegate, | 335 NotificationDelegate* delegate, |
326 Profile* profile) { | 336 Profile* profile) { |
327 | 337 |
328 if (message_center::IsRichNotificationEnabled()) { | 338 if (UsesTextNotifications()) { |
329 // For message center create a non-HTML notification with |icon|. | 339 // For message center create a non-HTML notification with |icon|. |
330 Notification notification(origin_url, icon, title, message, | 340 Notification notification(origin_url, icon, title, message, |
331 WebKit::WebTextDirectionDefault, | 341 WebKit::WebTextDirectionDefault, |
332 string16(), replace_id, delegate); | 342 string16(), replace_id, delegate); |
333 g_browser_process->notification_ui_manager()->Add(notification, profile); | 343 g_browser_process->notification_ui_manager()->Add(notification, profile); |
334 return notification.notification_id(); | 344 return notification.notification_id(); |
335 } | 345 } |
336 | 346 |
337 GURL icon_url; | 347 GURL icon_url; |
338 if (!icon.IsEmpty()) | 348 if (!icon.IsEmpty()) |
339 icon_url = GURL(webui::GetBitmapDataUrl(*icon.ToSkBitmap())); | 349 icon_url = GURL(webui::GetBitmapDataUrl(*icon.ToSkBitmap())); |
340 return AddNotification( | 350 return AddNotification( |
341 origin_url, title, message, icon_url, replace_id, delegate, profile); | 351 origin_url, title, message, icon_url, replace_id, delegate, profile); |
342 } | 352 } |
343 | 353 |
344 // static | 354 // static |
345 void DesktopNotificationService::RemoveNotification( | 355 void DesktopNotificationService::RemoveNotification( |
346 const std::string& notification_id) { | 356 const std::string& notification_id) { |
347 g_browser_process->notification_ui_manager()->CancelById(notification_id); | 357 g_browser_process->notification_ui_manager()->CancelById(notification_id); |
348 } | 358 } |
349 | 359 |
350 DesktopNotificationService::DesktopNotificationService( | 360 DesktopNotificationService::DesktopNotificationService( |
351 Profile* profile, | 361 Profile* profile, |
352 NotificationUIManager* ui_manager) | 362 NotificationUIManager* ui_manager) |
353 : profile_(profile), | 363 : profile_(profile), |
354 ui_manager_(ui_manager) { | 364 ui_manager_(ui_manager) { |
| 365 #if defined(ENABLE_MESSAGE_CENTER) |
355 OnDisabledExtensionIdsChanged(); | 366 OnDisabledExtensionIdsChanged(); |
356 OnDisabledSystemComponentIdsChanged(); | 367 OnDisabledSystemComponentIdsChanged(); |
357 disabled_extension_id_pref_.Init( | 368 disabled_extension_id_pref_.Init( |
358 prefs::kMessageCenterDisabledExtensionIds, | 369 prefs::kMessageCenterDisabledExtensionIds, |
359 profile_->GetPrefs(), | 370 profile_->GetPrefs(), |
360 base::Bind( | 371 base::Bind( |
361 &DesktopNotificationService::OnDisabledExtensionIdsChanged, | 372 &DesktopNotificationService::OnDisabledExtensionIdsChanged, |
362 base::Unretained(this))); | 373 base::Unretained(this))); |
363 disabled_system_component_id_pref_.Init( | 374 disabled_system_component_id_pref_.Init( |
364 prefs::kMessageCenterDisabledSystemComponentIds, | 375 prefs::kMessageCenterDisabledSystemComponentIds, |
365 profile_->GetPrefs(), | 376 profile_->GetPrefs(), |
366 base::Bind( | 377 base::Bind( |
367 &DesktopNotificationService::OnDisabledSystemComponentIdsChanged, | 378 &DesktopNotificationService::OnDisabledSystemComponentIdsChanged, |
368 base::Unretained(this))); | 379 base::Unretained(this))); |
| 380 #endif |
369 } | 381 } |
370 | 382 |
371 DesktopNotificationService::~DesktopNotificationService() { | 383 DesktopNotificationService::~DesktopNotificationService() { |
| 384 #if defined(ENABLE_MESSAGE_CENTER) |
| 385 disabled_extension_id_pref_.Destroy(); |
| 386 #endif |
372 } | 387 } |
373 | 388 |
374 void DesktopNotificationService::GrantPermission(const GURL& origin) { | 389 void DesktopNotificationService::GrantPermission(const GURL& origin) { |
375 ContentSettingsPattern primary_pattern = | 390 ContentSettingsPattern primary_pattern = |
376 ContentSettingsPattern::FromURLNoWildcard(origin); | 391 ContentSettingsPattern::FromURLNoWildcard(origin); |
377 profile_->GetHostContentSettingsMap()->SetContentSetting( | 392 profile_->GetHostContentSettingsMap()->SetContentSetting( |
378 primary_pattern, | 393 primary_pattern, |
379 ContentSettingsPattern::Wildcard(), | 394 ContentSettingsPattern::Wildcard(), |
380 CONTENT_SETTINGS_TYPE_NOTIFICATIONS, | 395 CONTENT_SETTINGS_TYPE_NOTIFICATIONS, |
381 NO_RESOURCE_IDENTIFIER, | 396 NO_RESOURCE_IDENTIFIER, |
(...skipping 188 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
570 id, | 585 id, |
571 !enabled); | 586 !enabled); |
572 } | 587 } |
573 | 588 |
574 void DesktopNotificationService::OnDisabledExtensionIdsChanged() { | 589 void DesktopNotificationService::OnDisabledExtensionIdsChanged() { |
575 CopySetFromPrefToMemory(profile_->GetPrefs(), | 590 CopySetFromPrefToMemory(profile_->GetPrefs(), |
576 prefs::kMessageCenterDisabledExtensionIds, | 591 prefs::kMessageCenterDisabledExtensionIds, |
577 &disabled_extension_ids_); | 592 &disabled_extension_ids_); |
578 } | 593 } |
579 | 594 |
| 595 #if defined(ENABLE_MESSAGE_CENTER) |
580 bool DesktopNotificationService::IsSystemComponentEnabled( | 596 bool DesktopNotificationService::IsSystemComponentEnabled( |
581 message_center::Notifier::SystemComponentNotifierType type) { | 597 message_center::Notifier::SystemComponentNotifierType type) { |
582 return disabled_system_component_ids_.find(message_center::ToString(type)) == | 598 return disabled_system_component_ids_.find(message_center::ToString(type)) == |
583 disabled_system_component_ids_.end(); | 599 disabled_system_component_ids_.end(); |
584 } | 600 } |
585 | 601 |
586 void DesktopNotificationService::SetSystemComponentEnabled( | 602 void DesktopNotificationService::SetSystemComponentEnabled( |
587 message_center::Notifier::SystemComponentNotifierType type, bool enabled) { | 603 message_center::Notifier::SystemComponentNotifierType type, bool enabled) { |
588 // Do not touch |disabled_extension_ids_|. It will be updated at | 604 // Do not touch |disabled_extension_ids_|. It will be updated at |
589 // OnDisabledExtensionIdsChanged() which will be called when the pref changes. | 605 // OnDisabledExtensionIdsChanged() which will be called when the pref changes. |
590 ToggleListPrefItem( | 606 ToggleListPrefItem( |
591 profile_->GetPrefs(), | 607 profile_->GetPrefs(), |
592 prefs::kMessageCenterDisabledSystemComponentIds, | 608 prefs::kMessageCenterDisabledSystemComponentIds, |
593 message_center::ToString(type), | 609 message_center::ToString(type), |
594 !enabled); | 610 !enabled); |
595 } | 611 } |
596 | 612 |
597 void DesktopNotificationService::OnDisabledSystemComponentIdsChanged() { | 613 void DesktopNotificationService::OnDisabledSystemComponentIdsChanged() { |
598 disabled_system_component_ids_.clear(); | 614 disabled_system_component_ids_.clear(); |
599 CopySetFromPrefToMemory(profile_->GetPrefs(), | 615 CopySetFromPrefToMemory(profile_->GetPrefs(), |
600 prefs::kMessageCenterDisabledSystemComponentIds, | 616 prefs::kMessageCenterDisabledSystemComponentIds, |
601 &disabled_system_component_ids_); | 617 &disabled_system_component_ids_); |
602 } | 618 } |
| 619 #endif |
603 | 620 |
604 WebKit::WebNotificationPresenter::Permission | 621 WebKit::WebNotificationPresenter::Permission |
605 DesktopNotificationService::HasPermission(const GURL& origin) { | 622 DesktopNotificationService::HasPermission(const GURL& origin) { |
606 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 623 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
607 HostContentSettingsMap* host_content_settings_map = | 624 HostContentSettingsMap* host_content_settings_map = |
608 profile_->GetHostContentSettingsMap(); | 625 profile_->GetHostContentSettingsMap(); |
609 ContentSetting setting = host_content_settings_map->GetContentSetting( | 626 ContentSetting setting = host_content_settings_map->GetContentSetting( |
610 origin, | 627 origin, |
611 origin, | 628 origin, |
612 CONTENT_SETTINGS_TYPE_NOTIFICATIONS, | 629 CONTENT_SETTINGS_TYPE_NOTIFICATIONS, |
613 NO_RESOURCE_IDENTIFIER); | 630 NO_RESOURCE_IDENTIFIER); |
614 | 631 |
615 if (setting == CONTENT_SETTING_ALLOW) | 632 if (setting == CONTENT_SETTING_ALLOW) |
616 return WebKit::WebNotificationPresenter::PermissionAllowed; | 633 return WebKit::WebNotificationPresenter::PermissionAllowed; |
617 if (setting == CONTENT_SETTING_BLOCK) | 634 if (setting == CONTENT_SETTING_BLOCK) |
618 return WebKit::WebNotificationPresenter::PermissionDenied; | 635 return WebKit::WebNotificationPresenter::PermissionDenied; |
619 if (setting == CONTENT_SETTING_ASK) | 636 if (setting == CONTENT_SETTING_ASK) |
620 return WebKit::WebNotificationPresenter::PermissionNotAllowed; | 637 return WebKit::WebNotificationPresenter::PermissionNotAllowed; |
621 NOTREACHED() << "Invalid notifications settings value: " << setting; | 638 NOTREACHED() << "Invalid notifications settings value: " << setting; |
622 return WebKit::WebNotificationPresenter::PermissionNotAllowed; | 639 return WebKit::WebNotificationPresenter::PermissionNotAllowed; |
623 } | 640 } |
OLD | NEW |