notifications_prefs_cache.h revision ddb351dbec246cf1fab5ec20d2d5520909041de1
1// Copyright (c) 2011 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#ifndef CHROME_BROWSER_NOTIFICATIONS_NOTIFICATIONS_PREFS_CACHE_H_ 6#define CHROME_BROWSER_NOTIFICATIONS_NOTIFICATIONS_PREFS_CACHE_H_ 7#pragma once 8 9#include <set> 10#include <vector> 11 12#include "base/memory/ref_counted.h" 13#include "chrome/common/content_settings.h" 14#include "googleurl/src/gurl.h" 15 16class ListValue; 17 18// Class which caches notification preferences. 19// Construction occurs on the UI thread when the contents 20// of the profile preferences are initialized. Once is_initialized() is set, 21// access can only be done from the IO thread. 22class NotificationsPrefsCache 23 : public base::RefCountedThreadSafe<NotificationsPrefsCache> { 24 public: 25 NotificationsPrefsCache(); 26 27 // Once is_initialized() is set, all accesses must happen on the IO thread. 28 // Before that, all accesses need to happen on the UI thread. 29 void set_is_initialized(bool val) { is_initialized_ = val; } 30 bool is_initialized() { return is_initialized_; } 31 32 // Checks to see if a given origin has permission to create desktop 33 // notifications. Returns a constant from WebNotificationPresenter 34 // class. 35 int HasPermission(const GURL& origin); 36 37 // Updates the cache with a new origin allowed or denied. 38 void CacheAllowedOrigin(const GURL& origin); 39 void CacheDeniedOrigin(const GURL& origin); 40 41 // Set the cache to the supplied values. This clears the current 42 // contents of the cache. 43 void SetCacheAllowedOrigins(const std::vector<GURL>& allowed); 44 void SetCacheDeniedOrigins(const std::vector<GURL>& denied); 45 void SetCacheDefaultContentSetting(ContentSetting setting); 46 47 static void ListValueToGurlVector(const ListValue& origin_list, 48 std::vector<GURL>* origin_vector); 49 50 // Exposed for testing. 51 ContentSetting CachedDefaultContentSetting() { 52 return default_content_setting_; 53 } 54 55 private: 56 friend class base::RefCountedThreadSafe<NotificationsPrefsCache>; 57 58 virtual ~NotificationsPrefsCache(); 59 60 // Helper functions which read preferences. 61 bool IsOriginAllowed(const GURL& origin); 62 bool IsOriginDenied(const GURL& origin); 63 64 // Helper that ensures we are running on the expected thread. 65 void CheckThreadAccess(); 66 67 // Storage of the actual preferences. 68 std::set<GURL> allowed_origins_; 69 std::set<GURL> denied_origins_; 70 71 // The default setting, used for origins that are neither in 72 // |allowed_origins_| nor |denied_origins_|. 73 ContentSetting default_content_setting_; 74 75 // Set to true once the initial cached settings have been completely read. 76 // Once this is done, the class can no longer be accessed on the UI thread. 77 bool is_initialized_; 78 79 DISALLOW_COPY_AND_ASSIGN(NotificationsPrefsCache); 80}; 81 82#endif // CHROME_BROWSER_NOTIFICATIONS_NOTIFICATIONS_PREFS_CACHE_H_ 83