- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / extensions / api / declarative_content / content_condition.h
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 <set>
10 #include <string>
11 #include <vector>
12
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"
19 #include "url/gurl.h"
20
21 namespace extensions {
22
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;
32 };
33
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.
37 //
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).
47 //
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 {
53  public:
54   typedef RendererContentMatchData MatchData;
55
56   ContentCondition(
57       scoped_refptr<URLMatcherConditionSet> url_matcher_conditions,
58       const std::vector<std::string>& css_selectors);
59   ~ContentCondition();
60
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,
68       std::string* error);
69
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;
73
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
77   // match.
78   URLMatcherConditionSet::ID url_matcher_condition_set_id() const {
79     return url_matcher_conditions_->id();
80   }
81
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_);
87   }
88
89   // True if GetURLMatcherConditionSets would append anything to its
90   // argument.
91   bool has_url_matcher_condition_set() const {
92     return url_matcher_conditions_.get() != NULL;
93   }
94
95   // Returns the CSS selectors required to match by this condition.
96   const std::vector<std::string>& css_selectors() const {
97     return css_selectors_;
98   }
99
100  private:
101   scoped_refptr<URLMatcherConditionSet> url_matcher_conditions_;
102   std::vector<std::string> css_selectors_;
103
104   DISALLOW_COPY_AND_ASSIGN(ContentCondition);
105 };
106
107 typedef DeclarativeConditionSet<ContentCondition> ContentConditionSet;
108
109 }  // namespace extensions
110
111 #endif  // CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_CONTENT_CONTENT_CONDITION_H_