diff options
author | thakis@chromium.org <thakis@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-07-03 02:29:52 +0000 |
---|---|---|
committer | thakis@chromium.org <thakis@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-07-03 02:29:52 +0000 |
commit | 78dcb900b1ae1209d87cd746c1ff7d4c13f9f04e (patch) | |
tree | 716fc33ee1e0c300f6b56e5921c7b183258458d3 /chrome | |
parent | 1350815259420160cfd2bdf8d94fbf9f80bc379b (diff) | |
download | chromium_src-78dcb900b1ae1209d87cd746c1ff7d4c13f9f04e.zip chromium_src-78dcb900b1ae1209d87cd746c1ff7d4c13f9f04e.tar.gz chromium_src-78dcb900b1ae1209d87cd746c1ff7d4c13f9f04e.tar.bz2 |
Add a default content setting to the notifications service.
Not yet used anywhere, no behavior change.
BUG=45547
TEST=none (unit tests)
Review URL: http://codereview.chromium.org/2842043
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@51587 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome')
8 files changed, 224 insertions, 9 deletions
diff --git a/chrome/browser/notifications/desktop_notification_service.cc b/chrome/browser/notifications/desktop_notification_service.cc index 683af4e..103c0f1 100644 --- a/chrome/browser/notifications/desktop_notification_service.cc +++ b/chrome/browser/notifications/desktop_notification_service.cc @@ -37,6 +37,8 @@ using WebKit::WebNotificationPresenter; +const ContentSetting kDefaultSetting = CONTENT_SETTING_ASK; + // static string16 DesktopNotificationService::CreateDataUrl( const GURL& icon_url, const string16& title, const string16& body) { @@ -203,6 +205,11 @@ DesktopNotificationService::~DesktopNotificationService() { } void DesktopNotificationService::RegisterUserPrefs(PrefService* user_prefs) { + if (!user_prefs->FindPreference( + prefs::kDesktopNotificationDefaultContentSetting)) { + user_prefs->RegisterIntegerPref( + prefs::kDesktopNotificationDefaultContentSetting, kDefaultSetting); + } if (!user_prefs->FindPreference(prefs::kDesktopNotificationAllowedOrigins)) user_prefs->RegisterListPref(prefs::kDesktopNotificationAllowedOrigins); if (!user_prefs->FindPreference(prefs::kDesktopNotificationDeniedOrigins)) @@ -215,8 +222,12 @@ void DesktopNotificationService::InitPrefs() { PrefService* prefs = profile_->GetPrefs(); std::vector<GURL> allowed_origins; std::vector<GURL> denied_origins; + ContentSetting default_content_setting = CONTENT_SETTING_DEFAULT; if (!profile_->IsOffTheRecord()) { + default_content_setting = IntToContentSetting( + prefs->GetInteger(prefs::kDesktopNotificationDefaultContentSetting)); + const ListValue* allowed_sites = prefs->GetList(prefs::kDesktopNotificationAllowedOrigins); if (allowed_sites) @@ -231,6 +242,7 @@ void DesktopNotificationService::InitPrefs() { } prefs_cache_ = new NotificationsPrefsCache(); + prefs_cache_->SetCacheDefaultContentSetting(default_content_setting); prefs_cache_->SetCacheAllowedOrigins(allowed_origins); prefs_cache_->SetCacheDeniedOrigins(denied_origins); prefs_cache_->set_is_initialized(true); @@ -239,6 +251,8 @@ void DesktopNotificationService::InitPrefs() { void DesktopNotificationService::StartObserving() { if (!profile_->IsOffTheRecord()) { PrefService* prefs = profile_->GetPrefs(); + prefs->AddPrefObserver(prefs::kDesktopNotificationDefaultContentSetting, + this); prefs->AddPrefObserver(prefs::kDesktopNotificationAllowedOrigins, this); prefs->AddPrefObserver(prefs::kDesktopNotificationDeniedOrigins, this); } @@ -247,6 +261,8 @@ void DesktopNotificationService::StartObserving() { void DesktopNotificationService::StopObserving() { if (!profile_->IsOffTheRecord()) { PrefService* prefs = profile_->GetPrefs(); + prefs->RemovePrefObserver(prefs::kDesktopNotificationDefaultContentSetting, + this); prefs->RemovePrefObserver(prefs::kDesktopNotificationAllowedOrigins, this); prefs->RemovePrefObserver(prefs::kDesktopNotificationDeniedOrigins, this); } @@ -313,6 +329,18 @@ void DesktopNotificationService::Observe(NotificationType type, prefs_cache_.get(), &NotificationsPrefsCache::SetCacheDeniedOrigins, denied_origins)); + } else if (0 == name->compare( + prefs::kDesktopNotificationDefaultContentSetting)) { + const ContentSetting default_content_setting = IntToContentSetting( + prefs->GetInteger(prefs::kDesktopNotificationDefaultContentSetting)); + + // Schedule a cache update on the IO thread. + ChromeThread::PostTask( + ChromeThread::IO, FROM_HERE, + NewRunnableMethod( + prefs_cache_.get(), + &NotificationsPrefsCache::SetCacheDefaultContentSetting, + default_content_setting)); } } @@ -377,6 +405,24 @@ void DesktopNotificationService::PersistPermissionChange( StartObserving(); } +ContentSetting DesktopNotificationService::GetDefaultContentSetting() { + PrefService* prefs = profile_->GetPrefs(); + ContentSetting setting = IntToContentSetting( + prefs->GetInteger(prefs::kDesktopNotificationDefaultContentSetting)); + if (setting == CONTENT_SETTING_DEFAULT) + setting = kDefaultSetting; + return setting; +} + +void DesktopNotificationService::SetDefaultContentSetting( + ContentSetting setting) { + DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); + profile_->GetPrefs()->SetInteger( + prefs::kDesktopNotificationDefaultContentSetting, + setting == CONTENT_SETTING_DEFAULT ? kDefaultSetting : setting); + // The cache is updated through the notification observer. +} + void DesktopNotificationService::RequestPermission( const GURL& origin, int process_id, int route_id, int callback_context, TabContents* tab) { diff --git a/chrome/browser/notifications/desktop_notification_service.h b/chrome/browser/notifications/desktop_notification_service.h index 4b76457..e2be439 100644 --- a/chrome/browser/notifications/desktop_notification_service.h +++ b/chrome/browser/notifications/desktop_notification_service.h @@ -1,4 +1,4 @@ -// Copyright (c) 2009 The Chromium Authors. All rights reserved. +// Copyright (c) 2010 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. @@ -9,6 +9,7 @@ #include "base/basictypes.h" #include "chrome/browser/notifications/notification.h" +#include "chrome/common/content_settings.h" #include "chrome/common/notification_observer.h" #include "chrome/common/notification_registrar.h" #include "chrome/common/notification_service.h" @@ -85,6 +86,9 @@ class DesktopNotificationService : public NotificationObserver { static string16 CreateDataUrl(const GURL& icon_url, const string16& title, const string16& body); + ContentSetting GetDefaultContentSetting(); + void SetDefaultContentSetting(ContentSetting setting); + static void RegisterUserPrefs(PrefService* user_prefs); private: void InitPrefs(); diff --git a/chrome/browser/notifications/desktop_notification_service_unittest.cc b/chrome/browser/notifications/desktop_notification_service_unittest.cc new file mode 100644 index 0000000..27ceb8c --- /dev/null +++ b/chrome/browser/notifications/desktop_notification_service_unittest.cc @@ -0,0 +1,66 @@ +// Copyright (c) 2010 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "chrome/browser/notifications/desktop_notification_service.h" + +#include "base/task.h" +#include "base/waitable_event.h" +#include "chrome/browser/notifications/notifications_prefs_cache.h" +#include "chrome/browser/renderer_host/test/test_render_view_host.h" +#include "chrome/test/testing_profile.h" +#include "grit/generated_resources.h" +#include "testing/gtest/include/gtest/gtest.h" + +class DesktopNotificationServiceTest : public RenderViewHostTestHarness { + public: + DesktopNotificationServiceTest() + : event_(false, false), + ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)) { + } + + void LetIOThreadWait() { + event_.Wait(); + } + + base::WaitableEvent event_; + ScopedRunnableMethodFactory<DesktopNotificationServiceTest> method_factory_; +}; + +TEST_F(DesktopNotificationServiceTest, DefaultContentSettingSentToCache) { + // The current message loop was already initalized by the superclass. + ChromeThread ui_thread(ChromeThread::UI, MessageLoop::current()); + + // Create IO thread, start its message loop. + ChromeThread io_thread(ChromeThread::IO); + io_thread.Start(); + ChromeThread::PostTask(ChromeThread::UI, FROM_HERE, + method_factory_.NewRunnableMethod( + &DesktopNotificationServiceTest::LetIOThreadWait)); + + // Creates the service, calls InitPrefs() on it which loads data from the + // profile into the cache and then puts the cache in io thread mode. + DesktopNotificationService* service = + profile()->GetDesktopNotificationService(); + NotificationsPrefsCache* cache = service->prefs_cache(); + + // The default pref registered in DesktopNotificationService is "ask", + // and that's what sent to the cache. + EXPECT_EQ(CONTENT_SETTING_ASK, cache->CachedDefaultContentSetting()); + + // Change the default content setting. This will post a task on the IO thread + // to update the cache. + service->SetDefaultContentSetting(CONTENT_SETTING_BLOCK); + + // The updated pref shouldn't be sent to the cache immediately. + EXPECT_EQ(CONTENT_SETTING_ASK, cache->CachedDefaultContentSetting()); + + // Run IO thread tasks. + event_.Signal(); + io_thread.Stop(); + + // Now that IO thread events have been processed, it should be there. + EXPECT_EQ(CONTENT_SETTING_BLOCK, cache->CachedDefaultContentSetting()); +} + + diff --git a/chrome/browser/notifications/notifications_prefs_cache.cc b/chrome/browser/notifications/notifications_prefs_cache.cc index 6a5172b..2fec1e8 100644 --- a/chrome/browser/notifications/notifications_prefs_cache.cc +++ b/chrome/browser/notifications/notifications_prefs_cache.cc @@ -1,4 +1,4 @@ -// Copyright (c) 2009 The Chromium Authors. All rights reserved. +// Copyright (c) 2010 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. @@ -11,7 +11,8 @@ #include "third_party/WebKit/WebKit/chromium/public/WebNotificationPresenter.h" NotificationsPrefsCache::NotificationsPrefsCache() - : is_initialized_(false) { + : default_content_setting_(CONTENT_SETTING_DEFAULT), + is_initialized_(false) { } void NotificationsPrefsCache::CacheAllowedOrigin( @@ -44,6 +45,11 @@ void NotificationsPrefsCache::SetCacheDeniedOrigins( denied_origins_.insert(denied.begin(), denied.end()); } +void NotificationsPrefsCache::SetCacheDefaultContentSetting( + ContentSetting setting) { + default_content_setting_ = setting; +} + // static void NotificationsPrefsCache::ListValueToGurlVector( const ListValue& origin_list, @@ -61,19 +67,28 @@ int NotificationsPrefsCache::HasPermission(const GURL& origin) { return WebKit::WebNotificationPresenter::PermissionAllowed; if (IsOriginDenied(origin)) return WebKit::WebNotificationPresenter::PermissionDenied; - return WebKit::WebNotificationPresenter::PermissionNotAllowed; + switch (default_content_setting_) { + case CONTENT_SETTING_ALLOW: + return WebKit::WebNotificationPresenter::PermissionAllowed; + case CONTENT_SETTING_BLOCK: + return WebKit::WebNotificationPresenter::PermissionDenied; + case CONTENT_SETTING_ASK: + case CONTENT_SETTING_DEFAULT: + default: // Make gcc happy. + return WebKit::WebNotificationPresenter::PermissionNotAllowed; + } } bool NotificationsPrefsCache::IsOriginAllowed( const GURL& origin) { CheckThreadAccess(); - return (allowed_origins_.find(origin) != allowed_origins_.end()); + return allowed_origins_.find(origin) != allowed_origins_.end(); } bool NotificationsPrefsCache::IsOriginDenied( const GURL& origin) { CheckThreadAccess(); - return (denied_origins_.find(origin) != denied_origins_.end()); + return denied_origins_.find(origin) != denied_origins_.end(); } void NotificationsPrefsCache::CheckThreadAccess() { diff --git a/chrome/browser/notifications/notifications_prefs_cache.h b/chrome/browser/notifications/notifications_prefs_cache.h index 1b87a29..e12fa14 100644 --- a/chrome/browser/notifications/notifications_prefs_cache.h +++ b/chrome/browser/notifications/notifications_prefs_cache.h @@ -1,4 +1,4 @@ -// Copyright (c) 2009 The Chromium Authors. All rights reserved. +// Copyright (c) 2010 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. @@ -9,6 +9,7 @@ #include <vector> #include "base/ref_counted.h" +#include "chrome/common/content_settings.h" #include "googleurl/src/gurl.h" class ListValue; @@ -22,6 +23,8 @@ class NotificationsPrefsCache public: NotificationsPrefsCache(); + // Once is_initialized_() is set, all accesses must happen on the IO thread. + // Before that, all accesses need to happen on the UI thread. void set_is_initialized(bool val) { is_initialized_ = val; } bool is_initialized() { return is_initialized_; } @@ -38,10 +41,16 @@ class NotificationsPrefsCache // contents of the cache. void SetCacheAllowedOrigins(const std::vector<GURL>& allowed); void SetCacheDeniedOrigins(const std::vector<GURL>& denied); + void SetCacheDefaultContentSetting(ContentSetting setting); static void ListValueToGurlVector(const ListValue& origin_list, std::vector<GURL>* origin_vector); + // Exposed for testing. + ContentSetting CachedDefaultContentSetting() { + return default_content_setting_; + } + private: friend class base::RefCountedThreadSafe<NotificationsPrefsCache>; @@ -58,6 +67,10 @@ class NotificationsPrefsCache std::set<GURL> allowed_origins_; std::set<GURL> denied_origins_; + // The default setting, used for origins that are neither in + // |allowed_origins_| nor |denied_origins_|. + ContentSetting default_content_setting_; + // Set to true once the initial cached settings have been completely read. // Once this is done, the class can no longer be accessed on the UI thread. bool is_initialized_; diff --git a/chrome/browser/notifications/notifications_prefs_cache_unittest.cc b/chrome/browser/notifications/notifications_prefs_cache_unittest.cc new file mode 100644 index 0000000..0cbea9e --- /dev/null +++ b/chrome/browser/notifications/notifications_prefs_cache_unittest.cc @@ -0,0 +1,63 @@ +// Copyright (c) 2010 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "chrome/browser/notifications/notifications_prefs_cache.h" + +#include "base/message_loop.h" +#include "chrome/browser/chrome_thread.h" +#include "testing/gtest/include/gtest/gtest.h" +#include "third_party/WebKit/WebKit/chromium/public/WebNotificationPresenter.h" + +TEST(NotificationsPrefsCacheTest, CanCreate) { + scoped_refptr<NotificationsPrefsCache> cache(new NotificationsPrefsCache()); + std::vector<GURL> allowed_origins; + allowed_origins.push_back(GURL("http://allowed.com")); + std::vector<GURL> denied_origins; + denied_origins.push_back(GURL("http://denied.com")); + + { + MessageLoop loop; + ChromeThread ui_thread(ChromeThread::UI, &loop); + + cache->SetCacheAllowedOrigins(allowed_origins); + cache->SetCacheDeniedOrigins(denied_origins); + cache->SetCacheDefaultContentSetting(CONTENT_SETTING_DEFAULT); + } + + cache->set_is_initialized(true); + + { + MessageLoop loop; + ChromeThread io_thread(ChromeThread::IO, &loop); + + cache->CacheAllowedOrigin(GURL("http://allowed2.com")); + cache->CacheDeniedOrigin(GURL("http://denied2.com")); + + EXPECT_EQ(cache->HasPermission(GURL("http://allowed.com")), + WebKit::WebNotificationPresenter::PermissionAllowed); + EXPECT_EQ(cache->HasPermission(GURL("http://allowed2.com")), + WebKit::WebNotificationPresenter::PermissionAllowed); + + EXPECT_EQ(cache->HasPermission(GURL("http://denied.com")), + WebKit::WebNotificationPresenter::PermissionDenied); + EXPECT_EQ(cache->HasPermission(GURL("http://denied2.com")), + WebKit::WebNotificationPresenter::PermissionDenied); + + EXPECT_EQ(cache->HasPermission(GURL("http://unkown.com")), + WebKit::WebNotificationPresenter::PermissionNotAllowed); + + cache->SetCacheDefaultContentSetting(CONTENT_SETTING_ASK); + EXPECT_EQ(cache->HasPermission(GURL("http://unkown.com")), + WebKit::WebNotificationPresenter::PermissionNotAllowed); + + cache->SetCacheDefaultContentSetting(CONTENT_SETTING_ALLOW); + EXPECT_EQ(cache->HasPermission(GURL("http://unkown.com")), + WebKit::WebNotificationPresenter::PermissionAllowed); + + cache->SetCacheDefaultContentSetting(CONTENT_SETTING_BLOCK); + EXPECT_EQ(cache->HasPermission(GURL("http://unkown.com")), + WebKit::WebNotificationPresenter::PermissionDenied); + } +} + diff --git a/chrome/chrome_tests.gypi b/chrome/chrome_tests.gypi index 4014fd8..bba32d2 100644 --- a/chrome/chrome_tests.gypi +++ b/chrome/chrome_tests.gypi @@ -885,10 +885,11 @@ 'browser/net/resolve_proxy_msg_helper_unittest.cc', 'browser/net/url_fixer_upper_unittest.cc', 'browser/net/url_info_unittest.cc', - 'browser/notifications/desktop_notifications_unittest.cc', + 'browser/notifications/desktop_notification_service_unittest.cc', 'browser/notifications/desktop_notifications_unittest.h', 'browser/notifications/notification_exceptions_table_model_unittest.cc', 'browser/notifications/notification_test_util.h', + 'browser/notifications/notifications_prefs_cache_unittest.cc', 'browser/page_menu_model_unittest.cc', 'browser/parsers/metadata_parser_filebase_unittest.cc', 'browser/password_manager/encryptor_unittest.cc', diff --git a/chrome/test/testing_profile.h b/chrome/test/testing_profile.h index fda2f57..3a0ca33 100644 --- a/chrome/test/testing_profile.h +++ b/chrome/test/testing_profile.h @@ -20,6 +20,7 @@ #include "chrome/browser/host_content_settings_map.h" #include "chrome/browser/history/history.h" #include "chrome/browser/in_process_webkit/webkit_context.h" +#include "chrome/browser/notifications/desktop_notification_service.h" #include "chrome/browser/pref_service.h" #include "chrome/browser/pref_value_store.h" #include "chrome/browser/profile.h" @@ -264,7 +265,12 @@ class TestingProfile : public Profile { virtual void InitWebResources() {} virtual NTPResourceCache* GetNTPResourceCache(); virtual DesktopNotificationService* GetDesktopNotificationService() { - return NULL; + DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); + if (!desktop_notification_service_.get()) { + desktop_notification_service_.reset(new DesktopNotificationService( + this, NULL)); + } + return desktop_notification_service_.get(); } virtual BackgroundContentsService* GetBackgroundContentsService() { return NULL; @@ -363,6 +369,7 @@ class TestingProfile : public Profile { scoped_refptr<GeolocationContentSettingsMap> geolocation_content_settings_map_; scoped_refptr<GeolocationPermissionContext> geolocation_permission_context_; + scoped_ptr<DesktopNotificationService> desktop_notification_service_; // Find bar state. Created lazily by GetFindBarState(). scoped_ptr<FindBarState> find_bar_state_; |