desktop_notification_service.cc revision 8bcbed890bc3ce4d7a057a8f32cab53fa534672e
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 3// found in the LICENSE file. 4 5#include "chrome/browser/notifications/desktop_notification_service.h" 6 7#include "base/metrics/histogram.h" 8#include "base/strings/utf_string_conversions.h" 9#include "base/threading/thread.h" 10#include "chrome/browser/browser_process.h" 11#include "chrome/browser/chrome_notification_types.h" 12#include "chrome/browser/content_settings/content_settings_details.h" 13#include "chrome/browser/content_settings/content_settings_provider.h" 14#include "chrome/browser/content_settings/host_content_settings_map.h" 15#include "chrome/browser/extensions/extension_info_map.h" 16#include "chrome/browser/extensions/extension_service.h" 17#include "chrome/browser/extensions/extension_system.h" 18#include "chrome/browser/infobars/confirm_infobar_delegate.h" 19#include "chrome/browser/infobars/infobar_service.h" 20#include "chrome/browser/notifications/desktop_notification_service_factory.h" 21#include "chrome/browser/notifications/notification.h" 22#include "chrome/browser/notifications/notification_object_proxy.h" 23#include "chrome/browser/notifications/notification_ui_manager.h" 24#include "chrome/browser/notifications/sync_notifier/chrome_notifier_service.h" 25#include "chrome/browser/notifications/sync_notifier/chrome_notifier_service_factory.h" 26#include "chrome/browser/prefs/scoped_user_pref_update.h" 27#include "chrome/browser/profiles/profile.h" 28#include "chrome/browser/ui/browser.h" 29#include "chrome/common/content_settings.h" 30#include "chrome/common/content_settings_pattern.h" 31#include "chrome/common/pref_names.h" 32#include "chrome/common/url_constants.h" 33#include "components/user_prefs/pref_registry_syncable.h" 34#include "content/public/browser/browser_thread.h" 35#include "content/public/browser/notification_service.h" 36#include "content/public/browser/render_view_host.h" 37#include "content/public/browser/web_contents.h" 38#include "content/public/common/show_desktop_notification_params.h" 39#include "extensions/common/constants.h" 40#include "grit/browser_resources.h" 41#include "grit/chromium_strings.h" 42#include "grit/generated_resources.h" 43#include "grit/theme_resources.h" 44#include "net/base/escape.h" 45#include "ui/base/l10n/l10n_util.h" 46#include "ui/base/resource/resource_bundle.h" 47#include "ui/base/webui/web_ui_util.h" 48#include "ui/message_center/message_center_util.h" 49#include "ui/message_center/notifier_settings.h" 50 51#if defined(OS_CHROMEOS) 52#include "ash/system/system_notifier.h" 53#endif 54 55using content::BrowserThread; 56using content::RenderViewHost; 57using content::WebContents; 58using message_center::NotifierId; 59using WebKit::WebNotificationPresenter; 60using WebKit::WebTextDirection; 61 62 63// NotificationPermissionInfoBarDelegate -------------------------------------- 64 65// The delegate for the infobar shown when an origin requests notification 66// permissions. 67class NotificationPermissionInfoBarDelegate : public ConfirmInfoBarDelegate { 68 public: 69 // Creates a notification permission infobar delegate and adds it to 70 // |infobar_service|. 71 static void Create(InfoBarService* infobar_service, 72 DesktopNotificationService* notification_service, 73 const GURL& origin, 74 const string16& display_name, 75 int process_id, 76 int route_id, 77 int callback_context); 78 79 private: 80 NotificationPermissionInfoBarDelegate( 81 InfoBarService* infobar_service, 82 DesktopNotificationService* notification_service, 83 const GURL& origin, 84 const string16& display_name, 85 int process_id, 86 int route_id, 87 int callback_context); 88 virtual ~NotificationPermissionInfoBarDelegate(); 89 90 // ConfirmInfoBarDelegate: 91 virtual int GetIconID() const OVERRIDE; 92 virtual Type GetInfoBarType() const OVERRIDE; 93 virtual string16 GetMessageText() const OVERRIDE; 94 virtual string16 GetButtonLabel(InfoBarButton button) const OVERRIDE; 95 virtual bool Accept() OVERRIDE; 96 virtual bool Cancel() OVERRIDE; 97 98 // The origin we are asking for permissions on. 99 GURL origin_; 100 101 // The display name for the origin to be displayed. Will be different from 102 // origin_ for extensions. 103 string16 display_name_; 104 105 // The notification service to be used. 106 DesktopNotificationService* notification_service_; 107 108 // The callback information that tells us how to respond to javascript via 109 // the correct RenderView. 110 int process_id_; 111 int route_id_; 112 int callback_context_; 113 114 // Whether the user clicked one of the buttons. 115 bool action_taken_; 116 117 DISALLOW_COPY_AND_ASSIGN(NotificationPermissionInfoBarDelegate); 118}; 119 120// static 121void NotificationPermissionInfoBarDelegate::Create( 122 InfoBarService* infobar_service, 123 DesktopNotificationService* notification_service, 124 const GURL& origin, 125 const string16& display_name, 126 int process_id, 127 int route_id, 128 int callback_context) { 129 infobar_service->AddInfoBar(scoped_ptr<InfoBarDelegate>( 130 new NotificationPermissionInfoBarDelegate( 131 infobar_service, notification_service, origin, display_name, 132 process_id, route_id, callback_context))); 133} 134 135NotificationPermissionInfoBarDelegate::NotificationPermissionInfoBarDelegate( 136 InfoBarService* infobar_service, 137 DesktopNotificationService* notification_service, 138 const GURL& origin, 139 const string16& display_name, 140 int process_id, 141 int route_id, 142 int callback_context) 143 : ConfirmInfoBarDelegate(infobar_service), 144 origin_(origin), 145 display_name_(display_name), 146 notification_service_(notification_service), 147 process_id_(process_id), 148 route_id_(route_id), 149 callback_context_(callback_context), 150 action_taken_(false) { 151} 152 153NotificationPermissionInfoBarDelegate:: 154 ~NotificationPermissionInfoBarDelegate() { 155 if (!action_taken_) 156 UMA_HISTOGRAM_COUNTS("NotificationPermissionRequest.Ignored", 1); 157 158 RenderViewHost* host = RenderViewHost::FromID(process_id_, route_id_); 159 if (host) 160 host->DesktopNotificationPermissionRequestDone(callback_context_); 161} 162 163int NotificationPermissionInfoBarDelegate::GetIconID() const { 164 return IDR_INFOBAR_DESKTOP_NOTIFICATIONS; 165} 166 167InfoBarDelegate::Type 168 NotificationPermissionInfoBarDelegate::GetInfoBarType() const { 169 return PAGE_ACTION_TYPE; 170} 171 172string16 NotificationPermissionInfoBarDelegate::GetMessageText() const { 173 return l10n_util::GetStringFUTF16(IDS_NOTIFICATION_PERMISSIONS, 174 display_name_); 175} 176 177string16 NotificationPermissionInfoBarDelegate::GetButtonLabel( 178 InfoBarButton button) const { 179 return l10n_util::GetStringUTF16((button == BUTTON_OK) ? 180 IDS_NOTIFICATION_PERMISSION_YES : IDS_NOTIFICATION_PERMISSION_NO); 181} 182 183bool NotificationPermissionInfoBarDelegate::Accept() { 184 UMA_HISTOGRAM_COUNTS("NotificationPermissionRequest.Allowed", 1); 185 notification_service_->GrantPermission(origin_); 186 action_taken_ = true; 187 return true; 188} 189 190bool NotificationPermissionInfoBarDelegate::Cancel() { 191 UMA_HISTOGRAM_COUNTS("NotificationPermissionRequest.Denied", 1); 192 notification_service_->DenyPermission(origin_); 193 action_taken_ = true; 194 return true; 195} 196 197 198// DesktopNotificationService ------------------------------------------------- 199 200// static 201void DesktopNotificationService::RegisterProfilePrefs( 202 user_prefs::PrefRegistrySyncable* registry) { 203 registry->RegisterListPref( 204 prefs::kMessageCenterDisabledExtensionIds, 205 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); 206 registry->RegisterListPref( 207 prefs::kMessageCenterDisabledSystemComponentIds, 208 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); 209 registry->RegisterListPref( 210 prefs::kMessageCenterEnabledSyncNotifierIds, 211 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); 212 WelcomeNotification::RegisterProfilePrefs(registry); 213} 214 215// static 216string16 DesktopNotificationService::CreateDataUrl( 217 const GURL& icon_url, const string16& title, const string16& body, 218 WebTextDirection dir) { 219 int resource; 220 std::vector<std::string> subst; 221 if (icon_url.is_valid()) { 222 resource = IDR_NOTIFICATION_ICON_HTML; 223 subst.push_back(icon_url.spec()); 224 subst.push_back(net::EscapeForHTML(UTF16ToUTF8(title))); 225 subst.push_back(net::EscapeForHTML(UTF16ToUTF8(body))); 226 // icon float position 227 subst.push_back(dir == WebKit::WebTextDirectionRightToLeft ? 228 "right" : "left"); 229 } else if (title.empty() || body.empty()) { 230 resource = IDR_NOTIFICATION_1LINE_HTML; 231 string16 line = title.empty() ? body : title; 232 // Strings are div names in the template file. 233 string16 line_name = title.empty() ? ASCIIToUTF16("description") 234 : ASCIIToUTF16("title"); 235 subst.push_back(net::EscapeForHTML(UTF16ToUTF8(line_name))); 236 subst.push_back(net::EscapeForHTML(UTF16ToUTF8(line))); 237 } else { 238 resource = IDR_NOTIFICATION_2LINE_HTML; 239 subst.push_back(net::EscapeForHTML(UTF16ToUTF8(title))); 240 subst.push_back(net::EscapeForHTML(UTF16ToUTF8(body))); 241 } 242 // body text direction 243 subst.push_back(dir == WebKit::WebTextDirectionRightToLeft ? 244 "rtl" : "ltr"); 245 246 return CreateDataUrl(resource, subst); 247} 248 249// static 250string16 DesktopNotificationService::CreateDataUrl( 251 int resource, const std::vector<std::string>& subst) { 252 const base::StringPiece template_html( 253 ResourceBundle::GetSharedInstance().GetRawDataResource( 254 resource)); 255 256 if (template_html.empty()) { 257 NOTREACHED() << "unable to load template. ID: " << resource; 258 return string16(); 259 } 260 261 std::string data = ReplaceStringPlaceholders(template_html, subst, NULL); 262 return UTF8ToUTF16("data:text/html;charset=utf-8," + 263 net::EscapeQueryParamValue(data, false)); 264} 265 266// static 267std::string DesktopNotificationService::AddNotification( 268 const GURL& origin_url, 269 const string16& title, 270 const string16& message, 271 const GURL& icon_url, 272 const string16& replace_id, 273 NotificationDelegate* delegate, 274 Profile* profile) { 275 if (message_center::IsRichNotificationEnabled()) { 276 // For message center create a non-HTML notification with |icon_url|. 277 Notification notification(origin_url, icon_url, title, message, 278 WebKit::WebTextDirectionDefault, 279 string16(), replace_id, delegate); 280 g_browser_process->notification_ui_manager()->Add(notification, profile); 281 return notification.notification_id(); 282 } 283 284 // Generate a data URL embedding the icon URL, title, and message. 285 GURL content_url(CreateDataUrl( 286 icon_url, title, message, WebKit::WebTextDirectionDefault)); 287 Notification notification( 288 GURL(), content_url, string16(), replace_id, delegate); 289 g_browser_process->notification_ui_manager()->Add(notification, profile); 290 return notification.notification_id(); 291} 292 293// static 294std::string DesktopNotificationService::AddIconNotification( 295 const GURL& origin_url, 296 const string16& title, 297 const string16& message, 298 const gfx::Image& icon, 299 const string16& replace_id, 300 NotificationDelegate* delegate, 301 Profile* profile) { 302 if (message_center::IsRichNotificationEnabled()) { 303 // For message center create a non-HTML notification with |icon|. 304 Notification notification(origin_url, icon, title, message, 305 WebKit::WebTextDirectionDefault, 306 string16(), replace_id, delegate); 307 g_browser_process->notification_ui_manager()->Add(notification, profile); 308 return notification.notification_id(); 309 } 310 311 GURL icon_url; 312 if (!icon.IsEmpty()) 313 icon_url = GURL(webui::GetBitmapDataUrl(*icon.ToSkBitmap())); 314 return AddNotification( 315 origin_url, title, message, icon_url, replace_id, delegate, profile); 316} 317 318// static 319void DesktopNotificationService::RemoveNotification( 320 const std::string& notification_id) { 321 g_browser_process->notification_ui_manager()->CancelById(notification_id); 322} 323 324DesktopNotificationService::DesktopNotificationService( 325 Profile* profile, 326 NotificationUIManager* ui_manager) 327 : profile_(profile), 328 ui_manager_(ui_manager) { 329 OnStringListPrefChanged( 330 prefs::kMessageCenterDisabledExtensionIds, &disabled_extension_ids_); 331 OnStringListPrefChanged( 332 prefs::kMessageCenterDisabledSystemComponentIds, 333 &disabled_system_component_ids_); 334 OnStringListPrefChanged( 335 prefs::kMessageCenterEnabledSyncNotifierIds, &enabled_sync_notifier_ids_); 336 disabled_extension_id_pref_.Init( 337 prefs::kMessageCenterDisabledExtensionIds, 338 profile_->GetPrefs(), 339 base::Bind( 340 &DesktopNotificationService::OnStringListPrefChanged, 341 base::Unretained(this), 342 base::Unretained(prefs::kMessageCenterDisabledExtensionIds), 343 base::Unretained(&disabled_extension_ids_))); 344 disabled_system_component_id_pref_.Init( 345 prefs::kMessageCenterDisabledSystemComponentIds, 346 profile_->GetPrefs(), 347 base::Bind( 348 &DesktopNotificationService::OnStringListPrefChanged, 349 base::Unretained(this), 350 base::Unretained(prefs::kMessageCenterDisabledSystemComponentIds), 351 base::Unretained(&disabled_system_component_ids_))); 352 enabled_sync_notifier_id_pref_.Init( 353 prefs::kMessageCenterEnabledSyncNotifierIds, 354 profile_->GetPrefs(), 355 base::Bind( 356 &DesktopNotificationService::OnStringListPrefChanged, 357 base::Unretained(this), 358 base::Unretained(prefs::kMessageCenterEnabledSyncNotifierIds), 359 base::Unretained(&enabled_sync_notifier_ids_))); 360 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNINSTALLED, 361 content::Source<Profile>(profile_)); 362} 363 364DesktopNotificationService::~DesktopNotificationService() { 365} 366 367void DesktopNotificationService::GrantPermission(const GURL& origin) { 368 ContentSettingsPattern primary_pattern = 369 ContentSettingsPattern::FromURLNoWildcard(origin); 370 profile_->GetHostContentSettingsMap()->SetContentSetting( 371 primary_pattern, 372 ContentSettingsPattern::Wildcard(), 373 CONTENT_SETTINGS_TYPE_NOTIFICATIONS, 374 NO_RESOURCE_IDENTIFIER, 375 CONTENT_SETTING_ALLOW); 376} 377 378void DesktopNotificationService::DenyPermission(const GURL& origin) { 379 ContentSettingsPattern primary_pattern = 380 ContentSettingsPattern::FromURLNoWildcard(origin); 381 profile_->GetHostContentSettingsMap()->SetContentSetting( 382 primary_pattern, 383 ContentSettingsPattern::Wildcard(), 384 CONTENT_SETTINGS_TYPE_NOTIFICATIONS, 385 NO_RESOURCE_IDENTIFIER, 386 CONTENT_SETTING_BLOCK); 387} 388 389ContentSetting DesktopNotificationService::GetDefaultContentSetting( 390 std::string* provider_id) { 391 return profile_->GetHostContentSettingsMap()->GetDefaultContentSetting( 392 CONTENT_SETTINGS_TYPE_NOTIFICATIONS, provider_id); 393} 394 395void DesktopNotificationService::SetDefaultContentSetting( 396 ContentSetting setting) { 397 profile_->GetHostContentSettingsMap()->SetDefaultContentSetting( 398 CONTENT_SETTINGS_TYPE_NOTIFICATIONS, setting); 399} 400 401void DesktopNotificationService::ResetToDefaultContentSetting() { 402 profile_->GetHostContentSettingsMap()->SetDefaultContentSetting( 403 CONTENT_SETTINGS_TYPE_NOTIFICATIONS, CONTENT_SETTING_DEFAULT); 404} 405 406void DesktopNotificationService::GetNotificationsSettings( 407 ContentSettingsForOneType* settings) { 408 profile_->GetHostContentSettingsMap()->GetSettingsForOneType( 409 CONTENT_SETTINGS_TYPE_NOTIFICATIONS, 410 NO_RESOURCE_IDENTIFIER, 411 settings); 412} 413 414void DesktopNotificationService::ClearSetting( 415 const ContentSettingsPattern& pattern) { 416 profile_->GetHostContentSettingsMap()->SetContentSetting( 417 pattern, 418 ContentSettingsPattern::Wildcard(), 419 CONTENT_SETTINGS_TYPE_NOTIFICATIONS, 420 NO_RESOURCE_IDENTIFIER, 421 CONTENT_SETTING_DEFAULT); 422} 423 424void DesktopNotificationService::ResetAllOrigins() { 425 profile_->GetHostContentSettingsMap()->ClearSettingsForOneType( 426 CONTENT_SETTINGS_TYPE_NOTIFICATIONS); 427} 428 429ContentSetting DesktopNotificationService::GetContentSetting( 430 const GURL& origin) { 431 return profile_->GetHostContentSettingsMap()->GetContentSetting( 432 origin, 433 origin, 434 CONTENT_SETTINGS_TYPE_NOTIFICATIONS, 435 NO_RESOURCE_IDENTIFIER); 436} 437 438void DesktopNotificationService::RequestPermission( 439 const GURL& origin, int process_id, int route_id, int callback_context, 440 WebContents* contents) { 441 // If |origin| hasn't been seen before and the default content setting for 442 // notifications is "ask", show an infobar. 443 // The cache can only answer queries on the IO thread once it's initialized, 444 // so don't ask the cache. 445 ContentSetting setting = GetContentSetting(origin); 446 if (setting == CONTENT_SETTING_ASK) { 447 // Show an info bar requesting permission. 448 InfoBarService* infobar_service = 449 InfoBarService::FromWebContents(contents); 450 // |infobar_service| may be NULL, e.g., if this request originated in a 451 // browser action popup, extension background page, or any HTML that runs 452 // outside of a tab. 453 if (infobar_service) { 454 NotificationPermissionInfoBarDelegate::Create( 455 infobar_service, 456 DesktopNotificationServiceFactory::GetForProfile( 457 Profile::FromBrowserContext(contents->GetBrowserContext())), 458 origin, DisplayNameForOriginInProcessId(origin, process_id), 459 process_id, route_id, callback_context); 460 return; 461 } 462 } 463 464 // Notify renderer immediately. 465 RenderViewHost* host = RenderViewHost::FromID(process_id, route_id); 466 if (host) 467 host->DesktopNotificationPermissionRequestDone(callback_context); 468} 469 470#if !defined(OS_WIN) 471void DesktopNotificationService::ShowNotification( 472 const Notification& notification) { 473 GetUIManager()->Add(notification, profile_); 474} 475 476bool DesktopNotificationService::CancelDesktopNotification( 477 int process_id, int route_id, int notification_id) { 478 scoped_refptr<NotificationObjectProxy> proxy( 479 new NotificationObjectProxy(process_id, route_id, notification_id, 480 false)); 481 return GetUIManager()->CancelById(proxy->id()); 482} 483#endif // OS_WIN 484 485bool DesktopNotificationService::ShowDesktopNotification( 486 const content::ShowDesktopNotificationHostMsgParams& params, 487 int process_id, int route_id, DesktopNotificationSource source) { 488 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 489 const GURL& origin = params.origin; 490 NotificationObjectProxy* proxy = 491 new NotificationObjectProxy(process_id, route_id, 492 params.notification_id, 493 source == WorkerNotification); 494 495 string16 display_source = DisplayNameForOriginInProcessId(origin, process_id); 496 if (params.is_html) { 497 ShowNotification(Notification(origin, params.contents_url, display_source, 498 params.replace_id, proxy)); 499 } else { 500 Notification notification(origin, params.icon_url, params.title, 501 params.body, params.direction, display_source, params.replace_id, 502 proxy); 503 // The webkit notification doesn't timeout. 504 notification.set_never_timeout(true); 505 ShowNotification(notification); 506 } 507 return true; 508} 509 510string16 DesktopNotificationService::DisplayNameForOriginInProcessId( 511 const GURL& origin, int process_id) { 512 // If the source is an extension, lookup the display name. 513 // Message center prefers to use extension name if the notification 514 // is allowed by an extension. 515 if (NotificationUIManager::DelegatesToMessageCenter() || 516 origin.SchemeIs(extensions::kExtensionScheme)) { 517 ExtensionInfoMap* extension_info_map = 518 extensions::ExtensionSystem::Get(profile_)->info_map(); 519 if (extension_info_map) { 520 ExtensionSet extensions; 521 extension_info_map->GetExtensionsWithAPIPermissionForSecurityOrigin( 522 origin, process_id, extensions::APIPermission::kNotification, 523 &extensions); 524 for (ExtensionSet::const_iterator iter = extensions.begin(); 525 iter != extensions.end(); ++iter) { 526 NotifierId notifier_id(NotifierId::APPLICATION, (*iter)->id()); 527 if (IsNotifierEnabled(notifier_id)) 528 return UTF8ToUTF16((*iter)->name()); 529 } 530 } 531 } 532 return UTF8ToUTF16(origin.host()); 533} 534 535void DesktopNotificationService::NotifySettingsChange() { 536 content::NotificationService::current()->Notify( 537 chrome::NOTIFICATION_DESKTOP_NOTIFICATION_SETTINGS_CHANGED, 538 content::Source<DesktopNotificationService>(this), 539 content::NotificationService::NoDetails()); 540} 541 542NotificationUIManager* DesktopNotificationService::GetUIManager() { 543 // We defer setting ui_manager_ to the global singleton until we need it 544 // in order to avoid UI dependent construction during startup. 545 if (!ui_manager_) 546 ui_manager_ = g_browser_process->notification_ui_manager(); 547 return ui_manager_; 548} 549 550bool DesktopNotificationService::IsNotifierEnabled( 551 const NotifierId& notifier_id) { 552 switch (notifier_id.type) { 553 case NotifierId::APPLICATION: 554 return disabled_extension_ids_.find(notifier_id.id) == 555 disabled_extension_ids_.end(); 556 case NotifierId::WEB_PAGE: 557 return GetContentSetting(notifier_id.url) == CONTENT_SETTING_ALLOW; 558 case NotifierId::SYSTEM_COMPONENT: 559#if defined(OS_CHROMEOS) 560 return disabled_system_component_ids_.find( 561 ash::system_notifier::SystemComponentTypeToString( 562 static_cast<ash::system_notifier::AshSystemComponentNotifierType>( 563 notifier_id.system_component_type))) 564 == disabled_system_component_ids_.end(); 565#else 566 return false; 567#endif 568 case NotifierId::SYNCED_NOTIFICATION_SERVICE: 569 return enabled_sync_notifier_ids_.find(notifier_id.id) != 570 enabled_sync_notifier_ids_.end(); 571 } 572 573 NOTREACHED(); 574 return false; 575} 576 577void DesktopNotificationService::SetNotifierEnabled( 578 const NotifierId& notifier_id, 579 bool enabled) { 580 DCHECK_NE(NotifierId::WEB_PAGE, notifier_id.type); 581 582 bool add_new_item = false; 583 const char* pref_name = NULL; 584 scoped_ptr<base::StringValue> id; 585 switch (notifier_id.type) { 586 case NotifierId::APPLICATION: 587 pref_name = prefs::kMessageCenterDisabledExtensionIds; 588 add_new_item = !enabled; 589 id.reset(new base::StringValue(notifier_id.id)); 590 break; 591 case NotifierId::SYSTEM_COMPONENT: 592#if defined(OS_CHROMEOS) 593 pref_name = prefs::kMessageCenterDisabledSystemComponentIds; 594 add_new_item = !enabled; 595 id.reset(new base::StringValue( 596 ash::system_notifier::SystemComponentTypeToString( 597 static_cast<ash::system_notifier::AshSystemComponentNotifierType>( 598 notifier_id.system_component_type)))); 599#else 600 return; 601#endif 602 break; 603 case NotifierId::SYNCED_NOTIFICATION_SERVICE: 604 pref_name = prefs::kMessageCenterEnabledSyncNotifierIds; 605 // Adding a new item if |enabled| == true, since synced notification 606 // services are opt-in. 607 add_new_item = enabled; 608 id.reset(new base::StringValue(notifier_id.id)); 609 break; 610 default: 611 NOTREACHED(); 612 } 613 DCHECK(pref_name != NULL); 614 615 ListPrefUpdate update(profile_->GetPrefs(), pref_name); 616 base::ListValue* const list = update.Get(); 617 if (add_new_item) { 618 // AppendIfNotPresent will delete |adding_value| when the same value 619 // already exists. 620 list->AppendIfNotPresent(id.release()); 621 } else { 622 list->Remove(*id, NULL); 623 } 624} 625 626void DesktopNotificationService::ShowWelcomeNotificationIfNecessary( 627 const Notification& notification) { 628 if (!welcome_notification && message_center::IsRichNotificationEnabled()) { 629 welcome_notification.reset( 630 new WelcomeNotification(profile_, g_browser_process->message_center())); 631 } 632 633 if (welcome_notification) 634 welcome_notification->ShowWelcomeNotificationIfNecessary(notification); 635} 636 637void DesktopNotificationService::OnStringListPrefChanged( 638 const char* pref_name, std::set<std::string>* ids_field) { 639 ids_field->clear(); 640 const base::ListValue* pref_list = profile_->GetPrefs()->GetList(pref_name); 641 for (size_t i = 0; i < pref_list->GetSize(); ++i) { 642 std::string element; 643 if (pref_list->GetString(i, &element) && !element.empty()) 644 ids_field->insert(element); 645 else 646 LOG(WARNING) << i << "-th element is not a string for " << pref_name; 647 } 648} 649 650WebKit::WebNotificationPresenter::Permission 651 DesktopNotificationService::HasPermission(const GURL& origin) { 652 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 653 HostContentSettingsMap* host_content_settings_map = 654 profile_->GetHostContentSettingsMap(); 655 ContentSetting setting = host_content_settings_map->GetContentSetting( 656 origin, 657 origin, 658 CONTENT_SETTINGS_TYPE_NOTIFICATIONS, 659 NO_RESOURCE_IDENTIFIER); 660 661 if (setting == CONTENT_SETTING_ALLOW) 662 return WebKit::WebNotificationPresenter::PermissionAllowed; 663 if (setting == CONTENT_SETTING_BLOCK) 664 return WebKit::WebNotificationPresenter::PermissionDenied; 665 if (setting == CONTENT_SETTING_ASK) 666 return WebKit::WebNotificationPresenter::PermissionNotAllowed; 667 NOTREACHED() << "Invalid notifications settings value: " << setting; 668 return WebKit::WebNotificationPresenter::PermissionNotAllowed; 669} 670 671void DesktopNotificationService::Observe( 672 int type, 673 const content::NotificationSource& source, 674 const content::NotificationDetails& details) { 675 DCHECK_EQ(chrome::NOTIFICATION_EXTENSION_UNINSTALLED, type); 676 677 extensions::Extension* extension = 678 content::Details<extensions::Extension>(details).ptr(); 679 NotifierId notifier_id(NotifierId::APPLICATION, extension->id()); 680 if (IsNotifierEnabled(notifier_id)) 681 return; 682 683 SetNotifierEnabled(notifier_id, true); 684} 685