OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 #ifndef CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_WEBREQUEST_WEBREQUEST_CONDITIO
N_H_ |
| 6 #define CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_WEBREQUEST_WEBREQUEST_CONDITIO
N_H_ |
| 7 #pragma once |
| 8 |
| 9 #include <string> |
| 10 #include <vector> |
| 11 |
| 12 #include "base/basictypes.h" |
| 13 #include "base/memory/linked_ptr.h" |
| 14 #include "chrome/browser/extensions/api/declarative/url_matcher.h" |
| 15 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_condit
ion_attribute.h" |
| 16 |
| 17 namespace extensions { |
| 18 |
| 19 // Representation of a condition in the Declarative WebRequest API. A condition |
| 20 // consists of several attributes. Each of these attributes needs to be |
| 21 // fulfilled in order for the condition to be fulfilled. |
| 22 // |
| 23 // We distinguish between two types of conditions: |
| 24 // - URL Matcher conditions are conditions that test the URL of a request. |
| 25 // These are treated separately because we use a URLMatcher to efficiently |
| 26 // test many of these conditions in parallel by using some advanced |
| 27 // data structures. The URLMatcher tells us if all URL Matcher conditions |
| 28 // are fulfilled for a WebRequestCondition. |
| 29 // - All other conditions are represented as WebRequestConditionAttributes. |
| 30 // These conditions are probed linearly (only if the URL Matcher found a hit). |
| 31 // |
| 32 // TODO(battre) Consider making the URLMatcher an owner of the |
| 33 // URLMatcherConditionSet and only pass a pointer to URLMatcherConditionSet |
| 34 // in url_matcher_condition_set(). This saves some copying in |
| 35 // WebRequestConditionSet::GetURLMatcherConditionSets. |
| 36 class WebRequestCondition { |
| 37 public: |
| 38 WebRequestCondition( |
| 39 const URLMatcherConditionSet& url_matcher_conditions, |
| 40 const WebRequestConditionAttributes& condition_attributes); |
| 41 ~WebRequestCondition(); |
| 42 |
| 43 // Factory method that instantiates a WebRequestCondition according to |
| 44 // the description |condition| passed by the extension API. |
| 45 static scoped_ptr<WebRequestCondition> Create( |
| 46 URLMatcherConditionFactory* url_matcher_condition_factory, |
| 47 const base::Value& condition, |
| 48 std::string* error); |
| 49 |
| 50 // Returns whether |request| is a match, given that the URLMatcher found |
| 51 // a match for |url_matcher_conditions_|. |
| 52 bool IsFulfilled(net::URLRequest* request) const; |
| 53 |
| 54 // Returns a URLMatcherConditionSet::ID which is the canonical representation |
| 55 // for all URL patterns that need to be matched by this WebRequestCondition. |
| 56 // This ID is registered in a URLMatcher that can inform us in case of a |
| 57 // match. |
| 58 URLMatcherConditionSet::ID url_matcher_condition_set_id() const { |
| 59 return url_matcher_conditions_.id(); |
| 60 } |
| 61 |
| 62 // Returns the set of conditions that are checked on the URL. This is the |
| 63 // primary trigger for WebRequestCondition and therefore never empty. |
| 64 // (If it was empty, the URLMatcher would never notify us about network |
| 65 // requests which might fulfill the entire WebRequestCondition). |
| 66 const URLMatcherConditionSet& url_matcher_condition_set() const { |
| 67 return url_matcher_conditions_; |
| 68 } |
| 69 |
| 70 private: |
| 71 // Returns whether a condition attribute with name |condition_attribute_name| |
| 72 // needs to be handled by the URLMatcher. |
| 73 static bool IsURLMatcherConditionAttribute( |
| 74 const std::string& condition_attribute_name); |
| 75 |
| 76 // Factory method of for URLMatcherConditions. |
| 77 static URLMatcherCondition CreateURLMatcherCondition( |
| 78 URLMatcherConditionFactory* url_matcher_condition_factory, |
| 79 const std::string& condition_attribute_name, |
| 80 const base::Value* value, |
| 81 std::string* error); |
| 82 |
| 83 URLMatcherConditionSet url_matcher_conditions_; |
| 84 WebRequestConditionAttributes condition_attributes_; |
| 85 |
| 86 DISALLOW_COPY_AND_ASSIGN(WebRequestCondition); |
| 87 }; |
| 88 |
| 89 // This class stores a set of conditions that may be part of a WebRequestRule. |
| 90 // If any condition is fulfilled, the WebRequestActions of the WebRequestRule |
| 91 // can be triggered. |
| 92 class WebRequestConditionSet { |
| 93 public: |
| 94 typedef std::vector<linked_ptr<json_schema_compiler::any::Any> > AnyVector; |
| 95 |
| 96 explicit WebRequestConditionSet( |
| 97 const std::vector<linked_ptr<WebRequestCondition> >& conditions); |
| 98 ~WebRequestConditionSet(); |
| 99 |
| 100 // Factory method that creates an WebRequestConditionSet according to the JSON |
| 101 // array |conditions| passed by the extension API. |
| 102 // Sets |error| and returns NULL in case of an error. |
| 103 static scoped_ptr<WebRequestConditionSet> Create( |
| 104 URLMatcherConditionFactory* url_matcher_condition_factory, |
| 105 const AnyVector& conditions, |
| 106 std::string* error); |
| 107 |
| 108 const std::vector<linked_ptr<WebRequestCondition> >& conditions() const { |
| 109 return conditions_; |
| 110 } |
| 111 |
| 112 // Returns whether any condition in the condition set is fulfilled |
| 113 // based on a match |url_match| and the value of |request|. This function |
| 114 // should be called for each URLMatcherConditionSet::ID that was found |
| 115 // by the URLMatcher to ensure that the each trigger in |match_triggers_| is |
| 116 // found. |
| 117 bool IsFulfilled(URLMatcherConditionSet::ID url_match, |
| 118 net::URLRequest* request) const; |
| 119 |
| 120 // Appends the URLMatcherConditionSet from all conditions to |condition_sets|. |
| 121 void GetURLMatcherConditionSets( |
| 122 std::vector<URLMatcherConditionSet>* condition_sets) const; |
| 123 |
| 124 private: |
| 125 typedef std::vector<linked_ptr<WebRequestCondition> > Conditions; |
| 126 Conditions conditions_; |
| 127 |
| 128 typedef std::map<URLMatcherConditionSet::ID, WebRequestCondition*> |
| 129 MatchTriggers; |
| 130 MatchTriggers match_triggers_; |
| 131 |
| 132 DISALLOW_COPY_AND_ASSIGN(WebRequestConditionSet); |
| 133 }; |
| 134 |
| 135 } // namespace extensions |
| 136 |
| 137 #endif // CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_WEBREQUEST_WEBREQUEST_CONDI
TION_H_ |
OLD | NEW |