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_
11 #include "base/basictypes.h"
12 #include "base/files/file_path.h"
13 #include "base/strings/string_piece.h"
14 #include "extensions/common/url_pattern.h"
15 #include "extensions/common/url_pattern_set.h"
21 namespace extensions
{
23 // Represents a user script, either a standalone one, or one that is part of an
27 // The file extension for standalone user scripts.
28 static const char kFileExtension
[];
30 // Check if a URL should be treated as a user script and converted to an
32 static bool IsURLUserScript(const GURL
& url
, const std::string
& mime_type
);
34 // Get the valid user script schemes for the current process. If
35 // canExecuteScriptEverywhere is true, this will return ALL_SCHEMES.
36 static int ValidUserScriptSchemes(bool canExecuteScriptEverywhere
= false);
38 // TODO(rdevlin.cronin) This and RunLocataion don't really belong here, since
39 // they are used for more than UserScripts (e.g., tabs.executeScript()).
40 // The type of injected script.
42 // A content script specified in the extension's manifest.
44 // A script injected via, e.g. tabs.executeScript().
47 // The last type of injected script; used for enum verification in IPC.
48 // Update this if you add more injected script types!
49 static const InjectionType INJECTION_TYPE_LAST
= PROGRAMMATIC_SCRIPT
;
51 // Locations that user scripts can be run inside the document.
54 DOCUMENT_START
, // After the documentElement is created, but before
55 // anything else happens.
56 DOCUMENT_END
, // After the entire document is parsed. Same as
58 DOCUMENT_IDLE
, // Sometime after DOMContentLoaded, as soon as the document
59 // is "idle". Currently this uses the simple heuristic of:
60 // min(DOM_CONTENT_LOADED + TIMEOUT, ONLOAD), but no
61 // particular injection point is guaranteed.
62 RUN_DEFERRED
, // The user script's injection was deferred for permissions
63 // reasons, and was executed at a later time.
64 RUN_LOCATION_LAST
// Leave this as the last item.
67 // Holds actual script file info.
70 File(const base::FilePath
& extension_root
,
71 const base::FilePath
& relative_path
,
76 const base::FilePath
& extension_root() const { return extension_root_
; }
77 const base::FilePath
& relative_path() const { return relative_path_
; }
79 const GURL
& url() const { return url_
; }
80 void set_url(const GURL
& url
) { url_
= url
; }
82 // If external_content_ is set returns it as content otherwise it returns
84 const base::StringPiece
GetContent() const {
85 if (external_content_
.data())
86 return external_content_
;
90 void set_external_content(const base::StringPiece
& content
) {
91 external_content_
= content
;
93 void set_content(const base::StringPiece
& content
) {
94 content_
.assign(content
.begin(), content
.end());
97 // Serialization support. The content and FilePath members will not be
99 void Pickle(::Pickle
* pickle
) const;
100 void Unpickle(const ::Pickle
& pickle
, PickleIterator
* iter
);
103 // Where the script file lives on the disk. We keep the path split so that
104 // it can be localized at will.
105 base::FilePath extension_root_
;
106 base::FilePath relative_path_
;
108 // The url to this scipt file.
111 // The script content. It can be set to either loaded_content_ or
112 // externally allocated string.
113 base::StringPiece external_content_
;
115 // Set when the content is loaded by LoadContent
116 std::string content_
;
119 typedef std::vector
<File
> FileList
;
121 // Constructor. Default the run location to document end, which is like
122 // Greasemonkey and probably more useful for typical scripts.
126 const std::string
& name_space() const { return name_space_
; }
127 void set_name_space(const std::string
& name_space
) {
128 name_space_
= name_space
;
131 const std::string
& name() const { return name_
; }
132 void set_name(const std::string
& name
) { name_
= name
; }
134 const std::string
& version() const { return version_
; }
135 void set_version(const std::string
& version
) {
139 const std::string
& description() const { return description_
; }
140 void set_description(const std::string
& description
) {
141 description_
= description
;
144 // The place in the document to run the script.
145 RunLocation
run_location() const { return run_location_
; }
146 void set_run_location(RunLocation location
) { run_location_
= location
; }
148 // Whether to emulate greasemonkey when running this script.
149 bool emulate_greasemonkey() const { return emulate_greasemonkey_
; }
150 void set_emulate_greasemonkey(bool val
) { emulate_greasemonkey_
= val
; }
152 // Whether to match all frames, or only the top one.
153 bool match_all_frames() const { return match_all_frames_
; }
154 void set_match_all_frames(bool val
) { match_all_frames_
= val
; }
156 // Whether to match about:blank and about:srcdoc.
157 bool match_about_blank() const { return match_about_blank_
; }
158 void set_match_about_blank(bool val
) { match_about_blank_
= val
; }
160 // The globs, if any, that determine which pages this script runs against.
161 // These are only used with "standalone" Greasemonkey-like user scripts.
162 const std::vector
<std::string
>& globs() const { return globs_
; }
163 void add_glob(const std::string
& glob
) { globs_
.push_back(glob
); }
164 void clear_globs() { globs_
.clear(); }
165 const std::vector
<std::string
>& exclude_globs() const {
166 return exclude_globs_
;
168 void add_exclude_glob(const std::string
& glob
) {
169 exclude_globs_
.push_back(glob
);
171 void clear_exclude_globs() { exclude_globs_
.clear(); }
173 // The URLPatterns, if any, that determine which pages this script runs
175 const URLPatternSet
& url_patterns() const { return url_set_
; }
176 void add_url_pattern(const URLPattern
& pattern
);
177 const URLPatternSet
& exclude_url_patterns() const {
178 return exclude_url_set_
;
180 void add_exclude_url_pattern(const URLPattern
& pattern
);
182 // List of js scripts for this user script
183 FileList
& js_scripts() { return js_scripts_
; }
184 const FileList
& js_scripts() const { return js_scripts_
; }
186 // List of css scripts for this user script
187 FileList
& css_scripts() { return css_scripts_
; }
188 const FileList
& css_scripts() const { return css_scripts_
; }
190 const std::string
& extension_id() const { return extension_id_
; }
191 void set_extension_id(const std::string
& id
) { extension_id_
= id
; }
193 int64
id() const { return user_script_id_
; }
194 void set_id(int64 id
) { user_script_id_
= id
; }
196 bool is_incognito_enabled() const { return incognito_enabled_
; }
197 void set_incognito_enabled(bool enabled
) { incognito_enabled_
= enabled
; }
199 bool is_standalone() const { return extension_id_
.empty(); }
201 // Returns true if the script should be applied to the specified URL, false
203 bool MatchesURL(const GURL
& url
) const;
205 // Serialize the UserScript into a pickle. The content of the scripts and
206 // paths to UserScript::Files will not be serialized!
207 void Pickle(::Pickle
* pickle
) const;
209 // Deserialize the script from a pickle. Note that this always succeeds
210 // because presumably we were the one that pickled it, and we did it
212 void Unpickle(const ::Pickle
& pickle
, PickleIterator
* iter
);
215 // Pickle helper functions used to pickle the individual types of components.
216 void PickleGlobs(::Pickle
* pickle
,
217 const std::vector
<std::string
>& globs
) const;
218 void PickleURLPatternSet(::Pickle
* pickle
,
219 const URLPatternSet
& pattern_list
) const;
220 void PickleScripts(::Pickle
* pickle
, const FileList
& scripts
) const;
222 // Unpickle helper functions used to unpickle individual types of components.
223 void UnpickleGlobs(const ::Pickle
& pickle
, PickleIterator
* iter
,
224 std::vector
<std::string
>* globs
);
225 void UnpickleURLPatternSet(const ::Pickle
& pickle
, PickleIterator
* iter
,
226 URLPatternSet
* pattern_list
);
227 void UnpickleScripts(const ::Pickle
& pickle
, PickleIterator
* iter
,
230 // The location to run the script inside the document.
231 RunLocation run_location_
;
233 // The namespace of the script. This is used by Greasemonkey in the same way
234 // as XML namespaces. Only used when parsing Greasemonkey-style scripts.
235 std::string name_space_
;
237 // The script's name. Only used when parsing Greasemonkey-style scripts.
240 // A longer description. Only used when parsing Greasemonkey-style scripts.
241 std::string description_
;
243 // A version number of the script. Only used when parsing Greasemonkey-style
245 std::string version_
;
247 // Greasemonkey-style globs that determine pages to inject the script into.
248 // These are only used with standalone scripts.
249 std::vector
<std::string
> globs_
;
250 std::vector
<std::string
> exclude_globs_
;
252 // URLPatterns that determine pages to inject the script into. These are
253 // only used with scripts that are part of extensions.
254 URLPatternSet url_set_
;
255 URLPatternSet exclude_url_set_
;
257 // List of js scripts defined in content_scripts
258 FileList js_scripts_
;
260 // List of css scripts defined in content_scripts
261 FileList css_scripts_
;
263 // The ID of the extension this script is a part of, if any. Can be empty if
264 // the script is a "standlone" user script.
265 std::string extension_id_
;
267 // The globally-unique id associated with this user script. Defaults to
269 int64 user_script_id_
;
271 // Whether we should try to emulate Greasemonkey's APIs when running this
273 bool emulate_greasemonkey_
;
275 // Whether the user script should run in all frames, or only just the top one.
276 // Defaults to false.
277 bool match_all_frames_
;
279 // Whether the user script should run in about:blank and about:srcdoc as well.
280 // Defaults to false.
281 bool match_about_blank_
;
283 // True if the script should be injected into an incognito tab.
284 bool incognito_enabled_
;
287 typedef std::vector
<UserScript
> UserScriptList
;
289 } // namespace extensions
291 #endif // EXTENSIONS_COMMON_USER_SCRIPT_H_