Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(112)

Side by Side Diff: chrome/browser/extensions/api/declarative_webrequest/webrequest_rule.cc

Issue 11572061: Create DeclarativeConditionSet, DeclarativeActionSet, and DeclarativeRule templates (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix kalman's nit Created 7 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_rule.h "
6
7 #include "base/logging.h"
8 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_action .h"
9 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_condit ion.h"
10 #include "chrome/browser/extensions/api/web_request/web_request_api_helpers.h"
11 #include "chrome/browser/extensions/api/web_request/web_request_permissions.h"
12 #include "chrome/browser/extensions/extension_info_map.h"
13 #include "chrome/common/extensions/extension.h"
14
15 namespace {
16 const char kInvalidActionDatatype[] = "An action of a rule set had an invalid "
17 "structure that should have been caught by the JSON validator.";
18 const char kActionCannotBeExecuted[] = "An action can never be executed "
19 "because there are is no time in the request life-cycle during which the "
20 "conditions can be checked and the action can possibly be executed.";
21 } // namespace
22
23 namespace extensions {
24
25 WebRequestRule::WebRequestRule(
26 const GlobalRuleId& id,
27 base::Time extension_installation_time,
28 scoped_ptr<WebRequestConditionSet> conditions,
29 scoped_ptr<WebRequestActionSet> actions,
30 Priority priority)
31 : id_(id),
32 extension_installation_time_(extension_installation_time),
33 conditions_(conditions.release()),
34 actions_(actions.release()),
35 priority_(priority) {
36 CHECK(conditions_.get());
37 CHECK(actions_.get());
38 }
39
40 WebRequestRule::~WebRequestRule() {}
41
42 // static
43 bool WebRequestRule::CheckConsistency(
44 WebRequestConditionSet* conditions,
45 WebRequestActionSet* actions,
46 std::string* error) {
47 // Actions and conditions can be checked and executed in specific phases
48 // of each web request. We consider a rule inconsistent if there is an action
49 // that cannot be triggered by any condition.
50 for (WebRequestActionSet::Actions::const_iterator action_iter =
51 actions->actions().begin();
52 action_iter != actions->actions().end();
53 ++action_iter) {
54 bool found_matching_condition = false;
55 for (WebRequestConditionSet::Conditions::const_iterator condition_iter =
56 conditions->conditions().begin();
57 condition_iter != conditions->conditions().end() &&
58 !found_matching_condition;
59 ++condition_iter) {
60 // Test the intersection of bit masks, this is intentionally & and not &&.
61 if ((*action_iter)->GetStages() & (*condition_iter)->stages())
62 found_matching_condition = true;
63 }
64 if (!found_matching_condition) {
65 *error = kActionCannotBeExecuted;
66 return false;
67 }
68 }
69 return true;
70 }
71
72 // static
73 scoped_ptr<WebRequestRule> WebRequestRule::Create(
74 URLMatcherConditionFactory* url_matcher_condition_factory,
75 const std::string& extension_id,
76 base::Time extension_installation_time,
77 linked_ptr<RulesRegistry::Rule> rule,
78 std::string* error) {
79 scoped_ptr<WebRequestRule> error_result;
80
81 scoped_ptr<WebRequestConditionSet> conditions =
82 WebRequestConditionSet::Create(
83 url_matcher_condition_factory, rule->conditions, error);
84 if (!error->empty())
85 return error_result.Pass();
86 CHECK(conditions.get());
87
88 bool bad_message = false;
89 scoped_ptr<WebRequestActionSet> actions =
90 WebRequestActionSet::Create(rule->actions, error, &bad_message);
91 if (bad_message) {
92 // TODO(battre) Export concept of bad_message to caller, the extension
93 // should be killed in case it is true.
94 *error = kInvalidActionDatatype;
95 return error_result.Pass();
96 }
97 if (!error->empty() || bad_message)
98 return error_result.Pass();
99 CHECK(actions.get());
100
101 if (!CheckConsistency(conditions.get(), actions.get(), error)) {
102 DCHECK(!error->empty());
103 return error_result.Pass();
104 }
105
106 CHECK(rule->priority.get());
107 int priority = *(rule->priority);
108
109 GlobalRuleId rule_id(extension_id, *(rule->id));
110 return scoped_ptr<WebRequestRule>(
111 new WebRequestRule(rule_id, extension_installation_time,
112 conditions.Pass(), actions.Pass(), priority));
113 }
114
115 std::list<LinkedPtrEventResponseDelta> WebRequestRule::CreateDeltas(
116 const ExtensionInfoMap* extension_info_map,
117 const RequestData& request_data,
118 bool crosses_incognito) const {
119 return actions_->CreateDeltas(extension_info_map, extension_id(),
120 request_data, crosses_incognito,
121 extension_installation_time_);
122 }
123
124 int WebRequestRule::GetMinimumPriority() const {
125 return actions_->GetMinimumPriority();
126 }
127
128 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698