| 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/prefs/scoped_user_pref_update.h" | 8 #include "base/prefs/scoped_user_pref_update.h" |
| 9 #include "base/strings/utf_string_conversions.h" | 9 #include "base/strings/utf_string_conversions.h" |
| 10 #include "base/threading/thread.h" | 10 #include "base/threading/thread.h" |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 51 #include "ui/message_center/notifier_settings.h" | 51 #include "ui/message_center/notifier_settings.h" |
| 52 | 52 |
| 53 #if defined(OS_CHROMEOS) | 53 #if defined(OS_CHROMEOS) |
| 54 #include "ash/system/system_notifier.h" | 54 #include "ash/system/system_notifier.h" |
| 55 #endif | 55 #endif |
| 56 | 56 |
| 57 using content::BrowserThread; | 57 using content::BrowserThread; |
| 58 using content::RenderViewHost; | 58 using content::RenderViewHost; |
| 59 using content::WebContents; | 59 using content::WebContents; |
| 60 using message_center::NotifierId; | 60 using message_center::NotifierId; |
| 61 using WebKit::WebNotificationPresenter; | 61 using blink::WebNotificationPresenter; |
| 62 using WebKit::WebTextDirection; | 62 using blink::WebTextDirection; |
| 63 | 63 |
| 64 | 64 |
| 65 // NotificationPermissionInfoBarDelegate -------------------------------------- | 65 // NotificationPermissionInfoBarDelegate -------------------------------------- |
| 66 | 66 |
| 67 // The delegate for the infobar shown when an origin requests notification | 67 // The delegate for the infobar shown when an origin requests notification |
| 68 // permissions. | 68 // permissions. |
| 69 class NotificationPermissionInfoBarDelegate : public ConfirmInfoBarDelegate { | 69 class NotificationPermissionInfoBarDelegate : public ConfirmInfoBarDelegate { |
| 70 public: | 70 public: |
| 71 // Creates a notification permission infobar delegate and adds it to | 71 // Creates a notification permission infobar delegate and adds it to |
| 72 // |infobar_service|. | 72 // |infobar_service|. |
| (...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 219 const GURL& icon_url, const string16& title, const string16& body, | 219 const GURL& icon_url, const string16& title, const string16& body, |
| 220 WebTextDirection dir) { | 220 WebTextDirection dir) { |
| 221 int resource; | 221 int resource; |
| 222 std::vector<std::string> subst; | 222 std::vector<std::string> subst; |
| 223 if (icon_url.is_valid()) { | 223 if (icon_url.is_valid()) { |
| 224 resource = IDR_NOTIFICATION_ICON_HTML; | 224 resource = IDR_NOTIFICATION_ICON_HTML; |
| 225 subst.push_back(icon_url.spec()); | 225 subst.push_back(icon_url.spec()); |
| 226 subst.push_back(net::EscapeForHTML(UTF16ToUTF8(title))); | 226 subst.push_back(net::EscapeForHTML(UTF16ToUTF8(title))); |
| 227 subst.push_back(net::EscapeForHTML(UTF16ToUTF8(body))); | 227 subst.push_back(net::EscapeForHTML(UTF16ToUTF8(body))); |
| 228 // icon float position | 228 // icon float position |
| 229 subst.push_back(dir == WebKit::WebTextDirectionRightToLeft ? | 229 subst.push_back(dir == blink::WebTextDirectionRightToLeft ? |
| 230 "right" : "left"); | 230 "right" : "left"); |
| 231 } else if (title.empty() || body.empty()) { | 231 } else if (title.empty() || body.empty()) { |
| 232 resource = IDR_NOTIFICATION_1LINE_HTML; | 232 resource = IDR_NOTIFICATION_1LINE_HTML; |
| 233 string16 line = title.empty() ? body : title; | 233 string16 line = title.empty() ? body : title; |
| 234 // Strings are div names in the template file. | 234 // Strings are div names in the template file. |
| 235 string16 line_name = title.empty() ? ASCIIToUTF16("description") | 235 string16 line_name = title.empty() ? ASCIIToUTF16("description") |
| 236 : ASCIIToUTF16("title"); | 236 : ASCIIToUTF16("title"); |
| 237 subst.push_back(net::EscapeForHTML(UTF16ToUTF8(line_name))); | 237 subst.push_back(net::EscapeForHTML(UTF16ToUTF8(line_name))); |
| 238 subst.push_back(net::EscapeForHTML(UTF16ToUTF8(line))); | 238 subst.push_back(net::EscapeForHTML(UTF16ToUTF8(line))); |
| 239 } else { | 239 } else { |
| 240 resource = IDR_NOTIFICATION_2LINE_HTML; | 240 resource = IDR_NOTIFICATION_2LINE_HTML; |
| 241 subst.push_back(net::EscapeForHTML(UTF16ToUTF8(title))); | 241 subst.push_back(net::EscapeForHTML(UTF16ToUTF8(title))); |
| 242 subst.push_back(net::EscapeForHTML(UTF16ToUTF8(body))); | 242 subst.push_back(net::EscapeForHTML(UTF16ToUTF8(body))); |
| 243 } | 243 } |
| 244 // body text direction | 244 // body text direction |
| 245 subst.push_back(dir == WebKit::WebTextDirectionRightToLeft ? | 245 subst.push_back(dir == blink::WebTextDirectionRightToLeft ? |
| 246 "rtl" : "ltr"); | 246 "rtl" : "ltr"); |
| 247 | 247 |
| 248 return CreateDataUrl(resource, subst); | 248 return CreateDataUrl(resource, subst); |
| 249 } | 249 } |
| 250 | 250 |
| 251 // static | 251 // static |
| 252 string16 DesktopNotificationService::CreateDataUrl( | 252 string16 DesktopNotificationService::CreateDataUrl( |
| 253 int resource, const std::vector<std::string>& subst) { | 253 int resource, const std::vector<std::string>& subst) { |
| 254 const base::StringPiece template_html( | 254 const base::StringPiece template_html( |
| 255 ResourceBundle::GetSharedInstance().GetRawDataResource( | 255 ResourceBundle::GetSharedInstance().GetRawDataResource( |
| (...skipping 14 matching lines...) Expand all Loading... |
| 270 const GURL& origin_url, | 270 const GURL& origin_url, |
| 271 const string16& title, | 271 const string16& title, |
| 272 const string16& message, | 272 const string16& message, |
| 273 const GURL& icon_url, | 273 const GURL& icon_url, |
| 274 const string16& replace_id, | 274 const string16& replace_id, |
| 275 NotificationDelegate* delegate, | 275 NotificationDelegate* delegate, |
| 276 Profile* profile) { | 276 Profile* profile) { |
| 277 if (message_center::IsRichNotificationEnabled()) { | 277 if (message_center::IsRichNotificationEnabled()) { |
| 278 // For message center create a non-HTML notification with |icon_url|. | 278 // For message center create a non-HTML notification with |icon_url|. |
| 279 Notification notification(origin_url, icon_url, title, message, | 279 Notification notification(origin_url, icon_url, title, message, |
| 280 WebKit::WebTextDirectionDefault, | 280 blink::WebTextDirectionDefault, |
| 281 string16(), replace_id, delegate); | 281 string16(), replace_id, delegate); |
| 282 g_browser_process->notification_ui_manager()->Add(notification, profile); | 282 g_browser_process->notification_ui_manager()->Add(notification, profile); |
| 283 return notification.notification_id(); | 283 return notification.notification_id(); |
| 284 } | 284 } |
| 285 | 285 |
| 286 // Generate a data URL embedding the icon URL, title, and message. | 286 // Generate a data URL embedding the icon URL, title, and message. |
| 287 GURL content_url(CreateDataUrl( | 287 GURL content_url(CreateDataUrl( |
| 288 icon_url, title, message, WebKit::WebTextDirectionDefault)); | 288 icon_url, title, message, blink::WebTextDirectionDefault)); |
| 289 Notification notification( | 289 Notification notification( |
| 290 GURL(), content_url, string16(), replace_id, delegate); | 290 GURL(), content_url, string16(), replace_id, delegate); |
| 291 g_browser_process->notification_ui_manager()->Add(notification, profile); | 291 g_browser_process->notification_ui_manager()->Add(notification, profile); |
| 292 return notification.notification_id(); | 292 return notification.notification_id(); |
| 293 } | 293 } |
| 294 | 294 |
| 295 // static | 295 // static |
| 296 std::string DesktopNotificationService::AddIconNotification( | 296 std::string DesktopNotificationService::AddIconNotification( |
| 297 const GURL& origin_url, | 297 const GURL& origin_url, |
| 298 const string16& title, | 298 const string16& title, |
| 299 const string16& message, | 299 const string16& message, |
| 300 const gfx::Image& icon, | 300 const gfx::Image& icon, |
| 301 const string16& replace_id, | 301 const string16& replace_id, |
| 302 NotificationDelegate* delegate, | 302 NotificationDelegate* delegate, |
| 303 Profile* profile) { | 303 Profile* profile) { |
| 304 if (message_center::IsRichNotificationEnabled()) { | 304 if (message_center::IsRichNotificationEnabled()) { |
| 305 // For message center create a non-HTML notification with |icon|. | 305 // For message center create a non-HTML notification with |icon|. |
| 306 Notification notification(origin_url, icon, title, message, | 306 Notification notification(origin_url, icon, title, message, |
| 307 WebKit::WebTextDirectionDefault, | 307 blink::WebTextDirectionDefault, |
| 308 string16(), replace_id, delegate); | 308 string16(), replace_id, delegate); |
| 309 g_browser_process->notification_ui_manager()->Add(notification, profile); | 309 g_browser_process->notification_ui_manager()->Add(notification, profile); |
| 310 return notification.notification_id(); | 310 return notification.notification_id(); |
| 311 } | 311 } |
| 312 | 312 |
| 313 GURL icon_url; | 313 GURL icon_url; |
| 314 if (!icon.IsEmpty()) | 314 if (!icon.IsEmpty()) |
| 315 icon_url = GURL(webui::GetBitmapDataUrl(*icon.ToSkBitmap())); | 315 icon_url = GURL(webui::GetBitmapDataUrl(*icon.ToSkBitmap())); |
| 316 return AddNotification( | 316 return AddNotification( |
| 317 origin_url, title, message, icon_url, replace_id, delegate, profile); | 317 origin_url, title, message, icon_url, replace_id, delegate, profile); |
| (...skipping 325 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 643 const base::ListValue* pref_list = profile_->GetPrefs()->GetList(pref_name); | 643 const base::ListValue* pref_list = profile_->GetPrefs()->GetList(pref_name); |
| 644 for (size_t i = 0; i < pref_list->GetSize(); ++i) { | 644 for (size_t i = 0; i < pref_list->GetSize(); ++i) { |
| 645 std::string element; | 645 std::string element; |
| 646 if (pref_list->GetString(i, &element) && !element.empty()) | 646 if (pref_list->GetString(i, &element) && !element.empty()) |
| 647 ids_field->insert(element); | 647 ids_field->insert(element); |
| 648 else | 648 else |
| 649 LOG(WARNING) << i << "-th element is not a string for " << pref_name; | 649 LOG(WARNING) << i << "-th element is not a string for " << pref_name; |
| 650 } | 650 } |
| 651 } | 651 } |
| 652 | 652 |
| 653 WebKit::WebNotificationPresenter::Permission | 653 blink::WebNotificationPresenter::Permission |
| 654 DesktopNotificationService::HasPermission(const GURL& origin) { | 654 DesktopNotificationService::HasPermission(const GURL& origin) { |
| 655 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 655 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 656 HostContentSettingsMap* host_content_settings_map = | 656 HostContentSettingsMap* host_content_settings_map = |
| 657 profile_->GetHostContentSettingsMap(); | 657 profile_->GetHostContentSettingsMap(); |
| 658 ContentSetting setting = host_content_settings_map->GetContentSetting( | 658 ContentSetting setting = host_content_settings_map->GetContentSetting( |
| 659 origin, | 659 origin, |
| 660 origin, | 660 origin, |
| 661 CONTENT_SETTINGS_TYPE_NOTIFICATIONS, | 661 CONTENT_SETTINGS_TYPE_NOTIFICATIONS, |
| 662 NO_RESOURCE_IDENTIFIER); | 662 NO_RESOURCE_IDENTIFIER); |
| 663 | 663 |
| 664 if (setting == CONTENT_SETTING_ALLOW) | 664 if (setting == CONTENT_SETTING_ALLOW) |
| 665 return WebKit::WebNotificationPresenter::PermissionAllowed; | 665 return blink::WebNotificationPresenter::PermissionAllowed; |
| 666 if (setting == CONTENT_SETTING_BLOCK) | 666 if (setting == CONTENT_SETTING_BLOCK) |
| 667 return WebKit::WebNotificationPresenter::PermissionDenied; | 667 return blink::WebNotificationPresenter::PermissionDenied; |
| 668 if (setting == CONTENT_SETTING_ASK) | 668 if (setting == CONTENT_SETTING_ASK) |
| 669 return WebKit::WebNotificationPresenter::PermissionNotAllowed; | 669 return blink::WebNotificationPresenter::PermissionNotAllowed; |
| 670 NOTREACHED() << "Invalid notifications settings value: " << setting; | 670 NOTREACHED() << "Invalid notifications settings value: " << setting; |
| 671 return WebKit::WebNotificationPresenter::PermissionNotAllowed; | 671 return blink::WebNotificationPresenter::PermissionNotAllowed; |
| 672 } | 672 } |
| 673 | 673 |
| 674 void DesktopNotificationService::Observe( | 674 void DesktopNotificationService::Observe( |
| 675 int type, | 675 int type, |
| 676 const content::NotificationSource& source, | 676 const content::NotificationSource& source, |
| 677 const content::NotificationDetails& details) { | 677 const content::NotificationDetails& details) { |
| 678 DCHECK_EQ(chrome::NOTIFICATION_EXTENSION_UNINSTALLED, type); | 678 DCHECK_EQ(chrome::NOTIFICATION_EXTENSION_UNINSTALLED, type); |
| 679 | 679 |
| 680 extensions::Extension* extension = | 680 extensions::Extension* extension = |
| 681 content::Details<extensions::Extension>(details).ptr(); | 681 content::Details<extensions::Extension>(details).ptr(); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 694 : extensions::api::notifications::PERMISSION_LEVEL_DENIED; | 694 : extensions::api::notifications::PERMISSION_LEVEL_DENIED; |
| 695 scoped_ptr<base::ListValue> args(new base::ListValue()); | 695 scoped_ptr<base::ListValue> args(new base::ListValue()); |
| 696 args->Append(new base::StringValue( | 696 args->Append(new base::StringValue( |
| 697 extensions::api::notifications::ToString(permission))); | 697 extensions::api::notifications::ToString(permission))); |
| 698 scoped_ptr<extensions::Event> event(new extensions::Event( | 698 scoped_ptr<extensions::Event> event(new extensions::Event( |
| 699 extensions::api::notifications::OnPermissionLevelChanged::kEventName, | 699 extensions::api::notifications::OnPermissionLevelChanged::kEventName, |
| 700 args.Pass())); | 700 args.Pass())); |
| 701 extensions::ExtensionSystem::Get(profile_)->event_router()-> | 701 extensions::ExtensionSystem::Get(profile_)->event_router()-> |
| 702 DispatchEventToExtension(notifier_id.id, event.Pass()); | 702 DispatchEventToExtension(notifier_id.id, event.Pass()); |
| 703 } | 703 } |
| OLD | NEW |