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/strings/utf_string_conversions.h" | 8 #include "base/strings/utf_string_conversions.h" |
9 #include "base/threading/thread.h" | 9 #include "base/threading/thread.h" |
10 #include "chrome/browser/browser_process.h" | 10 #include "chrome/browser/browser_process.h" |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
52 #include "ash/system/system_notifier.h" | 52 #include "ash/system/system_notifier.h" |
53 #endif | 53 #endif |
54 | 54 |
55 using content::BrowserThread; | 55 using content::BrowserThread; |
56 using content::RenderViewHost; | 56 using content::RenderViewHost; |
57 using content::WebContents; | 57 using content::WebContents; |
58 using message_center::NotifierId; | 58 using message_center::NotifierId; |
59 using WebKit::WebNotificationPresenter; | 59 using WebKit::WebNotificationPresenter; |
60 using WebKit::WebTextDirection; | 60 using WebKit::WebTextDirection; |
61 | 61 |
62 | |
63 // NotificationPermissionInfoBarDelegate -------------------------------------- | 62 // NotificationPermissionInfoBarDelegate -------------------------------------- |
64 | 63 |
65 // The delegate for the infobar shown when an origin requests notification | 64 // The delegate for the infobar shown when an origin requests notification |
66 // permissions. | 65 // permissions. |
67 class NotificationPermissionInfoBarDelegate : public ConfirmInfoBarDelegate { | 66 class NotificationPermissionInfoBarDelegate : public ConfirmInfoBarDelegate { |
68 public: | 67 public: |
69 // Creates a notification permission infobar delegate and adds it to | 68 // Creates a notification permission infobar delegate and adds it to |
70 // |infobar_service|. | 69 // |infobar_service|. |
71 static void Create(InfoBarService* infobar_service, | 70 static void Create(InfoBarService* infobar_service, |
72 DesktopNotificationService* notification_service, | 71 DesktopNotificationService* notification_service, |
(...skipping 553 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
626 for (size_t i = 0; i < pref_list->GetSize(); ++i) { | 625 for (size_t i = 0; i < pref_list->GetSize(); ++i) { |
627 std::string element; | 626 std::string element; |
628 if (pref_list->GetString(i, &element) && !element.empty()) | 627 if (pref_list->GetString(i, &element) && !element.empty()) |
629 ids_field->insert(element); | 628 ids_field->insert(element); |
630 else | 629 else |
631 LOG(WARNING) << i << "-th element is not a string for " << pref_name; | 630 LOG(WARNING) << i << "-th element is not a string for " << pref_name; |
632 } | 631 } |
633 } | 632 } |
634 | 633 |
635 WebKit::WebNotificationPresenter::Permission | 634 WebKit::WebNotificationPresenter::Permission |
636 DesktopNotificationService::HasPermission(const GURL& origin) { | 635 DesktopNotificationService::HasPermission(const GURL& origin, int process_id) { |
637 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 636 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 637 |
| 638 // There are two ways that permission can be granted: |
| 639 // * An extension running in the process requested |
| 640 |
| 641 ExtensionService* service = profile_->GetExtensionService(); |
| 642 std::set<std::string> process_extensions = |
| 643 service->process_map()->GetExtensionsInProcess(process_id); |
| 644 const ExtensionSet* profile_extensions = service->extensions(); |
| 645 |
| 646 ExtensionSet permission_extensions; |
| 647 |
| 648 if (origin.SchemeIs(extensions::kExtensionScheme)) { |
| 649 const std::string& id = origin.host(); |
| 650 |
| 651 if (process_extensions.find(id) != process_extensions.end()) { |
| 652 const extensions::Extension* extension = profile_extensions->GetByID(id); |
| 653 if (extension && ExtensionApplies(extension)) |
| 654 return WebKit::WebNotificationPresenter::PermissionAllowed; |
| 655 } |
| 656 } else { |
| 657 for (std::set<std::string>::iterator iter = process_extensions.begin(); |
| 658 iter != process_extensions.end(); |
| 659 ++iter) { |
| 660 const std::string& id = *iter; |
| 661 const extensions::Extension* extension = profile_extensions->GetByID(id); |
| 662 if (extension && ExtensionApplies(extension)) { |
| 663 return WebKit::WebNotificationPresenter::PermissionAllowed; |
| 664 } |
| 665 } |
| 666 } |
| 667 |
| 668 // Here, none of the known extensions apply to the security origin and have |
| 669 // the notifications permission, so just check the content settings for the |
| 670 // origin. |
638 HostContentSettingsMap* host_content_settings_map = | 671 HostContentSettingsMap* host_content_settings_map = |
639 profile_->GetHostContentSettingsMap(); | 672 profile_->GetHostContentSettingsMap(); |
640 ContentSetting setting = host_content_settings_map->GetContentSetting( | 673 ContentSetting setting = host_content_settings_map->GetContentSetting( |
641 origin, | 674 origin, |
642 origin, | 675 origin, |
643 CONTENT_SETTINGS_TYPE_NOTIFICATIONS, | 676 CONTENT_SETTINGS_TYPE_NOTIFICATIONS, |
644 NO_RESOURCE_IDENTIFIER); | 677 NO_RESOURCE_IDENTIFIER); |
645 | 678 |
646 if (setting == CONTENT_SETTING_ALLOW) | 679 if (setting == CONTENT_SETTING_ALLOW) |
647 return WebKit::WebNotificationPresenter::PermissionAllowed; | 680 return WebKit::WebNotificationPresenter::PermissionAllowed; |
648 if (setting == CONTENT_SETTING_BLOCK) | 681 if (setting == CONTENT_SETTING_BLOCK) |
649 return WebKit::WebNotificationPresenter::PermissionDenied; | 682 return WebKit::WebNotificationPresenter::PermissionDenied; |
650 if (setting == CONTENT_SETTING_ASK) | 683 if (setting == CONTENT_SETTING_ASK) |
651 return WebKit::WebNotificationPresenter::PermissionNotAllowed; | 684 return WebKit::WebNotificationPresenter::PermissionNotAllowed; |
| 685 |
652 NOTREACHED() << "Invalid notifications settings value: " << setting; | 686 NOTREACHED() << "Invalid notifications settings value: " << setting; |
653 return WebKit::WebNotificationPresenter::PermissionNotAllowed; | 687 return WebKit::WebNotificationPresenter::PermissionNotAllowed; |
654 } | 688 } |
655 | 689 |
656 void DesktopNotificationService::Observe( | 690 void DesktopNotificationService::Observe( |
657 int type, | 691 int type, |
658 const content::NotificationSource& source, | 692 const content::NotificationSource& source, |
659 const content::NotificationDetails& details) { | 693 const content::NotificationDetails& details) { |
660 DCHECK_EQ(chrome::NOTIFICATION_EXTENSION_UNINSTALLED, type); | 694 DCHECK_EQ(chrome::NOTIFICATION_EXTENSION_UNINSTALLED, type); |
661 | 695 |
662 extensions::Extension* extension = | 696 extensions::Extension* extension = |
663 content::Details<extensions::Extension>(details).ptr(); | 697 content::Details<extensions::Extension>(details).ptr(); |
664 NotifierId notifier_id(NotifierId::APPLICATION, extension->id()); | 698 NotifierId notifier_id(NotifierId::APPLICATION, extension->id()); |
665 if (IsNotifierEnabled(notifier_id)) | 699 if (IsNotifierEnabled(notifier_id)) |
666 return; | 700 return; |
667 | 701 |
668 SetNotifierEnabled(notifier_id, true); | 702 SetNotifierEnabled(notifier_id, true); |
669 } | 703 } |
| 704 |
| 705 bool DesktopNotificationService::ExtensionApplies( |
| 706 const extensions::Extension* extension) { |
| 707 message_center::NotifierId notifier_id( |
| 708 message_center::NotifierId::APPLICATION, extension->id()); |
| 709 return IsNotifierEnabled(notifier_id) && |
| 710 extension->HasAPIPermission(extensions::APIPermission::kNotification); |
| 711 } |
OLD | NEW |