[email protected] | 0c6c4be | 2011-03-23 12:31:20 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
[email protected] | 78dcb90 | 2010-07-03 02:29:52 | [diff] [blame] | 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 | |
[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame^] | 7 | #include "base/memory/ref_counted.h" |
[email protected] | 39d9f62c | 2010-12-03 10:48:50 | [diff] [blame] | 8 | #include "base/message_loop.h" |
[email protected] | 44f9c95 | 2011-01-02 06:05:39 | [diff] [blame] | 9 | #include "base/synchronization/waitable_event.h" |
[email protected] | 78dcb90 | 2010-07-03 02:29:52 | [diff] [blame] | 10 | #include "chrome/browser/notifications/notifications_prefs_cache.h" |
[email protected] | 37858e5 | 2010-08-26 00:22:02 | [diff] [blame] | 11 | #include "chrome/browser/prefs/pref_service.h" |
[email protected] | f89ee34 | 2011-03-07 09:28:27 | [diff] [blame] | 12 | #include "chrome/browser/prefs/scoped_user_pref_update.h" |
[email protected] | f4192f2 | 2010-07-06 16:45:22 | [diff] [blame] | 13 | #include "chrome/common/pref_names.h" |
[email protected] | 78dcb90 | 2010-07-03 02:29:52 | [diff] [blame] | 14 | #include "chrome/test/testing_profile.h" |
[email protected] | 1bda9755 | 2011-03-01 20:11:52 | [diff] [blame] | 15 | #include "content/browser/browser_thread.h" |
[email protected] | 79ea486 | 2011-02-24 00:46:44 | [diff] [blame] | 16 | #include "content/browser/renderer_host/test_render_view_host.h" |
[email protected] | 78dcb90 | 2010-07-03 02:29:52 | [diff] [blame] | 17 | #include "grit/generated_resources.h" |
| 18 | #include "testing/gtest/include/gtest/gtest.h" |
[email protected] | 8bd0fe6 | 2011-01-17 06:44:37 | [diff] [blame] | 19 | #include "third_party/WebKit/Source/WebKit/chromium/public/WebNotificationPresenter.h" |
[email protected] | 78dcb90 | 2010-07-03 02:29:52 | [diff] [blame] | 20 | |
[email protected] | afcd76b | 2010-07-03 03:14:44 | [diff] [blame] | 21 | namespace { |
| 22 | |
[email protected] | f4192f2 | 2010-07-06 16:45:22 | [diff] [blame] | 23 | // NotificationsPrefsCache wants to be called on the IO thread. This class |
| 24 | // routes calls to the cache on the IO thread. |
| 25 | class ThreadProxy : public base::RefCountedThreadSafe<ThreadProxy> { |
[email protected] | afcd76b | 2010-07-03 03:14:44 | [diff] [blame] | 26 | public: |
[email protected] | f4192f2 | 2010-07-06 16:45:22 | [diff] [blame] | 27 | ThreadProxy() |
| 28 | : io_event_(false, false), |
[email protected] | 4617b21 | 2010-07-23 22:41:41 | [diff] [blame] | 29 | ui_event_(false, false), |
| 30 | permission_(0) { |
[email protected] | f4192f2 | 2010-07-06 16:45:22 | [diff] [blame] | 31 | // The current message loop was already initalized by the test superclass. |
| 32 | ui_thread_.reset( |
[email protected] | 9d9a80a | 2010-10-10 13:18:10 | [diff] [blame] | 33 | new BrowserThread(BrowserThread::UI, MessageLoop::current())); |
[email protected] | f4192f2 | 2010-07-06 16:45:22 | [diff] [blame] | 34 | |
| 35 | // Create IO thread, start its message loop. |
[email protected] | 9d9a80a | 2010-10-10 13:18:10 | [diff] [blame] | 36 | io_thread_.reset(new BrowserThread(BrowserThread::IO)); |
[email protected] | f4192f2 | 2010-07-06 16:45:22 | [diff] [blame] | 37 | io_thread_->Start(); |
| 38 | |
| 39 | // Calling PauseIOThread() here isn't safe, because the runnable method |
| 40 | // could complete before the constructor is done, deleting |this|. |
[email protected] | afcd76b | 2010-07-03 03:14:44 | [diff] [blame] | 41 | } |
[email protected] | f4192f2 | 2010-07-06 16:45:22 | [diff] [blame] | 42 | |
| 43 | int CacheHasPermission(NotificationsPrefsCache* cache, const GURL& url) { |
[email protected] | 9d9a80a | 2010-10-10 13:18:10 | [diff] [blame] | 44 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 45 | BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, |
[email protected] | 0d7e79fa | 2010-10-08 23:35:47 | [diff] [blame] | 46 | NewRunnableMethod(this, &ThreadProxy::CacheHasPermissionIO, |
[email protected] | 8be8294d | 2010-11-01 16:24:55 | [diff] [blame] | 47 | make_scoped_refptr(cache), url)); |
[email protected] | f4192f2 | 2010-07-06 16:45:22 | [diff] [blame] | 48 | io_event_.Signal(); |
| 49 | ui_event_.Wait(); // Wait for IO thread to be done. |
[email protected] | 9d9a80a | 2010-10-10 13:18:10 | [diff] [blame] | 50 | BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, |
[email protected] | f4192f2 | 2010-07-06 16:45:22 | [diff] [blame] | 51 | NewRunnableMethod(this, &ThreadProxy::PauseIOThreadIO)); |
| 52 | |
| 53 | return permission_; |
| 54 | } |
| 55 | |
| 56 | void PauseIOThread() { |
[email protected] | 9d9a80a | 2010-10-10 13:18:10 | [diff] [blame] | 57 | BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, |
[email protected] | f4192f2 | 2010-07-06 16:45:22 | [diff] [blame] | 58 | NewRunnableMethod(this, &ThreadProxy::PauseIOThreadIO)); |
| 59 | } |
| 60 | |
| 61 | void DrainIOThread() { |
[email protected] | 9d9a80a | 2010-10-10 13:18:10 | [diff] [blame] | 62 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | f4192f2 | 2010-07-06 16:45:22 | [diff] [blame] | 63 | io_event_.Signal(); |
| 64 | io_thread_->Stop(); |
[email protected] | afcd76b | 2010-07-03 03:14:44 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | private: |
[email protected] | f4192f2 | 2010-07-06 16:45:22 | [diff] [blame] | 68 | friend class base::RefCountedThreadSafe<ThreadProxy>; |
| 69 | ~ThreadProxy() { |
| 70 | DrainIOThread(); |
| 71 | } |
| 72 | |
| 73 | void PauseIOThreadIO() { |
[email protected] | 9d9a80a | 2010-10-10 13:18:10 | [diff] [blame] | 74 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
[email protected] | f4192f2 | 2010-07-06 16:45:22 | [diff] [blame] | 75 | io_event_.Wait(); |
| 76 | } |
| 77 | |
| 78 | void CacheHasPermissionIO(NotificationsPrefsCache* cache, const GURL& url) { |
[email protected] | 9d9a80a | 2010-10-10 13:18:10 | [diff] [blame] | 79 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
[email protected] | f4192f2 | 2010-07-06 16:45:22 | [diff] [blame] | 80 | permission_ = cache->HasPermission(url); |
| 81 | ui_event_.Signal(); |
| 82 | } |
| 83 | |
| 84 | base::WaitableEvent io_event_; |
| 85 | base::WaitableEvent ui_event_; |
[email protected] | 9d9a80a | 2010-10-10 13:18:10 | [diff] [blame] | 86 | scoped_ptr<BrowserThread> ui_thread_; |
| 87 | scoped_ptr<BrowserThread> io_thread_; |
[email protected] | f4192f2 | 2010-07-06 16:45:22 | [diff] [blame] | 88 | |
| 89 | int permission_; |
[email protected] | afcd76b | 2010-07-03 03:14:44 | [diff] [blame] | 90 | }; |
| 91 | |
| 92 | |
[email protected] | 78dcb90 | 2010-07-03 02:29:52 | [diff] [blame] | 93 | class DesktopNotificationServiceTest : public RenderViewHostTestHarness { |
| 94 | public: |
[email protected] | f4192f2 | 2010-07-06 16:45:22 | [diff] [blame] | 95 | DesktopNotificationServiceTest() { |
[email protected] | 78dcb90 | 2010-07-03 02:29:52 | [diff] [blame] | 96 | } |
[email protected] | f4192f2 | 2010-07-06 16:45:22 | [diff] [blame] | 97 | |
| 98 | virtual void SetUp() { |
| 99 | RenderViewHostTestHarness::SetUp(); |
| 100 | proxy_ = new ThreadProxy; |
| 101 | proxy_->PauseIOThread(); |
| 102 | |
| 103 | // Creates the service, calls InitPrefs() on it which loads data from the |
| 104 | // profile into the cache and then puts the cache in io thread mode. |
| 105 | service_ = profile()->GetDesktopNotificationService(); |
| 106 | cache_ = service_->prefs_cache(); |
| 107 | } |
| 108 | |
| 109 | virtual void TearDown() { |
| 110 | // The io thread's waiting on the io_event_ might hold a ref to |proxy_|, |
| 111 | // preventing its destruction. Clear that ref. |
| 112 | proxy_->DrainIOThread(); |
| 113 | RenderViewHostTestHarness::TearDown(); |
| 114 | } |
| 115 | |
| 116 | DesktopNotificationService* service_; |
| 117 | NotificationsPrefsCache* cache_; |
| 118 | scoped_refptr<ThreadProxy> proxy_; |
[email protected] | 78dcb90 | 2010-07-03 02:29:52 | [diff] [blame] | 119 | }; |
| 120 | |
| 121 | TEST_F(DesktopNotificationServiceTest, DefaultContentSettingSentToCache) { |
[email protected] | 78dcb90 | 2010-07-03 02:29:52 | [diff] [blame] | 122 | // The default pref registered in DesktopNotificationService is "ask", |
| 123 | // and that's what sent to the cache. |
[email protected] | f4192f2 | 2010-07-06 16:45:22 | [diff] [blame] | 124 | EXPECT_EQ(CONTENT_SETTING_ASK, cache_->CachedDefaultContentSetting()); |
[email protected] | 78dcb90 | 2010-07-03 02:29:52 | [diff] [blame] | 125 | |
| 126 | // Change the default content setting. This will post a task on the IO thread |
| 127 | // to update the cache. |
[email protected] | f4192f2 | 2010-07-06 16:45:22 | [diff] [blame] | 128 | service_->SetDefaultContentSetting(CONTENT_SETTING_BLOCK); |
[email protected] | 78dcb90 | 2010-07-03 02:29:52 | [diff] [blame] | 129 | |
| 130 | // The updated pref shouldn't be sent to the cache immediately. |
[email protected] | f4192f2 | 2010-07-06 16:45:22 | [diff] [blame] | 131 | EXPECT_EQ(CONTENT_SETTING_ASK, cache_->CachedDefaultContentSetting()); |
[email protected] | 78dcb90 | 2010-07-03 02:29:52 | [diff] [blame] | 132 | |
| 133 | // Run IO thread tasks. |
[email protected] | f4192f2 | 2010-07-06 16:45:22 | [diff] [blame] | 134 | proxy_->DrainIOThread(); |
[email protected] | 78dcb90 | 2010-07-03 02:29:52 | [diff] [blame] | 135 | |
| 136 | // Now that IO thread events have been processed, it should be there. |
[email protected] | f4192f2 | 2010-07-06 16:45:22 | [diff] [blame] | 137 | EXPECT_EQ(CONTENT_SETTING_BLOCK, cache_->CachedDefaultContentSetting()); |
| 138 | } |
| 139 | |
[email protected] | 0c6c4be | 2011-03-23 12:31:20 | [diff] [blame] | 140 | TEST_F(DesktopNotificationServiceTest, SettingsForSchemes) { |
| 141 | GURL url("file:///html/test.html"); |
| 142 | |
| 143 | EXPECT_EQ(CONTENT_SETTING_ASK, cache_->CachedDefaultContentSetting()); |
| 144 | EXPECT_EQ(WebKit::WebNotificationPresenter::PermissionNotAllowed, |
| 145 | proxy_->CacheHasPermission(cache_, url)); |
| 146 | |
| 147 | service_->GrantPermission(url); |
| 148 | EXPECT_EQ(WebKit::WebNotificationPresenter::PermissionAllowed, |
| 149 | proxy_->CacheHasPermission(cache_, url)); |
| 150 | |
| 151 | service_->DenyPermission(url); |
| 152 | EXPECT_EQ(WebKit::WebNotificationPresenter::PermissionDenied, |
| 153 | proxy_->CacheHasPermission(cache_, url)); |
| 154 | |
| 155 | GURL https_url("https://testurl"); |
| 156 | GURL http_url("http://testurl"); |
| 157 | EXPECT_EQ(CONTENT_SETTING_ASK, cache_->CachedDefaultContentSetting()); |
| 158 | EXPECT_EQ(WebKit::WebNotificationPresenter::PermissionNotAllowed, |
| 159 | proxy_->CacheHasPermission(cache_, http_url)); |
| 160 | EXPECT_EQ(WebKit::WebNotificationPresenter::PermissionNotAllowed, |
| 161 | proxy_->CacheHasPermission(cache_, https_url)); |
| 162 | |
| 163 | service_->GrantPermission(https_url); |
| 164 | EXPECT_EQ(WebKit::WebNotificationPresenter::PermissionAllowed, |
| 165 | proxy_->CacheHasPermission(cache_, https_url)); |
| 166 | EXPECT_EQ(WebKit::WebNotificationPresenter::PermissionNotAllowed, |
| 167 | proxy_->CacheHasPermission(cache_, http_url)); |
| 168 | |
| 169 | service_->DenyPermission(http_url); |
| 170 | EXPECT_EQ(WebKit::WebNotificationPresenter::PermissionDenied, |
| 171 | proxy_->CacheHasPermission(cache_, http_url)); |
| 172 | EXPECT_EQ(WebKit::WebNotificationPresenter::PermissionAllowed, |
| 173 | proxy_->CacheHasPermission(cache_, https_url)); |
| 174 | } |
| 175 | |
[email protected] | f4192f2 | 2010-07-06 16:45:22 | [diff] [blame] | 176 | TEST_F(DesktopNotificationServiceTest, GrantPermissionSentToCache) { |
| 177 | GURL url("http://allowed.com"); |
| 178 | EXPECT_EQ(WebKit::WebNotificationPresenter::PermissionNotAllowed, |
| 179 | proxy_->CacheHasPermission(cache_, url)); |
| 180 | |
| 181 | service_->GrantPermission(url); |
| 182 | |
| 183 | EXPECT_EQ(WebKit::WebNotificationPresenter::PermissionAllowed, |
| 184 | proxy_->CacheHasPermission(cache_, url)); |
| 185 | } |
| 186 | |
| 187 | TEST_F(DesktopNotificationServiceTest, DenyPermissionSentToCache) { |
| 188 | GURL url("http://denied.com"); |
| 189 | EXPECT_EQ(WebKit::WebNotificationPresenter::PermissionNotAllowed, |
| 190 | proxy_->CacheHasPermission(cache_, url)); |
| 191 | |
| 192 | service_->DenyPermission(url); |
| 193 | |
| 194 | EXPECT_EQ(WebKit::WebNotificationPresenter::PermissionDenied, |
| 195 | proxy_->CacheHasPermission(cache_, url)); |
| 196 | } |
| 197 | |
| 198 | TEST_F(DesktopNotificationServiceTest, PrefChangesSentToCache) { |
| 199 | PrefService* prefs = profile()->GetPrefs(); |
| 200 | |
| 201 | ListValue* allowed_sites = |
| 202 | prefs->GetMutableList(prefs::kDesktopNotificationAllowedOrigins); |
| 203 | { |
| 204 | allowed_sites->Append(new StringValue(GURL("http://allowed.com").spec())); |
[email protected] | f89ee34 | 2011-03-07 09:28:27 | [diff] [blame] | 205 | ScopedUserPrefUpdate updateAllowed( |
[email protected] | f4192f2 | 2010-07-06 16:45:22 | [diff] [blame] | 206 | prefs, prefs::kDesktopNotificationAllowedOrigins); |
| 207 | } |
| 208 | |
| 209 | ListValue* denied_sites = |
| 210 | prefs->GetMutableList(prefs::kDesktopNotificationDeniedOrigins); |
| 211 | { |
| 212 | denied_sites->Append(new StringValue(GURL("http://denied.com").spec())); |
[email protected] | f89ee34 | 2011-03-07 09:28:27 | [diff] [blame] | 213 | ScopedUserPrefUpdate updateDenied( |
[email protected] | f4192f2 | 2010-07-06 16:45:22 | [diff] [blame] | 214 | prefs, prefs::kDesktopNotificationDeniedOrigins); |
| 215 | } |
| 216 | |
| 217 | EXPECT_EQ(WebKit::WebNotificationPresenter::PermissionAllowed, |
| 218 | proxy_->CacheHasPermission(cache_, GURL("http://allowed.com"))); |
| 219 | EXPECT_EQ(WebKit::WebNotificationPresenter::PermissionDenied, |
| 220 | proxy_->CacheHasPermission(cache_, GURL("http://denied.com"))); |
| 221 | } |
| 222 | |
| 223 | TEST_F(DesktopNotificationServiceTest, GetAllowedOrigins) { |
| 224 | service_->GrantPermission(GURL("http://allowed2.com")); |
| 225 | service_->GrantPermission(GURL("http://allowed.com")); |
| 226 | |
| 227 | std::vector<GURL> allowed_origins(service_->GetAllowedOrigins()); |
| 228 | ASSERT_EQ(2u, allowed_origins.size()); |
| 229 | EXPECT_EQ(GURL("http://allowed2.com"), allowed_origins[0]); |
| 230 | EXPECT_EQ(GURL("http://allowed.com"), allowed_origins[1]); |
| 231 | } |
| 232 | |
| 233 | TEST_F(DesktopNotificationServiceTest, GetBlockedOrigins) { |
| 234 | service_->DenyPermission(GURL("http://denied2.com")); |
| 235 | service_->DenyPermission(GURL("http://denied.com")); |
| 236 | |
| 237 | std::vector<GURL> denied_origins(service_->GetBlockedOrigins()); |
| 238 | ASSERT_EQ(2u, denied_origins.size()); |
| 239 | EXPECT_EQ(GURL("http://denied2.com"), denied_origins[0]); |
| 240 | EXPECT_EQ(GURL("http://denied.com"), denied_origins[1]); |
| 241 | } |
| 242 | |
| 243 | TEST_F(DesktopNotificationServiceTest, ResetAllSentToCache) { |
| 244 | GURL allowed_url("http://allowed.com"); |
| 245 | service_->GrantPermission(allowed_url); |
| 246 | EXPECT_EQ(WebKit::WebNotificationPresenter::PermissionAllowed, |
| 247 | proxy_->CacheHasPermission(cache_, allowed_url)); |
| 248 | GURL denied_url("http://denied.com"); |
| 249 | service_->DenyPermission(denied_url); |
| 250 | EXPECT_EQ(WebKit::WebNotificationPresenter::PermissionDenied, |
| 251 | proxy_->CacheHasPermission(cache_, denied_url)); |
| 252 | |
| 253 | service_->ResetAllOrigins(); |
| 254 | |
| 255 | EXPECT_EQ(WebKit::WebNotificationPresenter::PermissionNotAllowed, |
| 256 | proxy_->CacheHasPermission(cache_, allowed_url)); |
| 257 | EXPECT_EQ(WebKit::WebNotificationPresenter::PermissionNotAllowed, |
| 258 | proxy_->CacheHasPermission(cache_, denied_url)); |
| 259 | } |
| 260 | |
| 261 | TEST_F(DesktopNotificationServiceTest, ResetAllowedSentToCache) { |
| 262 | GURL allowed_url("http://allowed.com"); |
| 263 | service_->GrantPermission(allowed_url); |
| 264 | EXPECT_EQ(WebKit::WebNotificationPresenter::PermissionAllowed, |
| 265 | proxy_->CacheHasPermission(cache_, allowed_url)); |
| 266 | |
| 267 | service_->ResetAllowedOrigin(allowed_url); |
| 268 | |
| 269 | EXPECT_EQ(WebKit::WebNotificationPresenter::PermissionNotAllowed, |
| 270 | proxy_->CacheHasPermission(cache_, allowed_url)); |
| 271 | } |
| 272 | |
| 273 | TEST_F(DesktopNotificationServiceTest, ResetBlockedSentToCache) { |
| 274 | GURL denied_url("http://denied.com"); |
| 275 | service_->DenyPermission(denied_url); |
| 276 | EXPECT_EQ(WebKit::WebNotificationPresenter::PermissionDenied, |
| 277 | proxy_->CacheHasPermission(cache_, denied_url)); |
| 278 | |
| 279 | service_->ResetBlockedOrigin(denied_url); |
| 280 | |
| 281 | EXPECT_EQ(WebKit::WebNotificationPresenter::PermissionNotAllowed, |
| 282 | proxy_->CacheHasPermission(cache_, denied_url)); |
[email protected] | 78dcb90 | 2010-07-03 02:29:52 | [diff] [blame] | 283 | } |
| 284 | |
[email protected] | afcd76b | 2010-07-03 03:14:44 | [diff] [blame] | 285 | } // namespace |