| OLD | NEW |
| (Empty) |
| 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 | |
| 14 NotificationsPrefsCache::NotificationsPrefsCache() | |
| 15 : default_content_setting_(CONTENT_SETTING_DEFAULT), | |
| 16 is_initialized_(false) { | |
| 17 } | |
| 18 | |
| 19 void NotificationsPrefsCache::CacheAllowedOrigin( | |
| 20 const GURL& origin) { | |
| 21 CheckThreadAccess(); | |
| 22 std::set<GURL>::iterator iter; | |
| 23 allowed_origins_.insert(origin); | |
| 24 if ((iter = denied_origins_.find(origin)) != denied_origins_.end()) | |
| 25 denied_origins_.erase(iter); | |
| 26 } | |
| 27 | |
| 28 void NotificationsPrefsCache::CacheDeniedOrigin( | |
| 29 const GURL& origin) { | |
| 30 CheckThreadAccess(); | |
| 31 std::set<GURL>::iterator iter; | |
| 32 denied_origins_.insert(origin); | |
| 33 if ((iter = allowed_origins_.find(origin)) != allowed_origins_.end()) | |
| 34 allowed_origins_.erase(iter); | |
| 35 } | |
| 36 | |
| 37 void NotificationsPrefsCache::SetCacheAllowedOrigins( | |
| 38 const std::vector<GURL>& allowed) { | |
| 39 allowed_origins_.clear(); | |
| 40 allowed_origins_.insert(allowed.begin(), allowed.end()); | |
| 41 } | |
| 42 | |
| 43 void NotificationsPrefsCache::SetCacheDeniedOrigins( | |
| 44 const std::vector<GURL>& denied) { | |
| 45 denied_origins_.clear(); | |
| 46 denied_origins_.insert(denied.begin(), denied.end()); | |
| 47 } | |
| 48 | |
| 49 void NotificationsPrefsCache::SetCacheDefaultContentSetting( | |
| 50 ContentSetting setting) { | |
| 51 default_content_setting_ = setting; | |
| 52 } | |
| 53 | |
| 54 // static | |
| 55 void NotificationsPrefsCache::ListValueToGurlVector( | |
| 56 const ListValue& origin_list, | |
| 57 std::vector<GURL>* origin_vector) { | |
| 58 ListValue::const_iterator i; | |
| 59 std::string origin; | |
| 60 for (i = origin_list.begin(); i != origin_list.end(); ++i) { | |
| 61 (*i)->GetAsString(&origin); | |
| 62 origin_vector->push_back(GURL(origin)); | |
| 63 } | |
| 64 } | |
| 65 | |
| 66 WebKit::WebNotificationPresenter::Permission | |
| 67 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 | |
| 84 NotificationsPrefsCache::~NotificationsPrefsCache() {} | |
| 85 | |
| 86 bool NotificationsPrefsCache::IsOriginAllowed( | |
| 87 const GURL& origin) { | |
| 88 CheckThreadAccess(); | |
| 89 return allowed_origins_.find(origin) != allowed_origins_.end(); | |
| 90 } | |
| 91 | |
| 92 bool NotificationsPrefsCache::IsOriginDenied( | |
| 93 const GURL& origin) { | |
| 94 CheckThreadAccess(); | |
| 95 return denied_origins_.find(origin) != denied_origins_.end(); | |
| 96 } | |
| 97 | |
| 98 void NotificationsPrefsCache::CheckThreadAccess() { | |
| 99 if (is_initialized_) { | |
| 100 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 101 } else { | |
| 102 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 103 } | |
| 104 } | |
| OLD | NEW |