Add an UMA stat to be able to see if the User pods are show on start screen,
[chromium-blink-merge.git] / extensions / common / user_script.h
blob055338e32b6026d57133094cdd041c89acf376e1
1 // Copyright 2013 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.
5 #ifndef EXTENSIONS_COMMON_USER_SCRIPT_H_
6 #define EXTENSIONS_COMMON_USER_SCRIPT_H_
8 #include <string>
9 #include <vector>
11 #include "base/basictypes.h"
12 #include "base/files/file_path.h"
13 #include "base/strings/string_piece.h"
14 #include "extensions/common/host_id.h"
15 #include "extensions/common/url_pattern.h"
16 #include "extensions/common/url_pattern_set.h"
17 #include "url/gurl.h"
19 class Pickle;
20 class PickleIterator;
22 namespace extensions {
24 // Represents a user script, either a standalone one, or one that is part of an
25 // extension.
26 class UserScript {
27 public:
28 // The file extension for standalone user scripts.
29 static const char kFileExtension[];
31 static int GenerateUserScriptID();
33 // Check if a URL should be treated as a user script and converted to an
34 // extension.
35 static bool IsURLUserScript(const GURL& url, const std::string& mime_type);
37 // Get the valid user script schemes for the current process. If
38 // canExecuteScriptEverywhere is true, this will return ALL_SCHEMES.
39 static int ValidUserScriptSchemes(bool canExecuteScriptEverywhere = false);
41 // TODO(rdevlin.cronin) This and RunLocataion don't really belong here, since
42 // they are used for more than UserScripts (e.g., tabs.executeScript()).
43 // The type of injected script.
44 enum InjectionType {
45 // A content script specified in the extension's manifest.
46 CONTENT_SCRIPT,
47 // A script injected via, e.g. tabs.executeScript().
48 PROGRAMMATIC_SCRIPT
50 // The last type of injected script; used for enum verification in IPC.
51 // Update this if you add more injected script types!
52 static const InjectionType INJECTION_TYPE_LAST = PROGRAMMATIC_SCRIPT;
54 // Locations that user scripts can be run inside the document.
55 // The three run locations must strictly follow each other in both load order
56 // (i.e., start *always* comes before end) and numerically, as we use
57 // arithmetic checking (e.g., curr == last + 1). So, no bitmasks here!!
58 enum RunLocation {
59 UNDEFINED,
60 DOCUMENT_START, // After the documentElement is created, but before
61 // anything else happens.
62 DOCUMENT_END, // After the entire document is parsed. Same as
63 // DOMContentLoaded.
64 DOCUMENT_IDLE, // Sometime after DOMContentLoaded, as soon as the document
65 // is "idle". Currently this uses the simple heuristic of:
66 // min(DOM_CONTENT_LOADED + TIMEOUT, ONLOAD), but no
67 // particular injection point is guaranteed.
68 RUN_DEFERRED, // The user script's injection was deferred for permissions
69 // reasons, and was executed at a later time.
70 BROWSER_DRIVEN, // The user script will be injected when triggered by an
71 // IPC in the browser process.
72 RUN_LOCATION_LAST // Leave this as the last item.
75 // Holds actual script file info.
76 class File {
77 public:
78 File(const base::FilePath& extension_root,
79 const base::FilePath& relative_path,
80 const GURL& url);
81 File();
82 ~File();
84 const base::FilePath& extension_root() const { return extension_root_; }
85 const base::FilePath& relative_path() const { return relative_path_; }
87 const GURL& url() const { return url_; }
88 void set_url(const GURL& url) { url_ = url; }
90 // If external_content_ is set returns it as content otherwise it returns
91 // content_
92 const base::StringPiece GetContent() const {
93 if (external_content_.data())
94 return external_content_;
95 else
96 return content_;
98 void set_external_content(const base::StringPiece& content) {
99 external_content_ = content;
101 void set_content(const base::StringPiece& content) {
102 content_.assign(content.begin(), content.end());
105 // Serialization support. The content and FilePath members will not be
106 // serialized!
107 void Pickle(::Pickle* pickle) const;
108 void Unpickle(const ::Pickle& pickle, PickleIterator* iter);
110 private:
111 // Where the script file lives on the disk. We keep the path split so that
112 // it can be localized at will.
113 base::FilePath extension_root_;
114 base::FilePath relative_path_;
116 // The url to this scipt file.
117 GURL url_;
119 // The script content. It can be set to either loaded_content_ or
120 // externally allocated string.
121 base::StringPiece external_content_;
123 // Set when the content is loaded by LoadContent
124 std::string content_;
127 typedef std::vector<File> FileList;
129 // Render's routing info of a <webview> that the user script will be injected
130 // on. Only user scripts from <webview>s have a custom routing info.
131 struct RoutingInfo {
132 RoutingInfo() : render_process_id(-1), render_view_id(-1) {}
133 RoutingInfo(int render_process_id, int render_view_id)
134 : render_process_id(render_process_id),
135 render_view_id(render_view_id) {}
136 ~RoutingInfo() {}
138 int render_process_id;
139 int render_view_id;
142 // Type of a API consumer instance that user scripts will be injected on.
143 enum ConsumerInstanceType { TAB, WEBVIEW };
145 // Constructor. Default the run location to document end, which is like
146 // Greasemonkey and probably more useful for typical scripts.
147 UserScript();
148 ~UserScript();
150 const std::string& name_space() const { return name_space_; }
151 void set_name_space(const std::string& name_space) {
152 name_space_ = name_space;
155 const std::string& name() const { return name_; }
156 void set_name(const std::string& name) { name_ = name; }
158 const std::string& version() const { return version_; }
159 void set_version(const std::string& version) {
160 version_ = version;
163 const std::string& description() const { return description_; }
164 void set_description(const std::string& description) {
165 description_ = description;
168 // The place in the document to run the script.
169 RunLocation run_location() const { return run_location_; }
170 void set_run_location(RunLocation location) { run_location_ = location; }
172 // Whether to emulate greasemonkey when running this script.
173 bool emulate_greasemonkey() const { return emulate_greasemonkey_; }
174 void set_emulate_greasemonkey(bool val) { emulate_greasemonkey_ = val; }
176 // Whether to match all frames, or only the top one.
177 bool match_all_frames() const { return match_all_frames_; }
178 void set_match_all_frames(bool val) { match_all_frames_ = val; }
180 // Whether to match about:blank and about:srcdoc.
181 bool match_about_blank() const { return match_about_blank_; }
182 void set_match_about_blank(bool val) { match_about_blank_ = val; }
184 // The globs, if any, that determine which pages this script runs against.
185 // These are only used with "standalone" Greasemonkey-like user scripts.
186 const std::vector<std::string>& globs() const { return globs_; }
187 void add_glob(const std::string& glob) { globs_.push_back(glob); }
188 void clear_globs() { globs_.clear(); }
189 const std::vector<std::string>& exclude_globs() const {
190 return exclude_globs_;
192 void add_exclude_glob(const std::string& glob) {
193 exclude_globs_.push_back(glob);
195 void clear_exclude_globs() { exclude_globs_.clear(); }
197 // The URLPatterns, if any, that determine which pages this script runs
198 // against.
199 const URLPatternSet& url_patterns() const { return url_set_; }
200 void add_url_pattern(const URLPattern& pattern);
201 const URLPatternSet& exclude_url_patterns() const {
202 return exclude_url_set_;
204 void add_exclude_url_pattern(const URLPattern& pattern);
206 // List of js scripts for this user script
207 FileList& js_scripts() { return js_scripts_; }
208 const FileList& js_scripts() const { return js_scripts_; }
210 // List of css scripts for this user script
211 FileList& css_scripts() { return css_scripts_; }
212 const FileList& css_scripts() const { return css_scripts_; }
214 const std::string& extension_id() const { return host_id_.id(); }
216 const HostID& host_id() const { return host_id_; }
217 void set_host_id(const HostID& host_id) { host_id_ = host_id; }
219 const ConsumerInstanceType& consumer_instance_type() const {
220 return consumer_instance_type_;
222 void set_consumer_instance_type(
223 const ConsumerInstanceType& consumer_instance_type) {
224 consumer_instance_type_ = consumer_instance_type;
227 const RoutingInfo& routing_info() const { return routing_info_; }
228 void set_routing_info(const RoutingInfo& routing_info) {
229 routing_info_ = routing_info;
232 int id() const { return user_script_id_; }
233 void set_id(int id) { user_script_id_ = id; }
235 bool is_incognito_enabled() const { return incognito_enabled_; }
236 void set_incognito_enabled(bool enabled) { incognito_enabled_ = enabled; }
238 bool is_standalone() const { return extension_id().empty(); }
240 // Returns true if the script should be applied to the specified URL, false
241 // otherwise.
242 bool MatchesURL(const GURL& url) const;
244 // Serialize the UserScript into a pickle. The content of the scripts and
245 // paths to UserScript::Files will not be serialized!
246 void Pickle(::Pickle* pickle) const;
248 // Deserialize the script from a pickle. Note that this always succeeds
249 // because presumably we were the one that pickled it, and we did it
250 // correctly.
251 void Unpickle(const ::Pickle& pickle, PickleIterator* iter);
253 private:
254 // Pickle helper functions used to pickle the individual types of components.
255 void PickleGlobs(::Pickle* pickle,
256 const std::vector<std::string>& globs) const;
257 void PickleHostID(::Pickle* pickle, const HostID& host_id) const;
258 void PickleRoutingInfo(::Pickle* pickle,
259 const RoutingInfo& routing_info) const;
260 void PickleURLPatternSet(::Pickle* pickle,
261 const URLPatternSet& pattern_list) const;
262 void PickleScripts(::Pickle* pickle, const FileList& scripts) const;
264 // Unpickle helper functions used to unpickle individual types of components.
265 void UnpickleGlobs(const ::Pickle& pickle, PickleIterator* iter,
266 std::vector<std::string>* globs);
267 void UnpickleHostID(const ::Pickle& pickle,
268 PickleIterator* iter,
269 HostID* host_id);
270 void UnpickleRoutingInfo(const ::Pickle& pickle,
271 PickleIterator* iter,
272 RoutingInfo* routing_info);
273 void UnpickleURLPatternSet(const ::Pickle& pickle, PickleIterator* iter,
274 URLPatternSet* pattern_list);
275 void UnpickleScripts(const ::Pickle& pickle, PickleIterator* iter,
276 FileList* scripts);
278 // The location to run the script inside the document.
279 RunLocation run_location_;
281 // The namespace of the script. This is used by Greasemonkey in the same way
282 // as XML namespaces. Only used when parsing Greasemonkey-style scripts.
283 std::string name_space_;
285 // The script's name. Only used when parsing Greasemonkey-style scripts.
286 std::string name_;
288 // A longer description. Only used when parsing Greasemonkey-style scripts.
289 std::string description_;
291 // A version number of the script. Only used when parsing Greasemonkey-style
292 // scripts.
293 std::string version_;
295 // Greasemonkey-style globs that determine pages to inject the script into.
296 // These are only used with standalone scripts.
297 std::vector<std::string> globs_;
298 std::vector<std::string> exclude_globs_;
300 // URLPatterns that determine pages to inject the script into. These are
301 // only used with scripts that are part of extensions.
302 URLPatternSet url_set_;
303 URLPatternSet exclude_url_set_;
305 // List of js scripts defined in content_scripts
306 FileList js_scripts_;
308 // List of css scripts defined in content_scripts
309 FileList css_scripts_;
311 // The ID of the host this script is a part of. The |ID| of the
312 // |host_id| can be empty if the script is a "standlone" user script.
313 HostID host_id_;
315 // The type of the consumer instance that the script will be injected.
316 ConsumerInstanceType consumer_instance_type_;
318 // The render side's rounting info for content_scripts of <webview>.
319 RoutingInfo routing_info_;
321 // The globally-unique id associated with this user script. Defaults to
322 // -1 for invalid.
323 int user_script_id_;
325 // Whether we should try to emulate Greasemonkey's APIs when running this
326 // script.
327 bool emulate_greasemonkey_;
329 // Whether the user script should run in all frames, or only just the top one.
330 // Defaults to false.
331 bool match_all_frames_;
333 // Whether the user script should run in about:blank and about:srcdoc as well.
334 // Defaults to false.
335 bool match_about_blank_;
337 // True if the script should be injected into an incognito tab.
338 bool incognito_enabled_;
341 // For storing UserScripts with unique IDs in sets.
342 bool operator<(const UserScript& script1, const UserScript& script2);
344 typedef std::vector<UserScript> UserScriptList;
346 } // namespace extensions
348 #endif // EXTENSIONS_COMMON_USER_SCRIPT_H_