| 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_rules_
registry.h" | 5 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_rules_
registry.h" |
| 6 | 6 |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "base/memory/linked_ptr.h" | 9 #include "base/memory/linked_ptr.h" |
| 10 #include "base/message_loop.h" | 10 #include "base/message_loop.h" |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 94 scheme_http->Append(Value::CreateStringValue("https")); | 94 scheme_http->Append(Value::CreateStringValue("https")); |
| 95 DictionaryValue* https_condition_dict = new DictionaryValue(); | 95 DictionaryValue* https_condition_dict = new DictionaryValue(); |
| 96 https_condition_dict->Set(keys2::kSchemesKey, scheme_https); | 96 https_condition_dict->Set(keys2::kSchemesKey, scheme_https); |
| 97 https_condition_dict->SetString(keys2::kHostSuffixKey, "example.com"); | 97 https_condition_dict->SetString(keys2::kHostSuffixKey, "example.com"); |
| 98 https_condition_dict->SetString(keys2::kHostPrefixKey, "www"); | 98 https_condition_dict->SetString(keys2::kHostPrefixKey, "www"); |
| 99 DictionaryValue https_condition_url_filter; | 99 DictionaryValue https_condition_url_filter; |
| 100 https_condition_url_filter.Set(keys::kUrlKey, https_condition_dict); | 100 https_condition_url_filter.Set(keys::kUrlKey, https_condition_dict); |
| 101 https_condition_url_filter.SetString(keys::kInstanceTypeKey, | 101 https_condition_url_filter.SetString(keys::kInstanceTypeKey, |
| 102 keys::kRequestMatcherType); | 102 keys::kRequestMatcherType); |
| 103 | 103 |
| 104 linked_ptr<json_schema_compiler::any::Any> condition1 = make_linked_ptr( | |
| 105 new json_schema_compiler::any::Any); | |
| 106 condition1->Init(http_condition_url_filter); | |
| 107 | |
| 108 linked_ptr<json_schema_compiler::any::Any> condition2 = make_linked_ptr( | |
| 109 new json_schema_compiler::any::Any); | |
| 110 condition2->Init(https_condition_url_filter); | |
| 111 | |
| 112 DictionaryValue action_dict; | 104 DictionaryValue action_dict; |
| 113 action_dict.SetString(keys::kInstanceTypeKey, keys::kCancelRequestType); | 105 action_dict.SetString(keys::kInstanceTypeKey, keys::kCancelRequestType); |
| 114 | 106 |
| 115 linked_ptr<json_schema_compiler::any::Any> action1 = make_linked_ptr( | |
| 116 new json_schema_compiler::any::Any); | |
| 117 action1->Init(action_dict); | |
| 118 | |
| 119 linked_ptr<RulesRegistry::Rule> rule = | 107 linked_ptr<RulesRegistry::Rule> rule = |
| 120 make_linked_ptr(new RulesRegistry::Rule); | 108 make_linked_ptr(new RulesRegistry::Rule); |
| 121 rule->id.reset(new std::string(kRuleId1)); | 109 rule->id.reset(new std::string(kRuleId1)); |
| 122 rule->priority.reset(new int(100)); | 110 rule->priority.reset(new int(100)); |
| 123 rule->actions.push_back(action1); | 111 rule->actions.push_back( |
| 124 rule->conditions.push_back(condition1); | 112 make_linked_ptr<base::Value>(action_dict.DeepCopy())); |
| 125 rule->conditions.push_back(condition2); | 113 rule->conditions.push_back( |
| 114 linked_ptr<base::Value>(http_condition_url_filter.DeepCopy())); |
| 115 rule->conditions.push_back( |
| 116 linked_ptr<base::Value>(https_condition_url_filter.DeepCopy())); |
| 126 return rule; | 117 return rule; |
| 127 } | 118 } |
| 128 | 119 |
| 129 // Returns a rule that matches anything and cancels it. | 120 // Returns a rule that matches anything and cancels it. |
| 130 linked_ptr<RulesRegistry::Rule> CreateRule2() { | 121 linked_ptr<RulesRegistry::Rule> CreateRule2() { |
| 131 DictionaryValue condition_dict; | 122 DictionaryValue condition_dict; |
| 132 condition_dict.SetString(keys::kInstanceTypeKey, keys::kRequestMatcherType); | 123 condition_dict.SetString(keys::kInstanceTypeKey, keys::kRequestMatcherType); |
| 133 | 124 |
| 134 linked_ptr<json_schema_compiler::any::Any> condition = make_linked_ptr( | |
| 135 new json_schema_compiler::any::Any); | |
| 136 condition->Init(condition_dict); | |
| 137 | |
| 138 DictionaryValue action_dict; | 125 DictionaryValue action_dict; |
| 139 action_dict.SetString(keys::kInstanceTypeKey, keys::kCancelRequestType); | 126 action_dict.SetString(keys::kInstanceTypeKey, keys::kCancelRequestType); |
| 140 | 127 |
| 141 linked_ptr<json_schema_compiler::any::Any> action = make_linked_ptr( | |
| 142 new json_schema_compiler::any::Any); | |
| 143 action->Init(action_dict); | |
| 144 | |
| 145 linked_ptr<RulesRegistry::Rule> rule = | 128 linked_ptr<RulesRegistry::Rule> rule = |
| 146 make_linked_ptr(new RulesRegistry::Rule); | 129 make_linked_ptr(new RulesRegistry::Rule); |
| 147 rule->id.reset(new std::string(kRuleId2)); | 130 rule->id.reset(new std::string(kRuleId2)); |
| 148 rule->priority.reset(new int(100)); | 131 rule->priority.reset(new int(100)); |
| 149 rule->actions.push_back(action); | 132 rule->actions.push_back( |
| 150 rule->conditions.push_back(condition); | 133 linked_ptr<base::Value>(action_dict.DeepCopy())); |
| 134 rule->conditions.push_back( |
| 135 linked_ptr<base::Value>(condition_dict.DeepCopy())); |
| 151 return rule; | 136 return rule; |
| 152 } | 137 } |
| 153 | 138 |
| 154 linked_ptr<RulesRegistry::Rule> CreateRedirectRule( | 139 linked_ptr<RulesRegistry::Rule> CreateRedirectRule( |
| 155 const std::string& destination) { | 140 const std::string& destination) { |
| 156 DictionaryValue condition_dict; | 141 DictionaryValue condition_dict; |
| 157 condition_dict.SetString(keys::kInstanceTypeKey, keys::kRequestMatcherType); | 142 condition_dict.SetString(keys::kInstanceTypeKey, keys::kRequestMatcherType); |
| 158 | 143 |
| 159 linked_ptr<json_schema_compiler::any::Any> condition = make_linked_ptr( | |
| 160 new json_schema_compiler::any::Any); | |
| 161 condition->Init(condition_dict); | |
| 162 | |
| 163 DictionaryValue action_dict; | 144 DictionaryValue action_dict; |
| 164 action_dict.SetString(keys::kInstanceTypeKey, keys::kRedirectRequestType); | 145 action_dict.SetString(keys::kInstanceTypeKey, keys::kRedirectRequestType); |
| 165 action_dict.SetString(keys::kRedirectUrlKey, destination); | 146 action_dict.SetString(keys::kRedirectUrlKey, destination); |
| 166 | 147 |
| 167 linked_ptr<json_schema_compiler::any::Any> action = make_linked_ptr( | |
| 168 new json_schema_compiler::any::Any); | |
| 169 action->Init(action_dict); | |
| 170 | |
| 171 linked_ptr<RulesRegistry::Rule> rule = | 148 linked_ptr<RulesRegistry::Rule> rule = |
| 172 make_linked_ptr(new RulesRegistry::Rule); | 149 make_linked_ptr(new RulesRegistry::Rule); |
| 173 rule->id.reset(new std::string(kRuleId3)); | 150 rule->id.reset(new std::string(kRuleId3)); |
| 174 rule->priority.reset(new int(100)); | 151 rule->priority.reset(new int(100)); |
| 175 rule->actions.push_back(action); | 152 rule->actions.push_back( |
| 176 rule->conditions.push_back(condition); | 153 linked_ptr<base::Value>(action_dict.DeepCopy())); |
| 154 rule->conditions.push_back( |
| 155 linked_ptr<base::Value>(condition_dict.DeepCopy())); |
| 177 return rule; | 156 return rule; |
| 178 } | 157 } |
| 179 | 158 |
| 180 // Create a rule to ignore all other rules for a destination that | 159 // Create a rule to ignore all other rules for a destination that |
| 181 // contains index.html. | 160 // contains index.html. |
| 182 linked_ptr<RulesRegistry::Rule> CreateIgnoreRule() { | 161 linked_ptr<RulesRegistry::Rule> CreateIgnoreRule() { |
| 183 linked_ptr<json_schema_compiler::any::Any> condition = make_linked_ptr( | |
| 184 new json_schema_compiler::any::Any); | |
| 185 DictionaryValue condition_dict; | 162 DictionaryValue condition_dict; |
| 186 DictionaryValue* http_condition_dict = new DictionaryValue(); | 163 DictionaryValue* http_condition_dict = new DictionaryValue(); |
| 187 http_condition_dict->SetString(keys2::kPathContainsKey, "index.html"); | 164 http_condition_dict->SetString(keys2::kPathContainsKey, "index.html"); |
| 188 condition_dict.SetString(keys::kInstanceTypeKey, keys::kRequestMatcherType); | 165 condition_dict.SetString(keys::kInstanceTypeKey, keys::kRequestMatcherType); |
| 189 condition_dict.Set(keys::kUrlKey, http_condition_dict); | 166 condition_dict.Set(keys::kUrlKey, http_condition_dict); |
| 190 condition->Init(condition_dict); | |
| 191 | 167 |
| 192 DictionaryValue action_dict; | 168 DictionaryValue action_dict; |
| 193 action_dict.SetString(keys::kInstanceTypeKey, keys::kIgnoreRulesType); | 169 action_dict.SetString(keys::kInstanceTypeKey, keys::kIgnoreRulesType); |
| 194 action_dict.SetInteger(keys::kLowerPriorityThanKey, 150); | 170 action_dict.SetInteger(keys::kLowerPriorityThanKey, 150); |
| 195 linked_ptr<json_schema_compiler::any::Any> action = make_linked_ptr( | |
| 196 new json_schema_compiler::any::Any); | |
| 197 action->Init(action_dict); | |
| 198 | 171 |
| 199 linked_ptr<RulesRegistry::Rule> rule = | 172 linked_ptr<RulesRegistry::Rule> rule = |
| 200 make_linked_ptr(new RulesRegistry::Rule); | 173 make_linked_ptr(new RulesRegistry::Rule); |
| 201 rule->id.reset(new std::string(kRuleId4)); | 174 rule->id.reset(new std::string(kRuleId4)); |
| 202 rule->priority.reset(new int(200)); | 175 rule->priority.reset(new int(200)); |
| 203 rule->actions.push_back(action); | 176 rule->actions.push_back( |
| 204 rule->conditions.push_back(condition); | 177 linked_ptr<base::Value>(action_dict.DeepCopy())); |
| 178 rule->conditions.push_back( |
| 179 linked_ptr<base::Value>(condition_dict.DeepCopy())); |
| 205 return rule; | 180 return rule; |
| 206 } | 181 } |
| 207 | 182 |
| 208 protected: | 183 protected: |
| 209 MessageLoop message_loop; | 184 MessageLoop message_loop; |
| 210 content::TestBrowserThread ui; | 185 content::TestBrowserThread ui; |
| 211 content::TestBrowserThread io; | 186 content::TestBrowserThread io; |
| 212 }; | 187 }; |
| 213 | 188 |
| 214 TEST_F(WebRequestRulesRegistryTest, AddRulesImpl) { | 189 TEST_F(WebRequestRulesRegistryTest, AddRulesImpl) { |
| (...skipping 205 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 420 // The redirect by the first extension is ignored due to the ignore rule. | 395 // The redirect by the first extension is ignored due to the ignore rule. |
| 421 ASSERT_EQ(1u, deltas.size()); | 396 ASSERT_EQ(1u, deltas.size()); |
| 422 LinkedPtrEventResponseDelta effective_rule = *(deltas.begin()); | 397 LinkedPtrEventResponseDelta effective_rule = *(deltas.begin()); |
| 423 | 398 |
| 424 EXPECT_EQ(kExtensionId2, effective_rule->extension_id); | 399 EXPECT_EQ(kExtensionId2, effective_rule->extension_id); |
| 425 EXPECT_EQ(base::Time() + base::TimeDelta::FromDays(2), | 400 EXPECT_EQ(base::Time() + base::TimeDelta::FromDays(2), |
| 426 effective_rule->extension_install_time); | 401 effective_rule->extension_install_time); |
| 427 EXPECT_EQ(GURL("http://www.bar.com"), effective_rule->new_url); | 402 EXPECT_EQ(GURL("http://www.bar.com"), effective_rule->new_url); |
| 428 } | 403 } |
| 429 } // namespace extensions | 404 } // namespace extensions |
| OLD | NEW |