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 3 // found in the LICENSE file. 4 5 #include "chrome/browser/notifications/notifications_prefs_cache.h" 6 7 #include <string> 8 9 #include "base/string_util.h" 10 #include "base/values.h" 11 #include "base/utf_string_conversions.h" 12 #include "content/browser/browser_thread.h" 13 #include "third_party/WebKit/Source/WebKit/chromium/public/WebNotificationPresenter.h" 14 NotificationsPrefsCache()15NotificationsPrefsCache::NotificationsPrefsCache() 16 : default_content_setting_(CONTENT_SETTING_DEFAULT), 17 is_initialized_(false) { 18 } 19 CacheAllowedOrigin(const GURL & origin)20void NotificationsPrefsCache::CacheAllowedOrigin( 21 const GURL& origin) { 22 CheckThreadAccess(); 23 std::set<GURL>::iterator iter; 24 allowed_origins_.insert(origin); 25 if ((iter = denied_origins_.find(origin)) != denied_origins_.end()) 26 denied_origins_.erase(iter); 27 } 28 CacheDeniedOrigin(const GURL & origin)29void NotificationsPrefsCache::CacheDeniedOrigin( 30 const GURL& origin) { 31 CheckThreadAccess(); 32 std::set<GURL>::iterator iter; 33 denied_origins_.insert(origin); 34 if ((iter = allowed_origins_.find(origin)) != allowed_origins_.end()) 35 allowed_origins_.erase(iter); 36 } 37 SetCacheAllowedOrigins(const std::vector<GURL> & allowed)38void NotificationsPrefsCache::SetCacheAllowedOrigins( 39 const std::vector<GURL>& allowed) { 40 allowed_origins_.clear(); 41 allowed_origins_.insert(allowed.begin(), allowed.end()); 42 } 43 SetCacheDeniedOrigins(const std::vector<GURL> & denied)44void NotificationsPrefsCache::SetCacheDeniedOrigins( 45 const std::vector<GURL>& denied) { 46 denied_origins_.clear(); 47 denied_origins_.insert(denied.begin(), denied.end()); 48 } 49 SetCacheDefaultContentSetting(ContentSetting setting)50void NotificationsPrefsCache::SetCacheDefaultContentSetting( 51 ContentSetting setting) { 52 default_content_setting_ = setting; 53 } 54 55 // static ListValueToGurlVector(const ListValue & origin_list,std::vector<GURL> * origin_vector)56void NotificationsPrefsCache::ListValueToGurlVector( 57 const ListValue& origin_list, 58 std::vector<GURL>* origin_vector) { 59 ListValue::const_iterator i; 60 std::string origin; 61 for (i = origin_list.begin(); i != origin_list.end(); ++i) { 62 (*i)->GetAsString(&origin); 63 origin_vector->push_back(GURL(origin)); 64 } 65 } 66 HasPermission(const GURL & origin)67int NotificationsPrefsCache::HasPermission(const GURL& origin) { 68 if (IsOriginAllowed(origin)) 69 return WebKit::WebNotificationPresenter::PermissionAllowed; 70 if (IsOriginDenied(origin)) 71 return WebKit::WebNotificationPresenter::PermissionDenied; 72 switch (default_content_setting_) { 73 case CONTENT_SETTING_ALLOW: 74 return WebKit::WebNotificationPresenter::PermissionAllowed; 75 case CONTENT_SETTING_BLOCK: 76 return WebKit::WebNotificationPresenter::PermissionDenied; 77 case CONTENT_SETTING_ASK: 78 case CONTENT_SETTING_DEFAULT: 79 default: // Make gcc happy. 80 return WebKit::WebNotificationPresenter::PermissionNotAllowed; 81 } 82 } 83 ~NotificationsPrefsCache()84NotificationsPrefsCache::~NotificationsPrefsCache() {} 85 IsOriginAllowed(const GURL & origin)86bool NotificationsPrefsCache::IsOriginAllowed( 87 const GURL& origin) { 88 CheckThreadAccess(); 89 return allowed_origins_.find(origin) != allowed_origins_.end(); 90 } 91 IsOriginDenied(const GURL & origin)92bool NotificationsPrefsCache::IsOriginDenied( 93 const GURL& origin) { 94 CheckThreadAccess(); 95 return denied_origins_.find(origin) != denied_origins_.end(); 96 } 97 CheckThreadAccess()98void NotificationsPrefsCache::CheckThreadAccess() { 99 if (is_initialized_) { 100 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 101 } else { 102 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 103 } 104 } 105