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_
12 #include "base/containers/hash_tables.h"
13 #include "base/memory/ref_counted.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "components/url_matcher/url_matcher.h"
21 namespace extensions
{
25 struct RendererContentMatchData
{
26 RendererContentMatchData();
27 ~RendererContentMatchData();
28 // Match IDs for the URL of the top-level page the renderer is
29 // returning data for.
30 std::set
<url_matcher::URLMatcherConditionSet::ID
> page_url_matches
;
31 // All watched CSS selectors that match on frames with the same
32 // origin as the page's main frame.
33 base::hash_set
<std::string
> css_selectors
;
34 // True if the URL is bookmarked.
38 // Representation of a condition in the Declarative Content API. A condition
39 // consists of an optional URL and a list of CSS selectors. Each of these
40 // attributes needs to be fulfilled in order for the condition to be fulfilled.
42 // We distinguish between two types of attributes:
43 // - URL Matcher attributes are attributes that test the URL of a page.
44 // These are treated separately because we use a URLMatcher to efficiently
45 // test many of these attributes in parallel by using some advanced
46 // data structures. The URLMatcher tells us if all URL Matcher attributes
47 // are fulfilled for a ContentCondition.
48 // - All other conditions are represented individually as fields in
49 // ContentCondition. These conditions are probed linearly (only if the URL
50 // Matcher found a hit).
52 // TODO(battre) Consider making the URLMatcher an owner of the
53 // URLMatcherConditionSet and only pass a pointer to URLMatcherConditionSet
54 // in url_matcher_condition_set(). This saves some copying in
55 // ContentConditionSet::GetURLMatcherConditionSets.
56 class ContentCondition
{
58 // Possible states for matching bookmarked state.
59 enum BookmarkedStateMatch
{ NOT_BOOKMARKED
, BOOKMARKED
, DONT_CARE
};
62 scoped_refptr
<const Extension
> extension
,
63 scoped_refptr
<url_matcher::URLMatcherConditionSet
>
64 url_matcher_condition_set
,
65 const std::vector
<std::string
>& css_selectors
,
66 BookmarkedStateMatch bookmarked_state
);
69 // Factory method that instantiates a ContentCondition according to the
70 // description |condition| passed by the extension API. |condition| should be
71 // an instance of declarativeContent.PageStateMatcher.
72 static scoped_ptr
<ContentCondition
> Create(
73 scoped_refptr
<const Extension
> extension
,
74 url_matcher::URLMatcherConditionFactory
* url_matcher_condition_factory
,
75 const base::Value
& condition
,
78 // Returns whether the request is a match, given that the URLMatcher found
79 // a match for |url_matcher_condition_set_|.
80 bool IsFulfilled(const RendererContentMatchData
& renderer_data
) const;
82 // Returns null if there are no URLMatcher conditions.
83 url_matcher::URLMatcherConditionSet
* url_matcher_condition_set() const {
84 return url_matcher_condition_set_
.get();
87 // Returns the CSS selectors required to match by this condition.
88 const std::vector
<std::string
>& css_selectors() const {
89 return css_selectors_
;
93 const scoped_refptr
<const Extension
> extension_
;
94 scoped_refptr
<url_matcher::URLMatcherConditionSet
> url_matcher_condition_set_
;
95 std::vector
<std::string
> css_selectors_
;
96 BookmarkedStateMatch bookmarked_state_
;
98 DISALLOW_COPY_AND_ASSIGN(ContentCondition
);
101 } // namespace extensions
103 #endif // CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_CONTENT_CONTENT_CONDITION_H_