Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_rules_ checker.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "chrome/common/extensions/extension.h" | |
| 9 #include "extensions/common/error_utils.h" | |
| 10 | |
| 11 namespace { | |
| 12 | |
| 13 const char kActionCannotBeExecuted[] = "An action can never be executed " | |
| 14 "because there are is no time in the request life-cycle during which the " | |
| 15 "conditions can be checked and the action can possibly be executed."; | |
| 16 | |
| 17 const char kAllURLsPermissionNeeded[] = | |
| 18 "To execute the action '*', host permissions for all URLs are needed"; | |
|
Matt Perry
2013/04/25 19:59:21
nit: "To execute the action '*', you need to reque
vabr (Chromium)
2013/04/26 09:58:52
Done, but should we actually say "for all hosts" i
Matt Perry
2013/04/26 20:37:44
OK, how about "To execute the action '*', you need
vabr (Chromium)
2013/04/26 23:47:21
SGTM, done.
| |
| 19 | |
| 20 } // namespace | |
| 21 | |
| 22 namespace extensions { | |
| 23 | |
| 24 WebRequestRulesChecker::WebRequestRulesChecker(const Extension* extension) | |
| 25 : extension_(extension) {} | |
| 26 | |
| 27 WebRequestRule::ConsistencyChecker WebRequestRulesChecker::GetChecker() { | |
| 28 return base::Bind(&WebRequestRulesChecker::Checker, base::Unretained(this)); | |
|
Matt Perry
2013/04/25 19:59:21
Making this a class seems like overkill. Checker a
vabr (Chromium)
2013/04/26 09:58:52
Done.
| |
| 29 } | |
| 30 | |
| 31 bool WebRequestRulesChecker::Checker(const WebRequestConditionSet* conditions, | |
| 32 const WebRequestActionSet* actions, | |
| 33 std::string* error) { | |
| 34 return (StageChecker(conditions, actions, error) && | |
| 35 HostPermissionsChecker(actions, error)); | |
| 36 } | |
| 37 | |
| 38 bool WebRequestRulesChecker::HostPermissionsChecker( | |
| 39 const WebRequestActionSet* actions, | |
| 40 std::string* error) { | |
| 41 if (extension_->HasEffectiveAccessToAllHosts()) | |
| 42 return true; | |
| 43 | |
| 44 // Without the permission for all URLs, only actions with the following host | |
| 45 // strategies make sense to be registered: | |
| 46 // STRATEGY_NONE: because not host permissions are needed, | |
| 47 // STRATEGY_HOST: only host permissions for the host in the request's URL are | |
| 48 // required. | |
| 49 const int allowed_strategies = | |
| 50 WebRequestAction::STRATEGY_NONE | WebRequestAction::STRATEGY_HOST; | |
| 51 for (WebRequestActionSet::Actions::const_iterator action_iter = | |
| 52 actions->actions().begin(); | |
| 53 action_iter != actions->actions().end(); | |
| 54 ++action_iter) { | |
| 55 // Test the intersection of bit masks, this is intentionally & and not &&. | |
| 56 if ((*action_iter)->host_permissions_strategy() & allowed_strategies) | |
|
Matt Perry
2013/04/25 19:59:21
Oh, I see why you made them bitmasks now. I'd stil
vabr (Chromium)
2013/04/26 09:58:52
Done.
| |
| 57 continue; | |
| 58 // We only get here if this is a disallowed action. | |
| 59 *error = ErrorUtils::FormatErrorMessage(kAllURLsPermissionNeeded, | |
| 60 (*action_iter)->GetName()); | |
| 61 return false; | |
| 62 } | |
| 63 return true; | |
| 64 } | |
| 65 | |
| 66 // static | |
| 67 bool WebRequestRulesChecker::StageChecker( | |
| 68 const WebRequestConditionSet* conditions, | |
| 69 const WebRequestActionSet* actions, | |
| 70 std::string* error) { | |
| 71 // Actions and conditions can be checked and executed in specific stages | |
| 72 // of each web request. A rule is inconsistent if there is an action that | |
| 73 // can only be triggered in stages in which no condition can be evaluated. | |
| 74 | |
| 75 // In which stages there are conditions to evaluate. | |
| 76 int condition_stages = 0; | |
| 77 for (WebRequestConditionSet::Conditions::const_iterator condition_iter = | |
| 78 conditions->conditions().begin(); | |
| 79 condition_iter != conditions->conditions().end(); | |
| 80 ++condition_iter) { | |
| 81 condition_stages |= (*condition_iter)->stages(); | |
| 82 } | |
| 83 | |
| 84 for (WebRequestActionSet::Actions::const_iterator action_iter = | |
| 85 actions->actions().begin(); | |
| 86 action_iter != actions->actions().end(); | |
| 87 ++action_iter) { | |
| 88 // Test the intersection of bit masks, this is intentionally & and not &&. | |
| 89 if ((*action_iter)->GetStages() & condition_stages) | |
| 90 continue; | |
| 91 // We only get here if no matching condition was found. | |
| 92 *error = kActionCannotBeExecuted; | |
|
Matt Perry
2013/04/25 19:59:21
Add the action's name in the error message.
vabr (Chromium)
2013/04/26 09:58:52
Done.
| |
| 93 return false; | |
| 94 } | |
| 95 return true; | |
| 96 } | |
| 97 | |
| 98 } // namespace extensions | |
| OLD | NEW |