OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 // | 4 // |
5 // DeclarativeRule<>, DeclarativeConditionSet<>, and DeclarativeActionSet<> | 5 // DeclarativeRule<>, DeclarativeConditionSet<>, and DeclarativeActionSet<> |
6 // templates usable with multiple different declarativeFoo systems. These are | 6 // templates usable with multiple different declarativeFoo systems. These are |
7 // templated on the Condition and Action types that define the behavior of a | 7 // templated on the Condition and Action types that define the behavior of a |
8 // particular declarative event. | 8 // particular declarative event. |
9 | 9 |
10 #ifndef EXTENSIONS_BROWSER_API_DECLARATIVE_DECLARATIVE_RULE_H__ | 10 #ifndef EXTENSIONS_BROWSER_API_DECLARATIVE_DECLARATIVE_RULE_H__ |
11 #define EXTENSIONS_BROWSER_API_DECLARATIVE_DECLARATIVE_RULE_H__ | 11 #define EXTENSIONS_BROWSER_API_DECLARATIVE_DECLARATIVE_RULE_H__ |
12 | 12 |
13 #include <limits> | 13 #include <limits> |
14 #include <set> | 14 #include <set> |
15 #include <string> | 15 #include <string> |
16 #include <vector> | 16 #include <vector> |
17 | 17 |
18 #include "base/callback.h" | 18 #include "base/callback.h" |
19 #include "base/memory/linked_ptr.h" | 19 #include "base/memory/linked_ptr.h" |
20 #include "base/memory/scoped_vector.h" | 20 #include "base/memory/scoped_vector.h" |
21 #include "base/stl_util.h" | 21 #include "base/stl_util.h" |
22 #include "base/time/time.h" | 22 #include "base/time/time.h" |
23 #include "components/url_matcher/url_matcher.h" | 23 #include "components/url_matcher/url_matcher.h" |
24 #include "extensions/common/api/events.h" | 24 #include "extensions/common/api/events.h" |
25 #include "extensions/common/extension.h" | 25 #include "extensions/common/extension.h" |
26 #include "extensions/common/host_id.h" | |
27 | 26 |
28 namespace base { | 27 namespace base { |
29 class Time; | 28 class Time; |
30 class Value; | 29 class Value; |
31 } | 30 } |
32 | 31 |
33 namespace content { | 32 namespace content { |
34 class BrowserContext; | 33 class BrowserContext; |
35 } | 34 } |
36 | 35 |
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
146 typedef std::vector<linked_ptr<base::Value> > AnyVector; | 145 typedef std::vector<linked_ptr<base::Value> > AnyVector; |
147 typedef std::vector<scoped_refptr<const ActionT> > Actions; | 146 typedef std::vector<scoped_refptr<const ActionT> > Actions; |
148 | 147 |
149 explicit DeclarativeActionSet(const Actions& actions); | 148 explicit DeclarativeActionSet(const Actions& actions); |
150 | 149 |
151 // Factory method that instantiates a DeclarativeActionSet for |extension| | 150 // Factory method that instantiates a DeclarativeActionSet for |extension| |
152 // according to |actions| which represents the array of actions received from | 151 // according to |actions| which represents the array of actions received from |
153 // the extension API. | 152 // the extension API. |
154 static scoped_ptr<DeclarativeActionSet> Create( | 153 static scoped_ptr<DeclarativeActionSet> Create( |
155 content::BrowserContext* browser_context, | 154 content::BrowserContext* browser_context, |
156 const HostID& host_id, | |
157 const Extension* extension, | 155 const Extension* extension, |
158 const AnyVector& actions, | 156 const AnyVector& actions, |
159 std::string* error, | 157 std::string* error, |
160 bool* bad_message); | 158 bool* bad_message); |
161 | 159 |
162 // Rules call this method when their conditions are fulfilled. | 160 // Rules call this method when their conditions are fulfilled. |
163 void Apply(const std::string& extension_id, | 161 void Apply(const std::string& extension_id, |
164 const base::Time& extension_install_time, | 162 const base::Time& extension_install_time, |
165 typename ActionT::ApplyInfo* apply_info) const; | 163 typename ActionT::ApplyInfo* apply_info) const; |
166 | 164 |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
225 // format of each condition and action's json is up to the specific ConditionT | 223 // format of each condition and action's json is up to the specific ConditionT |
226 // and ActionT. |extension| may be NULL in tests. | 224 // and ActionT. |extension| may be NULL in tests. |
227 // | 225 // |
228 // Before constructing the final rule, calls check_consistency(conditions, | 226 // Before constructing the final rule, calls check_consistency(conditions, |
229 // actions, error) and returns NULL if it fails. Pass NULL if no consistency | 227 // actions, error) and returns NULL if it fails. Pass NULL if no consistency |
230 // check is needed. If |error| is empty, the translation was successful and | 228 // check is needed. If |error| is empty, the translation was successful and |
231 // the returned rule is internally consistent. | 229 // the returned rule is internally consistent. |
232 static scoped_ptr<DeclarativeRule> Create( | 230 static scoped_ptr<DeclarativeRule> Create( |
233 url_matcher::URLMatcherConditionFactory* url_matcher_condition_factory, | 231 url_matcher::URLMatcherConditionFactory* url_matcher_condition_factory, |
234 content::BrowserContext* browser_context, | 232 content::BrowserContext* browser_context, |
235 const HostID& host_id, | |
236 const Extension* extension, | 233 const Extension* extension, |
237 base::Time extension_installation_time, | 234 base::Time extension_installation_time, |
238 linked_ptr<JsonRule> rule, | 235 linked_ptr<JsonRule> rule, |
239 ConsistencyChecker check_consistency, | 236 ConsistencyChecker check_consistency, |
240 std::string* error); | 237 std::string* error); |
241 | 238 |
242 const GlobalRuleId& id() const { return id_; } | 239 const GlobalRuleId& id() const { return id_; } |
243 const Tags& tags() const { return tags_; } | 240 const Tags& tags() const { return tags_; } |
244 const std::string& extension_id() const { return id_.first; } | 241 const std::string& extension_id() const { return id_.first; } |
245 const ConditionSet& conditions() const { return *conditions_; } | 242 const ConditionSet& conditions() const { return *conditions_; } |
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
362 | 359 |
363 template<typename ActionT> | 360 template<typename ActionT> |
364 DeclarativeActionSet<ActionT>::DeclarativeActionSet(const Actions& actions) | 361 DeclarativeActionSet<ActionT>::DeclarativeActionSet(const Actions& actions) |
365 : actions_(actions) {} | 362 : actions_(actions) {} |
366 | 363 |
367 // static | 364 // static |
368 template<typename ActionT> | 365 template<typename ActionT> |
369 scoped_ptr<DeclarativeActionSet<ActionT> > | 366 scoped_ptr<DeclarativeActionSet<ActionT> > |
370 DeclarativeActionSet<ActionT>::Create( | 367 DeclarativeActionSet<ActionT>::Create( |
371 content::BrowserContext* browser_context, | 368 content::BrowserContext* browser_context, |
372 const HostID& host_id, | |
373 const Extension* extension, | 369 const Extension* extension, |
374 const AnyVector& actions, | 370 const AnyVector& actions, |
375 std::string* error, | 371 std::string* error, |
376 bool* bad_message) { | 372 bool* bad_message) { |
377 *error = ""; | 373 *error = ""; |
378 *bad_message = false; | 374 *bad_message = false; |
379 Actions result; | 375 Actions result; |
380 | 376 |
381 for (AnyVector::const_iterator i = actions.begin(); | 377 for (AnyVector::const_iterator i = actions.begin(); |
382 i != actions.end(); ++i) { | 378 i != actions.end(); ++i) { |
383 CHECK(i->get()); | 379 CHECK(i->get()); |
384 scoped_refptr<const ActionT> action = | 380 scoped_refptr<const ActionT> action = |
385 ActionT::Create( | 381 ActionT::Create(browser_context, extension, **i, error, bad_message); |
386 browser_context, host_id, extension, **i, error, bad_message); | |
387 if (!error->empty() || *bad_message) | 382 if (!error->empty() || *bad_message) |
388 return scoped_ptr<DeclarativeActionSet>(); | 383 return scoped_ptr<DeclarativeActionSet>(); |
389 result.push_back(action); | 384 result.push_back(action); |
390 } | 385 } |
391 | 386 |
392 return scoped_ptr<DeclarativeActionSet>(new DeclarativeActionSet(result)); | 387 return scoped_ptr<DeclarativeActionSet>(new DeclarativeActionSet(result)); |
393 } | 388 } |
394 | 389 |
395 template<typename ActionT> | 390 template<typename ActionT> |
396 void DeclarativeActionSet<ActionT>::Apply( | 391 void DeclarativeActionSet<ActionT>::Apply( |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
453 CHECK(conditions_.get()); | 448 CHECK(conditions_.get()); |
454 CHECK(actions_.get()); | 449 CHECK(actions_.get()); |
455 } | 450 } |
456 | 451 |
457 // static | 452 // static |
458 template<typename ConditionT, typename ActionT> | 453 template<typename ConditionT, typename ActionT> |
459 scoped_ptr<DeclarativeRule<ConditionT, ActionT> > | 454 scoped_ptr<DeclarativeRule<ConditionT, ActionT> > |
460 DeclarativeRule<ConditionT, ActionT>::Create( | 455 DeclarativeRule<ConditionT, ActionT>::Create( |
461 url_matcher::URLMatcherConditionFactory* url_matcher_condition_factory, | 456 url_matcher::URLMatcherConditionFactory* url_matcher_condition_factory, |
462 content::BrowserContext* browser_context, | 457 content::BrowserContext* browser_context, |
463 const HostID& host_id, | |
464 const Extension* extension, | 458 const Extension* extension, |
465 base::Time extension_installation_time, | 459 base::Time extension_installation_time, |
466 linked_ptr<JsonRule> rule, | 460 linked_ptr<JsonRule> rule, |
467 ConsistencyChecker check_consistency, | 461 ConsistencyChecker check_consistency, |
468 std::string* error) { | 462 std::string* error) { |
469 scoped_ptr<DeclarativeRule> error_result; | 463 scoped_ptr<DeclarativeRule> error_result; |
470 | 464 |
471 scoped_ptr<ConditionSet> conditions = ConditionSet::Create( | 465 scoped_ptr<ConditionSet> conditions = ConditionSet::Create( |
472 extension, url_matcher_condition_factory, rule->conditions, error); | 466 extension, url_matcher_condition_factory, rule->conditions, error); |
473 if (!error->empty()) | 467 if (!error->empty()) |
474 return error_result.Pass(); | 468 return error_result.Pass(); |
475 CHECK(conditions.get()); | 469 CHECK(conditions.get()); |
476 | 470 |
477 bool bad_message = false; | 471 bool bad_message = false; |
478 scoped_ptr<ActionSet> actions = | 472 scoped_ptr<ActionSet> actions = |
479 ActionSet::Create(browser_context, host_id, extension, | 473 ActionSet::Create( |
480 rule->actions, error, &bad_message); | 474 browser_context, extension, rule->actions, error, &bad_message); |
481 if (bad_message) { | 475 if (bad_message) { |
482 // TODO(battre) Export concept of bad_message to caller, the extension | 476 // TODO(battre) Export concept of bad_message to caller, the extension |
483 // should be killed in case it is true. | 477 // should be killed in case it is true. |
484 *error = "An action of a rule set had an invalid " | 478 *error = "An action of a rule set had an invalid " |
485 "structure that should have been caught by the JSON validator."; | 479 "structure that should have been caught by the JSON validator."; |
486 return error_result.Pass(); | 480 return error_result.Pass(); |
487 } | 481 } |
488 if (!error->empty() || bad_message) | 482 if (!error->empty() || bad_message) |
489 return error_result.Pass(); | 483 return error_result.Pass(); |
490 CHECK(actions.get()); | 484 CHECK(actions.get()); |
(...skipping 23 matching lines...) Expand all Loading... |
514 } | 508 } |
515 | 509 |
516 template<typename ConditionT, typename ActionT> | 510 template<typename ConditionT, typename ActionT> |
517 int DeclarativeRule<ConditionT, ActionT>::GetMinimumPriority() const { | 511 int DeclarativeRule<ConditionT, ActionT>::GetMinimumPriority() const { |
518 return actions_->GetMinimumPriority(); | 512 return actions_->GetMinimumPriority(); |
519 } | 513 } |
520 | 514 |
521 } // namespace extensions | 515 } // namespace extensions |
522 | 516 |
523 #endif // EXTENSIONS_BROWSER_API_DECLARATIVE_DECLARATIVE_RULE_H__ | 517 #endif // EXTENSIONS_BROWSER_API_DECLARATIVE_DECLARATIVE_RULE_H__ |
OLD | NEW |