Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(58)

Side by Side Diff: chrome/browser/extensions/api/declarative/declarative_rule.h

Issue 11414230: Declarative Web Request: firstPartyForCookiesUrl (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: More checks and one more unit test Created 7 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_DECLARATIVE_RULE_H__ 10 #ifndef CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_DECLARATIVE_RULE_H__
11 #define CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_DECLARATIVE_RULE_H__ 11 #define CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_DECLARATIVE_RULE_H__
12 12
13 #include <limits> 13 #include <limits>
14 #include <set>
15 #include <string>
16 #include <vector>
14 17
15 #include "base/memory/linked_ptr.h" 18 #include "base/memory/linked_ptr.h"
16 #include "base/memory/scoped_vector.h" 19 #include "base/memory/scoped_vector.h"
20 #include "base/stl_util.h"
17 #include "base/time.h" 21 #include "base/time.h"
18 #include "chrome/common/extensions/api/events.h" 22 #include "chrome/common/extensions/api/events.h"
19 #include "chrome/common/extensions/matcher/url_matcher.h" 23 #include "chrome/common/extensions/matcher/url_matcher.h"
20 24
21 namespace base { 25 namespace base {
22 class Time; 26 class Time;
23 class Value; 27 class Value;
24 } 28 }
25 29
26 namespace extensions { 30 namespace extensions {
27 31
28 // This class stores a set of conditions that may be part of a DeclarativeRule. 32 // This class stores a set of conditions that may be part of a DeclarativeRule.
29 // If any condition is fulfilled, the Actions of the DeclarativeRule can be 33 // If any condition is fulfilled, the Actions of the DeclarativeRule can be
30 // triggered. 34 // triggered.
31 // 35 //
32 // ConditionT should be immutable after creation. It must define the following 36 // ConditionT should be immutable after creation. It must define the following
33 // members: 37 // members:
34 // 38 //
35 // // Arguments passed through from ConditionSet::Create. 39 // // Arguments passed through from DeclarativeConditionSet::Create.
36 // static scoped_ptr<ConditionT> Create( 40 // static scoped_ptr<ConditionT> Create(
37 // URLMatcherConditionFactory*, 41 // const std::vector<URLMatcherConditionFactory*>&
42 // url_matcher_condition_factories,
38 // // Except this argument gets elements of the AnyVector. 43 // // Except this argument gets elements of the AnyVector.
39 // const base::Value& definition, 44 // const base::Value& definition,
40 // std::string* error); 45 // std::string* error);
41 // // If the Condition needs to be filtered by some 46 // // If the Condition needs to be filtered by some URLMatcherConditionSets,
42 // // URLMatcherConditionSets, append them to this argument. 47 // // append them to |condition_sets|. Use |index| to select the type of a
48 // // URL attribute, multiple can be present. Valid values of |index| start
49 // // at 0 and increase by 1. The return value always gives the next higher
50 // // valid index, or -1 if there is none. It is thus possible to iterate over
51 // // all valid indices by starting with 0 and using the return values in
52 // // subsequent calls until -1 is returned.
43 // // DeclarativeConditionSet::GetURLMatcherConditionSets forwards here. 53 // // DeclarativeConditionSet::GetURLMatcherConditionSets forwards here.
44 // void GetURLMatcherConditionSets( 54 // int GetURLMatcherConditionSets(
45 // URLMatcherConditionSet::Vector* condition_sets) 55 // URLMatcherConditionSet::Vector* condition_sets,
46 // // True if GetURLMatcherConditionSets would append anything to its 56 // int index);
47 // // argument.
48 // bool has_url_matcher_condition_set();
49 // // |url_matches| and |match_data| passed through from 57 // // |url_matches| and |match_data| passed through from
50 // // ConditionSet::IsFulfilled. 58 // // DeclarativeConditionSet::IsFulfilled.
51 // bool IsFulfilled( 59 // bool IsFulfilled(
52 // const std::set<URLMatcherConditionSet::ID>& url_matches, 60 // const std::set<URLMatcherConditionSet::ID>& url_matches,
53 // const ConditionT::MatchData&); 61 // const ConditionT::MatchData&);
54 template<typename ConditionT> 62 template<typename ConditionT>
55 class DeclarativeConditionSet { 63 class DeclarativeConditionSet {
56 public: 64 public:
57 typedef std::vector<linked_ptr<base::Value> > AnyVector; 65 typedef std::vector<linked_ptr<base::Value> > AnyVector;
58 typedef std::vector<linked_ptr<const ConditionT> > Conditions; 66 typedef std::vector<linked_ptr<const ConditionT> > Conditions;
59 typedef typename Conditions::const_iterator const_iterator; 67 typedef typename Conditions::const_iterator const_iterator;
60 68
61 // Factory method that creates an WebRequestConditionSet according to the JSON 69 // Factory method that creates a WebRequestConditionSet according to the JSON
62 // array |conditions| passed by the extension API. 70 // array |conditions| passed by the extension API. Because IsFulfilled gets
71 // passed the union of IDs of all matching URLMatcherConditionSets coming
72 // from all URLMatcherConditionFactories, you should make sure that the
73 // factories use strictly disjoined URLMatcherConditionSets::IDs. This allows
74 // one to tell which attribute had a match in IsFulfilled.
63 // Sets |error| and returns NULL in case of an error. 75 // Sets |error| and returns NULL in case of an error.
64 static scoped_ptr<DeclarativeConditionSet> Create( 76 static scoped_ptr<DeclarativeConditionSet> Create(
65 URLMatcherConditionFactory* url_matcher_condition_factory, 77 const std::vector<URLMatcherConditionFactory*>&
78 url_matcher_condition_factories,
66 const AnyVector& conditions, 79 const AnyVector& conditions,
67 std::string* error); 80 std::string* error);
68 81
69 const Conditions& conditions() const { 82 const Conditions& conditions() const {
70 return conditions_; 83 return conditions_;
71 } 84 }
72 85
73 const_iterator begin() const { return conditions_.begin(); } 86 const_iterator begin() const { return conditions_.begin(); }
74 const_iterator end() const { return conditions_.end(); } 87 const_iterator end() const { return conditions_.end(); }
75 88
76 // If |url_match_trigger| is a member of |url_matches|, then this 89 // If |url_match_trigger| is a member of |url_matches|, then this returns
77 // returns whether the corresponding condition is fulfilled 90 // whether the corresponding condition is fulfilled wrt. |request_data|.
78 // wrt. |request_data|. If |url_match_trigger| is -1, this function 91 // Therefore, if |match_data| contains URL matches from more matchers, the
79 // returns whether any of the conditions without URL attributes is 92 // union of the matches should appear in |url_matches|. For kinds of MatchData
80 // satisfied. 93 // that only have one type of URL, |url_matches| is forwarded on to
81 //
82 // Conditions for which has_url_matcher_condition_set() is false are always
83 // checked (aside from short-circuiting when an earlier condition already
84 // matched.)
85 //
86 // Conditions for which has_url_matcher_condition_set() is true are only
87 // checked if one of the URLMatcherConditionSets returned by
88 // GetURLMatcherConditionSets() has an id listed in url_matches. That means
89 // that if |match_data| contains URL matches for two pieces of a request,
90 // their union should appear in |url_matches|. For kinds of MatchData that
91 // only have one type of URL, |url_matches| is forwarded on to
92 // ConditionT::IsFulfilled(), so it doesn't need to appear in |match_data|. 94 // ConditionT::IsFulfilled(), so it doesn't need to appear in |match_data|.
95 // If |url_match_trigger| is -1, this function returns whether any of the
96 // conditions without URL attributes is satisfied.
93 bool IsFulfilled(URLMatcherConditionSet::ID url_match_trigger, 97 bool IsFulfilled(URLMatcherConditionSet::ID url_match_trigger,
94 const std::set<URLMatcherConditionSet::ID>& url_matches, 98 const std::set<URLMatcherConditionSet::ID>& url_matches,
95 const typename ConditionT::MatchData& match_data) const; 99 const typename ConditionT::MatchData& match_data) const;
96 100
97 // Appends the URLMatcherConditionSet from all conditions to |condition_sets|. 101 // Appends the URLMatcherConditionSet from all conditions to |condition_sets|.
98 void GetURLMatcherConditionSets( 102 // Returns |index| + 1 if at least one condition's GetURLMatcherConditionSets
99 URLMatcherConditionSet::Vector* condition_sets) const; 103 // returns |index| + 1, otherwise returns -1.
104 int GetURLMatcherConditionSets(
105 URLMatcherConditionSet::Vector* condition_sets,
106 int index) const;
100 107
101 // Returns whether there are some conditions without UrlFilter attributes. 108 // Returns whether there are some conditions without UrlFilter attributes.
102 bool HasConditionsWithoutUrls() const { 109 bool HasConditionsWithoutUrls() const {
103 return !conditions_without_urls_.empty(); 110 return !conditions_without_urls_.empty();
104 } 111 }
105 112
106 private: 113 private:
107 typedef std::map<URLMatcherConditionSet::ID, const ConditionT*> 114 typedef std::map<URLMatcherConditionSet::ID, const ConditionT*>
108 URLMatcherIdToCondition; 115 URLMatcherIdToCondition;
109 116
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after
216 223
217 // Creates a DeclarativeRule for an extension given a json definition. The 224 // Creates a DeclarativeRule for an extension given a json definition. The
218 // 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
219 // and ActionT. 226 // and ActionT.
220 // 227 //
221 // Before constructing the final rule, calls check_consistency(conditions, 228 // Before constructing the final rule, calls check_consistency(conditions,
222 // 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
223 // 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
224 // the returned rule is internally consistent. 231 // the returned rule is internally consistent.
225 static scoped_ptr<DeclarativeRule> Create( 232 static scoped_ptr<DeclarativeRule> Create(
226 URLMatcherConditionFactory* url_matcher_condition_factory, 233 const std::vector<URLMatcherConditionFactory*>&
234 url_matcher_condition_factories,
227 const std::string& extension_id, 235 const std::string& extension_id,
228 base::Time extension_installation_time, 236 base::Time extension_installation_time,
229 linked_ptr<JsonRule> rule, 237 linked_ptr<JsonRule> rule,
230 ConsistencyChecker check_consistency, 238 ConsistencyChecker check_consistency,
231 std::string* error); 239 std::string* error);
232 240
233 const GlobalRuleId& id() const { return id_; } 241 const GlobalRuleId& id() const { return id_; }
234 const std::string& extension_id() const { return id_.first; } 242 const std::string& extension_id() const { return id_.first; }
235 const ConditionSet& conditions() const { return *conditions_; } 243 const ConditionSet& conditions() const { return *conditions_; }
236 const ActionSet& actions() const { return *actions_; } 244 const ActionSet& actions() const { return *actions_; }
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
280 return false; 288 return false;
281 } 289 }
282 290
283 typename URLMatcherIdToCondition::const_iterator triggered = 291 typename URLMatcherIdToCondition::const_iterator triggered =
284 match_id_to_condition_.find(url_match_trigger); 292 match_id_to_condition_.find(url_match_trigger);
285 return (triggered != match_id_to_condition_.end() && 293 return (triggered != match_id_to_condition_.end() &&
286 triggered->second->IsFulfilled(url_matches, match_data)); 294 triggered->second->IsFulfilled(url_matches, match_data));
287 } 295 }
288 296
289 template<typename ConditionT> 297 template<typename ConditionT>
290 void DeclarativeConditionSet<ConditionT>::GetURLMatcherConditionSets( 298 int DeclarativeConditionSet<ConditionT>::GetURLMatcherConditionSets(
291 URLMatcherConditionSet::Vector* condition_sets) const { 299 URLMatcherConditionSet::Vector* condition_sets,
300 int index) const {
301 bool more_condition_sets_exist = false;
292 for (typename Conditions::const_iterator i = conditions_.begin(); 302 for (typename Conditions::const_iterator i = conditions_.begin();
293 i != conditions_.end(); ++i) { 303 i != conditions_.end(); ++i) {
294 (*i)->GetURLMatcherConditionSets(condition_sets); 304 if ((*i)->GetURLMatcherConditionSets(condition_sets, index) >= 0)
305 more_condition_sets_exist = true;
295 } 306 }
307 return (more_condition_sets_exist ? (index + 1) : -1);
296 } 308 }
297 309
298 // static 310 // static
299 template<typename ConditionT> 311 template<typename ConditionT>
300 scoped_ptr<DeclarativeConditionSet<ConditionT> > 312 scoped_ptr<DeclarativeConditionSet<ConditionT> >
301 DeclarativeConditionSet<ConditionT>::Create( 313 DeclarativeConditionSet<ConditionT>::Create(
302 URLMatcherConditionFactory* url_matcher_condition_factory, 314 const std::vector<URLMatcherConditionFactory*>&
315 url_matcher_condition_factories,
303 const AnyVector& conditions, 316 const AnyVector& conditions,
304 std::string* error) { 317 std::string* error) {
305 Conditions result; 318 Conditions result;
306 319
307 for (AnyVector::const_iterator i = conditions.begin(); 320 for (AnyVector::const_iterator i = conditions.begin();
308 i != conditions.end(); ++i) { 321 i != conditions.end(); ++i) {
309 CHECK(i->get()); 322 CHECK(i->get());
310 scoped_ptr<ConditionT> condition = 323 scoped_ptr<ConditionT> condition =
311 ConditionT::Create(url_matcher_condition_factory, **i, error); 324 ConditionT::Create(url_matcher_condition_factories, **i, error);
312 if (!error->empty()) 325 if (!error->empty())
313 return scoped_ptr<DeclarativeConditionSet>(NULL); 326 return scoped_ptr<DeclarativeConditionSet>(NULL);
314 result.push_back(make_linked_ptr(condition.release())); 327 result.push_back(make_linked_ptr(condition.release()));
315 } 328 }
316 329
317 URLMatcherIdToCondition match_id_to_condition; 330 URLMatcherIdToCondition match_id_to_condition;
318 std::vector<const ConditionT*> conditions_without_urls; 331 std::vector<const ConditionT*> conditions_without_urls;
319 URLMatcherConditionSet::Vector condition_sets; 332 URLMatcherConditionSet::Vector condition_sets;
333 std::set<URLMatcherConditionSet::ID> all_ids;
320 334
321 for (typename Conditions::const_iterator i = result.begin(); 335 for (typename Conditions::const_iterator i = result.begin();
322 i != result.end(); ++i) { 336 i != result.end(); ++i) {
323 condition_sets.clear(); 337 condition_sets.clear();
324 (*i)->GetURLMatcherConditionSets(&condition_sets); 338 int index = 0;
339 int old_index = 0;
340 while (index >= 0) {
341 old_index = index;
342 index = (*i)->GetURLMatcherConditionSets(&condition_sets, index);
343 CHECK(index < 0 || index == old_index + 1);
344 }
325 if (condition_sets.empty()) { 345 if (condition_sets.empty()) {
326 conditions_without_urls.push_back(i->get()); 346 conditions_without_urls.push_back(i->get());
327 } else { 347 } else {
328 for (URLMatcherConditionSet::Vector::const_iterator 348 for (URLMatcherConditionSet::Vector::const_iterator
329 match_set = condition_sets.begin(); 349 match_set = condition_sets.begin();
330 match_set != condition_sets.end(); ++match_set) 350 match_set != condition_sets.end(); ++match_set) {
331 match_id_to_condition[(*match_set)->id()] = i->get(); 351 const URLMatcherConditionSet::ID id = (*match_set)->id();
352 match_id_to_condition[id] = i->get();
353 // If the following check fails, then likely URLMatcherConditionFactory
354 // no longer generates condition set ids unique across all instances.
355 CHECK(!ContainsKey(all_ids, id));
356 all_ids.insert(id);
357 }
332 } 358 }
333 } 359 }
334 360
335 return make_scoped_ptr(new DeclarativeConditionSet( 361 return make_scoped_ptr(new DeclarativeConditionSet(
336 result, match_id_to_condition, conditions_without_urls)); 362 result, match_id_to_condition, conditions_without_urls));
337 } 363 }
338 364
339 template<typename ConditionT> 365 template<typename ConditionT>
340 DeclarativeConditionSet<ConditionT>::DeclarativeConditionSet( 366 DeclarativeConditionSet<ConditionT>::DeclarativeConditionSet(
341 const Conditions& conditions, 367 const Conditions& conditions,
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after
423 actions_(actions.release()), 449 actions_(actions.release()),
424 priority_(priority) { 450 priority_(priority) {
425 CHECK(conditions_.get()); 451 CHECK(conditions_.get());
426 CHECK(actions_.get()); 452 CHECK(actions_.get());
427 } 453 }
428 454
429 // static 455 // static
430 template<typename ConditionT, typename ActionT> 456 template<typename ConditionT, typename ActionT>
431 scoped_ptr<DeclarativeRule<ConditionT, ActionT> > 457 scoped_ptr<DeclarativeRule<ConditionT, ActionT> >
432 DeclarativeRule<ConditionT, ActionT>::Create( 458 DeclarativeRule<ConditionT, ActionT>::Create(
433 URLMatcherConditionFactory* url_matcher_condition_factory, 459 const std::vector<URLMatcherConditionFactory*>&
460 url_matcher_condition_factories,
434 const std::string& extension_id, 461 const std::string& extension_id,
435 base::Time extension_installation_time, 462 base::Time extension_installation_time,
436 linked_ptr<JsonRule> rule, 463 linked_ptr<JsonRule> rule,
437 ConsistencyChecker check_consistency, 464 ConsistencyChecker check_consistency,
438 std::string* error) { 465 std::string* error) {
439 scoped_ptr<DeclarativeRule> error_result; 466 scoped_ptr<DeclarativeRule> error_result;
440 467
441 scoped_ptr<ConditionSet> conditions = ConditionSet::Create( 468 scoped_ptr<ConditionSet> conditions = ConditionSet::Create(
442 url_matcher_condition_factory, rule->conditions, error); 469 url_matcher_condition_factories, rule->conditions, error);
443 if (!error->empty()) 470 if (!error->empty())
444 return error_result.Pass(); 471 return error_result.Pass();
445 CHECK(conditions.get()); 472 CHECK(conditions.get());
446 473
447 bool bad_message = false; 474 bool bad_message = false;
448 scoped_ptr<ActionSet> actions = 475 scoped_ptr<ActionSet> actions =
449 ActionSet::Create(rule->actions, error, &bad_message); 476 ActionSet::Create(rule->actions, error, &bad_message);
450 if (bad_message) { 477 if (bad_message) {
451 // TODO(battre) Export concept of bad_message to caller, the extension 478 // TODO(battre) Export concept of bad_message to caller, the extension
452 // should be killed in case it is true. 479 // should be killed in case it is true.
(...skipping 29 matching lines...) Expand all
482 } 509 }
483 510
484 template<typename ConditionT, typename ActionT> 511 template<typename ConditionT, typename ActionT>
485 int DeclarativeRule<ConditionT, ActionT>::GetMinimumPriority() const { 512 int DeclarativeRule<ConditionT, ActionT>::GetMinimumPriority() const {
486 return actions_->GetMinimumPriority(); 513 return actions_->GetMinimumPriority();
487 } 514 }
488 515
489 } // namespace extensions 516 } // namespace extensions
490 517
491 #endif // CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_DECLARATIVE_RULE_H__ 518 #endif // CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_DECLARATIVE_RULE_H__
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698