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 CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_RULES_CACHE_DELEGATE_H__
6 #define CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_RULES_CACHE_DELEGATE_H__
11 #include "base/gtest_prod_util.h"
12 #include "base/values.h"
13 #include "content/public/browser/browser_thread.h"
17 namespace extensions
{
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
{
28 explicit RulesCacheDelegate(bool log_storage_init_delay
);
30 virtual ~RulesCacheDelegate();
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
35 static std::string
GetRulesStoredKey(const std::string
& event_name
,
38 // Initialize the storage functionality.
39 void Init(RulesRegistry
* registry
);
41 void WriteToStorage(const std::string
& extension_id
,
42 scoped_ptr
<base::Value
> value
);
44 base::WeakPtr
<RulesCacheDelegate
> GetWeakPtr() {
45 DCHECK_CURRENTLY_ON(content::BrowserThread::UI
);
46 return weak_ptr_factory_
.GetWeakPtr();
50 FRIEND_TEST_ALL_PREFIXES(RulesRegistryWithCacheTest
,
51 DeclarativeRulesStored
);
52 FRIEND_TEST_ALL_PREFIXES(RulesRegistryWithCacheTest
,
53 RulesStoredFlagMultipleRegistries
);
55 static const char kRulesStoredKey
[];
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
62 // Schedules retrieving rules for already loaded extensions where
64 void ReadRulesForInstalledExtensions();
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
);
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
,
81 // The key under which rules are stored.
82 std::string storage_key_
;
84 // The key under which we store whether the rules have been stored.
85 std::string rules_stored_key_
;
87 // A set of extension IDs that have rules we are reading from storage.
88 std::set
<std::string
> waiting_for_extensions_
;
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_
;
95 // Weak pointer to post tasks to the owning rules registry.
96 base::WeakPtr
<RulesRegistry
> registry_
;
98 // The thread |registry_| lives on.
99 content::BrowserThread::ID rules_registry_thread_
;
101 // We notified the RulesRegistry that the rules are loaded.
102 bool notified_registry_
;
104 // Use this factory to generate weak pointers bound to the UI thread.
105 base::WeakPtrFactory
<RulesCacheDelegate
> weak_ptr_factory_
;
108 } // namespace extensions
110 #endif // CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_RULES_CACHE_DELEGATE_H__