| OLD | NEW |
| 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" |
| (...skipping 209 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 220 // create the preferences if they don't exist yet. | 220 // create the preferences if they don't exist yet. |
| 221 void DesktopNotificationService::InitPrefs() { | 221 void DesktopNotificationService::InitPrefs() { |
| 222 PrefService* prefs = profile_->GetPrefs(); | 222 PrefService* prefs = profile_->GetPrefs(); |
| 223 std::vector<GURL> allowed_origins; | 223 std::vector<GURL> allowed_origins; |
| 224 std::vector<GURL> denied_origins; | 224 std::vector<GURL> denied_origins; |
| 225 ContentSetting default_content_setting = CONTENT_SETTING_DEFAULT; | 225 ContentSetting default_content_setting = CONTENT_SETTING_DEFAULT; |
| 226 | 226 |
| 227 if (!profile_->IsOffTheRecord()) { | 227 if (!profile_->IsOffTheRecord()) { |
| 228 default_content_setting = IntToContentSetting( | 228 default_content_setting = IntToContentSetting( |
| 229 prefs->GetInteger(prefs::kDesktopNotificationDefaultContentSetting)); | 229 prefs->GetInteger(prefs::kDesktopNotificationDefaultContentSetting)); |
| 230 | 230 allowed_origins = GetAllowedOrigins(); |
| 231 const ListValue* allowed_sites = | 231 denied_origins = GetBlockedOrigins(); |
| 232 prefs->GetList(prefs::kDesktopNotificationAllowedOrigins); | |
| 233 if (allowed_sites) | |
| 234 NotificationsPrefsCache::ListValueToGurlVector(*allowed_sites, | |
| 235 &allowed_origins); | |
| 236 | |
| 237 const ListValue* denied_sites = | |
| 238 prefs->GetList(prefs::kDesktopNotificationDeniedOrigins); | |
| 239 if (denied_sites) | |
| 240 NotificationsPrefsCache::ListValueToGurlVector(*denied_sites, | |
| 241 &denied_origins); | |
| 242 } | 232 } |
| 243 | 233 |
| 244 prefs_cache_ = new NotificationsPrefsCache(); | 234 prefs_cache_ = new NotificationsPrefsCache(); |
| 245 prefs_cache_->SetCacheDefaultContentSetting(default_content_setting); | 235 prefs_cache_->SetCacheDefaultContentSetting(default_content_setting); |
| 246 prefs_cache_->SetCacheAllowedOrigins(allowed_origins); | 236 prefs_cache_->SetCacheAllowedOrigins(allowed_origins); |
| 247 prefs_cache_->SetCacheDeniedOrigins(denied_origins); | 237 prefs_cache_->SetCacheDeniedOrigins(denied_origins); |
| 248 prefs_cache_->set_is_initialized(true); | 238 prefs_cache_->set_is_initialized(true); |
| 249 } | 239 } |
| 250 | 240 |
| 251 void DesktopNotificationService::StartObserving() { | 241 void DesktopNotificationService::StartObserving() { |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 293 } | 283 } |
| 294 | 284 |
| 295 void DesktopNotificationService::Observe(NotificationType type, | 285 void DesktopNotificationService::Observe(NotificationType type, |
| 296 const NotificationSource& source, | 286 const NotificationSource& source, |
| 297 const NotificationDetails& details) { | 287 const NotificationDetails& details) { |
| 298 DCHECK(NotificationType::PREF_CHANGED == type); | 288 DCHECK(NotificationType::PREF_CHANGED == type); |
| 299 PrefService* prefs = profile_->GetPrefs(); | 289 PrefService* prefs = profile_->GetPrefs(); |
| 300 std::wstring* name = Details<std::wstring>(details).ptr(); | 290 std::wstring* name = Details<std::wstring>(details).ptr(); |
| 301 | 291 |
| 302 if (0 == name->compare(prefs::kDesktopNotificationAllowedOrigins)) { | 292 if (0 == name->compare(prefs::kDesktopNotificationAllowedOrigins)) { |
| 303 const ListValue* allowed_sites = | 293 std::vector<GURL> allowed_origins(GetAllowedOrigins()); |
| 304 prefs->GetList(prefs::kDesktopNotificationAllowedOrigins); | |
| 305 std::vector<GURL> allowed_origins; | |
| 306 if (allowed_sites) { | |
| 307 NotificationsPrefsCache::ListValueToGurlVector(*allowed_sites, | |
| 308 &allowed_origins); | |
| 309 } | |
| 310 // Schedule a cache update on the IO thread. | 294 // Schedule a cache update on the IO thread. |
| 311 ChromeThread::PostTask( | 295 ChromeThread::PostTask( |
| 312 ChromeThread::IO, FROM_HERE, | 296 ChromeThread::IO, FROM_HERE, |
| 313 NewRunnableMethod( | 297 NewRunnableMethod( |
| 314 prefs_cache_.get(), | 298 prefs_cache_.get(), |
| 315 &NotificationsPrefsCache::SetCacheAllowedOrigins, | 299 &NotificationsPrefsCache::SetCacheAllowedOrigins, |
| 316 allowed_origins)); | 300 allowed_origins)); |
| 317 } else if (0 == name->compare(prefs::kDesktopNotificationDeniedOrigins)) { | 301 } else if (0 == name->compare(prefs::kDesktopNotificationDeniedOrigins)) { |
| 318 const ListValue* denied_sites = | 302 std::vector<GURL> denied_origins(GetBlockedOrigins()); |
| 319 prefs->GetList(prefs::kDesktopNotificationDeniedOrigins); | |
| 320 std::vector<GURL> denied_origins; | |
| 321 if (denied_sites) { | |
| 322 NotificationsPrefsCache::ListValueToGurlVector(*denied_sites, | |
| 323 &denied_origins); | |
| 324 } | |
| 325 // Schedule a cache update on the IO thread. | 303 // Schedule a cache update on the IO thread. |
| 326 ChromeThread::PostTask( | 304 ChromeThread::PostTask( |
| 327 ChromeThread::IO, FROM_HERE, | 305 ChromeThread::IO, FROM_HERE, |
| 328 NewRunnableMethod( | 306 NewRunnableMethod( |
| 329 prefs_cache_.get(), | 307 prefs_cache_.get(), |
| 330 &NotificationsPrefsCache::SetCacheDeniedOrigins, | 308 &NotificationsPrefsCache::SetCacheDeniedOrigins, |
| 331 denied_origins)); | 309 denied_origins)); |
| 332 } else if (0 == name->compare( | 310 } else if (0 == name->compare( |
| 333 prefs::kDesktopNotificationDefaultContentSetting)) { | 311 prefs::kDesktopNotificationDefaultContentSetting)) { |
| 334 const ContentSetting default_content_setting = IntToContentSetting( | 312 const ContentSetting default_content_setting = IntToContentSetting( |
| (...skipping 10 matching lines...) Expand all Loading... |
| 345 } | 323 } |
| 346 | 324 |
| 347 void DesktopNotificationService::PersistPermissionChange( | 325 void DesktopNotificationService::PersistPermissionChange( |
| 348 const GURL& origin, bool is_allowed) { | 326 const GURL& origin, bool is_allowed) { |
| 349 // Don't persist changes when off the record. | 327 // Don't persist changes when off the record. |
| 350 if (profile_->IsOffTheRecord()) | 328 if (profile_->IsOffTheRecord()) |
| 351 return; | 329 return; |
| 352 | 330 |
| 353 PrefService* prefs = profile_->GetPrefs(); | 331 PrefService* prefs = profile_->GetPrefs(); |
| 354 | 332 |
| 333 // |Observe()| updates the whole permission set in the cache, but only a |
| 334 // single origin has changed. Hence, callers of this method manually |
| 335 // schedule a task to update the prefs cache, and the prefs observer is |
| 336 // disabled while the update runs. |
| 355 StopObserving(); | 337 StopObserving(); |
| 356 | 338 |
| 357 bool allowed_changed = false; | 339 bool allowed_changed = false; |
| 358 bool denied_changed = false; | 340 bool denied_changed = false; |
| 359 | 341 |
| 360 ListValue* allowed_sites = | 342 ListValue* allowed_sites = |
| 361 prefs->GetMutableList(prefs::kDesktopNotificationAllowedOrigins); | 343 prefs->GetMutableList(prefs::kDesktopNotificationAllowedOrigins); |
| 362 ListValue* denied_sites = | 344 ListValue* denied_sites = |
| 363 prefs->GetMutableList(prefs::kDesktopNotificationDeniedOrigins); | 345 prefs->GetMutableList(prefs::kDesktopNotificationDeniedOrigins); |
| 364 { | 346 { |
| (...skipping 21 matching lines...) Expand all Loading... |
| 386 denied_changed = true; | 368 denied_changed = true; |
| 387 else | 369 else |
| 388 delete value; | 370 delete value; |
| 389 } | 371 } |
| 390 } | 372 } |
| 391 | 373 |
| 392 // Persist the pref if anthing changed, but only send updates for the | 374 // Persist the pref if anthing changed, but only send updates for the |
| 393 // list that changed. | 375 // list that changed. |
| 394 if (allowed_changed || denied_changed) { | 376 if (allowed_changed || denied_changed) { |
| 395 if (allowed_changed) { | 377 if (allowed_changed) { |
| 396 ScopedPrefUpdate updateAllowed( | 378 ScopedPrefUpdate update_allowed( |
| 397 prefs, prefs::kDesktopNotificationAllowedOrigins); | 379 prefs, prefs::kDesktopNotificationAllowedOrigins); |
| 398 } | 380 } |
| 399 if (denied_changed) { | 381 if (denied_changed) { |
| 400 ScopedPrefUpdate updateDenied( | 382 ScopedPrefUpdate updateDenied( |
| 401 prefs, prefs::kDesktopNotificationDeniedOrigins); | 383 prefs, prefs::kDesktopNotificationDeniedOrigins); |
| 402 } | 384 } |
| 403 prefs->ScheduleSavePersistentPrefs(); | 385 prefs->ScheduleSavePersistentPrefs(); |
| 404 } | 386 } |
| 405 StartObserving(); | 387 StartObserving(); |
| 406 } | 388 } |
| 407 | 389 |
| 408 ContentSetting DesktopNotificationService::GetDefaultContentSetting() { | 390 ContentSetting DesktopNotificationService::GetDefaultContentSetting() { |
| 409 PrefService* prefs = profile_->GetPrefs(); | 391 PrefService* prefs = profile_->GetPrefs(); |
| 410 ContentSetting setting = IntToContentSetting( | 392 ContentSetting setting = IntToContentSetting( |
| 411 prefs->GetInteger(prefs::kDesktopNotificationDefaultContentSetting)); | 393 prefs->GetInteger(prefs::kDesktopNotificationDefaultContentSetting)); |
| 412 if (setting == CONTENT_SETTING_DEFAULT) | 394 if (setting == CONTENT_SETTING_DEFAULT) |
| 413 setting = kDefaultSetting; | 395 setting = kDefaultSetting; |
| 414 return setting; | 396 return setting; |
| 415 } | 397 } |
| 416 | 398 |
| 417 void DesktopNotificationService::SetDefaultContentSetting( | 399 void DesktopNotificationService::SetDefaultContentSetting( |
| 418 ContentSetting setting) { | 400 ContentSetting setting) { |
| 419 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); | 401 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); |
| 420 profile_->GetPrefs()->SetInteger( | 402 profile_->GetPrefs()->SetInteger( |
| 421 prefs::kDesktopNotificationDefaultContentSetting, | 403 prefs::kDesktopNotificationDefaultContentSetting, |
| 422 setting == CONTENT_SETTING_DEFAULT ? kDefaultSetting : setting); | 404 setting == CONTENT_SETTING_DEFAULT ? kDefaultSetting : setting); |
| 423 // The cache is updated through the notification observer. | 405 // The cache is updated through the notification observer. |
| 424 } | 406 } |
| 425 | 407 |
| 408 std::vector<GURL> DesktopNotificationService::GetAllowedOrigins() { |
| 409 std::vector<GURL> allowed_origins; |
| 410 PrefService* prefs = profile_->GetPrefs(); |
| 411 const ListValue* allowed_sites = |
| 412 prefs->GetList(prefs::kDesktopNotificationAllowedOrigins); |
| 413 if (allowed_sites) { |
| 414 NotificationsPrefsCache::ListValueToGurlVector(*allowed_sites, |
| 415 &allowed_origins); |
| 416 } |
| 417 return allowed_origins; |
| 418 } |
| 419 |
| 420 std::vector<GURL> DesktopNotificationService::GetBlockedOrigins() { |
| 421 std::vector<GURL> denied_origins; |
| 422 PrefService* prefs = profile_->GetPrefs(); |
| 423 const ListValue* denied_sites = |
| 424 prefs->GetList(prefs::kDesktopNotificationDeniedOrigins); |
| 425 if (denied_sites) { |
| 426 NotificationsPrefsCache::ListValueToGurlVector(*denied_sites, |
| 427 &denied_origins); |
| 428 } |
| 429 return denied_origins; |
| 430 } |
| 431 |
| 432 void DesktopNotificationService::ResetAllowedOrigin(const GURL& origin) { |
| 433 if (profile_->IsOffTheRecord()) |
| 434 return; |
| 435 |
| 436 // Since this isn't called often, let the normal observer behavior update the |
| 437 // cache in this case. |
| 438 PrefService* prefs = profile_->GetPrefs(); |
| 439 ListValue* allowed_sites = |
| 440 prefs->GetMutableList(prefs::kDesktopNotificationAllowedOrigins); |
| 441 { |
| 442 StringValue value(origin.spec()); |
| 443 int removed_index = allowed_sites->Remove(value); |
| 444 DCHECK_NE(-1, removed_index) << origin << " was not allowed"; |
| 445 ScopedPrefUpdate update_allowed( |
| 446 prefs, prefs::kDesktopNotificationAllowedOrigins); |
| 447 } |
| 448 prefs->ScheduleSavePersistentPrefs(); |
| 449 } |
| 450 |
| 451 void DesktopNotificationService::ResetBlockedOrigin(const GURL& origin) { |
| 452 if (profile_->IsOffTheRecord()) |
| 453 return; |
| 454 |
| 455 // Since this isn't called often, let the normal observer behavior update the |
| 456 // cache in this case. |
| 457 PrefService* prefs = profile_->GetPrefs(); |
| 458 ListValue* denied_sites = |
| 459 prefs->GetMutableList(prefs::kDesktopNotificationDeniedOrigins); |
| 460 { |
| 461 StringValue value(origin.spec()); |
| 462 int removed_index = denied_sites->Remove(value); |
| 463 DCHECK_NE(-1, removed_index) << origin << " was not blocked"; |
| 464 ScopedPrefUpdate update_allowed( |
| 465 prefs, prefs::kDesktopNotificationDeniedOrigins); |
| 466 } |
| 467 prefs->ScheduleSavePersistentPrefs(); |
| 468 } |
| 469 |
| 470 void DesktopNotificationService::ResetAllOrigins() { |
| 471 PrefService* prefs = profile_->GetPrefs(); |
| 472 prefs->ClearPref(prefs::kDesktopNotificationAllowedOrigins); |
| 473 prefs->ClearPref(prefs::kDesktopNotificationDeniedOrigins); |
| 474 } |
| 475 |
| 476 ContentSetting DesktopNotificationService::GetContentSetting( |
| 477 const GURL& origin) { |
| 478 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); |
| 479 if (profile_->IsOffTheRecord()) |
| 480 return kDefaultSetting; |
| 481 |
| 482 std::vector<GURL> allowed_origins(GetAllowedOrigins()); |
| 483 if (std::find(allowed_origins.begin(), allowed_origins.end(), origin) != |
| 484 allowed_origins.end()) |
| 485 return CONTENT_SETTING_ALLOW; |
| 486 |
| 487 std::vector<GURL> denied_origins(GetBlockedOrigins()); |
| 488 if (std::find(denied_origins.begin(), denied_origins.end(), origin) != |
| 489 denied_origins.end()) |
| 490 return CONTENT_SETTING_BLOCK; |
| 491 |
| 492 return GetDefaultContentSetting(); |
| 493 } |
| 494 |
| 426 void DesktopNotificationService::RequestPermission( | 495 void DesktopNotificationService::RequestPermission( |
| 427 const GURL& origin, int process_id, int route_id, int callback_context, | 496 const GURL& origin, int process_id, int route_id, int callback_context, |
| 428 TabContents* tab) { | 497 TabContents* tab) { |
| 429 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); | 498 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); |
| 430 if (!tab) | 499 if (!tab) |
| 431 return; | 500 return; |
| 432 // Show an info bar requesting permission. | 501 // Show an info bar requesting permission. |
| 433 std::wstring display_name = DisplayNameForOrigin(origin); | 502 std::wstring display_name = DisplayNameForOrigin(origin); |
| 434 | 503 |
| 435 tab->AddInfoBar(new NotificationPermissionInfoBarDelegate( | 504 tab->AddInfoBar(new NotificationPermissionInfoBarDelegate( |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 487 if (origin.SchemeIs(chrome::kExtensionScheme)) { | 556 if (origin.SchemeIs(chrome::kExtensionScheme)) { |
| 488 ExtensionsService* ext_service = profile_->GetExtensionsService(); | 557 ExtensionsService* ext_service = profile_->GetExtensionsService(); |
| 489 if (ext_service) { | 558 if (ext_service) { |
| 490 Extension* extension = ext_service->GetExtensionByURL(origin); | 559 Extension* extension = ext_service->GetExtensionByURL(origin); |
| 491 if (extension) | 560 if (extension) |
| 492 return UTF8ToWide(extension->name()); | 561 return UTF8ToWide(extension->name()); |
| 493 } | 562 } |
| 494 } | 563 } |
| 495 return UTF8ToWide(origin.host()); | 564 return UTF8ToWide(origin.host()); |
| 496 } | 565 } |
| OLD | NEW |