1 // Copyright (c) 2012 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 "extensions/browser/api/declarative/rules_registry.h"
10 #include "base/logging.h"
11 #include "base/message_loop/message_loop.h"
12 #include "base/metrics/histogram.h"
13 #include "base/stl_util.h"
14 #include "base/strings/stringprintf.h"
15 #include "base/time/time.h"
16 #include "base/values.h"
17 #include "content/public/browser/browser_thread.h"
18 #include "content/public/browser/notification_details.h"
19 #include "content/public/browser/notification_source.h"
20 #include "extensions/browser/api/declarative/rules_cache_delegate.h"
21 #include "extensions/browser/extension_prefs.h"
22 #include "extensions/browser/extension_system.h"
23 #include "extensions/browser/state_store.h"
24 #include "extensions/common/extension.h"
28 const char kSuccess
[] = "";
29 const char kDuplicateRuleId
[] = "Duplicate rule ID: %s";
31 scoped_ptr
<base::Value
> RulesToValue(
32 const std::vector
<linked_ptr
<extensions::RulesRegistry::Rule
> >& rules
) {
33 scoped_ptr
<base::ListValue
> list(new base::ListValue());
34 for (size_t i
= 0; i
< rules
.size(); ++i
)
35 list
->Append(rules
[i
]->ToValue().release());
39 std::vector
<linked_ptr
<extensions::RulesRegistry::Rule
> > RulesFromValue(
40 const base::Value
* value
) {
41 std::vector
<linked_ptr
<extensions::RulesRegistry::Rule
> > rules
;
43 const base::ListValue
* list
= NULL
;
44 if (!value
|| !value
->GetAsList(&list
))
47 rules
.reserve(list
->GetSize());
48 for (size_t i
= 0; i
< list
->GetSize(); ++i
) {
49 const base::DictionaryValue
* dict
= NULL
;
50 if (!list
->GetDictionary(i
, &dict
))
52 linked_ptr
<extensions::RulesRegistry::Rule
> rule(
53 new extensions::RulesRegistry::Rule());
54 if (extensions::RulesRegistry::Rule::Populate(*dict
, rule
.get()))
55 rules
.push_back(rule
);
61 std::string
ToId(int identifier
) {
62 return base::StringPrintf("_%d_", identifier
);
68 namespace extensions
{
72 RulesRegistry::RulesRegistry(content::BrowserContext
* browser_context
,
73 const std::string
& event_name
,
74 content::BrowserThread::ID owner_thread
,
75 RulesCacheDelegate
* cache_delegate
,
77 : browser_context_(browser_context
),
78 owner_thread_(owner_thread
),
79 event_name_(event_name
),
81 ready_(/*signaled=*/!cache_delegate
), // Immediately ready if no cache
82 // delegate to wait for.
83 last_generated_rule_identifier_id_(0),
84 weak_ptr_factory_(browser_context_
? this : NULL
) {
86 cache_delegate_
= cache_delegate
->GetWeakPtr();
87 cache_delegate
->Init(this);
91 std::string
RulesRegistry::AddRulesNoFill(
92 const std::string
& extension_id
,
93 const std::vector
<linked_ptr
<Rule
> >& rules
) {
94 DCHECK_CURRENTLY_ON(owner_thread());
96 // Verify that all rule IDs are new.
97 for (std::vector
<linked_ptr
<Rule
> >::const_iterator i
=
98 rules
.begin(); i
!= rules
.end(); ++i
) {
99 const RuleId
& rule_id
= *((*i
)->id
);
100 // Every rule should have a priority assigned.
101 DCHECK((*i
)->priority
);
102 RulesDictionaryKey
key(extension_id
, rule_id
);
103 if (rules_
.find(key
) != rules_
.end())
104 return base::StringPrintf(kDuplicateRuleId
, rule_id
.c_str());
107 std::string error
= AddRulesImpl(extension_id
, rules
);
112 // Commit all rules into |rules_| on success.
113 for (std::vector
<linked_ptr
<Rule
> >::const_iterator i
=
114 rules
.begin(); i
!= rules
.end(); ++i
) {
115 const RuleId
& rule_id
= *((*i
)->id
);
116 RulesDictionaryKey
key(extension_id
, rule_id
);
120 MaybeProcessChangedRules(extension_id
);
124 std::string
RulesRegistry::AddRules(
125 const std::string
& extension_id
,
126 const std::vector
<linked_ptr
<Rule
> >& rules
) {
127 DCHECK_CURRENTLY_ON(owner_thread());
129 std::string error
= CheckAndFillInOptionalRules(extension_id
, rules
);
132 FillInOptionalPriorities(rules
);
134 return AddRulesNoFill(extension_id
, rules
);
137 std::string
RulesRegistry::RemoveRules(
138 const std::string
& extension_id
,
139 const std::vector
<std::string
>& rule_identifiers
) {
140 DCHECK_CURRENTLY_ON(owner_thread());
142 std::string error
= RemoveRulesImpl(extension_id
, rule_identifiers
);
147 for (std::vector
<std::string
>::const_iterator i
= rule_identifiers
.begin();
148 i
!= rule_identifiers
.end();
150 RulesDictionaryKey
lookup_key(extension_id
, *i
);
151 rules_
.erase(lookup_key
);
154 MaybeProcessChangedRules(extension_id
);
155 RemoveUsedRuleIdentifiers(extension_id
, rule_identifiers
);
159 std::string
RulesRegistry::RemoveAllRules(const std::string
& extension_id
) {
160 std::string result
= RulesRegistry::RemoveAllRulesNoStoreUpdate(extension_id
);
161 MaybeProcessChangedRules(extension_id
); // Now update the prefs and store.
165 std::string
RulesRegistry::RemoveAllRulesNoStoreUpdate(
166 const std::string
& extension_id
) {
167 DCHECK_CURRENTLY_ON(owner_thread());
169 std::string error
= RemoveAllRulesImpl(extension_id
);
174 for (RulesDictionary::const_iterator i
= rules_
.begin();
175 i
!= rules_
.end();) {
176 const RulesDictionaryKey
& key
= i
->first
;
178 if (key
.first
== extension_id
)
182 RemoveAllUsedRuleIdentifiers(extension_id
);
186 void RulesRegistry::GetRules(const std::string
& extension_id
,
187 const std::vector
<std::string
>& rule_identifiers
,
188 std::vector
<linked_ptr
<Rule
> >* out
) {
189 DCHECK_CURRENTLY_ON(owner_thread());
191 for (std::vector
<std::string
>::const_iterator i
= rule_identifiers
.begin();
192 i
!= rule_identifiers
.end(); ++i
) {
193 RulesDictionaryKey
lookup_key(extension_id
, *i
);
194 RulesDictionary::iterator entry
= rules_
.find(lookup_key
);
195 if (entry
!= rules_
.end())
196 out
->push_back(entry
->second
);
200 void RulesRegistry::GetAllRules(const std::string
& extension_id
,
201 std::vector
<linked_ptr
<Rule
> >* out
) {
202 DCHECK_CURRENTLY_ON(owner_thread());
203 for (RulesDictionary::const_iterator i
= rules_
.begin();
204 i
!= rules_
.end(); ++i
) {
205 const RulesDictionaryKey
& key
= i
->first
;
206 if (key
.first
== extension_id
)
207 out
->push_back(i
->second
);
211 void RulesRegistry::OnExtensionUnloaded(const std::string
& extension_id
) {
212 DCHECK_CURRENTLY_ON(owner_thread());
213 std::string error
= RemoveAllRulesImpl(extension_id
);
218 void RulesRegistry::OnExtensionUninstalled(const std::string
& extension_id
) {
219 DCHECK_CURRENTLY_ON(owner_thread());
220 std::string error
= RemoveAllRulesNoStoreUpdate(extension_id
);
225 void RulesRegistry::OnExtensionLoaded(const std::string
& extension_id
) {
226 DCHECK_CURRENTLY_ON(owner_thread());
227 std::vector
<linked_ptr
<Rule
> > rules
;
228 GetAllRules(extension_id
, &rules
);
229 std::string error
= AddRulesImpl(extension_id
, rules
);
234 size_t RulesRegistry::GetNumberOfUsedRuleIdentifiersForTesting() const {
235 size_t entry_count
= 0u;
236 for (RuleIdentifiersMap::const_iterator extension
=
237 used_rule_identifiers_
.begin();
238 extension
!= used_rule_identifiers_
.end();
240 // Each extension is counted as 1 just for being there. Otherwise we miss
241 // keys with empty values.
242 entry_count
+= 1u + extension
->second
.size();
247 void RulesRegistry::DeserializeAndAddRules(
248 const std::string
& extension_id
,
249 scoped_ptr
<base::Value
> rules
) {
250 DCHECK_CURRENTLY_ON(owner_thread());
252 AddRulesNoFill(extension_id
, RulesFromValue(rules
.get()));
255 RulesRegistry::~RulesRegistry() {
258 void RulesRegistry::MarkReady(base::Time storage_init_time
) {
259 DCHECK_CURRENTLY_ON(owner_thread());
261 if (!storage_init_time
.is_null()) {
262 UMA_HISTOGRAM_TIMES("Extensions.DeclarativeRulesStorageInitialization",
263 base::Time::Now() - storage_init_time
);
269 void RulesRegistry::ProcessChangedRules(const std::string
& extension_id
) {
270 DCHECK_CURRENTLY_ON(owner_thread());
272 DCHECK(ContainsKey(process_changed_rules_requested_
, extension_id
));
273 process_changed_rules_requested_
[extension_id
] = NOT_SCHEDULED_FOR_PROCESSING
;
275 std::vector
<linked_ptr
<Rule
> > new_rules
;
276 GetAllRules(extension_id
, &new_rules
);
277 content::BrowserThread::PostTask(
278 content::BrowserThread::UI
,
280 base::Bind(&RulesCacheDelegate::WriteToStorage
,
283 base::Passed(RulesToValue(new_rules
))));
286 void RulesRegistry::MaybeProcessChangedRules(const std::string
& extension_id
) {
287 // Read and initialize |process_changed_rules_requested_[extension_id]| if
288 // necessary. (Note that the insertion below will not overwrite
289 // |process_changed_rules_requested_[extension_id]| if that already exists.
290 std::pair
<ProcessStateMap::iterator
, bool> insertion
=
291 process_changed_rules_requested_
.insert(std::make_pair(
293 browser_context_
? NOT_SCHEDULED_FOR_PROCESSING
: NEVER_PROCESS
));
294 if (insertion
.first
->second
!= NOT_SCHEDULED_FOR_PROCESSING
)
297 process_changed_rules_requested_
[extension_id
] = SCHEDULED_FOR_PROCESSING
;
298 ready_
.Post(FROM_HERE
,
299 base::Bind(&RulesRegistry::ProcessChangedRules
,
300 weak_ptr_factory_
.GetWeakPtr(),
304 bool RulesRegistry::IsUniqueId(const std::string
& extension_id
,
305 const std::string
& rule_id
) const {
306 RuleIdentifiersMap::const_iterator identifiers
=
307 used_rule_identifiers_
.find(extension_id
);
308 if (identifiers
== used_rule_identifiers_
.end())
310 return identifiers
->second
.find(rule_id
) == identifiers
->second
.end();
313 std::string
RulesRegistry::GenerateUniqueId(const std::string
& extension_id
) {
314 while (!IsUniqueId(extension_id
, ToId(last_generated_rule_identifier_id_
)))
315 ++last_generated_rule_identifier_id_
;
316 return ToId(last_generated_rule_identifier_id_
);
319 std::string
RulesRegistry::CheckAndFillInOptionalRules(
320 const std::string
& extension_id
,
321 const std::vector
<linked_ptr
<Rule
> >& rules
) {
322 // IDs we have inserted, in case we need to rollback this operation.
323 std::vector
<std::string
> rollback_log
;
325 // First we insert all rules with existing identifier, so that generated
326 // identifiers cannot collide with identifiers passed by the caller.
327 for (std::vector
<linked_ptr
<Rule
> >::const_iterator i
= rules
.begin();
330 Rule
* rule
= i
->get();
331 if (rule
->id
.get()) {
332 std::string id
= *(rule
->id
);
333 if (!IsUniqueId(extension_id
, id
)) {
334 RemoveUsedRuleIdentifiers(extension_id
, rollback_log
);
335 return "Id " + id
+ " was used multiple times.";
337 used_rule_identifiers_
[extension_id
].insert(id
);
340 // Now we generate IDs in case they were not specified in the rules. This
341 // cannot fail so we do not need to keep track of a rollback log.
342 for (std::vector
<linked_ptr
<Rule
> >::const_iterator i
= rules
.begin();
345 Rule
* rule
= i
->get();
346 if (!rule
->id
.get()) {
347 rule
->id
.reset(new std::string(GenerateUniqueId(extension_id
)));
348 used_rule_identifiers_
[extension_id
].insert(*(rule
->id
));
351 return std::string();
354 void RulesRegistry::FillInOptionalPriorities(
355 const std::vector
<linked_ptr
<Rule
> >& rules
) {
356 std::vector
<linked_ptr
<Rule
> >::const_iterator i
;
357 for (i
= rules
.begin(); i
!= rules
.end(); ++i
) {
358 if (!(*i
)->priority
.get())
359 (*i
)->priority
.reset(new int(DEFAULT_PRIORITY
));
363 void RulesRegistry::RemoveUsedRuleIdentifiers(
364 const std::string
& extension_id
,
365 const std::vector
<std::string
>& identifiers
) {
366 std::vector
<std::string
>::const_iterator i
;
367 for (i
= identifiers
.begin(); i
!= identifiers
.end(); ++i
)
368 used_rule_identifiers_
[extension_id
].erase(*i
);
371 void RulesRegistry::RemoveAllUsedRuleIdentifiers(
372 const std::string
& extension_id
) {
373 used_rule_identifiers_
.erase(extension_id
);
376 } // namespace extensions