Chromium Code Reviews| 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 19 matching lines...) Expand all Loading... | |
| 30 #include "chrome/common/url_constants.h" | 30 #include "chrome/common/url_constants.h" |
| 31 #include "grit/browser_resources.h" | 31 #include "grit/browser_resources.h" |
| 32 #include "grit/chromium_strings.h" | 32 #include "grit/chromium_strings.h" |
| 33 #include "grit/generated_resources.h" | 33 #include "grit/generated_resources.h" |
| 34 #include "grit/theme_resources.h" | 34 #include "grit/theme_resources.h" |
| 35 #include "net/base/escape.h" | 35 #include "net/base/escape.h" |
| 36 #include "third_party/WebKit/WebKit/chromium/public/WebNotificationPresenter.h" | 36 #include "third_party/WebKit/WebKit/chromium/public/WebNotificationPresenter.h" |
| 37 | 37 |
| 38 using WebKit::WebNotificationPresenter; | 38 using WebKit::WebNotificationPresenter; |
| 39 | 39 |
| 40 const ContentSetting kDefaultSetting = CONTENT_SETTING_ASK; | |
|
dhollowa
2010/07/03 02:12:01
Please wrap in anonymous namespace.
Nico
2010/07/03 02:18:29
Not necessary, consts implicitly have internal lin
| |
| 41 | |
| 40 // static | 42 // static |
| 41 string16 DesktopNotificationService::CreateDataUrl( | 43 string16 DesktopNotificationService::CreateDataUrl( |
| 42 const GURL& icon_url, const string16& title, const string16& body) { | 44 const GURL& icon_url, const string16& title, const string16& body) { |
| 43 int resource; | 45 int resource; |
| 44 string16 line_name; | 46 string16 line_name; |
| 45 string16 line; | 47 string16 line; |
| 46 std::vector<std::string> subst; | 48 std::vector<std::string> subst; |
| 47 if (icon_url.is_valid()) { | 49 if (icon_url.is_valid()) { |
| 48 resource = IDR_NOTIFICATION_ICON_HTML; | 50 resource = IDR_NOTIFICATION_ICON_HTML; |
| 49 subst.push_back(icon_url.spec()); | 51 subst.push_back(icon_url.spec()); |
| (...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 196 ui_manager_(ui_manager) { | 198 ui_manager_(ui_manager) { |
| 197 InitPrefs(); | 199 InitPrefs(); |
| 198 StartObserving(); | 200 StartObserving(); |
| 199 } | 201 } |
| 200 | 202 |
| 201 DesktopNotificationService::~DesktopNotificationService() { | 203 DesktopNotificationService::~DesktopNotificationService() { |
| 202 StopObserving(); | 204 StopObserving(); |
| 203 } | 205 } |
| 204 | 206 |
| 205 void DesktopNotificationService::RegisterUserPrefs(PrefService* user_prefs) { | 207 void DesktopNotificationService::RegisterUserPrefs(PrefService* user_prefs) { |
| 208 if (!user_prefs->FindPreference( | |
| 209 prefs::kDesktopNotificationDefaultContentSetting)) { | |
| 210 user_prefs->RegisterIntegerPref( | |
| 211 prefs::kDesktopNotificationDefaultContentSetting, kDefaultSetting); | |
| 212 } | |
| 206 if (!user_prefs->FindPreference(prefs::kDesktopNotificationAllowedOrigins)) | 213 if (!user_prefs->FindPreference(prefs::kDesktopNotificationAllowedOrigins)) |
| 207 user_prefs->RegisterListPref(prefs::kDesktopNotificationAllowedOrigins); | 214 user_prefs->RegisterListPref(prefs::kDesktopNotificationAllowedOrigins); |
| 208 if (!user_prefs->FindPreference(prefs::kDesktopNotificationDeniedOrigins)) | 215 if (!user_prefs->FindPreference(prefs::kDesktopNotificationDeniedOrigins)) |
| 209 user_prefs->RegisterListPref(prefs::kDesktopNotificationDeniedOrigins); | 216 user_prefs->RegisterListPref(prefs::kDesktopNotificationDeniedOrigins); |
| 210 } | 217 } |
| 211 | 218 |
| 212 // Initialize the cache with the allowed and denied origins, or | 219 // Initialize the cache with the allowed and denied origins, or |
| 213 // create the preferences if they don't exist yet. | 220 // create the preferences if they don't exist yet. |
| 214 void DesktopNotificationService::InitPrefs() { | 221 void DesktopNotificationService::InitPrefs() { |
| 215 PrefService* prefs = profile_->GetPrefs(); | 222 PrefService* prefs = profile_->GetPrefs(); |
| 216 std::vector<GURL> allowed_origins; | 223 std::vector<GURL> allowed_origins; |
| 217 std::vector<GURL> denied_origins; | 224 std::vector<GURL> denied_origins; |
| 225 ContentSetting default_content_setting = CONTENT_SETTING_DEFAULT; | |
| 218 | 226 |
| 219 if (!profile_->IsOffTheRecord()) { | 227 if (!profile_->IsOffTheRecord()) { |
| 228 default_content_setting = IntToContentSetting( | |
| 229 prefs->GetInteger(prefs::kDesktopNotificationDefaultContentSetting)); | |
| 230 | |
| 220 const ListValue* allowed_sites = | 231 const ListValue* allowed_sites = |
| 221 prefs->GetList(prefs::kDesktopNotificationAllowedOrigins); | 232 prefs->GetList(prefs::kDesktopNotificationAllowedOrigins); |
| 222 if (allowed_sites) | 233 if (allowed_sites) |
| 223 NotificationsPrefsCache::ListValueToGurlVector(*allowed_sites, | 234 NotificationsPrefsCache::ListValueToGurlVector(*allowed_sites, |
| 224 &allowed_origins); | 235 &allowed_origins); |
| 225 | 236 |
| 226 const ListValue* denied_sites = | 237 const ListValue* denied_sites = |
| 227 prefs->GetList(prefs::kDesktopNotificationDeniedOrigins); | 238 prefs->GetList(prefs::kDesktopNotificationDeniedOrigins); |
| 228 if (denied_sites) | 239 if (denied_sites) |
| 229 NotificationsPrefsCache::ListValueToGurlVector(*denied_sites, | 240 NotificationsPrefsCache::ListValueToGurlVector(*denied_sites, |
| 230 &denied_origins); | 241 &denied_origins); |
| 231 } | 242 } |
| 232 | 243 |
| 233 prefs_cache_ = new NotificationsPrefsCache(); | 244 prefs_cache_ = new NotificationsPrefsCache(); |
| 245 prefs_cache_->SetCacheDefaultContentSetting(default_content_setting); | |
| 234 prefs_cache_->SetCacheAllowedOrigins(allowed_origins); | 246 prefs_cache_->SetCacheAllowedOrigins(allowed_origins); |
| 235 prefs_cache_->SetCacheDeniedOrigins(denied_origins); | 247 prefs_cache_->SetCacheDeniedOrigins(denied_origins); |
| 236 prefs_cache_->set_is_initialized(true); | 248 prefs_cache_->set_is_initialized(true); |
| 237 } | 249 } |
| 238 | 250 |
| 239 void DesktopNotificationService::StartObserving() { | 251 void DesktopNotificationService::StartObserving() { |
| 240 if (!profile_->IsOffTheRecord()) { | 252 if (!profile_->IsOffTheRecord()) { |
| 241 PrefService* prefs = profile_->GetPrefs(); | 253 PrefService* prefs = profile_->GetPrefs(); |
| 254 prefs->AddPrefObserver(prefs::kDesktopNotificationDefaultContentSetting, | |
| 255 this); | |
| 242 prefs->AddPrefObserver(prefs::kDesktopNotificationAllowedOrigins, this); | 256 prefs->AddPrefObserver(prefs::kDesktopNotificationAllowedOrigins, this); |
| 243 prefs->AddPrefObserver(prefs::kDesktopNotificationDeniedOrigins, this); | 257 prefs->AddPrefObserver(prefs::kDesktopNotificationDeniedOrigins, this); |
| 244 } | 258 } |
| 245 } | 259 } |
| 246 | 260 |
| 247 void DesktopNotificationService::StopObserving() { | 261 void DesktopNotificationService::StopObserving() { |
| 248 if (!profile_->IsOffTheRecord()) { | 262 if (!profile_->IsOffTheRecord()) { |
| 249 PrefService* prefs = profile_->GetPrefs(); | 263 PrefService* prefs = profile_->GetPrefs(); |
| 264 prefs->RemovePrefObserver(prefs::kDesktopNotificationDefaultContentSetting, | |
| 265 this); | |
| 250 prefs->RemovePrefObserver(prefs::kDesktopNotificationAllowedOrigins, this); | 266 prefs->RemovePrefObserver(prefs::kDesktopNotificationAllowedOrigins, this); |
| 251 prefs->RemovePrefObserver(prefs::kDesktopNotificationDeniedOrigins, this); | 267 prefs->RemovePrefObserver(prefs::kDesktopNotificationDeniedOrigins, this); |
| 252 } | 268 } |
| 253 } | 269 } |
| 254 | 270 |
| 255 void DesktopNotificationService::GrantPermission(const GURL& origin) { | 271 void DesktopNotificationService::GrantPermission(const GURL& origin) { |
| 256 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); | 272 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); |
| 257 PersistPermissionChange(origin, true); | 273 PersistPermissionChange(origin, true); |
| 258 | 274 |
| 259 // Schedule a cache update on the IO thread. | 275 // Schedule a cache update on the IO thread. |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 306 NotificationsPrefsCache::ListValueToGurlVector(*denied_sites, | 322 NotificationsPrefsCache::ListValueToGurlVector(*denied_sites, |
| 307 &denied_origins); | 323 &denied_origins); |
| 308 } | 324 } |
| 309 // Schedule a cache update on the IO thread. | 325 // Schedule a cache update on the IO thread. |
| 310 ChromeThread::PostTask( | 326 ChromeThread::PostTask( |
| 311 ChromeThread::IO, FROM_HERE, | 327 ChromeThread::IO, FROM_HERE, |
| 312 NewRunnableMethod( | 328 NewRunnableMethod( |
| 313 prefs_cache_.get(), | 329 prefs_cache_.get(), |
| 314 &NotificationsPrefsCache::SetCacheDeniedOrigins, | 330 &NotificationsPrefsCache::SetCacheDeniedOrigins, |
| 315 denied_origins)); | 331 denied_origins)); |
| 332 } else if (0 == name->compare( | |
| 333 prefs::kDesktopNotificationDefaultContentSetting)) { | |
| 334 const ContentSetting default_content_setting = IntToContentSetting( | |
| 335 prefs->GetInteger(prefs::kDesktopNotificationDefaultContentSetting)); | |
| 336 | |
| 337 // Schedule a cache update on the IO thread. | |
| 338 ChromeThread::PostTask( | |
| 339 ChromeThread::IO, FROM_HERE, | |
| 340 NewRunnableMethod( | |
| 341 prefs_cache_.get(), | |
| 342 &NotificationsPrefsCache::SetCacheDefaultContentSetting, | |
| 343 default_content_setting)); | |
| 316 } | 344 } |
| 317 } | 345 } |
| 318 | 346 |
| 319 void DesktopNotificationService::PersistPermissionChange( | 347 void DesktopNotificationService::PersistPermissionChange( |
| 320 const GURL& origin, bool is_allowed) { | 348 const GURL& origin, bool is_allowed) { |
| 321 // Don't persist changes when off the record. | 349 // Don't persist changes when off the record. |
| 322 if (profile_->IsOffTheRecord()) | 350 if (profile_->IsOffTheRecord()) |
| 323 return; | 351 return; |
| 324 | 352 |
| 325 PrefService* prefs = profile_->GetPrefs(); | 353 PrefService* prefs = profile_->GetPrefs(); |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 370 } | 398 } |
| 371 if (denied_changed) { | 399 if (denied_changed) { |
| 372 ScopedPrefUpdate updateDenied( | 400 ScopedPrefUpdate updateDenied( |
| 373 prefs, prefs::kDesktopNotificationDeniedOrigins); | 401 prefs, prefs::kDesktopNotificationDeniedOrigins); |
| 374 } | 402 } |
| 375 prefs->ScheduleSavePersistentPrefs(); | 403 prefs->ScheduleSavePersistentPrefs(); |
| 376 } | 404 } |
| 377 StartObserving(); | 405 StartObserving(); |
| 378 } | 406 } |
| 379 | 407 |
| 408 ContentSetting DesktopNotificationService::GetDefaultContentSetting() { | |
| 409 PrefService* prefs = profile_->GetPrefs(); | |
| 410 ContentSetting setting = IntToContentSetting( | |
| 411 prefs->GetInteger(prefs::kDesktopNotificationDefaultContentSetting)); | |
| 412 if (setting == CONTENT_SETTING_DEFAULT) | |
| 413 setting = kDefaultSetting; | |
| 414 return setting; | |
| 415 } | |
| 416 | |
| 417 void DesktopNotificationService::SetDefaultContentSetting( | |
| 418 ContentSetting setting) { | |
| 419 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); | |
| 420 profile_->GetPrefs()->SetInteger( | |
| 421 prefs::kDesktopNotificationDefaultContentSetting, | |
| 422 setting == CONTENT_SETTING_DEFAULT ? kDefaultSetting : setting); | |
|
dhollowa
2010/07/03 02:12:01
There's an asymmetry here with respect to |GetDefa
Nico
2010/07/03 02:18:29
Yes, CONTENT_SETTING_DEFAULT should never be store
Nico
2010/07/03 02:33:18
(This mirrors SetDefaultContentSetting() and ReadD
| |
| 423 // The cache is updated through the notification observer. | |
| 424 } | |
| 425 | |
| 380 void DesktopNotificationService::RequestPermission( | 426 void DesktopNotificationService::RequestPermission( |
| 381 const GURL& origin, int process_id, int route_id, int callback_context, | 427 const GURL& origin, int process_id, int route_id, int callback_context, |
| 382 TabContents* tab) { | 428 TabContents* tab) { |
| 383 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); | 429 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); |
| 384 if (!tab) | 430 if (!tab) |
| 385 return; | 431 return; |
| 386 // Show an info bar requesting permission. | 432 // Show an info bar requesting permission. |
| 387 std::wstring display_name = DisplayNameForOrigin(origin); | 433 std::wstring display_name = DisplayNameForOrigin(origin); |
| 388 | 434 |
| 389 tab->AddInfoBar(new NotificationPermissionInfoBarDelegate( | 435 tab->AddInfoBar(new NotificationPermissionInfoBarDelegate( |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 441 if (origin.SchemeIs(chrome::kExtensionScheme)) { | 487 if (origin.SchemeIs(chrome::kExtensionScheme)) { |
| 442 ExtensionsService* ext_service = profile_->GetExtensionsService(); | 488 ExtensionsService* ext_service = profile_->GetExtensionsService(); |
| 443 if (ext_service) { | 489 if (ext_service) { |
| 444 Extension* extension = ext_service->GetExtensionByURL(origin); | 490 Extension* extension = ext_service->GetExtensionByURL(origin); |
| 445 if (extension) | 491 if (extension) |
| 446 return UTF8ToWide(extension->name()); | 492 return UTF8ToWide(extension->name()); |
| 447 } | 493 } |
| 448 } | 494 } |
| 449 return UTF8ToWide(origin.host()); | 495 return UTF8ToWide(origin.host()); |
| 450 } | 496 } |
| OLD | NEW |