| 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 <utility> | 16 #include <utility> |
| 17 #include <vector> | 17 #include <vector> |
| 18 | 18 |
| 19 #include "base/callback.h" | 19 #include "base/callback.h" |
| 20 #include "base/macros.h" | 20 #include "base/macros.h" |
| 21 #include "base/memory/linked_ptr.h" | 21 #include "base/memory/linked_ptr.h" |
| 22 #include "base/memory/ptr_util.h" |
| 22 #include "base/stl_util.h" | 23 #include "base/stl_util.h" |
| 23 #include "base/time/time.h" | 24 #include "base/time/time.h" |
| 24 #include "components/url_matcher/url_matcher.h" | 25 #include "components/url_matcher/url_matcher.h" |
| 25 #include "extensions/common/api/events.h" | 26 #include "extensions/common/api/events.h" |
| 26 #include "extensions/common/extension.h" | 27 #include "extensions/common/extension.h" |
| 27 | 28 |
| 28 namespace base { | 29 namespace base { |
| 29 class Time; | 30 class Time; |
| 30 class Value; | 31 class Value; |
| 31 } | 32 } |
| 32 | 33 |
| 33 namespace content { | 34 namespace content { |
| 34 class BrowserContext; | 35 class BrowserContext; |
| 35 } | 36 } |
| 36 | 37 |
| 37 namespace extensions { | 38 namespace extensions { |
| 38 | 39 |
| 39 // This class stores a set of conditions that may be part of a DeclarativeRule. | 40 // This class stores a set of conditions that may be part of a DeclarativeRule. |
| 40 // If any condition is fulfilled, the Actions of the DeclarativeRule can be | 41 // If any condition is fulfilled, the Actions of the DeclarativeRule can be |
| 41 // triggered. | 42 // triggered. |
| 42 // | 43 // |
| 43 // ConditionT should be immutable after creation. It must define the following | 44 // ConditionT should be immutable after creation. It must define the following |
| 44 // members: | 45 // members: |
| 45 // | 46 // |
| 46 // // Arguments passed through from DeclarativeConditionSet::Create. | 47 // // Arguments passed through from DeclarativeConditionSet::Create. |
| 47 // static scoped_ptr<ConditionT> Create( | 48 // static std::unique_ptr<ConditionT> Create( |
| 48 // const Extension* extension, | 49 // const Extension* extension, |
| 49 // URLMatcherConditionFactory* url_matcher_condition_factory, | 50 // URLMatcherConditionFactory* url_matcher_condition_factory, |
| 50 // // Except this argument gets elements of the Values array. | 51 // // Except this argument gets elements of the Values array. |
| 51 // const base::Value& definition, | 52 // const base::Value& definition, |
| 52 // std::string* error); | 53 // std::string* error); |
| 53 // // If the Condition needs to be filtered by some URLMatcherConditionSets, | 54 // // If the Condition needs to be filtered by some URLMatcherConditionSets, |
| 54 // // append them to |condition_sets|. | 55 // // append them to |condition_sets|. |
| 55 // // DeclarativeConditionSet::GetURLMatcherConditionSets forwards here. | 56 // // DeclarativeConditionSet::GetURLMatcherConditionSets forwards here. |
| 56 // void GetURLMatcherConditionSets( | 57 // void GetURLMatcherConditionSets( |
| 57 // URLMatcherConditionSet::Vector* condition_sets); | 58 // URLMatcherConditionSet::Vector* condition_sets); |
| 58 // // |match_data| passed through from DeclarativeConditionSet::IsFulfilled. | 59 // // |match_data| passed through from DeclarativeConditionSet::IsFulfilled. |
| 59 // bool IsFulfilled(const ConditionT::MatchData& match_data); | 60 // bool IsFulfilled(const ConditionT::MatchData& match_data); |
| 60 template<typename ConditionT> | 61 template<typename ConditionT> |
| 61 class DeclarativeConditionSet { | 62 class DeclarativeConditionSet { |
| 62 public: | 63 public: |
| 63 typedef std::vector<scoped_ptr<base::Value>> Values; | 64 typedef std::vector<std::unique_ptr<base::Value>> Values; |
| 64 typedef std::vector<linked_ptr<const ConditionT> > Conditions; | 65 typedef std::vector<linked_ptr<const ConditionT> > Conditions; |
| 65 typedef typename Conditions::const_iterator const_iterator; | 66 typedef typename Conditions::const_iterator const_iterator; |
| 66 | 67 |
| 67 // Factory method that creates a DeclarativeConditionSet for |extension| | 68 // Factory method that creates a DeclarativeConditionSet for |extension| |
| 68 // according to the JSON array |conditions| passed by the extension API. Sets | 69 // according to the JSON array |conditions| passed by the extension API. Sets |
| 69 // |error| and returns NULL in case of an error. | 70 // |error| and returns NULL in case of an error. |
| 70 static scoped_ptr<DeclarativeConditionSet> Create( | 71 static std::unique_ptr<DeclarativeConditionSet> Create( |
| 71 const Extension* extension, | 72 const Extension* extension, |
| 72 url_matcher::URLMatcherConditionFactory* url_matcher_condition_factory, | 73 url_matcher::URLMatcherConditionFactory* url_matcher_condition_factory, |
| 73 const Values& condition_values, | 74 const Values& condition_values, |
| 74 std::string* error); | 75 std::string* error); |
| 75 | 76 |
| 76 const Conditions& conditions() const { | 77 const Conditions& conditions() const { |
| 77 return conditions_; | 78 return conditions_; |
| 78 } | 79 } |
| 79 | 80 |
| 80 const_iterator begin() const { return conditions_.begin(); } | 81 const_iterator begin() const { return conditions_.begin(); } |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 112 | 113 |
| 113 DISALLOW_COPY_AND_ASSIGN(DeclarativeConditionSet); | 114 DISALLOW_COPY_AND_ASSIGN(DeclarativeConditionSet); |
| 114 }; | 115 }; |
| 115 | 116 |
| 116 // Immutable container for multiple actions. | 117 // Immutable container for multiple actions. |
| 117 // | 118 // |
| 118 // ActionT should be immutable after creation. It must define the following | 119 // ActionT should be immutable after creation. It must define the following |
| 119 // members: | 120 // members: |
| 120 // | 121 // |
| 121 // // Arguments passed through from ActionSet::Create. | 122 // // Arguments passed through from ActionSet::Create. |
| 122 // static scoped_ptr<ActionT> Create( | 123 // static std::unique_ptr<ActionT> Create( |
| 123 // const Extension* extension, | 124 // const Extension* extension, |
| 124 // // Except this argument gets elements of the Values array. | 125 // // Except this argument gets elements of the Values array. |
| 125 // const base::Value& definition, | 126 // const base::Value& definition, |
| 126 // std::string* error, bool* bad_message); | 127 // std::string* error, bool* bad_message); |
| 127 // void Apply(const std::string& extension_id, | 128 // void Apply(const std::string& extension_id, |
| 128 // const base::Time& extension_install_time, | 129 // const base::Time& extension_install_time, |
| 129 // // Contains action-type-specific in/out parameters. | 130 // // Contains action-type-specific in/out parameters. |
| 130 // typename ActionT::ApplyInfo* apply_info) const; | 131 // typename ActionT::ApplyInfo* apply_info) const; |
| 131 // // Only needed if the RulesRegistry calls DeclarativeActionSet::Revert(). | 132 // // Only needed if the RulesRegistry calls DeclarativeActionSet::Revert(). |
| 132 // void Revert(const std::string& extension_id, | 133 // void Revert(const std::string& extension_id, |
| 133 // const base::Time& extension_install_time, | 134 // const base::Time& extension_install_time, |
| 134 // // Contains action-type-specific in/out parameters. | 135 // // Contains action-type-specific in/out parameters. |
| 135 // typename ActionT::ApplyInfo* apply_info) const; | 136 // typename ActionT::ApplyInfo* apply_info) const; |
| 136 // // Return the minimum priority of rules that can be evaluated after this | 137 // // Return the minimum priority of rules that can be evaluated after this |
| 137 // // action runs. A suitable default value is MIN_INT. | 138 // // action runs. A suitable default value is MIN_INT. |
| 138 // int minimum_priority() const; | 139 // int minimum_priority() const; |
| 139 // | 140 // |
| 140 // TODO(battre): As DeclarativeActionSet can become the single owner of all | 141 // TODO(battre): As DeclarativeActionSet can become the single owner of all |
| 141 // actions, we can optimize here by making some of them singletons (e.g. Cancel | 142 // actions, we can optimize here by making some of them singletons (e.g. Cancel |
| 142 // actions). | 143 // actions). |
| 143 template<typename ActionT> | 144 template<typename ActionT> |
| 144 class DeclarativeActionSet { | 145 class DeclarativeActionSet { |
| 145 public: | 146 public: |
| 146 typedef std::vector<scoped_ptr<base::Value>> Values; | 147 typedef std::vector<std::unique_ptr<base::Value>> Values; |
| 147 typedef std::vector<scoped_refptr<const ActionT> > Actions; | 148 typedef std::vector<scoped_refptr<const ActionT> > Actions; |
| 148 | 149 |
| 149 explicit DeclarativeActionSet(const Actions& actions); | 150 explicit DeclarativeActionSet(const Actions& actions); |
| 150 | 151 |
| 151 // Factory method that instantiates a DeclarativeActionSet for |extension| | 152 // Factory method that instantiates a DeclarativeActionSet for |extension| |
| 152 // according to |actions| which represents the array of actions received from | 153 // according to |actions| which represents the array of actions received from |
| 153 // the extension API. | 154 // the extension API. |
| 154 static scoped_ptr<DeclarativeActionSet> Create( | 155 static std::unique_ptr<DeclarativeActionSet> Create( |
| 155 content::BrowserContext* browser_context, | 156 content::BrowserContext* browser_context, |
| 156 const Extension* extension, | 157 const Extension* extension, |
| 157 const Values& action_values, | 158 const Values& action_values, |
| 158 std::string* error, | 159 std::string* error, |
| 159 bool* bad_message); | 160 bool* bad_message); |
| 160 | 161 |
| 161 // Rules call this method when their conditions are fulfilled. | 162 // Rules call this method when their conditions are fulfilled. |
| 162 void Apply(const std::string& extension_id, | 163 void Apply(const std::string& extension_id, |
| 163 const base::Time& extension_install_time, | 164 const base::Time& extension_install_time, |
| 164 typename ActionT::ApplyInfo* apply_info) const; | 165 typename ActionT::ApplyInfo* apply_info) const; |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 209 | 210 |
| 210 // Checks whether the set of |conditions| and |actions| are consistent. | 211 // Checks whether the set of |conditions| and |actions| are consistent. |
| 211 // Returns true in case of consistency and MUST set |error| otherwise. | 212 // Returns true in case of consistency and MUST set |error| otherwise. |
| 212 typedef base::Callback<bool(const ConditionSet* conditions, | 213 typedef base::Callback<bool(const ConditionSet* conditions, |
| 213 const ActionSet* actions, | 214 const ActionSet* actions, |
| 214 std::string* error)> ConsistencyChecker; | 215 std::string* error)> ConsistencyChecker; |
| 215 | 216 |
| 216 DeclarativeRule(const GlobalRuleId& id, | 217 DeclarativeRule(const GlobalRuleId& id, |
| 217 const Tags& tags, | 218 const Tags& tags, |
| 218 base::Time extension_installation_time, | 219 base::Time extension_installation_time, |
| 219 scoped_ptr<ConditionSet> conditions, | 220 std::unique_ptr<ConditionSet> conditions, |
| 220 scoped_ptr<ActionSet> actions, | 221 std::unique_ptr<ActionSet> actions, |
| 221 Priority priority); | 222 Priority priority); |
| 222 | 223 |
| 223 // Creates a DeclarativeRule for |extension| given a json definition. The | 224 // Creates a DeclarativeRule for |extension| given a json definition. The |
| 224 // format of each condition and action's json is up to the specific ConditionT | 225 // format of each condition and action's json is up to the specific ConditionT |
| 225 // and ActionT. |extension| may be NULL in tests. | 226 // and ActionT. |extension| may be NULL in tests. |
| 226 // | 227 // |
| 227 // Before constructing the final rule, calls check_consistency(conditions, | 228 // Before constructing the final rule, calls check_consistency(conditions, |
| 228 // actions, error) and returns NULL if it fails. Pass NULL if no consistency | 229 // actions, error) and returns NULL if it fails. Pass NULL if no consistency |
| 229 // check is needed. If |error| is empty, the translation was successful and | 230 // check is needed. If |error| is empty, the translation was successful and |
| 230 // the returned rule is internally consistent. | 231 // the returned rule is internally consistent. |
| 231 static scoped_ptr<DeclarativeRule> Create( | 232 static std::unique_ptr<DeclarativeRule> Create( |
| 232 url_matcher::URLMatcherConditionFactory* url_matcher_condition_factory, | 233 url_matcher::URLMatcherConditionFactory* url_matcher_condition_factory, |
| 233 content::BrowserContext* browser_context, | 234 content::BrowserContext* browser_context, |
| 234 const Extension* extension, | 235 const Extension* extension, |
| 235 base::Time extension_installation_time, | 236 base::Time extension_installation_time, |
| 236 linked_ptr<JsonRule> rule, | 237 linked_ptr<JsonRule> rule, |
| 237 ConsistencyChecker check_consistency, | 238 ConsistencyChecker check_consistency, |
| 238 std::string* error); | 239 std::string* error); |
| 239 | 240 |
| 240 const GlobalRuleId& id() const { return id_; } | 241 const GlobalRuleId& id() const { return id_; } |
| 241 const Tags& tags() const { return tags_; } | 242 const Tags& tags() const { return tags_; } |
| (...skipping 10 matching lines...) Expand all Loading... |
| 252 | 253 |
| 253 // Returns the minimum priority of rules that may be evaluated after | 254 // Returns the minimum priority of rules that may be evaluated after |
| 254 // this rule. Defaults to MIN_INT. Only valid if the conditions of this rule | 255 // this rule. Defaults to MIN_INT. Only valid if the conditions of this rule |
| 255 // are fulfilled. | 256 // are fulfilled. |
| 256 Priority GetMinimumPriority() const; | 257 Priority GetMinimumPriority() const; |
| 257 | 258 |
| 258 private: | 259 private: |
| 259 GlobalRuleId id_; | 260 GlobalRuleId id_; |
| 260 Tags tags_; | 261 Tags tags_; |
| 261 base::Time extension_installation_time_; // For precedences of rules. | 262 base::Time extension_installation_time_; // For precedences of rules. |
| 262 scoped_ptr<ConditionSet> conditions_; | 263 std::unique_ptr<ConditionSet> conditions_; |
| 263 scoped_ptr<ActionSet> actions_; | 264 std::unique_ptr<ActionSet> actions_; |
| 264 Priority priority_; | 265 Priority priority_; |
| 265 | 266 |
| 266 DISALLOW_COPY_AND_ASSIGN(DeclarativeRule); | 267 DISALLOW_COPY_AND_ASSIGN(DeclarativeRule); |
| 267 }; | 268 }; |
| 268 | 269 |
| 269 // Implementation details below here. | 270 // Implementation details below here. |
| 270 | 271 |
| 271 // | 272 // |
| 272 // DeclarativeConditionSet | 273 // DeclarativeConditionSet |
| 273 // | 274 // |
| (...skipping 20 matching lines...) Expand all Loading... |
| 294 | 295 |
| 295 template<typename ConditionT> | 296 template<typename ConditionT> |
| 296 void DeclarativeConditionSet<ConditionT>::GetURLMatcherConditionSets( | 297 void DeclarativeConditionSet<ConditionT>::GetURLMatcherConditionSets( |
| 297 url_matcher::URLMatcherConditionSet::Vector* condition_sets) const { | 298 url_matcher::URLMatcherConditionSet::Vector* condition_sets) const { |
| 298 for (const linked_ptr<const ConditionT>& condition : conditions_) | 299 for (const linked_ptr<const ConditionT>& condition : conditions_) |
| 299 condition->GetURLMatcherConditionSets(condition_sets); | 300 condition->GetURLMatcherConditionSets(condition_sets); |
| 300 } | 301 } |
| 301 | 302 |
| 302 // static | 303 // static |
| 303 template <typename ConditionT> | 304 template <typename ConditionT> |
| 304 scoped_ptr<DeclarativeConditionSet<ConditionT>> | 305 std::unique_ptr<DeclarativeConditionSet<ConditionT>> |
| 305 DeclarativeConditionSet<ConditionT>::Create( | 306 DeclarativeConditionSet<ConditionT>::Create( |
| 306 const Extension* extension, | 307 const Extension* extension, |
| 307 url_matcher::URLMatcherConditionFactory* url_matcher_condition_factory, | 308 url_matcher::URLMatcherConditionFactory* url_matcher_condition_factory, |
| 308 const Values& condition_values, | 309 const Values& condition_values, |
| 309 std::string* error) { | 310 std::string* error) { |
| 310 Conditions result; | 311 Conditions result; |
| 311 | 312 |
| 312 for (const scoped_ptr<base::Value>& value : condition_values) { | 313 for (const std::unique_ptr<base::Value>& value : condition_values) { |
| 313 CHECK(value.get()); | 314 CHECK(value.get()); |
| 314 scoped_ptr<ConditionT> condition = ConditionT::Create( | 315 std::unique_ptr<ConditionT> condition = ConditionT::Create( |
| 315 extension, url_matcher_condition_factory, *value, error); | 316 extension, url_matcher_condition_factory, *value, error); |
| 316 if (!error->empty()) | 317 if (!error->empty()) |
| 317 return scoped_ptr<DeclarativeConditionSet>(); | 318 return std::unique_ptr<DeclarativeConditionSet>(); |
| 318 result.push_back(make_linked_ptr(condition.release())); | 319 result.push_back(make_linked_ptr(condition.release())); |
| 319 } | 320 } |
| 320 | 321 |
| 321 URLMatcherIdToCondition match_id_to_condition; | 322 URLMatcherIdToCondition match_id_to_condition; |
| 322 std::vector<const ConditionT*> conditions_without_urls; | 323 std::vector<const ConditionT*> conditions_without_urls; |
| 323 url_matcher::URLMatcherConditionSet::Vector condition_sets; | 324 url_matcher::URLMatcherConditionSet::Vector condition_sets; |
| 324 | 325 |
| 325 for (const linked_ptr<const ConditionT>& condition : result) { | 326 for (const linked_ptr<const ConditionT>& condition : result) { |
| 326 condition_sets.clear(); | 327 condition_sets.clear(); |
| 327 condition->GetURLMatcherConditionSets(&condition_sets); | 328 condition->GetURLMatcherConditionSets(&condition_sets); |
| 328 if (condition_sets.empty()) { | 329 if (condition_sets.empty()) { |
| 329 conditions_without_urls.push_back(condition.get()); | 330 conditions_without_urls.push_back(condition.get()); |
| 330 } else { | 331 } else { |
| 331 for (const scoped_refptr<url_matcher::URLMatcherConditionSet>& match_set : | 332 for (const scoped_refptr<url_matcher::URLMatcherConditionSet>& match_set : |
| 332 condition_sets) | 333 condition_sets) |
| 333 match_id_to_condition[match_set->id()] = condition.get(); | 334 match_id_to_condition[match_set->id()] = condition.get(); |
| 334 } | 335 } |
| 335 } | 336 } |
| 336 | 337 |
| 337 return make_scoped_ptr(new DeclarativeConditionSet( | 338 return base::WrapUnique(new DeclarativeConditionSet( |
| 338 result, match_id_to_condition, conditions_without_urls)); | 339 result, match_id_to_condition, conditions_without_urls)); |
| 339 } | 340 } |
| 340 | 341 |
| 341 template<typename ConditionT> | 342 template<typename ConditionT> |
| 342 DeclarativeConditionSet<ConditionT>::DeclarativeConditionSet( | 343 DeclarativeConditionSet<ConditionT>::DeclarativeConditionSet( |
| 343 const Conditions& conditions, | 344 const Conditions& conditions, |
| 344 const URLMatcherIdToCondition& match_id_to_condition, | 345 const URLMatcherIdToCondition& match_id_to_condition, |
| 345 const std::vector<const ConditionT*>& conditions_without_urls) | 346 const std::vector<const ConditionT*>& conditions_without_urls) |
| 346 : match_id_to_condition_(match_id_to_condition), | 347 : match_id_to_condition_(match_id_to_condition), |
| 347 conditions_(conditions), | 348 conditions_(conditions), |
| 348 conditions_without_urls_(conditions_without_urls) {} | 349 conditions_without_urls_(conditions_without_urls) {} |
| 349 | 350 |
| 350 // | 351 // |
| 351 // DeclarativeActionSet | 352 // DeclarativeActionSet |
| 352 // | 353 // |
| 353 | 354 |
| 354 template<typename ActionT> | 355 template<typename ActionT> |
| 355 DeclarativeActionSet<ActionT>::DeclarativeActionSet(const Actions& actions) | 356 DeclarativeActionSet<ActionT>::DeclarativeActionSet(const Actions& actions) |
| 356 : actions_(actions) {} | 357 : actions_(actions) {} |
| 357 | 358 |
| 358 // static | 359 // static |
| 359 template <typename ActionT> | 360 template <typename ActionT> |
| 360 scoped_ptr<DeclarativeActionSet<ActionT>> DeclarativeActionSet<ActionT>::Create( | 361 std::unique_ptr<DeclarativeActionSet<ActionT>> |
| 361 content::BrowserContext* browser_context, | 362 DeclarativeActionSet<ActionT>::Create(content::BrowserContext* browser_context, |
| 362 const Extension* extension, | 363 const Extension* extension, |
| 363 const Values& action_values, | 364 const Values& action_values, |
| 364 std::string* error, | 365 std::string* error, |
| 365 bool* bad_message) { | 366 bool* bad_message) { |
| 366 *error = ""; | 367 *error = ""; |
| 367 *bad_message = false; | 368 *bad_message = false; |
| 368 Actions result; | 369 Actions result; |
| 369 | 370 |
| 370 for (const scoped_ptr<base::Value>& value : action_values) { | 371 for (const std::unique_ptr<base::Value>& value : action_values) { |
| 371 CHECK(value.get()); | 372 CHECK(value.get()); |
| 372 scoped_refptr<const ActionT> action = | 373 scoped_refptr<const ActionT> action = |
| 373 ActionT::Create(browser_context, extension, *value, error, bad_message); | 374 ActionT::Create(browser_context, extension, *value, error, bad_message); |
| 374 if (!error->empty() || *bad_message) | 375 if (!error->empty() || *bad_message) |
| 375 return scoped_ptr<DeclarativeActionSet>(); | 376 return std::unique_ptr<DeclarativeActionSet>(); |
| 376 result.push_back(action); | 377 result.push_back(action); |
| 377 } | 378 } |
| 378 | 379 |
| 379 return scoped_ptr<DeclarativeActionSet>(new DeclarativeActionSet(result)); | 380 return std::unique_ptr<DeclarativeActionSet>( |
| 381 new DeclarativeActionSet(result)); |
| 380 } | 382 } |
| 381 | 383 |
| 382 template<typename ActionT> | 384 template<typename ActionT> |
| 383 void DeclarativeActionSet<ActionT>::Apply( | 385 void DeclarativeActionSet<ActionT>::Apply( |
| 384 const std::string& extension_id, | 386 const std::string& extension_id, |
| 385 const base::Time& extension_install_time, | 387 const base::Time& extension_install_time, |
| 386 typename ActionT::ApplyInfo* apply_info) const { | 388 typename ActionT::ApplyInfo* apply_info) const { |
| 387 for (const scoped_refptr<const ActionT>& action : actions_) | 389 for (const scoped_refptr<const ActionT>& action : actions_) |
| 388 action->Apply(extension_id, extension_install_time, apply_info); | 390 action->Apply(extension_id, extension_install_time, apply_info); |
| 389 } | 391 } |
| (...skipping 23 matching lines...) Expand all Loading... |
| 413 i != actions_.end(); ++i) { | 415 i != actions_.end(); ++i) { |
| 414 minimum_priority = std::max(minimum_priority, (*i)->minimum_priority()); | 416 minimum_priority = std::max(minimum_priority, (*i)->minimum_priority()); |
| 415 } | 417 } |
| 416 return minimum_priority; | 418 return minimum_priority; |
| 417 } | 419 } |
| 418 | 420 |
| 419 // | 421 // |
| 420 // DeclarativeRule | 422 // DeclarativeRule |
| 421 // | 423 // |
| 422 | 424 |
| 423 template<typename ConditionT, typename ActionT> | 425 template <typename ConditionT, typename ActionT> |
| 424 DeclarativeRule<ConditionT, ActionT>::DeclarativeRule( | 426 DeclarativeRule<ConditionT, ActionT>::DeclarativeRule( |
| 425 const GlobalRuleId& id, | 427 const GlobalRuleId& id, |
| 426 const Tags& tags, | 428 const Tags& tags, |
| 427 base::Time extension_installation_time, | 429 base::Time extension_installation_time, |
| 428 scoped_ptr<ConditionSet> conditions, | 430 std::unique_ptr<ConditionSet> conditions, |
| 429 scoped_ptr<ActionSet> actions, | 431 std::unique_ptr<ActionSet> actions, |
| 430 Priority priority) | 432 Priority priority) |
| 431 : id_(id), | 433 : id_(id), |
| 432 tags_(tags), | 434 tags_(tags), |
| 433 extension_installation_time_(extension_installation_time), | 435 extension_installation_time_(extension_installation_time), |
| 434 conditions_(conditions.release()), | 436 conditions_(conditions.release()), |
| 435 actions_(actions.release()), | 437 actions_(actions.release()), |
| 436 priority_(priority) { | 438 priority_(priority) { |
| 437 CHECK(conditions_.get()); | 439 CHECK(conditions_.get()); |
| 438 CHECK(actions_.get()); | 440 CHECK(actions_.get()); |
| 439 } | 441 } |
| 440 | 442 |
| 441 // static | 443 // static |
| 442 template<typename ConditionT, typename ActionT> | 444 template <typename ConditionT, typename ActionT> |
| 443 scoped_ptr<DeclarativeRule<ConditionT, ActionT> > | 445 std::unique_ptr<DeclarativeRule<ConditionT, ActionT>> |
| 444 DeclarativeRule<ConditionT, ActionT>::Create( | 446 DeclarativeRule<ConditionT, ActionT>::Create( |
| 445 url_matcher::URLMatcherConditionFactory* url_matcher_condition_factory, | 447 url_matcher::URLMatcherConditionFactory* url_matcher_condition_factory, |
| 446 content::BrowserContext* browser_context, | 448 content::BrowserContext* browser_context, |
| 447 const Extension* extension, | 449 const Extension* extension, |
| 448 base::Time extension_installation_time, | 450 base::Time extension_installation_time, |
| 449 linked_ptr<JsonRule> rule, | 451 linked_ptr<JsonRule> rule, |
| 450 ConsistencyChecker check_consistency, | 452 ConsistencyChecker check_consistency, |
| 451 std::string* error) { | 453 std::string* error) { |
| 452 scoped_ptr<DeclarativeRule> error_result; | 454 std::unique_ptr<DeclarativeRule> error_result; |
| 453 | 455 |
| 454 scoped_ptr<ConditionSet> conditions = ConditionSet::Create( | 456 std::unique_ptr<ConditionSet> conditions = ConditionSet::Create( |
| 455 extension, url_matcher_condition_factory, rule->conditions, error); | 457 extension, url_matcher_condition_factory, rule->conditions, error); |
| 456 if (!error->empty()) | 458 if (!error->empty()) |
| 457 return std::move(error_result); | 459 return std::move(error_result); |
| 458 CHECK(conditions.get()); | 460 CHECK(conditions.get()); |
| 459 | 461 |
| 460 bool bad_message = false; | 462 bool bad_message = false; |
| 461 scoped_ptr<ActionSet> actions = | 463 std::unique_ptr<ActionSet> actions = ActionSet::Create( |
| 462 ActionSet::Create( | 464 browser_context, extension, rule->actions, error, &bad_message); |
| 463 browser_context, extension, rule->actions, error, &bad_message); | |
| 464 if (bad_message) { | 465 if (bad_message) { |
| 465 // TODO(battre) Export concept of bad_message to caller, the extension | 466 // TODO(battre) Export concept of bad_message to caller, the extension |
| 466 // should be killed in case it is true. | 467 // should be killed in case it is true. |
| 467 *error = "An action of a rule set had an invalid " | 468 *error = "An action of a rule set had an invalid " |
| 468 "structure that should have been caught by the JSON validator."; | 469 "structure that should have been caught by the JSON validator."; |
| 469 return std::move(error_result); | 470 return std::move(error_result); |
| 470 } | 471 } |
| 471 if (!error->empty() || bad_message) | 472 if (!error->empty() || bad_message) |
| 472 return std::move(error_result); | 473 return std::move(error_result); |
| 473 CHECK(actions.get()); | 474 CHECK(actions.get()); |
| 474 | 475 |
| 475 if (!check_consistency.is_null() && | 476 if (!check_consistency.is_null() && |
| 476 !check_consistency.Run(conditions.get(), actions.get(), error)) { | 477 !check_consistency.Run(conditions.get(), actions.get(), error)) { |
| 477 DCHECK(!error->empty()); | 478 DCHECK(!error->empty()); |
| 478 return std::move(error_result); | 479 return std::move(error_result); |
| 479 } | 480 } |
| 480 | 481 |
| 481 CHECK(rule->priority.get()); | 482 CHECK(rule->priority.get()); |
| 482 int priority = *(rule->priority); | 483 int priority = *(rule->priority); |
| 483 | 484 |
| 484 GlobalRuleId rule_id(extension->id(), *(rule->id)); | 485 GlobalRuleId rule_id(extension->id(), *(rule->id)); |
| 485 Tags tags = rule->tags ? *rule->tags : Tags(); | 486 Tags tags = rule->tags ? *rule->tags : Tags(); |
| 486 return scoped_ptr<DeclarativeRule>( | 487 return std::unique_ptr<DeclarativeRule>( |
| 487 new DeclarativeRule(rule_id, tags, extension_installation_time, | 488 new DeclarativeRule(rule_id, tags, extension_installation_time, |
| 488 std::move(conditions), std::move(actions), priority)); | 489 std::move(conditions), std::move(actions), priority)); |
| 489 } | 490 } |
| 490 | 491 |
| 491 template<typename ConditionT, typename ActionT> | 492 template<typename ConditionT, typename ActionT> |
| 492 void DeclarativeRule<ConditionT, ActionT>::Apply( | 493 void DeclarativeRule<ConditionT, ActionT>::Apply( |
| 493 typename ActionT::ApplyInfo* apply_info) const { | 494 typename ActionT::ApplyInfo* apply_info) const { |
| 494 return actions_->Apply(extension_id(), | 495 return actions_->Apply(extension_id(), |
| 495 extension_installation_time_, | 496 extension_installation_time_, |
| 496 apply_info); | 497 apply_info); |
| 497 } | 498 } |
| 498 | 499 |
| 499 template<typename ConditionT, typename ActionT> | 500 template<typename ConditionT, typename ActionT> |
| 500 int DeclarativeRule<ConditionT, ActionT>::GetMinimumPriority() const { | 501 int DeclarativeRule<ConditionT, ActionT>::GetMinimumPriority() const { |
| 501 return actions_->GetMinimumPriority(); | 502 return actions_->GetMinimumPriority(); |
| 502 } | 503 } |
| 503 | 504 |
| 504 } // namespace extensions | 505 } // namespace extensions |
| 505 | 506 |
| 506 #endif // EXTENSIONS_BROWSER_API_DECLARATIVE_DECLARATIVE_RULE_H__ | 507 #endif // EXTENSIONS_BROWSER_API_DECLARATIVE_DECLARATIVE_RULE_H__ |
| OLD | NEW |