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