| 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/metrics/histogram.h" | 9 #include "base/metrics/histogram.h" |
| 10 #include "base/thread.h" | 10 #include "base/thread.h" |
| (...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 208 // Whether the user clicked one of the buttons. | 208 // Whether the user clicked one of the buttons. |
| 209 bool action_taken_; | 209 bool action_taken_; |
| 210 | 210 |
| 211 DISALLOW_COPY_AND_ASSIGN(NotificationPermissionInfoBarDelegate); | 211 DISALLOW_COPY_AND_ASSIGN(NotificationPermissionInfoBarDelegate); |
| 212 }; | 212 }; |
| 213 | 213 |
| 214 DesktopNotificationService::DesktopNotificationService(Profile* profile, | 214 DesktopNotificationService::DesktopNotificationService(Profile* profile, |
| 215 NotificationUIManager* ui_manager) | 215 NotificationUIManager* ui_manager) |
| 216 : profile_(profile), | 216 : profile_(profile), |
| 217 ui_manager_(ui_manager) { | 217 ui_manager_(ui_manager) { |
| 218 registrar_.Init(profile_->GetPrefs()); | 218 prefs_registrar_.Init(profile_->GetPrefs()); |
| 219 InitPrefs(); | 219 InitPrefs(); |
| 220 StartObserving(); | 220 StartObserving(); |
| 221 } | 221 } |
| 222 | 222 |
| 223 DesktopNotificationService::~DesktopNotificationService() { | 223 DesktopNotificationService::~DesktopNotificationService() { |
| 224 StopObserving(); | 224 StopObserving(); |
| 225 } | 225 } |
| 226 | 226 |
| 227 void DesktopNotificationService::RegisterUserPrefs(PrefService* user_prefs) { | 227 void DesktopNotificationService::RegisterUserPrefs(PrefService* user_prefs) { |
| 228 if (!user_prefs->FindPreference( | 228 if (!user_prefs->FindPreference( |
| (...skipping 24 matching lines...) Expand all Loading... |
| 253 | 253 |
| 254 prefs_cache_ = new NotificationsPrefsCache(); | 254 prefs_cache_ = new NotificationsPrefsCache(); |
| 255 prefs_cache_->SetCacheDefaultContentSetting(default_content_setting); | 255 prefs_cache_->SetCacheDefaultContentSetting(default_content_setting); |
| 256 prefs_cache_->SetCacheAllowedOrigins(allowed_origins); | 256 prefs_cache_->SetCacheAllowedOrigins(allowed_origins); |
| 257 prefs_cache_->SetCacheDeniedOrigins(denied_origins); | 257 prefs_cache_->SetCacheDeniedOrigins(denied_origins); |
| 258 prefs_cache_->set_is_initialized(true); | 258 prefs_cache_->set_is_initialized(true); |
| 259 } | 259 } |
| 260 | 260 |
| 261 void DesktopNotificationService::StartObserving() { | 261 void DesktopNotificationService::StartObserving() { |
| 262 if (!profile_->IsOffTheRecord()) { | 262 if (!profile_->IsOffTheRecord()) { |
| 263 registrar_.Add(prefs::kDesktopNotificationDefaultContentSetting, this); | 263 prefs_registrar_.Add(prefs::kDesktopNotificationDefaultContentSetting, |
| 264 registrar_.Add(prefs::kDesktopNotificationAllowedOrigins, this); | 264 this); |
| 265 registrar_.Add(prefs::kDesktopNotificationDeniedOrigins, this); | 265 prefs_registrar_.Add(prefs::kDesktopNotificationAllowedOrigins, this); |
| 266 prefs_registrar_.Add(prefs::kDesktopNotificationDeniedOrigins, this); |
| 267 |
| 268 notification_registrar_.Add(this, NotificationType::EXTENSION_UNLOADED, |
| 269 NotificationService::AllSources()); |
| 266 } | 270 } |
| 271 |
| 272 notification_registrar_.Add(this, NotificationType::PROFILE_DESTROYED, |
| 273 Source<Profile>(profile_)); |
| 267 } | 274 } |
| 268 | 275 |
| 269 void DesktopNotificationService::StopObserving() { | 276 void DesktopNotificationService::StopObserving() { |
| 270 if (!profile_->IsOffTheRecord()) { | 277 if (!profile_->IsOffTheRecord()) { |
| 271 registrar_.RemoveAll(); | 278 prefs_registrar_.RemoveAll(); |
| 272 } | 279 } |
| 280 notification_registrar_.RemoveAll(); |
| 273 } | 281 } |
| 274 | 282 |
| 275 void DesktopNotificationService::GrantPermission(const GURL& origin) { | 283 void DesktopNotificationService::GrantPermission(const GURL& origin) { |
| 276 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 284 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 277 PersistPermissionChange(origin, true); | 285 PersistPermissionChange(origin, true); |
| 278 | 286 |
| 279 // Schedule a cache update on the IO thread. | 287 // Schedule a cache update on the IO thread. |
| 280 BrowserThread::PostTask( | 288 BrowserThread::PostTask( |
| 281 BrowserThread::IO, FROM_HERE, | 289 BrowserThread::IO, FROM_HERE, |
| 282 NewRunnableMethod( | 290 NewRunnableMethod( |
| 283 prefs_cache_.get(), &NotificationsPrefsCache::CacheAllowedOrigin, | 291 prefs_cache_.get(), &NotificationsPrefsCache::CacheAllowedOrigin, |
| 284 origin)); | 292 origin)); |
| 285 } | 293 } |
| 286 | 294 |
| 287 void DesktopNotificationService::DenyPermission(const GURL& origin) { | 295 void DesktopNotificationService::DenyPermission(const GURL& origin) { |
| 288 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 296 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 289 PersistPermissionChange(origin, false); | 297 PersistPermissionChange(origin, false); |
| 290 | 298 |
| 291 // Schedule a cache update on the IO thread. | 299 // Schedule a cache update on the IO thread. |
| 292 BrowserThread::PostTask( | 300 BrowserThread::PostTask( |
| 293 BrowserThread::IO, FROM_HERE, | 301 BrowserThread::IO, FROM_HERE, |
| 294 NewRunnableMethod( | 302 NewRunnableMethod( |
| 295 prefs_cache_.get(), &NotificationsPrefsCache::CacheDeniedOrigin, | 303 prefs_cache_.get(), &NotificationsPrefsCache::CacheDeniedOrigin, |
| 296 origin)); | 304 origin)); |
| 297 } | 305 } |
| 298 | 306 |
| 299 void DesktopNotificationService::Observe(NotificationType type, | 307 void DesktopNotificationService::Observe(NotificationType type, |
| 300 const NotificationSource& source, | 308 const NotificationSource& source, |
| 301 const NotificationDetails& details) { | 309 const NotificationDetails& details) { |
| 302 DCHECK(NotificationType::PREF_CHANGED == type); | 310 if (NotificationType::PREF_CHANGED == type) { |
| 311 const std::string& name = *Details<std::string>(details).ptr(); |
| 312 OnPrefsChanged(name); |
| 313 } else if (NotificationType::EXTENSION_UNLOADED == type) { |
| 314 // Remove all notifications currently shown or queued by the extension |
| 315 // which was unloaded. |
| 316 Extension* extension = Details<Extension>(details).ptr(); |
| 317 if (extension) |
| 318 ui_manager_->CancelAllBySourceOrigin(extension->url()); |
| 319 } else if (NotificationType::PROFILE_DESTROYED == type) { |
| 320 StopObserving(); |
| 321 } |
| 322 } |
| 323 |
| 324 void DesktopNotificationService::OnPrefsChanged(const std::string& pref_name) { |
| 303 PrefService* prefs = profile_->GetPrefs(); | 325 PrefService* prefs = profile_->GetPrefs(); |
| 304 const std::string& name = *Details<std::string>(details).ptr(); | |
| 305 | 326 |
| 306 if (name == prefs::kDesktopNotificationAllowedOrigins) { | 327 if (pref_name == prefs::kDesktopNotificationAllowedOrigins) { |
| 307 NotificationService::current()->Notify( | 328 NotificationService::current()->Notify( |
| 308 NotificationType::DESKTOP_NOTIFICATION_SETTINGS_CHANGED, | 329 NotificationType::DESKTOP_NOTIFICATION_SETTINGS_CHANGED, |
| 309 Source<DesktopNotificationService>(this), | 330 Source<DesktopNotificationService>(this), |
| 310 NotificationService::NoDetails()); | 331 NotificationService::NoDetails()); |
| 311 | 332 |
| 312 std::vector<GURL> allowed_origins(GetAllowedOrigins()); | 333 std::vector<GURL> allowed_origins(GetAllowedOrigins()); |
| 313 // Schedule a cache update on the IO thread. | 334 // Schedule a cache update on the IO thread. |
| 314 BrowserThread::PostTask( | 335 BrowserThread::PostTask( |
| 315 BrowserThread::IO, FROM_HERE, | 336 BrowserThread::IO, FROM_HERE, |
| 316 NewRunnableMethod( | 337 NewRunnableMethod( |
| 317 prefs_cache_.get(), | 338 prefs_cache_.get(), |
| 318 &NotificationsPrefsCache::SetCacheAllowedOrigins, | 339 &NotificationsPrefsCache::SetCacheAllowedOrigins, |
| 319 allowed_origins)); | 340 allowed_origins)); |
| 320 } else if (name == prefs::kDesktopNotificationDeniedOrigins) { | 341 } else if (pref_name == prefs::kDesktopNotificationDeniedOrigins) { |
| 321 NotificationService::current()->Notify( | 342 NotificationService::current()->Notify( |
| 322 NotificationType::DESKTOP_NOTIFICATION_SETTINGS_CHANGED, | 343 NotificationType::DESKTOP_NOTIFICATION_SETTINGS_CHANGED, |
| 323 Source<DesktopNotificationService>(this), | 344 Source<DesktopNotificationService>(this), |
| 324 NotificationService::NoDetails()); | 345 NotificationService::NoDetails()); |
| 325 | 346 |
| 326 std::vector<GURL> denied_origins(GetBlockedOrigins()); | 347 std::vector<GURL> denied_origins(GetBlockedOrigins()); |
| 327 // Schedule a cache update on the IO thread. | 348 // Schedule a cache update on the IO thread. |
| 328 BrowserThread::PostTask( | 349 BrowserThread::PostTask( |
| 329 BrowserThread::IO, FROM_HERE, | 350 BrowserThread::IO, FROM_HERE, |
| 330 NewRunnableMethod( | 351 NewRunnableMethod( |
| 331 prefs_cache_.get(), | 352 prefs_cache_.get(), |
| 332 &NotificationsPrefsCache::SetCacheDeniedOrigins, | 353 &NotificationsPrefsCache::SetCacheDeniedOrigins, |
| 333 denied_origins)); | 354 denied_origins)); |
| 334 } else if (name == prefs::kDesktopNotificationDefaultContentSetting) { | 355 } else if (pref_name == prefs::kDesktopNotificationDefaultContentSetting) { |
| 335 NotificationService::current()->Notify( | 356 NotificationService::current()->Notify( |
| 336 NotificationType::DESKTOP_NOTIFICATION_DEFAULT_CHANGED, | 357 NotificationType::DESKTOP_NOTIFICATION_DEFAULT_CHANGED, |
| 337 Source<DesktopNotificationService>(this), | 358 Source<DesktopNotificationService>(this), |
| 338 NotificationService::NoDetails()); | 359 NotificationService::NoDetails()); |
| 339 | 360 |
| 340 const ContentSetting default_content_setting = IntToContentSetting( | 361 const ContentSetting default_content_setting = IntToContentSetting( |
| 341 prefs->GetInteger(prefs::kDesktopNotificationDefaultContentSetting)); | 362 prefs->GetInteger(prefs::kDesktopNotificationDefaultContentSetting)); |
| 342 | 363 |
| 343 // Schedule a cache update on the IO thread. | 364 // Schedule a cache update on the IO thread. |
| 344 BrowserThread::PostTask( | 365 BrowserThread::PostTask( |
| (...skipping 211 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 556 void DesktopNotificationService::ShowNotification( | 577 void DesktopNotificationService::ShowNotification( |
| 557 const Notification& notification) { | 578 const Notification& notification) { |
| 558 ui_manager_->Add(notification, profile_); | 579 ui_manager_->Add(notification, profile_); |
| 559 } | 580 } |
| 560 | 581 |
| 561 bool DesktopNotificationService::CancelDesktopNotification( | 582 bool DesktopNotificationService::CancelDesktopNotification( |
| 562 int process_id, int route_id, int notification_id) { | 583 int process_id, int route_id, int notification_id) { |
| 563 scoped_refptr<NotificationObjectProxy> proxy( | 584 scoped_refptr<NotificationObjectProxy> proxy( |
| 564 new NotificationObjectProxy(process_id, route_id, notification_id, | 585 new NotificationObjectProxy(process_id, route_id, notification_id, |
| 565 false)); | 586 false)); |
| 566 // TODO(johnnyg): clean up this "empty" notification. | 587 return ui_manager_->CancelById(proxy->id()); |
| 567 Notification notif(GURL(), GURL(), string16(), string16(), proxy); | |
| 568 return ui_manager_->Cancel(notif); | |
| 569 } | 588 } |
| 570 | 589 |
| 571 | 590 |
| 572 bool DesktopNotificationService::ShowDesktopNotification( | 591 bool DesktopNotificationService::ShowDesktopNotification( |
| 573 const ViewHostMsg_ShowNotification_Params& params, | 592 const ViewHostMsg_ShowNotification_Params& params, |
| 574 int process_id, int route_id, DesktopNotificationSource source) { | 593 int process_id, int route_id, DesktopNotificationSource source) { |
| 575 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 594 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 576 const GURL& origin = params.origin; | 595 const GURL& origin = params.origin; |
| 577 NotificationObjectProxy* proxy = | 596 NotificationObjectProxy* proxy = |
| 578 new NotificationObjectProxy(process_id, route_id, | 597 new NotificationObjectProxy(process_id, route_id, |
| (...skipping 21 matching lines...) Expand all Loading... |
| 600 if (origin.SchemeIs(chrome::kExtensionScheme)) { | 619 if (origin.SchemeIs(chrome::kExtensionScheme)) { |
| 601 ExtensionsService* ext_service = profile_->GetExtensionsService(); | 620 ExtensionsService* ext_service = profile_->GetExtensionsService(); |
| 602 if (ext_service) { | 621 if (ext_service) { |
| 603 const Extension* extension = ext_service->GetExtensionByURL(origin); | 622 const Extension* extension = ext_service->GetExtensionByURL(origin); |
| 604 if (extension) | 623 if (extension) |
| 605 return UTF8ToUTF16(extension->name()); | 624 return UTF8ToUTF16(extension->name()); |
| 606 } | 625 } |
| 607 } | 626 } |
| 608 return UTF8ToUTF16(origin.host()); | 627 return UTF8ToUTF16(origin.host()); |
| 609 } | 628 } |
| OLD | NEW |