| 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 176 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 187 bool action_taken_; | 187 bool action_taken_; |
| 188 | 188 |
| 189 DISALLOW_COPY_AND_ASSIGN(NotificationPermissionInfoBarDelegate); | 189 DISALLOW_COPY_AND_ASSIGN(NotificationPermissionInfoBarDelegate); |
| 190 }; | 190 }; |
| 191 | 191 |
| 192 DesktopNotificationService::DesktopNotificationService(Profile* profile, | 192 DesktopNotificationService::DesktopNotificationService(Profile* profile, |
| 193 NotificationUIManager* ui_manager) | 193 NotificationUIManager* ui_manager) |
| 194 : profile_(profile), | 194 : profile_(profile), |
| 195 ui_manager_(ui_manager) { | 195 ui_manager_(ui_manager) { |
| 196 InitPrefs(); | 196 InitPrefs(); |
| 197 | |
| 198 // Listen for new extension installations so we can cache permissions. | |
| 199 notification_registrar_.Add(this, | |
| 200 NotificationType::EXTENSION_LOADED, | |
| 201 Source<Profile>(profile_)); | |
| 202 } | 197 } |
| 203 | 198 |
| 204 DesktopNotificationService::~DesktopNotificationService() { | 199 DesktopNotificationService::~DesktopNotificationService() { |
| 205 } | 200 } |
| 206 | 201 |
| 207 // Initialize the cache with the allowed and denied origins, or | 202 // Initialize the cache with the allowed and denied origins, or |
| 208 // create the preferences if they don't exist yet. | 203 // create the preferences if they don't exist yet. |
| 209 void DesktopNotificationService::InitPrefs() { | 204 void DesktopNotificationService::InitPrefs() { |
| 210 PrefService* prefs = profile_->GetPrefs(); | 205 PrefService* prefs = profile_->GetPrefs(); |
| 211 const ListValue* allowed_sites = NULL; | 206 const ListValue* allowed_sites = NULL; |
| 212 const ListValue* denied_sites = NULL; | 207 const ListValue* denied_sites = NULL; |
| 213 | 208 |
| 214 if (!profile_->IsOffTheRecord()) { | 209 if (!profile_->IsOffTheRecord()) { |
| 215 if (!prefs->FindPreference(prefs::kDesktopNotificationAllowedOrigins)) | 210 if (!prefs->FindPreference(prefs::kDesktopNotificationAllowedOrigins)) |
| 216 prefs->RegisterListPref(prefs::kDesktopNotificationAllowedOrigins); | 211 prefs->RegisterListPref(prefs::kDesktopNotificationAllowedOrigins); |
| 217 allowed_sites = prefs->GetList(prefs::kDesktopNotificationAllowedOrigins); | 212 allowed_sites = prefs->GetList(prefs::kDesktopNotificationAllowedOrigins); |
| 218 | 213 |
| 219 if (!prefs->FindPreference(prefs::kDesktopNotificationDeniedOrigins)) | 214 if (!prefs->FindPreference(prefs::kDesktopNotificationDeniedOrigins)) |
| 220 prefs->RegisterListPref(prefs::kDesktopNotificationDeniedOrigins); | 215 prefs->RegisterListPref(prefs::kDesktopNotificationDeniedOrigins); |
| 221 denied_sites = prefs->GetList(prefs::kDesktopNotificationDeniedOrigins); | 216 denied_sites = prefs->GetList(prefs::kDesktopNotificationDeniedOrigins); |
| 222 } | 217 } |
| 223 | 218 |
| 224 prefs_cache_ = new NotificationsPrefsCache(allowed_sites, denied_sites); | 219 prefs_cache_ = new NotificationsPrefsCache(allowed_sites, denied_sites); |
| 225 | |
| 226 ExtensionsService* ext_service = profile_->GetExtensionsService(); | |
| 227 if (ext_service) { | |
| 228 const ExtensionList* extensions = ext_service->extensions(); | |
| 229 for (ExtensionList::const_iterator iter = extensions->begin(); | |
| 230 iter != extensions->end(); ++iter) { | |
| 231 if ((*iter)->HasApiPermission(Extension::kNotificationPermission)) { | |
| 232 prefs_cache_->CacheAllowedOrigin((*iter)->url()); | |
| 233 } | |
| 234 } | |
| 235 } | |
| 236 | |
| 237 prefs_cache_->set_is_initialized(true); | 220 prefs_cache_->set_is_initialized(true); |
| 238 } | 221 } |
| 239 | 222 |
| 240 void DesktopNotificationService::Observe(NotificationType type, | |
| 241 const NotificationSource& source, | |
| 242 const NotificationDetails& details) { | |
| 243 if (type != NotificationType::EXTENSION_LOADED) { | |
| 244 NOTREACHED(); | |
| 245 return; | |
| 246 } | |
| 247 | |
| 248 Details<Extension> extension = static_cast<Details<Extension> >(details); | |
| 249 if (extension->HasApiPermission(Extension::kNotificationPermission)) { | |
| 250 GrantPermission(extension->url()); | |
| 251 } | |
| 252 } | |
| 253 | |
| 254 void DesktopNotificationService::GrantPermission(const GURL& origin) { | 223 void DesktopNotificationService::GrantPermission(const GURL& origin) { |
| 255 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); | 224 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); |
| 256 PersistPermissionChange(origin, true); | 225 PersistPermissionChange(origin, true); |
| 257 | 226 |
| 258 // Schedule a cache update on the IO thread. | 227 // Schedule a cache update on the IO thread. |
| 259 ChromeThread::PostTask( | 228 ChromeThread::PostTask( |
| 260 ChromeThread::IO, FROM_HERE, | 229 ChromeThread::IO, FROM_HERE, |
| 261 NewRunnableMethod( | 230 NewRunnableMethod( |
| 262 prefs_cache_.get(), &NotificationsPrefsCache::CacheAllowedOrigin, | 231 prefs_cache_.get(), &NotificationsPrefsCache::CacheAllowedOrigin, |
| 263 origin)); | 232 origin)); |
| (...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 368 if (origin.SchemeIs(chrome::kExtensionScheme)) { | 337 if (origin.SchemeIs(chrome::kExtensionScheme)) { |
| 369 ExtensionsService* ext_service = profile_->GetExtensionsService(); | 338 ExtensionsService* ext_service = profile_->GetExtensionsService(); |
| 370 if (ext_service) { | 339 if (ext_service) { |
| 371 Extension* extension = ext_service->GetExtensionByURL(origin); | 340 Extension* extension = ext_service->GetExtensionByURL(origin); |
| 372 if (extension) | 341 if (extension) |
| 373 return ASCIIToWide(extension->name()); | 342 return ASCIIToWide(extension->name()); |
| 374 } | 343 } |
| 375 } | 344 } |
| 376 return UTF8ToWide(origin.spec()); | 345 return UTF8ToWide(origin.spec()); |
| 377 } | 346 } |
| OLD | NEW |