Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(224)

Side by Side Diff: chrome/browser/notifications/desktop_notification_service.cc

Issue 1578023: Add notifications to allow desktop notification permissions to be synced. (Closed)
Patch Set: pre-commit Created 10 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2010 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 "app/l10n_util.h" 7 #include "app/l10n_util.h"
8 #include "app/resource_bundle.h" 8 #include "app/resource_bundle.h"
9 #include "base/thread.h" 9 #include "base/thread.h"
10 #include "base/utf_string_conversions.h" 10 #include "base/utf_string_conversions.h"
11 #include "chrome/browser/browser_list.h" 11 #include "chrome/browser/browser_list.h"
12 #include "chrome/browser/child_process_host.h" 12 #include "chrome/browser/child_process_host.h"
13 #include "chrome/browser/chrome_thread.h" 13 #include "chrome/browser/chrome_thread.h"
14 #include "chrome/browser/extensions/extensions_service.h" 14 #include "chrome/browser/extensions/extensions_service.h"
15 #include "chrome/browser/notifications/notification.h" 15 #include "chrome/browser/notifications/notification.h"
16 #include "chrome/browser/notifications/notification_object_proxy.h" 16 #include "chrome/browser/notifications/notification_object_proxy.h"
17 #include "chrome/browser/notifications/notification_ui_manager.h" 17 #include "chrome/browser/notifications/notification_ui_manager.h"
18 #include "chrome/browser/notifications/notifications_prefs_cache.h" 18 #include "chrome/browser/notifications/notifications_prefs_cache.h"
19 #include "chrome/browser/pref_service.h" 19 #include "chrome/browser/pref_service.h"
20 #include "chrome/browser/profile.h" 20 #include "chrome/browser/profile.h"
21 #include "chrome/browser/renderer_host/render_process_host.h" 21 #include "chrome/browser/renderer_host/render_process_host.h"
22 #include "chrome/browser/renderer_host/render_view_host.h" 22 #include "chrome/browser/renderer_host/render_view_host.h"
23 #include "chrome/browser/renderer_host/site_instance.h" 23 #include "chrome/browser/renderer_host/site_instance.h"
24 #include "chrome/browser/scoped_pref_update.h"
24 #include "chrome/browser/tab_contents/infobar_delegate.h" 25 #include "chrome/browser/tab_contents/infobar_delegate.h"
25 #include "chrome/browser/tab_contents/tab_contents.h" 26 #include "chrome/browser/tab_contents/tab_contents.h"
26 #include "chrome/browser/worker_host/worker_process_host.h" 27 #include "chrome/browser/worker_host/worker_process_host.h"
28 #include "chrome/common/notification_service.h"
27 #include "chrome/common/pref_names.h" 29 #include "chrome/common/pref_names.h"
28 #include "chrome/common/render_messages.h" 30 #include "chrome/common/render_messages.h"
29 #include "chrome/common/url_constants.h" 31 #include "chrome/common/url_constants.h"
30 #include "grit/browser_resources.h" 32 #include "grit/browser_resources.h"
31 #include "grit/chromium_strings.h" 33 #include "grit/chromium_strings.h"
32 #include "grit/generated_resources.h" 34 #include "grit/generated_resources.h"
33 #include "grit/theme_resources.h" 35 #include "grit/theme_resources.h"
34 #include "net/base/escape.h" 36 #include "net/base/escape.h"
35 #include "third_party/WebKit/WebKit/chromium/public/WebNotificationPresenter.h" 37 #include "third_party/WebKit/WebKit/chromium/public/WebNotificationPresenter.h"
36 38
(...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after
187 bool action_taken_; 189 bool action_taken_;
188 190
189 DISALLOW_COPY_AND_ASSIGN(NotificationPermissionInfoBarDelegate); 191 DISALLOW_COPY_AND_ASSIGN(NotificationPermissionInfoBarDelegate);
190 }; 192 };
191 193
192 DesktopNotificationService::DesktopNotificationService(Profile* profile, 194 DesktopNotificationService::DesktopNotificationService(Profile* profile,
193 NotificationUIManager* ui_manager) 195 NotificationUIManager* ui_manager)
194 : profile_(profile), 196 : profile_(profile),
195 ui_manager_(ui_manager) { 197 ui_manager_(ui_manager) {
196 InitPrefs(); 198 InitPrefs();
199 StartObserving();
197 } 200 }
198 201
199 DesktopNotificationService::~DesktopNotificationService() { 202 DesktopNotificationService::~DesktopNotificationService() {
203 StopObserving();
200 } 204 }
201 205
202 // Initialize the cache with the allowed and denied origins, or 206 // Initialize the cache with the allowed and denied origins, or
203 // create the preferences if they don't exist yet. 207 // create the preferences if they don't exist yet.
204 void DesktopNotificationService::InitPrefs() { 208 void DesktopNotificationService::InitPrefs() {
205 PrefService* prefs = profile_->GetPrefs(); 209 PrefService* prefs = profile_->GetPrefs();
206 const ListValue* allowed_sites = NULL; 210 std::vector<GURL> allowed_origins;
207 const ListValue* denied_sites = NULL; 211 std::vector<GURL> denied_origins;
208 212
209 if (!profile_->IsOffTheRecord()) { 213 if (!profile_->IsOffTheRecord()) {
210 if (!prefs->FindPreference(prefs::kDesktopNotificationAllowedOrigins)) 214 if (!prefs->FindPreference(prefs::kDesktopNotificationAllowedOrigins))
211 prefs->RegisterListPref(prefs::kDesktopNotificationAllowedOrigins); 215 prefs->RegisterListPref(prefs::kDesktopNotificationAllowedOrigins);
212 allowed_sites = prefs->GetList(prefs::kDesktopNotificationAllowedOrigins); 216 const ListValue* allowed_sites =
217 prefs->GetList(prefs::kDesktopNotificationAllowedOrigins);
218 if (allowed_sites)
219 NotificationsPrefsCache::ListValueToGurlVector(*allowed_sites,
220 &allowed_origins);
213 221
214 if (!prefs->FindPreference(prefs::kDesktopNotificationDeniedOrigins)) 222 if (!prefs->FindPreference(prefs::kDesktopNotificationDeniedOrigins))
215 prefs->RegisterListPref(prefs::kDesktopNotificationDeniedOrigins); 223 prefs->RegisterListPref(prefs::kDesktopNotificationDeniedOrigins);
216 denied_sites = prefs->GetList(prefs::kDesktopNotificationDeniedOrigins); 224 const ListValue* denied_sites =
225 prefs->GetList(prefs::kDesktopNotificationDeniedOrigins);
226 if (denied_sites)
227 NotificationsPrefsCache::ListValueToGurlVector(*denied_sites,
228 &denied_origins);
217 } 229 }
218 230
219 prefs_cache_ = new NotificationsPrefsCache(allowed_sites, denied_sites); 231 prefs_cache_ = new NotificationsPrefsCache();
232 prefs_cache_->SetCacheAllowedOrigins(allowed_origins);
233 prefs_cache_->SetCacheDeniedOrigins(denied_origins);
220 prefs_cache_->set_is_initialized(true); 234 prefs_cache_->set_is_initialized(true);
221 } 235 }
222 236
237 void DesktopNotificationService::StartObserving() {
238 if (!profile_->IsOffTheRecord()) {
239 PrefService* prefs = profile_->GetPrefs();
240 prefs->AddPrefObserver(prefs::kDesktopNotificationAllowedOrigins, this);
241 prefs->AddPrefObserver(prefs::kDesktopNotificationDeniedOrigins, this);
242 }
243 }
244
245 void DesktopNotificationService::StopObserving() {
246 if (!profile_->IsOffTheRecord()) {
247 PrefService* prefs = profile_->GetPrefs();
248 prefs->RemovePrefObserver(prefs::kDesktopNotificationAllowedOrigins, this);
249 prefs->RemovePrefObserver(prefs::kDesktopNotificationDeniedOrigins, this);
250 }
251 }
252
223 void DesktopNotificationService::GrantPermission(const GURL& origin) { 253 void DesktopNotificationService::GrantPermission(const GURL& origin) {
224 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); 254 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI));
225 PersistPermissionChange(origin, true); 255 PersistPermissionChange(origin, true);
226 256
227 // Schedule a cache update on the IO thread. 257 // Schedule a cache update on the IO thread.
228 ChromeThread::PostTask( 258 ChromeThread::PostTask(
229 ChromeThread::IO, FROM_HERE, 259 ChromeThread::IO, FROM_HERE,
230 NewRunnableMethod( 260 NewRunnableMethod(
231 prefs_cache_.get(), &NotificationsPrefsCache::CacheAllowedOrigin, 261 prefs_cache_.get(), &NotificationsPrefsCache::CacheAllowedOrigin,
232 origin)); 262 origin));
233 } 263 }
234 264
235 void DesktopNotificationService::DenyPermission(const GURL& origin) { 265 void DesktopNotificationService::DenyPermission(const GURL& origin) {
236 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); 266 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI));
237 PersistPermissionChange(origin, false); 267 PersistPermissionChange(origin, false);
238 268
239 // Schedule a cache update on the IO thread. 269 // Schedule a cache update on the IO thread.
240 ChromeThread::PostTask( 270 ChromeThread::PostTask(
241 ChromeThread::IO, FROM_HERE, 271 ChromeThread::IO, FROM_HERE,
242 NewRunnableMethod( 272 NewRunnableMethod(
243 prefs_cache_.get(), &NotificationsPrefsCache::CacheDeniedOrigin, 273 prefs_cache_.get(), &NotificationsPrefsCache::CacheDeniedOrigin,
244 origin)); 274 origin));
245 } 275 }
246 276
277 void DesktopNotificationService::Observe(NotificationType type,
278 const NotificationSource& source,
279 const NotificationDetails& details) {
280 DCHECK(NotificationType::PREF_CHANGED == type);
281 PrefService* prefs = profile_->GetPrefs();
282 std::wstring* name = Details<std::wstring>(details).ptr();
283
284 if (0 == name->compare(prefs::kDesktopNotificationAllowedOrigins)) {
285 const ListValue* allowed_sites =
286 prefs->GetList(prefs::kDesktopNotificationAllowedOrigins);
287 std::vector<GURL> allowed_origins;
288 NotificationsPrefsCache::ListValueToGurlVector(*allowed_sites,
289 &allowed_origins);
290 // Schedule a cache update on the IO thread.
291 ChromeThread::PostTask(
292 ChromeThread::IO, FROM_HERE,
293 NewRunnableMethod(
294 prefs_cache_.get(),
295 &NotificationsPrefsCache::SetCacheAllowedOrigins,
296 allowed_origins));
297 } else if (0 == name->compare(prefs::kDesktopNotificationDeniedOrigins)) {
298 const ListValue* denied_sites =
299 prefs->GetList(prefs::kDesktopNotificationDeniedOrigins);
300 std::vector<GURL> denied_origins;
301 NotificationsPrefsCache::ListValueToGurlVector(*denied_sites,
302 &denied_origins);
303 // Schedule a cache update on the IO thread.
304 ChromeThread::PostTask(
305 ChromeThread::IO, FROM_HERE,
306 NewRunnableMethod(
307 prefs_cache_.get(),
308 &NotificationsPrefsCache::SetCacheDeniedOrigins,
309 denied_origins));
310 }
311 }
312
247 void DesktopNotificationService::PersistPermissionChange( 313 void DesktopNotificationService::PersistPermissionChange(
248 const GURL& origin, bool is_allowed) { 314 const GURL& origin, bool is_allowed) {
249 // Don't persist changes when off the record. 315 // Don't persist changes when off the record.
250 if (profile_->IsOffTheRecord()) 316 if (profile_->IsOffTheRecord())
251 return; 317 return;
252 318
253 PrefService* prefs = profile_->GetPrefs(); 319 PrefService* prefs = profile_->GetPrefs();
320
321 StopObserving();
322
323 bool allowed_changed = false;
324 bool denied_changed = false;
325
254 ListValue* allowed_sites = 326 ListValue* allowed_sites =
255 prefs->GetMutableList(prefs::kDesktopNotificationAllowedOrigins); 327 prefs->GetMutableList(prefs::kDesktopNotificationAllowedOrigins);
256 ListValue* denied_sites = 328 ListValue* denied_sites =
257 prefs->GetMutableList(prefs::kDesktopNotificationDeniedOrigins); 329 prefs->GetMutableList(prefs::kDesktopNotificationDeniedOrigins);
258 StringValue* value = new StringValue(origin.spec()); 330 {
259 // Remove from one list and add to the other. 331 // value is passed to the preferences list, or deleted.
260 if (is_allowed) { 332 StringValue* value = new StringValue(origin.spec());
261 allowed_sites->Append(value); 333
262 denied_sites->Remove(*value); 334 // Remove from one list and add to the other.
263 } else { 335 if (is_allowed) {
264 allowed_sites->Remove(*value); 336 // Remove from the denied list.
265 denied_sites->Append(value); 337 if (denied_sites->Remove(*value) != -1)
338 denied_changed = true;
339
340 // Add to the allowed list.
341 if (allowed_sites->AppendIfNotPresent(value))
342 allowed_changed = true;
343 else
344 delete value;
345 } else {
346 // Remove from the allowed list.
347 if (allowed_sites->Remove(*value) != -1)
348 allowed_changed = true;
349
350 // Add to the denied list.
351 if (denied_sites->AppendIfNotPresent(value))
352 denied_changed = true;
353 else
354 delete value;
355 }
266 } 356 }
267 prefs->ScheduleSavePersistentPrefs(); 357
358 // Persist the pref if anthing changed, but only send updates for the
359 // list that changed.
360 if (allowed_changed || denied_changed) {
361 if (allowed_changed) {
362 ScopedPrefUpdate updateAllowed(
363 prefs, prefs::kDesktopNotificationAllowedOrigins);
364 }
365 if (denied_changed) {
366 ScopedPrefUpdate updateDenied(
367 prefs, prefs::kDesktopNotificationDeniedOrigins);
368 }
369 prefs->ScheduleSavePersistentPrefs();
370 }
371 StartObserving();
268 } 372 }
269 373
270 void DesktopNotificationService::RequestPermission( 374 void DesktopNotificationService::RequestPermission(
271 const GURL& origin, int process_id, int route_id, int callback_context) { 375 const GURL& origin, int process_id, int route_id, int callback_context) {
272 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); 376 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI));
273 // Show an info bar requesting permission. 377 // Show an info bar requesting permission.
274 Browser* browser = BrowserList::GetLastActive(); 378 Browser* browser = BrowserList::GetLastActive();
275 if (!browser) { 379 if (!browser) {
276 // Reached during ui tests. 380 // Reached during ui tests.
277 return; 381 return;
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
337 if (origin.SchemeIs(chrome::kExtensionScheme)) { 441 if (origin.SchemeIs(chrome::kExtensionScheme)) {
338 ExtensionsService* ext_service = profile_->GetExtensionsService(); 442 ExtensionsService* ext_service = profile_->GetExtensionsService();
339 if (ext_service) { 443 if (ext_service) {
340 Extension* extension = ext_service->GetExtensionByURL(origin); 444 Extension* extension = ext_service->GetExtensionByURL(origin);
341 if (extension) 445 if (extension)
342 return ASCIIToWide(extension->name()); 446 return ASCIIToWide(extension->name());
343 } 447 }
344 } 448 }
345 return UTF8ToWide(origin.spec()); 449 return UTF8ToWide(origin.spec());
346 } 450 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698