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

Side by Side Diff: chrome/browser/extensions/api/declarative_webrequest/webrequest_condition.h

Issue 9820003: Implementation of beginning of Declarative Web Request API backend (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 9 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 #ifndef CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_WEBREQUEST_WEBREQUEST_CONDITIO N_H_
6 #define CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_WEBREQUEST_WEBREQUEST_CONDITIO N_H_
7 #pragma once
8
9 #include <string>
10 #include <vector>
11
12 #include "base/basictypes.h"
13 #include "base/gtest_prod_util.h"
14 #include "base/memory/linked_ptr.h"
15 #include "chrome/browser/extensions/api/declarative/url_matcher.h"
16 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_condit ion_attribute.h"
17
18 namespace extensions {
19 namespace declarative_webrequest {
20
21 // Representation of a condition in the Declarative WebRequest API. A condition
22 // consists of several attributes. Each of these attributes needs to be
23 // fulfilled in order for the condition to be fulfilled.
24 //
25 // We distinguish between two types of conditions:
26 // - URL Matcher conditions are conditions that test the URL of a request.
27 // These are treated separately because we use a URLMatcher to efficiently
28 // test many of these conditions in parallel by using some advanced
29 // data structures. The URLMatcher tells us if all URL Matcher conditions
30 // are fulfilled for a WebRequestCondition.
31 // - All other conditions are represented as WebRequestConditionAttributes.
32 // These conditions are probed linearly (only if the URL Matcher found a hit).
33 class WebRequestCondition {
34 public:
35 WebRequestCondition(
36 const URLMatcherConditionSet& url_matcher_conditions,
37 const WebRequestConditionAttributes& condition_attributes);
38 ~WebRequestCondition();
39
40 // Returns whether |request| is a match, given that the URLMatcher found
41 // |url_match|.
42 bool IsFulfilled(URLMatcherConditionSet::ID url_match,
43 net::URLRequest* request) const;
44
45 // Returns a URLMatcherConditionSet::ID which is the canonical representation
46 // for all URL patterns that need to be matched by this WebRequestCondition.
47 // This ID is registered in a URLMatcher that can inform us in case of a
48 // match.
49 URLMatcherConditionSet::ID url_matcher_condition_set_id() const {
50 return url_matcher_conditions_.id();
51 }
52
53 // Appends the url matcher condition set to |condition_sets|.
54 void AppendURLMatcherConditionSet(
55 std::vector<URLMatcherConditionSet>* condition_sets) const;
Matt Perry 2012/03/22 23:07:25 There's only one condition set - just provide an a
battre 2012/03/26 18:35:51 Done.
56
57 private:
58 URLMatcherConditionSet url_matcher_conditions_;
59 WebRequestConditionAttributes condition_attributes_;
60
61 DISALLOW_COPY_AND_ASSIGN(WebRequestCondition);
62 };
63
64 // This class stores a set of conditions that may be part of a WebRequestRule.
65 // If any condition is fulfilled, the WebRequestActions of the WebRequestRule
66 // can be triggered.
67 class WebRequestConditionCollection {
Matt Perry 2012/03/22 23:07:25 nit: name WebRequestConditionSet to mirror URLMatc
battre 2012/03/26 18:35:51 In the case of URLMatcherConditionSet, you favored
Matt Perry 2012/03/26 19:11:06 Do you expect duplicate WebRequestConditions/Actio
battre 2012/03/26 20:38:10 No, there should not be any duplicates. If there a
68 public:
69 explicit WebRequestConditionCollection(
70 const std::vector<linked_ptr<WebRequestCondition> >& conditions);
71 ~WebRequestConditionCollection();
72
73 const std::vector<linked_ptr<WebRequestCondition> >& conditions() const {
74 return conditions_;
75 }
76
77 bool IsFulfilled(const std::set<URLMatcherConditionSet::ID>& url_matches,
78 net::URLRequest* request) const;
79
80 // Appends the URLMatcherConditionSet from all conditions to |condition_sets|.
81 void AppendURLMatcherConditionSets(
82 std::vector<URLMatcherConditionSet>* condition_sets) const;
Matt Perry 2012/03/22 23:07:25 I think this should collect a vector of const-refe
battre 2012/03/26 18:35:51 The URLMatcherConditionSet consists of an ID (int)
Matt Perry 2012/03/26 19:11:06 True, but both copies are still stored in memory f
83
84 private:
85 typedef std::vector<linked_ptr<WebRequestCondition> > Collection;
86 Collection conditions_;
87
88 typedef std::map<URLMatcherConditionSet::ID,
89 WebRequestCondition*> MatchTriggers;
90 MatchTriggers match_triggers_;
91
92 DISALLOW_COPY_AND_ASSIGN(WebRequestConditionCollection);
93 };
94
95 class WebRequestConditionFactory {
Matt Perry 2012/03/22 23:07:25 since this is just a class with all static methods
battre 2012/03/26 18:35:51 Done.
96 public:
97 typedef std::vector<linked_ptr<json_schema_compiler::any::Any> > AnyVector;
98
99 static scoped_ptr<WebRequestCondition> CreateCondition(
100 URLMatcherConditionFactory* url_matcher_condition_factory,
101 const base::Value& condition,
102 std::string* error);
103
104 static scoped_ptr<WebRequestConditionCollection> CreateConditionCollection(
105 URLMatcherConditionFactory* url_matcher_condition_factory,
106 const AnyVector& conditions,
107 std::string* error);
108
109 private:
110 FRIEND_TEST_ALL_PREFIXES(WebRequestConditionFactoryTest,
111 GetAllPathAttributesSorted);
112 // A method for testing that an internal array kAllPathAttributes remains
113 // sorted.
114 static std::vector<std::string> GetAllPathAttributes();
Matt Perry 2012/03/22 23:07:25 this and the test are unnecessary if you take my s
battre 2012/03/26 18:35:51 Done.
115
116 // Returns whether an element of name |name| of a Condition needs to be
117 // handled by the UrlMatcher.
118 static bool IsUrlMatcherConditionAttribute(const std::string& name);
119
120 static URLMatcherCondition CreateURLMatcherCondition(
121 URLMatcherConditionFactory* url_matcher_condition_factory,
122 const std::string& condition_attribute_name,
123 const base::Value* value,
124 std::string* error);
125
126 DISALLOW_IMPLICIT_CONSTRUCTORS(WebRequestConditionFactory);
127 };
128
129 } // namespace declarative_webrequest
130 } // namespace extensions
131
132 #endif // CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_WEBREQUEST_WEBREQUEST_CONDI TION_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698