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

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

Issue 4635007: When an extension is uninstalled, close all desktop notifications from that e... (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: '' Created 10 years, 1 month 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 | Annotate | Revision Log
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/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
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
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 }
267 } 271 }
268 272
269 void DesktopNotificationService::StopObserving() { 273 void DesktopNotificationService::StopObserving() {
270 if (!profile_->IsOffTheRecord()) { 274 if (!profile_->IsOffTheRecord()) {
271 registrar_.RemoveAll(); 275 prefs_registrar_.RemoveAll();
276 notification_registrar_.RemoveAll();
272 } 277 }
273 } 278 }
274 279
275 void DesktopNotificationService::GrantPermission(const GURL& origin) { 280 void DesktopNotificationService::GrantPermission(const GURL& origin) {
276 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 281 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
277 PersistPermissionChange(origin, true); 282 PersistPermissionChange(origin, true);
278 283
279 // Schedule a cache update on the IO thread. 284 // Schedule a cache update on the IO thread.
280 BrowserThread::PostTask( 285 BrowserThread::PostTask(
281 BrowserThread::IO, FROM_HERE, 286 BrowserThread::IO, FROM_HERE,
(...skipping 10 matching lines...) Expand all
292 BrowserThread::PostTask( 297 BrowserThread::PostTask(
293 BrowserThread::IO, FROM_HERE, 298 BrowserThread::IO, FROM_HERE,
294 NewRunnableMethod( 299 NewRunnableMethod(
295 prefs_cache_.get(), &NotificationsPrefsCache::CacheDeniedOrigin, 300 prefs_cache_.get(), &NotificationsPrefsCache::CacheDeniedOrigin,
296 origin)); 301 origin));
297 } 302 }
298 303
299 void DesktopNotificationService::Observe(NotificationType type, 304 void DesktopNotificationService::Observe(NotificationType type,
300 const NotificationSource& source, 305 const NotificationSource& source,
301 const NotificationDetails& details) { 306 const NotificationDetails& details) {
302 DCHECK(NotificationType::PREF_CHANGED == type); 307 if (NotificationType::PREF_CHANGED == type) {
308 const std::string& name = *Details<std::string>(details).ptr();
309 OnPrefsChanged(name);
310 } else if (NotificationType::EXTENSION_UNLOADED == type) {
311 // Remove all notifications currently shown or queued by the extension
312 // which was unloaded.
313 Extension* extension = Details<Extension>(details).ptr();
314 if (extension)
315 ui_manager_->CancelAllBySourceOrigin(extension->url());
316 }
317 }
318
319 void DesktopNotificationService::OnPrefsChanged(const std::string& pref_name) {
303 PrefService* prefs = profile_->GetPrefs(); 320 PrefService* prefs = profile_->GetPrefs();
304 const std::string& name = *Details<std::string>(details).ptr();
305 321
306 if (name == prefs::kDesktopNotificationAllowedOrigins) { 322 if (pref_name == prefs::kDesktopNotificationAllowedOrigins) {
307 NotificationService::current()->Notify( 323 NotificationService::current()->Notify(
308 NotificationType::DESKTOP_NOTIFICATION_SETTINGS_CHANGED, 324 NotificationType::DESKTOP_NOTIFICATION_SETTINGS_CHANGED,
309 Source<DesktopNotificationService>(this), 325 Source<DesktopNotificationService>(this),
310 NotificationService::NoDetails()); 326 NotificationService::NoDetails());
311 327
312 std::vector<GURL> allowed_origins(GetAllowedOrigins()); 328 std::vector<GURL> allowed_origins(GetAllowedOrigins());
313 // Schedule a cache update on the IO thread. 329 // Schedule a cache update on the IO thread.
314 BrowserThread::PostTask( 330 BrowserThread::PostTask(
315 BrowserThread::IO, FROM_HERE, 331 BrowserThread::IO, FROM_HERE,
316 NewRunnableMethod( 332 NewRunnableMethod(
317 prefs_cache_.get(), 333 prefs_cache_.get(),
318 &NotificationsPrefsCache::SetCacheAllowedOrigins, 334 &NotificationsPrefsCache::SetCacheAllowedOrigins,
319 allowed_origins)); 335 allowed_origins));
320 } else if (name == prefs::kDesktopNotificationDeniedOrigins) { 336 } else if (pref_name == prefs::kDesktopNotificationDeniedOrigins) {
321 NotificationService::current()->Notify( 337 NotificationService::current()->Notify(
322 NotificationType::DESKTOP_NOTIFICATION_SETTINGS_CHANGED, 338 NotificationType::DESKTOP_NOTIFICATION_SETTINGS_CHANGED,
323 Source<DesktopNotificationService>(this), 339 Source<DesktopNotificationService>(this),
324 NotificationService::NoDetails()); 340 NotificationService::NoDetails());
325 341
326 std::vector<GURL> denied_origins(GetBlockedOrigins()); 342 std::vector<GURL> denied_origins(GetBlockedOrigins());
327 // Schedule a cache update on the IO thread. 343 // Schedule a cache update on the IO thread.
328 BrowserThread::PostTask( 344 BrowserThread::PostTask(
329 BrowserThread::IO, FROM_HERE, 345 BrowserThread::IO, FROM_HERE,
330 NewRunnableMethod( 346 NewRunnableMethod(
331 prefs_cache_.get(), 347 prefs_cache_.get(),
332 &NotificationsPrefsCache::SetCacheDeniedOrigins, 348 &NotificationsPrefsCache::SetCacheDeniedOrigins,
333 denied_origins)); 349 denied_origins));
334 } else if (name == prefs::kDesktopNotificationDefaultContentSetting) { 350 } else if (pref_name == prefs::kDesktopNotificationDefaultContentSetting) {
335 NotificationService::current()->Notify( 351 NotificationService::current()->Notify(
336 NotificationType::DESKTOP_NOTIFICATION_DEFAULT_CHANGED, 352 NotificationType::DESKTOP_NOTIFICATION_DEFAULT_CHANGED,
337 Source<DesktopNotificationService>(this), 353 Source<DesktopNotificationService>(this),
338 NotificationService::NoDetails()); 354 NotificationService::NoDetails());
339 355
340 const ContentSetting default_content_setting = IntToContentSetting( 356 const ContentSetting default_content_setting = IntToContentSetting(
341 prefs->GetInteger(prefs::kDesktopNotificationDefaultContentSetting)); 357 prefs->GetInteger(prefs::kDesktopNotificationDefaultContentSetting));
342 358
343 // Schedule a cache update on the IO thread. 359 // Schedule a cache update on the IO thread.
344 BrowserThread::PostTask( 360 BrowserThread::PostTask(
(...skipping 211 matching lines...) Expand 10 before | Expand all | Expand 10 after
556 void DesktopNotificationService::ShowNotification( 572 void DesktopNotificationService::ShowNotification(
557 const Notification& notification) { 573 const Notification& notification) {
558 ui_manager_->Add(notification, profile_); 574 ui_manager_->Add(notification, profile_);
559 } 575 }
560 576
561 bool DesktopNotificationService::CancelDesktopNotification( 577 bool DesktopNotificationService::CancelDesktopNotification(
562 int process_id, int route_id, int notification_id) { 578 int process_id, int route_id, int notification_id) {
563 scoped_refptr<NotificationObjectProxy> proxy( 579 scoped_refptr<NotificationObjectProxy> proxy(
564 new NotificationObjectProxy(process_id, route_id, notification_id, 580 new NotificationObjectProxy(process_id, route_id, notification_id,
565 false)); 581 false));
566 // TODO(johnnyg): clean up this "empty" notification. 582 return ui_manager_->CancelById(proxy->id());
567 Notification notif(GURL(), GURL(), string16(), string16(), proxy);
568 return ui_manager_->Cancel(notif);
569 } 583 }
570 584
571 585
572 bool DesktopNotificationService::ShowDesktopNotification( 586 bool DesktopNotificationService::ShowDesktopNotification(
573 const ViewHostMsg_ShowNotification_Params& params, 587 const ViewHostMsg_ShowNotification_Params& params,
574 int process_id, int route_id, DesktopNotificationSource source) { 588 int process_id, int route_id, DesktopNotificationSource source) {
575 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 589 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
576 const GURL& origin = params.origin; 590 const GURL& origin = params.origin;
577 NotificationObjectProxy* proxy = 591 NotificationObjectProxy* proxy =
578 new NotificationObjectProxy(process_id, route_id, 592 new NotificationObjectProxy(process_id, route_id,
(...skipping 21 matching lines...) Expand all
600 if (origin.SchemeIs(chrome::kExtensionScheme)) { 614 if (origin.SchemeIs(chrome::kExtensionScheme)) {
601 ExtensionsService* ext_service = profile_->GetExtensionsService(); 615 ExtensionsService* ext_service = profile_->GetExtensionsService();
602 if (ext_service) { 616 if (ext_service) {
603 const Extension* extension = ext_service->GetExtensionByURL(origin); 617 const Extension* extension = ext_service->GetExtensionByURL(origin);
604 if (extension) 618 if (extension)
605 return UTF8ToUTF16(extension->name()); 619 return UTF8ToUTF16(extension->name());
606 } 620 }
607 } 621 }
608 return UTF8ToUTF16(origin.host()); 622 return UTF8ToUTF16(origin.host());
609 } 623 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698