1 // Copyright 2013 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_EXTENSIONS_API_DECLARATIVE_RULES_CACHE_DELEGATE_H__ 6 #define CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_RULES_CACHE_DELEGATE_H__ 7 8 #include <set> 9 #include <string> 10 11 #include "base/gtest_prod_util.h" 12 #include "base/values.h" 13 #include "content/public/browser/browser_thread.h" 14 15 class Profile; 16 17 namespace extensions { 18 19 class RulesRegistry; 20 21 // RulesCacheDelegate implements the part of the RulesRegistry which works on 22 // the UI thread. It should only be used on the UI thread. 23 // If |log_storage_init_delay| is set, the delay caused by loading and 24 // registering rules on initialization will be logged with UMA. 25 class RulesCacheDelegate { 26 public: 27 28 explicit RulesCacheDelegate(bool log_storage_init_delay); 29 30 virtual ~RulesCacheDelegate(); 31 32 // Returns a key for the state store. The associated preference is a boolean 33 // indicating whether there are some declarative rules stored in the rule 34 // store. 35 static std::string GetRulesStoredKey(const std::string& event_name, 36 bool incognito); 37 38 // Initialize the storage functionality. 39 void Init(RulesRegistry* registry); 40 41 void WriteToStorage(const std::string& extension_id, 42 scoped_ptr<base::Value> value); 43 44 base::WeakPtr<RulesCacheDelegate> GetWeakPtr() { 45 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 46 return weak_ptr_factory_.GetWeakPtr(); 47 } 48 49 private: 50 FRIEND_TEST_ALL_PREFIXES(RulesRegistryWithCacheTest, 51 DeclarativeRulesStored); 52 FRIEND_TEST_ALL_PREFIXES(RulesRegistryWithCacheTest, 53 RulesStoredFlagMultipleRegistries); 54 55 static const char kRulesStoredKey[]; 56 57 // Check if we are done reading all data from storage on startup, and notify 58 // the RulesRegistry on its thread if so. The notification is delivered 59 // exactly once. 60 void CheckIfReady(); 61 62 // Schedules retrieving rules for already loaded extensions where 63 // appropriate. 64 void ReadRulesForInstalledExtensions(); 65 66 // Read/write a list of rules serialized to Values. 67 void ReadFromStorage(const std::string& extension_id); 68 void ReadFromStorageCallback(const std::string& extension_id, 69 scoped_ptr<base::Value> value); 70 71 // Check the preferences whether the extension with |extension_id| has some 72 // rules stored on disk. If this information is not in the preferences, true 73 // is returned as a safe default value. 74 bool GetDeclarativeRulesStored(const std::string& extension_id) const; 75 // Modify the preference to |rules_stored|. 76 void SetDeclarativeRulesStored(const std::string& extension_id, 77 bool rules_stored); 78 79 Profile* profile_; 80 81 // The key under which rules are stored. 82 std::string storage_key_; 83 84 // The key under which we store whether the rules have been stored. 85 std::string rules_stored_key_; 86 87 // A set of extension IDs that have rules we are reading from storage. 88 std::set<std::string> waiting_for_extensions_; 89 90 // We measure the time spent on loading rules on init. The result is logged 91 // with UMA once per each RulesCacheDelegate instance, unless in Incognito. 92 base::Time storage_init_time_; 93 bool log_storage_init_delay_; 94 95 // Weak pointer to post tasks to the owning rules registry. 96 base::WeakPtr<RulesRegistry> registry_; 97 98 // The thread |registry_| lives on. 99 content::BrowserThread::ID rules_registry_thread_; 100 101 // We notified the RulesRegistry that the rules are loaded. 102 bool notified_registry_; 103 104 // Use this factory to generate weak pointers bound to the UI thread. 105 base::WeakPtrFactory<RulesCacheDelegate> weak_ptr_factory_; 106 }; 107 108 } // namespace extensions 109 110 #endif // CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_RULES_CACHE_DELEGATE_H__ 111