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: |
| 100 NOTREACHED(); |
| 101 return -1; |
| 102 } |
| 103 } |
| 104 |
82 // static | 105 // static |
83 scoped_ptr<WebRequestCondition> WebRequestCondition::Create( | 106 scoped_ptr<WebRequestCondition> WebRequestCondition::Create( |
84 URLMatcherConditionFactory* url_matcher_condition_factory, | 107 const std::vector<URLMatcherConditionFactory*>& |
| 108 url_matcher_condition_factories, |
85 const base::Value& condition, | 109 const base::Value& condition, |
86 std::string* error) { | 110 std::string* error) { |
| 111 CHECK_EQ(2u, url_matcher_condition_factories.size()); |
87 const base::DictionaryValue* condition_dict = NULL; | 112 const base::DictionaryValue* condition_dict = NULL; |
88 if (!condition.GetAsDictionary(&condition_dict)) { | 113 if (!condition.GetAsDictionary(&condition_dict)) { |
89 *error = kExpectedDictionary; | 114 *error = kExpectedDictionary; |
90 return scoped_ptr<WebRequestCondition>(NULL); | 115 return scoped_ptr<WebRequestCondition>(NULL); |
91 } | 116 } |
92 | 117 |
93 // Verify that we are dealing with a Condition whose type we understand. | 118 // Verify that we are dealing with a Condition whose type we understand. |
94 std::string instance_type; | 119 std::string instance_type; |
95 if (!condition_dict->GetString(keys::kInstanceTypeKey, &instance_type)) { | 120 if (!condition_dict->GetString(keys::kInstanceTypeKey, &instance_type)) { |
96 *error = kConditionWithoutInstanceType; | 121 *error = kConditionWithoutInstanceType; |
97 return scoped_ptr<WebRequestCondition>(NULL); | 122 return scoped_ptr<WebRequestCondition>(NULL); |
98 } | 123 } |
99 if (instance_type != keys::kRequestMatcherType) { | 124 if (instance_type != keys::kRequestMatcherType) { |
100 *error = kExpectedOtherConditionType; | 125 *error = kExpectedOtherConditionType; |
101 return scoped_ptr<WebRequestCondition>(NULL); | 126 return scoped_ptr<WebRequestCondition>(NULL); |
102 } | 127 } |
103 | 128 |
104 WebRequestConditionAttributes attributes; | 129 WebRequestConditionAttributes attributes; |
105 scoped_refptr<URLMatcherConditionSet> url_matcher_condition_set; | 130 scoped_refptr<URLMatcherConditionSet> url_matcher_condition_set; |
| 131 scoped_refptr<URLMatcherConditionSet> first_party_url_matcher_condition_set; |
106 | 132 |
107 for (base::DictionaryValue::Iterator iter(*condition_dict); | 133 for (base::DictionaryValue::Iterator iter(*condition_dict); |
108 iter.HasNext(); iter.Advance()) { | 134 iter.HasNext(); iter.Advance()) { |
109 const std::string& condition_attribute_name = iter.key(); | 135 const std::string& condition_attribute_name = iter.key(); |
110 const Value& condition_attribute_value = iter.value(); | 136 const Value& condition_attribute_value = iter.value(); |
| 137 const bool name_is_url = condition_attribute_name == keys::kUrlKey; |
111 if (condition_attribute_name == keys::kInstanceTypeKey) { | 138 if (condition_attribute_name == keys::kInstanceTypeKey) { |
112 // Skip this. | 139 // Skip this. |
113 } else if (condition_attribute_name == keys::kUrlKey) { | 140 } else if (name_is_url || |
| 141 condition_attribute_name == keys::kFirstPartyForCookiesUrlKey) { |
114 const base::DictionaryValue* dict = NULL; | 142 const base::DictionaryValue* dict = NULL; |
115 if (!condition_attribute_value.GetAsDictionary(&dict)) { | 143 if (!condition_attribute_value.GetAsDictionary(&dict)) { |
116 *error = base::StringPrintf(kInvalidTypeOfParamter, | 144 *error = base::StringPrintf(kInvalidTypeOfParamter, |
117 condition_attribute_name.c_str()); | 145 condition_attribute_name.c_str()); |
118 } else { | 146 } else { |
119 url_matcher_condition_set = | 147 if (!name_is_url) { |
120 URLMatcherFactory::CreateFromURLFilterDictionary( | 148 first_party_url_matcher_condition_set = |
121 url_matcher_condition_factory, dict, ++g_next_id, error); | 149 URLMatcherFactory::CreateFromURLFilterDictionary( |
| 150 url_matcher_condition_factories[1], dict, ++g_next_id, error); |
| 151 } else { |
| 152 url_matcher_condition_set = |
| 153 URLMatcherFactory::CreateFromURLFilterDictionary( |
| 154 url_matcher_condition_factories[0], dict, ++g_next_id, error); |
| 155 } |
122 } | 156 } |
123 } else if (WebRequestConditionAttribute::IsKnownType( | 157 } else if (WebRequestConditionAttribute::IsKnownType( |
124 condition_attribute_name)) { | 158 condition_attribute_name)) { |
125 scoped_ptr<WebRequestConditionAttribute> attribute = | 159 scoped_ptr<WebRequestConditionAttribute> attribute = |
126 WebRequestConditionAttribute::Create( | 160 WebRequestConditionAttribute::Create( |
127 condition_attribute_name, | 161 condition_attribute_name, |
128 &condition_attribute_value, | 162 &condition_attribute_value, |
129 error); | 163 error); |
130 if (attribute.get()) | 164 if (attribute.get()) |
131 attributes.push_back(make_linked_ptr(attribute.release())); | 165 attributes.push_back(make_linked_ptr(attribute.release())); |
132 } else { | 166 } else { |
133 *error = base::StringPrintf(kUnknownConditionAttribute, | 167 *error = base::StringPrintf(kUnknownConditionAttribute, |
134 condition_attribute_name.c_str()); | 168 condition_attribute_name.c_str()); |
135 } | 169 } |
136 if (!error->empty()) | 170 if (!error->empty()) |
137 return scoped_ptr<WebRequestCondition>(NULL); | 171 return scoped_ptr<WebRequestCondition>(NULL); |
138 } | 172 } |
139 | 173 |
140 scoped_ptr<WebRequestCondition> result( | 174 scoped_ptr<WebRequestCondition> result( |
141 new WebRequestCondition(url_matcher_condition_set, attributes)); | 175 new WebRequestCondition(url_matcher_condition_set, |
| 176 first_party_url_matcher_condition_set, |
| 177 attributes)); |
142 | 178 |
143 if (!result->stages()) { | 179 if (!result->stages()) { |
144 *error = kConditionCannotBeFulfilled; | 180 *error = kConditionCannotBeFulfilled; |
145 return scoped_ptr<WebRequestCondition>(NULL); | 181 return scoped_ptr<WebRequestCondition>(NULL); |
146 } | 182 } |
147 | 183 |
148 return result.Pass(); | 184 return result.Pass(); |
149 } | 185 } |
150 | 186 |
151 } // namespace extensions | 187 } // namespace extensions |
OLD | NEW |