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