Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
|
not at google - send to devlin
2015/06/01 22:51:12
(note, maybe of the comments in this file apply to
| |
| 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_DECLARATIVE_CONTENT_RU LE_H__ | |
| 6 #define CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_CONTENT_DECLARATIVE_CONTENT_RU LE_H__ | |
| 7 | |
| 8 #include <string> | |
| 9 #include <vector> | |
| 10 | |
| 11 #include "base/callback.h" | |
| 12 #include "base/memory/linked_ptr.h" | |
| 13 #include "base/time/time.h" | |
| 14 #include "chrome/browser/extensions/api/declarative_content/content_action.h" | |
| 15 #include "chrome/browser/extensions/api/declarative_content/content_condition.h" | |
| 16 #include "components/url_matcher/url_matcher.h" | |
| 17 #include "extensions/common/api/events.h" | |
| 18 | |
| 19 namespace base { | |
| 20 class Time; | |
| 21 class Value; | |
| 22 } | |
| 23 | |
| 24 namespace content { | |
| 25 class BrowserContext; | |
| 26 } | |
| 27 | |
| 28 namespace extensions { | |
| 29 | |
| 30 class Extension; | |
| 31 | |
| 32 // This class stores a set of conditions that may be part of a | |
| 33 // DeclarativeContentRule. If any condition is fulfilled, the Actions of the | |
| 34 // DeclarativeContentRule can be triggered. | |
| 35 class DeclarativeContentConditionSet { | |
|
not at google - send to devlin
2015/06/01 22:51:11
I'm not sure that DeclarativeContentConditionSet/D
| |
| 36 public: | |
| 37 typedef std::vector<linked_ptr<base::Value> > Values; | |
|
not at google - send to devlin
2015/06/01 22:51:11
- Use "using Values = ..." rather than "typedef".
| |
| 38 typedef std::vector<linked_ptr<const ContentCondition> > Conditions; | |
| 39 typedef Conditions::const_iterator const_iterator; | |
| 40 | |
| 41 // Factory method that creates a DeclarativeContentConditionSet for | |
| 42 // |extension| according to the JSON array |conditions| passed by the | |
| 43 // extension API. Sets |error| and returns NULL in case of an error. | |
| 44 static scoped_ptr<DeclarativeContentConditionSet> Create( | |
| 45 const Extension* extension, | |
| 46 url_matcher::URLMatcherConditionFactory* url_matcher_condition_factory, | |
| 47 const Values& condition_values, | |
| 48 std::string* error); | |
| 49 | |
| 50 ~DeclarativeContentConditionSet(); | |
| 51 | |
| 52 const Conditions& conditions() const { | |
| 53 return conditions_; | |
| 54 } | |
| 55 | |
| 56 const_iterator begin() const { return conditions_.begin(); } | |
| 57 const_iterator end() const { return conditions_.end(); } | |
| 58 | |
| 59 // If |url_match_trigger| is not -1, this function looks for a condition | |
| 60 // with this URLMatcherConditionSet, and forwards to that condition's | |
| 61 // IsFulfilled(|match_data|). If there is no such condition, then false is | |
| 62 // returned. If |url_match_trigger| is -1, this function returns whether any | |
| 63 // of the conditions without URL attributes is satisfied. | |
|
not at google - send to devlin
2015/06/01 22:51:11
Can this be 2 separate interface methods, with an
| |
| 64 bool IsFulfilled(url_matcher::URLMatcherConditionSet::ID url_match_trigger, | |
| 65 const RendererContentMatchData& match_data) const; | |
| 66 | |
| 67 // Appends the URLMatcherConditionSet from all conditions to |condition_sets|. | |
| 68 void GetURLMatcherConditionSets( | |
| 69 url_matcher::URLMatcherConditionSet::Vector* condition_sets) const; | |
| 70 | |
| 71 // Returns whether there are some conditions without UrlFilter attributes. | |
| 72 bool HasConditionsWithoutUrls() const { | |
| 73 return !conditions_without_urls_.empty(); | |
| 74 } | |
| 75 | |
| 76 private: | |
| 77 typedef std::map<url_matcher::URLMatcherConditionSet::ID, | |
| 78 const ContentCondition*> | |
| 79 URLMatcherIdToCondition; | |
| 80 | |
| 81 DeclarativeContentConditionSet( | |
| 82 const Conditions& conditions, | |
| 83 const URLMatcherIdToCondition& match_id_to_condition, | |
| 84 const std::vector<const ContentCondition*>& conditions_without_urls); | |
| 85 | |
| 86 const URLMatcherIdToCondition match_id_to_condition_; | |
| 87 const Conditions conditions_; | |
| 88 const std::vector<const ContentCondition*> conditions_without_urls_; | |
| 89 | |
| 90 DISALLOW_COPY_AND_ASSIGN(DeclarativeContentConditionSet); | |
| 91 }; | |
| 92 | |
| 93 typedef DeclarativeContentConditionSet ContentConditionSet; | |
|
not at google - send to devlin
2015/06/01 22:51:11
Why not just directly call "DeclarativeContentCond
| |
| 94 | |
| 95 // Immutable container for multiple actions. | |
| 96 // | |
| 97 // TODO(battre): As DeclarativeContentActionSet can become the single owner of | |
| 98 // all actions, we can optimize here by making some of them singletons | |
| 99 // (e.g. Cancel actions). | |
| 100 class DeclarativeContentActionSet { | |
| 101 public: | |
| 102 typedef std::vector<linked_ptr<base::Value> > Values; | |
| 103 typedef std::vector<scoped_refptr<const ContentAction> > Actions; | |
| 104 | |
| 105 explicit DeclarativeContentActionSet(const Actions& actions); | |
| 106 ~DeclarativeContentActionSet(); | |
| 107 | |
| 108 // Factory method that instantiates a DeclarativeContentActionSet for | |
| 109 // |extension| according to |actions| which represents the array of actions | |
| 110 // received from the extension API. | |
| 111 static scoped_ptr<DeclarativeContentActionSet> Create( | |
| 112 content::BrowserContext* browser_context, | |
| 113 const Extension* extension, | |
| 114 const Values& action_values, | |
| 115 std::string* error, | |
| 116 bool* bad_message); | |
| 117 | |
| 118 // Rules call this method when their conditions are fulfilled. | |
| 119 void Apply(const std::string& extension_id, | |
| 120 const base::Time& extension_install_time, | |
|
not at google - send to devlin
2015/06/01 22:51:12
This method taking the extension's install time se
| |
| 121 ContentAction::ApplyInfo* apply_info) const; | |
| 122 | |
| 123 // Rules call this method when their conditions are fulfilled, but Apply has | |
| 124 // already been called. | |
| 125 void Reapply(const std::string& extension_id, | |
| 126 const base::Time& extension_install_time, | |
| 127 ContentAction::ApplyInfo* apply_info) const; | |
| 128 | |
| 129 // Rules call this method when they have stateful conditions, and those | |
| 130 // conditions stop being fulfilled. Rules with event-based conditions (e.g. a | |
| 131 // network request happened) will never Revert() an action. | |
| 132 void Revert(const std::string& extension_id, | |
| 133 const base::Time& extension_install_time, | |
| 134 ContentAction::ApplyInfo* apply_info) const; | |
| 135 | |
| 136 const Actions& actions() const { return actions_; } | |
| 137 | |
| 138 private: | |
| 139 const Actions actions_; | |
| 140 | |
| 141 DISALLOW_COPY_AND_ASSIGN(DeclarativeContentActionSet); | |
| 142 }; | |
| 143 | |
| 144 typedef DeclarativeContentActionSet ContentActionSet; | |
| 145 | |
| 146 // Representation of a rule of a declarative API: | |
| 147 // https://developer.chrome.com/beta/extensions/events.html#declarative. | |
| 148 // Generally a RulesRegistry will hold a collection of Rules for a given | |
| 149 // declarative API and contain the logic for matching and applying them. | |
| 150 class DeclarativeContentRule { | |
| 151 public: | |
| 152 typedef std::string ExtensionId; | |
|
not at google - send to devlin
2015/06/01 22:51:11
This typedef already exists in extension.h.
| |
| 153 typedef std::string RuleId; | |
| 154 typedef std::pair<ExtensionId, RuleId> GlobalRuleId; | |
| 155 typedef int Priority; | |
| 156 typedef DeclarativeContentConditionSet ConditionSet; | |
| 157 typedef DeclarativeContentActionSet ActionSet; | |
| 158 typedef core_api::events::Rule JsonRule; | |
| 159 typedef std::vector<std::string> Tags; | |
|
not at google - send to devlin
2015/06/01 22:51:11
That's a lot of typedefs. Some of them like Priori
| |
| 160 | |
| 161 // Checks whether the set of |conditions| and |actions| are consistent. | |
| 162 // Returns true in case of consistency and MUST set |error| otherwise. | |
| 163 typedef base::Callback<bool(const ConditionSet* conditions, | |
| 164 const ActionSet* actions, | |
| 165 std::string* error)> ConsistencyChecker; | |
| 166 | |
| 167 DeclarativeContentRule(const GlobalRuleId& id, | |
| 168 const Tags& tags, | |
| 169 base::Time extension_installation_time, | |
| 170 scoped_ptr<ConditionSet> conditions, | |
| 171 scoped_ptr<ActionSet> actions, | |
| 172 Priority priority); | |
| 173 ~DeclarativeContentRule(); | |
| 174 | |
| 175 // Creates a DeclarativeContentRule for |extension| given a json definition. | |
| 176 // The format of each condition and action's json is up to the specific | |
| 177 // ContentCondition and ContentAction. |extension| may be NULL in tests. | |
| 178 // | |
| 179 // Before constructing the final rule, calls check_consistency(conditions, | |
| 180 // actions, error) and returns NULL if it fails. Pass NULL if no consistency | |
| 181 // check is needed. If |error| is empty, the translation was successful and | |
| 182 // the returned rule is internally consistent. | |
| 183 static scoped_ptr<DeclarativeContentRule> Create( | |
| 184 url_matcher::URLMatcherConditionFactory* url_matcher_condition_factory, | |
| 185 content::BrowserContext* browser_context, | |
| 186 const Extension* extension, | |
| 187 base::Time extension_installation_time, | |
| 188 linked_ptr<JsonRule> rule, | |
| 189 ConsistencyChecker check_consistency, | |
| 190 std::string* error); | |
| 191 | |
| 192 const GlobalRuleId& id() const { return id_; } | |
| 193 const Tags& tags() const { return tags_; } | |
| 194 const std::string& extension_id() const { return id_.first; } | |
| 195 const ConditionSet& conditions() const { return *conditions_; } | |
| 196 const ActionSet& actions() const { return *actions_; } | |
| 197 Priority priority() const { return priority_; } | |
| 198 | |
| 199 // Calls actions().Apply(extension_id(), extension_installation_time_, | |
| 200 // apply_info). This function should only be called when the conditions_ are | |
| 201 // fulfilled (from a semantic point of view; no harm is done if this function | |
| 202 // is called at other times for testing purposes). | |
| 203 void Apply(ContentAction::ApplyInfo* apply_info) const; | |
| 204 | |
| 205 private: | |
| 206 GlobalRuleId id_; | |
| 207 Tags tags_; | |
| 208 base::Time extension_installation_time_; // For precedences of rules. | |
| 209 scoped_ptr<ConditionSet> conditions_; | |
| 210 scoped_ptr<ActionSet> actions_; | |
| 211 Priority priority_; | |
| 212 | |
| 213 DISALLOW_COPY_AND_ASSIGN(DeclarativeContentRule); | |
| 214 }; | |
| 215 | |
| 216 } // namespace extensions | |
| 217 | |
| 218 #endif // CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_CONTENT_DECLARATIVE_CONTENT _RULE_H__ | |
| OLD | NEW |