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