Extract SIGPIPE ignoring code to a common place.
[chromium-blink-merge.git] / chrome / common / extensions / user_script.h
blob4d52999068820ca7f93ee8f6f47ed37c519a5657
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.
5 #ifndef CHROME_COMMON_EXTENSIONS_USER_SCRIPT_H_
6 #define CHROME_COMMON_EXTENSIONS_USER_SCRIPT_H_
8 #include <string>
9 #include <vector>
11 #include "base/file_path.h"
12 #include "base/string_piece.h"
13 #include "chrome/common/extensions/url_pattern_set.h"
14 #include "extensions/common/url_pattern.h"
15 #include "googleurl/src/gurl.h"
17 class Pickle;
18 class PickleIterator;
20 namespace extensions {
22 // Represents a user script, either a standalone one, or one that is part of an
23 // extension.
24 class UserScript {
25 public:
26 // The file extension for standalone user scripts.
27 static const char kFileExtension[];
29 // The bitmask for valid user script injectable schemes used by URLPattern.
30 enum {
31 kValidUserScriptSchemes = URLPattern::SCHEME_HTTP |
32 URLPattern::SCHEME_HTTPS |
33 URLPattern::SCHEME_FILE |
34 URLPattern::SCHEME_FTP
37 // Check if a URL should be treated as a user script and converted to an
38 // extension.
39 static bool IsURLUserScript(const GURL& url, const std::string& mime_type);
41 // Locations that user scripts can be run inside the document.
42 enum RunLocation {
43 UNDEFINED,
44 DOCUMENT_START, // After the documentElemnet is created, but before
45 // anything else happens.
46 DOCUMENT_END, // After the entire document is parsed. Same as
47 // DOMContentLoaded.
48 DOCUMENT_IDLE, // Sometime after DOMContentLoaded, as soon as the document
49 // is "idle". Currently this uses the simple heuristic of:
50 // min(DOM_CONTENT_LOADED + TIMEOUT, ONLOAD), but no
51 // particular injection point is guaranteed.
52 RUN_LOCATION_LAST // Leave this as the last item.
55 // Holds actual script file info.
56 class File {
57 public:
58 File(const FilePath& extension_root, const FilePath& relative_path,
59 const GURL& url);
60 File();
61 ~File();
63 const FilePath& extension_root() const { return extension_root_; }
64 const FilePath& relative_path() const { return relative_path_; }
66 const GURL& url() const { return url_; }
67 void set_url(const GURL& url) { url_ = url; }
69 // If external_content_ is set returns it as content otherwise it returns
70 // content_
71 const base::StringPiece GetContent() const {
72 if (external_content_.data())
73 return external_content_;
74 else
75 return content_;
77 void set_external_content(const base::StringPiece& content) {
78 external_content_ = content;
80 void set_content(const base::StringPiece& content) {
81 content_.assign(content.begin(), content.end());
84 // Serialization support. The content and FilePath members will not be
85 // serialized!
86 void Pickle(::Pickle* pickle) const;
87 void Unpickle(const ::Pickle& pickle, PickleIterator* iter);
89 private:
90 // Where the script file lives on the disk. We keep the path split so that
91 // it can be localized at will.
92 FilePath extension_root_;
93 FilePath relative_path_;
95 // The url to this scipt file.
96 GURL url_;
98 // The script content. It can be set to either loaded_content_ or
99 // externally allocated string.
100 base::StringPiece external_content_;
102 // Set when the content is loaded by LoadContent
103 std::string content_;
106 typedef std::vector<File> FileList;
108 // Constructor. Default the run location to document end, which is like
109 // Greasemonkey and probably more useful for typical scripts.
110 UserScript();
111 ~UserScript();
113 const std::string& name_space() const { return name_space_; }
114 void set_name_space(const std::string& name_space) {
115 name_space_ = name_space;
118 const std::string& name() const { return name_; }
119 void set_name(const std::string& name) { name_ = name; }
121 const std::string& version() const { return version_; }
122 void set_version(const std::string& version) {
123 version_ = version;
126 const std::string& description() const { return description_; }
127 void set_description(const std::string& description) {
128 description_ = description;
131 // The place in the document to run the script.
132 RunLocation run_location() const { return run_location_; }
133 void set_run_location(RunLocation location) { run_location_ = location; }
135 // Whether to emulate greasemonkey when running this script.
136 bool emulate_greasemonkey() const { return emulate_greasemonkey_; }
137 void set_emulate_greasemonkey(bool val) { emulate_greasemonkey_ = val; }
139 // Whether to match all frames, or only the top one.
140 bool match_all_frames() const { return match_all_frames_; }
141 void set_match_all_frames(bool val) { match_all_frames_ = val; }
143 // The globs, if any, that determine which pages this script runs against.
144 // These are only used with "standalone" Greasemonkey-like user scripts.
145 const std::vector<std::string>& globs() const { return globs_; }
146 void add_glob(const std::string& glob) { globs_.push_back(glob); }
147 void clear_globs() { globs_.clear(); }
148 const std::vector<std::string>& exclude_globs() const {
149 return exclude_globs_;
151 void add_exclude_glob(const std::string& glob) {
152 exclude_globs_.push_back(glob);
154 void clear_exclude_globs() { exclude_globs_.clear(); }
156 // The URLPatterns, if any, that determine which pages this script runs
157 // against.
158 const URLPatternSet& url_patterns() const { return url_set_; }
159 void add_url_pattern(const URLPattern& pattern);
160 const URLPatternSet& exclude_url_patterns() const {
161 return exclude_url_set_;
163 void add_exclude_url_pattern(const URLPattern& pattern);
165 // List of js scripts for this user script
166 FileList& js_scripts() { return js_scripts_; }
167 const FileList& js_scripts() const { return js_scripts_; }
169 // List of css scripts for this user script
170 FileList& css_scripts() { return css_scripts_; }
171 const FileList& css_scripts() const { return css_scripts_; }
173 const std::string& extension_id() const { return extension_id_; }
174 void set_extension_id(const std::string& id) { extension_id_ = id; }
176 bool is_incognito_enabled() const { return incognito_enabled_; }
177 void set_incognito_enabled(bool enabled) { incognito_enabled_ = enabled; }
179 bool is_standalone() const { return extension_id_.empty(); }
181 // Returns true if the script should be applied to the specified URL, false
182 // otherwise.
183 bool MatchesURL(const GURL& url) const;
185 // Serialize the UserScript into a pickle. The content of the scripts and
186 // paths to UserScript::Files will not be serialized!
187 void Pickle(::Pickle* pickle) const;
189 // Deserialize the script from a pickle. Note that this always succeeds
190 // because presumably we were the one that pickled it, and we did it
191 // correctly.
192 void Unpickle(const ::Pickle& pickle, PickleIterator* iter);
194 private:
195 // Pickle helper functions used to pickle the individual types of components.
196 void PickleGlobs(::Pickle* pickle,
197 const std::vector<std::string>& globs) const;
198 void PickleURLPatternSet(::Pickle* pickle,
199 const URLPatternSet& pattern_list) const;
200 void PickleScripts(::Pickle* pickle, const FileList& scripts) const;
202 // Unpickle helper functions used to unpickle individual types of components.
203 void UnpickleGlobs(const ::Pickle& pickle, PickleIterator* iter,
204 std::vector<std::string>* globs);
205 void UnpickleURLPatternSet(const ::Pickle& pickle, PickleIterator* iter,
206 URLPatternSet* pattern_list);
207 void UnpickleScripts(const ::Pickle& pickle, PickleIterator* iter,
208 FileList* scripts);
210 // The location to run the script inside the document.
211 RunLocation run_location_;
213 // The namespace of the script. This is used by Greasemonkey in the same way
214 // as XML namespaces. Only used when parsing Greasemonkey-style scripts.
215 std::string name_space_;
217 // The script's name. Only used when parsing Greasemonkey-style scripts.
218 std::string name_;
220 // A longer description. Only used when parsing Greasemonkey-style scripts.
221 std::string description_;
223 // A version number of the script. Only used when parsing Greasemonkey-style
224 // scripts.
225 std::string version_;
227 // Greasemonkey-style globs that determine pages to inject the script into.
228 // These are only used with standalone scripts.
229 std::vector<std::string> globs_;
230 std::vector<std::string> exclude_globs_;
232 // URLPatterns that determine pages to inject the script into. These are
233 // only used with scripts that are part of extensions.
234 URLPatternSet url_set_;
235 URLPatternSet exclude_url_set_;
237 // List of js scripts defined in content_scripts
238 FileList js_scripts_;
240 // List of css scripts defined in content_scripts
241 FileList css_scripts_;
243 // The ID of the extension this script is a part of, if any. Can be empty if
244 // the script is a "standlone" user script.
245 std::string extension_id_;
247 // Whether we should try to emulate Greasemonkey's APIs when running this
248 // script.
249 bool emulate_greasemonkey_;
251 // Whether the user script should run in all frames, or only just the top one.
252 // Defaults to false.
253 bool match_all_frames_;
255 // True if the script should be injected into an incognito tab.
256 bool incognito_enabled_;
259 typedef std::vector<UserScript> UserScriptList;
261 } // namespace extensions
263 #endif // CHROME_COMMON_EXTENSIONS_USER_SCRIPT_H_