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.
5 #ifndef CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_CONTENT_CONTENT_CONDITION_H_
6 #define CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_CONTENT_CONTENT_CONDITION_H_
13 #include "base/containers/hash_tables.h"
14 #include "base/memory/ref_counted.h"
15 #include "base/memory/scoped_ptr.h"
16 #include "base/values.h"
17 #include "chrome/browser/extensions/api/declarative/declarative_rule.h"
18 #include "extensions/common/matcher/url_matcher.h"
21 namespace extensions {
23 struct RendererContentMatchData {
24 RendererContentMatchData();
25 ~RendererContentMatchData();
26 // Match IDs for the URL of the top-level page the renderer is
27 // returning data for.
28 std::set<URLMatcherConditionSet::ID> page_url_matches;
29 // All watched CSS selectors that match on frames with the same
30 // origin as the page's main frame.
31 base::hash_set<std::string> css_selectors;
34 // Representation of a condition in the Declarative Content API. A condition
35 // consists of an optional URL and a list of CSS selectors. Each of these
36 // attributes needs to be fulfilled in order for the condition to be fulfilled.
38 // We distinguish between two types of attributes:
39 // - URL Matcher attributes are attributes that test the URL of a page.
40 // These are treated separately because we use a URLMatcher to efficiently
41 // test many of these attributes in parallel by using some advanced
42 // data structures. The URLMatcher tells us if all URL Matcher attributes
43 // are fulfilled for a ContentCondition.
44 // - All other conditions are represented individually as fields in
45 // ContentCondition. These conditions are probed linearly (only if the URL
46 // Matcher found a hit).
48 // TODO(battre) Consider making the URLMatcher an owner of the
49 // URLMatcherConditionSet and only pass a pointer to URLMatcherConditionSet
50 // in url_matcher_condition_set(). This saves some copying in
51 // ContentConditionSet::GetURLMatcherConditionSets.
52 class ContentCondition {
54 typedef RendererContentMatchData MatchData;
57 scoped_refptr<URLMatcherConditionSet> url_matcher_conditions,
58 const std::vector<std::string>& css_selectors);
61 // Factory method that instantiates a ContentCondition according to the
62 // description |condition| passed by the extension API. |condition| should be
63 // an instance of declarativeContent.PageStateMatcher.
64 static scoped_ptr<ContentCondition> Create(
65 const Extension* extension,
66 URLMatcherConditionFactory* url_matcher_condition_factory,
67 const base::Value& condition,
70 // Returns whether the request is a match, given that the URLMatcher found
71 // a match for |url_matcher_conditions_|.
72 bool IsFulfilled(const RendererContentMatchData& renderer_data) const;
74 // Returns a URLMatcherConditionSet::ID which is the canonical representation
75 // for all URL patterns that need to be matched by this ContentCondition.
76 // This ID is registered in a URLMatcher that can inform us in case of a
78 URLMatcherConditionSet::ID url_matcher_condition_set_id() const {
79 return url_matcher_conditions_->id();
82 // If this Condition has a url filter, appends it to |condition_sets|.
83 void GetURLMatcherConditionSets(
84 URLMatcherConditionSet::Vector* condition_sets) const {
85 if (url_matcher_conditions_.get())
86 condition_sets->push_back(url_matcher_conditions_);
89 // True if GetURLMatcherConditionSets would append anything to its
91 bool has_url_matcher_condition_set() const {
92 return url_matcher_conditions_.get() != NULL;
95 // Returns the CSS selectors required to match by this condition.
96 const std::vector<std::string>& css_selectors() const {
97 return css_selectors_;
101 scoped_refptr<URLMatcherConditionSet> url_matcher_conditions_;
102 std::vector<std::string> css_selectors_;
104 DISALLOW_COPY_AND_ASSIGN(ContentCondition);
107 typedef DeclarativeConditionSet<ContentCondition> ContentConditionSet;
109 } // namespace extensions
111 #endif // CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_CONTENT_CONTENT_CONDITION_H_