OLD | NEW |
(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> |
| 10 #include <vector> |
| 11 |
| 12 #include "base/hash_tables.h" |
| 13 #include "base/memory/linked_ptr.h" |
| 14 #include "base/memory/ref_counted.h" |
| 15 #include "base/memory/scoped_ptr.h" |
| 16 #include "base/memory/scoped_vector.h" |
| 17 #include "base/values.h" |
| 18 #include "chrome/browser/extensions/api/declarative/declarative_rule.h" |
| 19 #include "chrome/common/extensions/matcher/url_matcher.h" |
| 20 #include "googleurl/src/gurl.h" |
| 21 |
| 22 namespace extensions { |
| 23 |
| 24 struct RendererContentMatchData { |
| 25 RendererContentMatchData(); |
| 26 ~RendererContentMatchData(); |
| 27 GURL page_url; |
| 28 base::hash_set<std::string> css_rules; |
| 29 }; |
| 30 |
| 31 // Representation of a condition in the Declarative Content API. A condition |
| 32 // consists of an optional URL and a list of CSS selectors. Each of these |
| 33 // attributes needs to be fulfilled in order for the condition to be fulfilled. |
| 34 // |
| 35 // We distinguish between two types of attributes: |
| 36 // - URL Matcher attributes are attributes that test the URL of a page. |
| 37 // These are treated separately because we use a URLMatcher to efficiently |
| 38 // test many of these attributes in parallel by using some advanced |
| 39 // data structures. The URLMatcher tells us if all URL Matcher attributes |
| 40 // are fulfilled for a ContentCondition. |
| 41 // - All other conditions are represented individually as fields in |
| 42 // ContentCondition. These conditions are probed linearly (only if the URL |
| 43 // Matcher found a hit). |
| 44 // |
| 45 // TODO(battre) Consider making the URLMatcher an owner of the |
| 46 // URLMatcherConditionSet and only pass a pointer to URLMatcherConditionSet |
| 47 // in url_matcher_condition_set(). This saves some copying in |
| 48 // ContentConditionSet::GetURLMatcherConditionSets. |
| 49 class ContentCondition { |
| 50 public: |
| 51 typedef RendererContentMatchData MatchData; |
| 52 |
| 53 ContentCondition( |
| 54 scoped_refptr<URLMatcherConditionSet> url_matcher_conditions, |
| 55 const std::vector<std::string>& css_rules); |
| 56 ~ContentCondition(); |
| 57 |
| 58 // Factory method that instantiates a ContentCondition according to the |
| 59 // description |condition| passed by the extension API. |condition| should be |
| 60 // an instance of declarativeContent.PageStateMatcher. |
| 61 static scoped_ptr<ContentCondition> Create( |
| 62 URLMatcherConditionFactory* url_matcher_condition_factory, |
| 63 const base::Value& condition, |
| 64 std::string* error); |
| 65 |
| 66 // Returns whether the request is a match, given that the URLMatcher found |
| 67 // a match for |url_matcher_conditions_|. |
| 68 bool IsFulfilled(const std::set<URLMatcherConditionSet::ID>& url_matches, |
| 69 const RendererContentMatchData& renderer_data) const; |
| 70 |
| 71 // Returns a URLMatcherConditionSet::ID which is the canonical representation |
| 72 // for all URL patterns that need to be matched by this ContentCondition. |
| 73 // This ID is registered in a URLMatcher that can inform us in case of a |
| 74 // match. |
| 75 URLMatcherConditionSet::ID url_matcher_condition_set_id() const { |
| 76 return url_matcher_conditions_->id(); |
| 77 } |
| 78 |
| 79 // If this Condition has a url filter, appends it to |condition_sets|. |
| 80 void GetURLMatcherConditionSets( |
| 81 URLMatcherConditionSet::Vector* condition_sets) const { |
| 82 if (url_matcher_conditions_) |
| 83 condition_sets->push_back(url_matcher_conditions_); |
| 84 } |
| 85 |
| 86 // True if GetURLMatcherConditionSets would append anything to its |
| 87 // argument. |
| 88 bool has_url_matcher_condition_set() const { |
| 89 return url_matcher_conditions_ != NULL; |
| 90 } |
| 91 |
| 92 // Returns the CSS rules required to match by this condition. |
| 93 const std::vector<std::string>& css_rules() const { |
| 94 return css_rules_; |
| 95 } |
| 96 |
| 97 private: |
| 98 scoped_refptr<URLMatcherConditionSet> url_matcher_conditions_; |
| 99 std::vector<std::string> css_rules_; |
| 100 |
| 101 DISALLOW_COPY_AND_ASSIGN(ContentCondition); |
| 102 }; |
| 103 |
| 104 typedef DeclarativeConditionSet<ContentCondition> ContentConditionSet; |
| 105 |
| 106 } // namespace extensions |
| 107 |
| 108 #endif // CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_CONTENT_CONTENT_CONDITION_H
_ |
OLD | NEW |