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 #include "chrome/browser/extensions/api/declarative/rules_cache_delegate.h"
7 #include "chrome/browser/chrome_notification_types.h"
8 #include "chrome/browser/extensions/api/declarative/rules_registry.h"
9 #include "chrome/browser/extensions/extension_service.h"
10 #include "chrome/browser/extensions/extension_util.h"
11 #include "chrome/browser/extensions/state_store.h"
12 #include "chrome/browser/profiles/profile.h"
13 #include "content/public/browser/notification_details.h"
14 #include "content/public/browser/notification_source.h"
15 #include "extensions/browser/extension_system.h"
16 #include "extensions/browser/info_map.h"
20 // Returns the key to use for storing declarative rules in the state store.
21 std::string
GetDeclarativeRuleStorageKey(const std::string
& event_name
,
24 return "declarative_rules.incognito." + event_name
;
26 return "declarative_rules." + event_name
;
32 namespace extensions
{
36 const char RulesCacheDelegate::kRulesStoredKey
[] =
37 "has_declarative_rules";
39 RulesCacheDelegate::RulesCacheDelegate(bool log_storage_init_delay
)
41 log_storage_init_delay_(log_storage_init_delay
),
42 notified_registry_(false),
43 weak_ptr_factory_(this) {
46 RulesCacheDelegate::~RulesCacheDelegate() {}
48 // Returns the key to use for storing whether the rules have been stored.
50 std::string
RulesCacheDelegate::GetRulesStoredKey(const std::string
& event_name
,
52 std::string
result(kRulesStoredKey
);
53 result
+= incognito
? ".incognito." : ".";
54 return result
+ event_name
;
57 // This is called from the constructor of RulesRegistry, so it is
58 // important that it both
59 // 1. calls no (in particular virtual) methods of the rules registry, and
60 // 2. does not create scoped_refptr holding the registry. (A short-lived
61 // scoped_refptr might delete the rules registry before it is constructed.)
62 void RulesCacheDelegate::Init(RulesRegistry
* registry
) {
63 DCHECK_CURRENTLY_ON(content::BrowserThread::UI
);
65 // WARNING: The first use of |registry_| will bind it to the calling thread
66 // so don't use this here.
67 registry_
= registry
->GetWeakPtr();
69 profile_
= registry
->profile();
71 GetDeclarativeRuleStorageKey(registry
->event_name(),
72 profile_
->IsOffTheRecord());
73 rules_stored_key_
= GetRulesStoredKey(registry
->event_name(),
74 profile_
->IsOffTheRecord());
75 rules_registry_thread_
= registry
->owner_thread();
77 ExtensionSystem
& system
= *ExtensionSystem::Get(profile_
);
78 extensions::StateStore
* store
= system
.rules_store();
80 store
->RegisterKey(storage_key_
);
82 if (profile_
->IsOffTheRecord())
83 log_storage_init_delay_
= false;
87 base::Bind(&RulesCacheDelegate::ReadRulesForInstalledExtensions
,
88 weak_ptr_factory_
.GetWeakPtr()));
89 system
.ready().Post(FROM_HERE
,
90 base::Bind(&RulesCacheDelegate::CheckIfReady
,
91 weak_ptr_factory_
.GetWeakPtr()));
94 void RulesCacheDelegate::WriteToStorage(const std::string
& extension_id
,
95 scoped_ptr
<base::Value
> value
) {
96 DCHECK_CURRENTLY_ON(content::BrowserThread::UI
);
100 const base::ListValue
* rules
= NULL
;
101 CHECK(value
->GetAsList(&rules
));
102 bool rules_stored_previously
= GetDeclarativeRulesStored(extension_id
);
103 bool store_rules
= !rules
->empty();
104 SetDeclarativeRulesStored(extension_id
, store_rules
);
105 if (!rules_stored_previously
&& !store_rules
)
108 StateStore
* store
= ExtensionSystem::Get(profile_
)->rules_store();
110 store
->SetExtensionValue(extension_id
, storage_key_
, value
.Pass());
113 void RulesCacheDelegate::CheckIfReady() {
114 DCHECK_CURRENTLY_ON(content::BrowserThread::UI
);
115 if (notified_registry_
|| !waiting_for_extensions_
.empty())
118 content::BrowserThread::PostTask(
119 rules_registry_thread_
,
122 &RulesRegistry::MarkReady
, registry_
, storage_init_time_
));
123 notified_registry_
= true;
126 void RulesCacheDelegate::ReadRulesForInstalledExtensions() {
127 ExtensionSystem
& system
= *ExtensionSystem::Get(profile_
);
128 ExtensionService
* extension_service
= system
.extension_service();
129 DCHECK(extension_service
);
130 // In an OTR profile, we start on top of a normal profile already, so the
131 // extension service should be ready.
132 DCHECK(!profile_
->IsOffTheRecord() || extension_service
->is_ready());
133 if (extension_service
->is_ready()) {
134 const ExtensionSet
* extensions
= extension_service
->extensions();
135 for (ExtensionSet::const_iterator i
= extensions
->begin();
136 i
!= extensions
->end();
138 bool needs_apis_storing_rules
=
139 (*i
)->HasAPIPermission(APIPermission::kDeclarativeContent
) ||
140 (*i
)->HasAPIPermission(APIPermission::kDeclarativeWebRequest
);
141 bool respects_off_the_record
=
142 !(profile_
->IsOffTheRecord()) ||
143 util::IsIncognitoEnabled((*i
)->id(), profile_
);
144 if (needs_apis_storing_rules
&& respects_off_the_record
)
145 ReadFromStorage((*i
)->id());
150 void RulesCacheDelegate::ReadFromStorage(const std::string
& extension_id
) {
151 DCHECK_CURRENTLY_ON(content::BrowserThread::UI
);
155 if (log_storage_init_delay_
&& storage_init_time_
.is_null())
156 storage_init_time_
= base::Time::Now();
158 if (!GetDeclarativeRulesStored(extension_id
)) {
159 ExtensionSystem::Get(profile_
)->ready().Post(
160 FROM_HERE
, base::Bind(&RulesCacheDelegate::CheckIfReady
,
161 weak_ptr_factory_
.GetWeakPtr()));
165 extensions::StateStore
* store
= ExtensionSystem::Get(profile_
)->rules_store();
168 waiting_for_extensions_
.insert(extension_id
);
169 store
->GetExtensionValue(
172 base::Bind(&RulesCacheDelegate::ReadFromStorageCallback
,
173 weak_ptr_factory_
.GetWeakPtr(),
177 void RulesCacheDelegate::ReadFromStorageCallback(
178 const std::string
& extension_id
,
179 scoped_ptr
<base::Value
> value
) {
180 DCHECK_CURRENTLY_ON(content::BrowserThread::UI
);
181 content::BrowserThread::PostTask(
182 rules_registry_thread_
,
184 base::Bind(&RulesRegistry::DeserializeAndAddRules
,
187 base::Passed(&value
)));
189 waiting_for_extensions_
.erase(extension_id
);
191 if (waiting_for_extensions_
.empty())
192 ExtensionSystem::Get(profile_
)->ready().Post(
193 FROM_HERE
, base::Bind(&RulesCacheDelegate::CheckIfReady
,
194 weak_ptr_factory_
.GetWeakPtr()));
197 bool RulesCacheDelegate::GetDeclarativeRulesStored(
198 const std::string
& extension_id
) const {
200 const ExtensionScopedPrefs
* extension_prefs
= ExtensionPrefs::Get(profile_
);
202 bool rules_stored
= true;
203 if (extension_prefs
->ReadPrefAsBoolean(
204 extension_id
, rules_stored_key_
, &rules_stored
))
207 // Safe default -- if we don't know that the rules are not stored, we force
208 // a read by returning true.
212 void RulesCacheDelegate::SetDeclarativeRulesStored(
213 const std::string
& extension_id
,
216 ExtensionSystem
& system
= *ExtensionSystem::Get(profile_
);
217 ExtensionService
* extension_service
= system
.extension_service();
218 DCHECK(extension_service
);
219 DCHECK(extension_service
->GetInstalledExtension(extension_id
));
220 ExtensionScopedPrefs
* extension_prefs
= ExtensionPrefs::Get(profile_
);
221 extension_prefs
->UpdateExtensionPref(
224 new base::FundamentalValue(rules_stored
));
227 } // namespace extensions