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