OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_condit ion.h" | 5 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_condit ion.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/stl_util.h" | 9 #include "base/stl_util.h" |
10 #include "base/stringprintf.h" | 10 #include "base/stringprintf.h" |
(...skipping 27 matching lines...) Expand all Loading... | |
38 namespace extensions { | 38 namespace extensions { |
39 | 39 |
40 namespace keys = declarative_webrequest_constants; | 40 namespace keys = declarative_webrequest_constants; |
41 | 41 |
42 // | 42 // |
43 // WebRequestCondition | 43 // WebRequestCondition |
44 // | 44 // |
45 | 45 |
46 WebRequestCondition::WebRequestCondition( | 46 WebRequestCondition::WebRequestCondition( |
47 scoped_refptr<URLMatcherConditionSet> url_matcher_conditions, | 47 scoped_refptr<URLMatcherConditionSet> url_matcher_conditions, |
48 scoped_refptr<URLMatcherConditionSet> first_party_url_matcher_conditions, | |
48 const WebRequestConditionAttributes& condition_attributes) | 49 const WebRequestConditionAttributes& condition_attributes) |
49 : url_matcher_conditions_(url_matcher_conditions), | 50 : url_matcher_conditions_(url_matcher_conditions), |
51 first_party_url_matcher_conditions_(first_party_url_matcher_conditions), | |
50 condition_attributes_(condition_attributes), | 52 condition_attributes_(condition_attributes), |
51 applicable_request_stages_(~0) { | 53 applicable_request_stages_(~0) { |
52 for (WebRequestConditionAttributes::const_iterator i = | 54 for (WebRequestConditionAttributes::const_iterator i = |
53 condition_attributes_.begin(); i != condition_attributes_.end(); ++i) { | 55 condition_attributes_.begin(); i != condition_attributes_.end(); ++i) { |
54 applicable_request_stages_ &= (*i)->GetStages(); | 56 applicable_request_stages_ &= (*i)->GetStages(); |
55 } | 57 } |
56 } | 58 } |
57 | 59 |
58 WebRequestCondition::~WebRequestCondition() {} | 60 WebRequestCondition::~WebRequestCondition() {} |
59 | 61 |
60 bool WebRequestCondition::IsFulfilled( | 62 bool WebRequestCondition::IsFulfilled( |
61 const std::set<URLMatcherConditionSet::ID>& url_matches, | 63 const std::set<URLMatcherConditionSet::ID>& url_matches, |
62 const DeclarativeWebRequestData& request_data) const { | 64 const DeclarativeWebRequestData& request_data) const { |
63 if (!(request_data.stage & applicable_request_stages_)) { | 65 if (!(request_data.stage & applicable_request_stages_)) { |
64 // A condition that cannot be evaluated is considered as violated. | 66 // A condition that cannot be evaluated is considered as violated. |
65 return false; | 67 return false; |
66 } | 68 } |
67 | 69 |
68 // Check a UrlFilter attribute if present. | 70 // Check URL attributes if present. |
69 if (url_matcher_conditions_.get() && | 71 if ((url_matcher_conditions_.get() && |
70 !ContainsKey(url_matches, url_matcher_conditions_->id())) | 72 !ContainsKey(url_matches, url_matcher_conditions_->id())) || |
73 (first_party_url_matcher_conditions_.get() && | |
74 !ContainsKey(url_matches, first_party_url_matcher_conditions_->id()))) | |
71 return false; | 75 return false; |
72 | 76 |
73 // All condition attributes must be fulfilled for a fulfilled condition. | 77 // All condition attributes must be fulfilled for a fulfilled condition. |
74 for (WebRequestConditionAttributes::const_iterator i = | 78 for (WebRequestConditionAttributes::const_iterator i = |
75 condition_attributes_.begin(); i != condition_attributes_.end(); ++i) { | 79 condition_attributes_.begin(); |
80 i != condition_attributes_.end(); ++i) { | |
76 if (!(*i)->IsFulfilled(request_data)) | 81 if (!(*i)->IsFulfilled(request_data)) |
77 return false; | 82 return false; |
78 } | 83 } |
79 return true; | 84 return true; |
80 } | 85 } |
81 | 86 |
87 int WebRequestCondition::GetURLMatcherConditionSets( | |
88 URLMatcherConditionSet::Vector* condition_sets, | |
89 int index) const { | |
90 switch (index) { | |
91 case 0: | |
92 if (url_matcher_conditions_) | |
93 condition_sets->push_back(url_matcher_conditions_); | |
94 return 1; | |
95 case 1: | |
96 if (first_party_url_matcher_conditions_) | |
97 condition_sets->push_back(first_party_url_matcher_conditions_); | |
98 return -1; | |
99 default: | |
battre
2013/01/21 17:32:08
nit: I would add a NOTREACHED(); here.
vabr (Chromium)
2013/01/21 18:39:29
Done.
| |
100 return -1; | |
101 } | |
102 } | |
103 | |
82 // static | 104 // static |
83 scoped_ptr<WebRequestCondition> WebRequestCondition::Create( | 105 scoped_ptr<WebRequestCondition> WebRequestCondition::Create( |
84 URLMatcherConditionFactory* url_matcher_condition_factory, | 106 const std::vector<URLMatcherConditionFactory*>& |
107 url_matcher_condition_factories, | |
85 const base::Value& condition, | 108 const base::Value& condition, |
86 std::string* error) { | 109 std::string* error) { |
110 CHECK_EQ(2u, url_matcher_condition_factories.size()); | |
87 const base::DictionaryValue* condition_dict = NULL; | 111 const base::DictionaryValue* condition_dict = NULL; |
88 if (!condition.GetAsDictionary(&condition_dict)) { | 112 if (!condition.GetAsDictionary(&condition_dict)) { |
89 *error = kExpectedDictionary; | 113 *error = kExpectedDictionary; |
90 return scoped_ptr<WebRequestCondition>(NULL); | 114 return scoped_ptr<WebRequestCondition>(NULL); |
91 } | 115 } |
92 | 116 |
93 // Verify that we are dealing with a Condition whose type we understand. | 117 // Verify that we are dealing with a Condition whose type we understand. |
94 std::string instance_type; | 118 std::string instance_type; |
95 if (!condition_dict->GetString(keys::kInstanceTypeKey, &instance_type)) { | 119 if (!condition_dict->GetString(keys::kInstanceTypeKey, &instance_type)) { |
96 *error = kConditionWithoutInstanceType; | 120 *error = kConditionWithoutInstanceType; |
97 return scoped_ptr<WebRequestCondition>(NULL); | 121 return scoped_ptr<WebRequestCondition>(NULL); |
98 } | 122 } |
99 if (instance_type != keys::kRequestMatcherType) { | 123 if (instance_type != keys::kRequestMatcherType) { |
100 *error = kExpectedOtherConditionType; | 124 *error = kExpectedOtherConditionType; |
101 return scoped_ptr<WebRequestCondition>(NULL); | 125 return scoped_ptr<WebRequestCondition>(NULL); |
102 } | 126 } |
103 | 127 |
104 WebRequestConditionAttributes attributes; | 128 WebRequestConditionAttributes attributes; |
105 scoped_refptr<URLMatcherConditionSet> url_matcher_condition_set; | 129 scoped_refptr<URLMatcherConditionSet> url_matcher_condition_set; |
130 scoped_refptr<URLMatcherConditionSet> first_party_url_matcher_condition_set; | |
106 | 131 |
107 for (base::DictionaryValue::Iterator iter(*condition_dict); | 132 for (base::DictionaryValue::Iterator iter(*condition_dict); |
108 iter.HasNext(); iter.Advance()) { | 133 iter.HasNext(); iter.Advance()) { |
109 const std::string& condition_attribute_name = iter.key(); | 134 const std::string& condition_attribute_name = iter.key(); |
110 const Value& condition_attribute_value = iter.value(); | 135 const Value& condition_attribute_value = iter.value(); |
136 const bool name_is_url = condition_attribute_name == keys::kUrlKey; | |
111 if (condition_attribute_name == keys::kInstanceTypeKey) { | 137 if (condition_attribute_name == keys::kInstanceTypeKey) { |
112 // Skip this. | 138 // Skip this. |
113 } else if (condition_attribute_name == keys::kUrlKey) { | 139 } else if (name_is_url || |
140 condition_attribute_name == keys::kFirstPartyForCookiesUrlKey) { | |
114 const base::DictionaryValue* dict = NULL; | 141 const base::DictionaryValue* dict = NULL; |
115 if (!condition_attribute_value.GetAsDictionary(&dict)) { | 142 if (!condition_attribute_value.GetAsDictionary(&dict)) { |
116 *error = base::StringPrintf(kInvalidTypeOfParamter, | 143 *error = base::StringPrintf(kInvalidTypeOfParamter, |
117 condition_attribute_name.c_str()); | 144 condition_attribute_name.c_str()); |
118 } else { | 145 } else { |
119 url_matcher_condition_set = | 146 if (!name_is_url) { |
120 URLMatcherFactory::CreateFromURLFilterDictionary( | 147 first_party_url_matcher_condition_set = |
121 url_matcher_condition_factory, dict, ++g_next_id, error); | 148 URLMatcherFactory::CreateFromURLFilterDictionary( |
149 url_matcher_condition_factories[1], dict, ++g_next_id, error); | |
150 } else { | |
151 url_matcher_condition_set = | |
152 URLMatcherFactory::CreateFromURLFilterDictionary( | |
153 url_matcher_condition_factories[0], dict, ++g_next_id, error); | |
154 } | |
122 } | 155 } |
123 } else if (WebRequestConditionAttribute::IsKnownType( | 156 } else if (WebRequestConditionAttribute::IsKnownType( |
124 condition_attribute_name)) { | 157 condition_attribute_name)) { |
125 scoped_ptr<WebRequestConditionAttribute> attribute = | 158 scoped_ptr<WebRequestConditionAttribute> attribute = |
126 WebRequestConditionAttribute::Create( | 159 WebRequestConditionAttribute::Create( |
127 condition_attribute_name, | 160 condition_attribute_name, |
128 &condition_attribute_value, | 161 &condition_attribute_value, |
129 error); | 162 error); |
130 if (attribute.get()) | 163 if (attribute.get()) |
131 attributes.push_back(make_linked_ptr(attribute.release())); | 164 attributes.push_back(make_linked_ptr(attribute.release())); |
132 } else { | 165 } else { |
133 *error = base::StringPrintf(kUnknownConditionAttribute, | 166 *error = base::StringPrintf(kUnknownConditionAttribute, |
134 condition_attribute_name.c_str()); | 167 condition_attribute_name.c_str()); |
135 } | 168 } |
136 if (!error->empty()) | 169 if (!error->empty()) |
137 return scoped_ptr<WebRequestCondition>(NULL); | 170 return scoped_ptr<WebRequestCondition>(NULL); |
138 } | 171 } |
139 | 172 |
140 scoped_ptr<WebRequestCondition> result( | 173 scoped_ptr<WebRequestCondition> result( |
141 new WebRequestCondition(url_matcher_condition_set, attributes)); | 174 new WebRequestCondition(url_matcher_condition_set, |
175 first_party_url_matcher_condition_set, | |
176 attributes)); | |
142 | 177 |
143 if (!result->stages()) { | 178 if (!result->stages()) { |
144 *error = kConditionCannotBeFulfilled; | 179 *error = kConditionCannotBeFulfilled; |
145 return scoped_ptr<WebRequestCondition>(NULL); | 180 return scoped_ptr<WebRequestCondition>(NULL); |
146 } | 181 } |
147 | 182 |
148 return result.Pass(); | 183 return result.Pass(); |
149 } | 184 } |
150 | 185 |
151 } // namespace extensions | 186 } // namespace extensions |
OLD | NEW |