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

Side by Side Diff: chrome/browser/extensions/api/declarative_content/content_condition.h

Issue 11547033: Implement declarativeContent API. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Sync 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 #ifndef CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_CONTENT_CONTENT_CONDITION_H_
6 #define CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_CONTENT_CONTENT_CONDITION_H_
7
8 #include <map>
9 #include <string>
vabr (Chromium) 2013/01/23 12:32:56 Lint: add #include for set<>. (It also asked for
Jeffrey Yasskin 2013/01/23 22:13:17 Done. (How did you run lint? I haven't seen a way
vabr (Chromium) 2013/01/24 08:21:01 Rietveld does that for me -- the links in the patc
Jeffrey Yasskin 2013/01/25 00:08:29 Neat. ... unfortunately, that UI is so broken that
10 #include <vector>
11
12 #include "base/hash_tables.h"
13 #include "base/memory/ref_counted.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/values.h"
16 #include "chrome/browser/extensions/api/declarative/declarative_rule.h"
17 #include "chrome/common/extensions/matcher/url_matcher.h"
18 #include "googleurl/src/gurl.h"
19
20 namespace extensions {
21
22 struct RendererContentMatchData {
23 RendererContentMatchData();
24 ~RendererContentMatchData();
25 // The URL of the top-level page the renderer is returning data for.
26 GURL page_url;
27 // All watched CSS selectors that match on frames with the same
28 // origin as the page's main frame.
29 base::hash_set<std::string> css_selectors;
30 };
31
32 // Representation of a condition in the Declarative Content API. A condition
33 // consists of an optional URL and a list of CSS selectors. Each of these
34 // attributes needs to be fulfilled in order for the condition to be fulfilled.
35 //
36 // We distinguish between two types of attributes:
37 // - URL Matcher attributes are attributes that test the URL of a page.
38 // These are treated separately because we use a URLMatcher to efficiently
39 // test many of these attributes in parallel by using some advanced
40 // data structures. The URLMatcher tells us if all URL Matcher attributes
41 // are fulfilled for a ContentCondition.
42 // - All other conditions are represented individually as fields in
43 // ContentCondition. These conditions are probed linearly (only if the URL
44 // Matcher found a hit).
45 //
46 // TODO(battre) Consider making the URLMatcher an owner of the
47 // URLMatcherConditionSet and only pass a pointer to URLMatcherConditionSet
48 // in url_matcher_condition_set(). This saves some copying in
49 // ContentConditionSet::GetURLMatcherConditionSets.
50 class ContentCondition {
51 public:
52 typedef RendererContentMatchData MatchData;
53
54 ContentCondition(
55 scoped_refptr<URLMatcherConditionSet> url_matcher_conditions,
56 const std::vector<std::string>& css_selectors);
57 ~ContentCondition();
58
59 // Factory method that instantiates a ContentCondition according to the
60 // description |condition| passed by the extension API. |condition| should be
61 // an instance of declarativeContent.PageStateMatcher.
62 static scoped_ptr<ContentCondition> Create(
63 URLMatcherConditionFactory* url_matcher_condition_factory,
64 const base::Value& condition,
65 std::string* error);
66
67 // Returns whether the request is a match, given that the URLMatcher found
68 // a match for |url_matcher_conditions_|.
69 bool IsFulfilled(const std::set<URLMatcherConditionSet::ID>& url_matches,
70 const RendererContentMatchData& renderer_data) const;
71
72 // Returns a URLMatcherConditionSet::ID which is the canonical representation
73 // for all URL patterns that need to be matched by this ContentCondition.
74 // This ID is registered in a URLMatcher that can inform us in case of a
75 // match.
76 URLMatcherConditionSet::ID url_matcher_condition_set_id() const {
77 return url_matcher_conditions_->id();
78 }
79
80 // If this Condition has a url filter, appends it to |condition_sets|.
81 void GetURLMatcherConditionSets(
82 URLMatcherConditionSet::Vector* condition_sets) const {
83 if (url_matcher_conditions_)
84 condition_sets->push_back(url_matcher_conditions_);
85 }
86
87 // True if GetURLMatcherConditionSets would append anything to its
88 // argument.
89 bool has_url_matcher_condition_set() const {
90 return url_matcher_conditions_ != NULL;
91 }
92
93 // Returns the CSS selectors required to match by this condition.
94 const std::vector<std::string>& css_selectors() const {
95 return css_selectors_;
96 }
97
98 private:
99 scoped_refptr<URLMatcherConditionSet> url_matcher_conditions_;
100 std::vector<std::string> css_selectors_;
101
102 DISALLOW_COPY_AND_ASSIGN(ContentCondition);
103 };
104
105 typedef DeclarativeConditionSet<ContentCondition> ContentConditionSet;
106
107 } // namespace extensions
108
109 #endif // CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_CONTENT_CONTENT_CONDITION_H _
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698