• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 #ifndef EXTENSIONS_COMMON_USER_SCRIPT_H_
6 #define EXTENSIONS_COMMON_USER_SCRIPT_H_
7 
8 #include <string>
9 #include <vector>
10 
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"
16 #include "url/gurl.h"
17 
18 class Pickle;
19 class PickleIterator;
20 
21 namespace extensions {
22 
23 // Represents a user script, either a standalone one, or one that is part of an
24 // extension.
25 class UserScript {
26  public:
27   // The file extension for standalone user scripts.
28   static const char kFileExtension[];
29 
30   // Check if a URL should be treated as a user script and converted to an
31   // extension.
32   static bool IsURLUserScript(const GURL& url, const std::string& mime_type);
33 
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);
37 
38   // Locations that user scripts can be run inside the document.
39   enum RunLocation {
40     UNDEFINED,
41     DOCUMENT_START,  // After the documentElement is created, but before
42                      // anything else happens.
43     DOCUMENT_END,  // After the entire document is parsed. Same as
44                    // DOMContentLoaded.
45     DOCUMENT_IDLE,  // Sometime after DOMContentLoaded, as soon as the document
46                     // is "idle". Currently this uses the simple heuristic of:
47                     // min(DOM_CONTENT_LOADED + TIMEOUT, ONLOAD), but no
48                     // particular injection point is guaranteed.
49     RUN_DEFERRED,  // The user script's injection was deferred for permissions
50                    // reasons, and was executed at a later time.
51     RUN_LOCATION_LAST  // Leave this as the last item.
52   };
53 
54   // Holds actual script file info.
55   class File {
56    public:
57     File(const base::FilePath& extension_root,
58          const base::FilePath& relative_path,
59          const GURL& url);
60     File();
61     ~File();
62 
extension_root()63     const base::FilePath& extension_root() const { return extension_root_; }
relative_path()64     const base::FilePath& relative_path() const { return relative_path_; }
65 
url()66     const GURL& url() const { return url_; }
set_url(const GURL & url)67     void set_url(const GURL& url) { url_ = url; }
68 
69     // If external_content_ is set returns it as content otherwise it returns
70     // content_
GetContent()71     const base::StringPiece GetContent() const {
72       if (external_content_.data())
73         return external_content_;
74       else
75         return content_;
76     }
set_external_content(const base::StringPiece & content)77     void set_external_content(const base::StringPiece& content) {
78       external_content_ = content;
79     }
set_content(const base::StringPiece & content)80     void set_content(const base::StringPiece& content) {
81       content_.assign(content.begin(), content.end());
82     }
83 
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);
88 
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     base::FilePath extension_root_;
93     base::FilePath relative_path_;
94 
95     // The url to this scipt file.
96     GURL url_;
97 
98     // The script content. It can be set to either loaded_content_ or
99     // externally allocated string.
100     base::StringPiece external_content_;
101 
102     // Set when the content is loaded by LoadContent
103     std::string content_;
104   };
105 
106   typedef std::vector<File> FileList;
107 
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();
112 
name_space()113   const std::string& name_space() const { return name_space_; }
set_name_space(const std::string & name_space)114   void set_name_space(const std::string& name_space) {
115     name_space_ = name_space;
116   }
117 
name()118   const std::string& name() const { return name_; }
set_name(const std::string & name)119   void set_name(const std::string& name) { name_ = name; }
120 
version()121   const std::string& version() const { return version_; }
set_version(const std::string & version)122   void set_version(const std::string& version) {
123     version_ = version;
124   }
125 
description()126   const std::string& description() const { return description_; }
set_description(const std::string & description)127   void set_description(const std::string& description) {
128     description_ = description;
129   }
130 
131   // The place in the document to run the script.
run_location()132   RunLocation run_location() const { return run_location_; }
set_run_location(RunLocation location)133   void set_run_location(RunLocation location) { run_location_ = location; }
134 
135   // Whether to emulate greasemonkey when running this script.
emulate_greasemonkey()136   bool emulate_greasemonkey() const { return emulate_greasemonkey_; }
set_emulate_greasemonkey(bool val)137   void set_emulate_greasemonkey(bool val) { emulate_greasemonkey_ = val; }
138 
139   // Whether to match all frames, or only the top one.
match_all_frames()140   bool match_all_frames() const { return match_all_frames_; }
set_match_all_frames(bool val)141   void set_match_all_frames(bool val) { match_all_frames_ = val; }
142 
143   // Whether to match about:blank and about:srcdoc.
match_about_blank()144   bool match_about_blank() const { return match_about_blank_; }
set_match_about_blank(bool val)145   void set_match_about_blank(bool val) { match_about_blank_ = val; }
146 
147   // The globs, if any, that determine which pages this script runs against.
148   // These are only used with "standalone" Greasemonkey-like user scripts.
globs()149   const std::vector<std::string>& globs() const { return globs_; }
add_glob(const std::string & glob)150   void add_glob(const std::string& glob) { globs_.push_back(glob); }
clear_globs()151   void clear_globs() { globs_.clear(); }
exclude_globs()152   const std::vector<std::string>& exclude_globs() const {
153     return exclude_globs_;
154   }
add_exclude_glob(const std::string & glob)155   void add_exclude_glob(const std::string& glob) {
156     exclude_globs_.push_back(glob);
157   }
clear_exclude_globs()158   void clear_exclude_globs() { exclude_globs_.clear(); }
159 
160   // The URLPatterns, if any, that determine which pages this script runs
161   // against.
url_patterns()162   const URLPatternSet& url_patterns() const { return url_set_; }
163   void add_url_pattern(const URLPattern& pattern);
exclude_url_patterns()164   const URLPatternSet& exclude_url_patterns() const {
165     return exclude_url_set_;
166   }
167   void add_exclude_url_pattern(const URLPattern& pattern);
168 
169   // List of js scripts for this user script
js_scripts()170   FileList& js_scripts() { return js_scripts_; }
js_scripts()171   const FileList& js_scripts() const { return js_scripts_; }
172 
173   // List of css scripts for this user script
css_scripts()174   FileList& css_scripts() { return css_scripts_; }
css_scripts()175   const FileList& css_scripts() const { return css_scripts_; }
176 
extension_id()177   const std::string& extension_id() const { return extension_id_; }
set_extension_id(const std::string & id)178   void set_extension_id(const std::string& id) { extension_id_ = id; }
179 
id()180   int64 id() const { return user_script_id_; }
set_id(int64 id)181   void set_id(int64 id) { user_script_id_ = id; }
182 
is_incognito_enabled()183   bool is_incognito_enabled() const { return incognito_enabled_; }
set_incognito_enabled(bool enabled)184   void set_incognito_enabled(bool enabled) { incognito_enabled_ = enabled; }
185 
is_standalone()186   bool is_standalone() const { return extension_id_.empty(); }
187 
188   // Returns true if the script should be applied to the specified URL, false
189   // otherwise.
190   bool MatchesURL(const GURL& url) const;
191 
192   // Serialize the UserScript into a pickle. The content of the scripts and
193   // paths to UserScript::Files will not be serialized!
194   void Pickle(::Pickle* pickle) const;
195 
196   // Deserialize the script from a pickle. Note that this always succeeds
197   // because presumably we were the one that pickled it, and we did it
198   // correctly.
199   void Unpickle(const ::Pickle& pickle, PickleIterator* iter);
200 
201  private:
202   // Pickle helper functions used to pickle the individual types of components.
203   void PickleGlobs(::Pickle* pickle,
204                    const std::vector<std::string>& globs) const;
205   void PickleURLPatternSet(::Pickle* pickle,
206                            const URLPatternSet& pattern_list) const;
207   void PickleScripts(::Pickle* pickle, const FileList& scripts) const;
208 
209   // Unpickle helper functions used to unpickle individual types of components.
210   void UnpickleGlobs(const ::Pickle& pickle, PickleIterator* iter,
211                      std::vector<std::string>* globs);
212   void UnpickleURLPatternSet(const ::Pickle& pickle, PickleIterator* iter,
213                              URLPatternSet* pattern_list);
214   void UnpickleScripts(const ::Pickle& pickle, PickleIterator* iter,
215                        FileList* scripts);
216 
217   // The location to run the script inside the document.
218   RunLocation run_location_;
219 
220   // The namespace of the script. This is used by Greasemonkey in the same way
221   // as XML namespaces. Only used when parsing Greasemonkey-style scripts.
222   std::string name_space_;
223 
224   // The script's name. Only used when parsing Greasemonkey-style scripts.
225   std::string name_;
226 
227   // A longer description. Only used when parsing Greasemonkey-style scripts.
228   std::string description_;
229 
230   // A version number of the script. Only used when parsing Greasemonkey-style
231   // scripts.
232   std::string version_;
233 
234   // Greasemonkey-style globs that determine pages to inject the script into.
235   // These are only used with standalone scripts.
236   std::vector<std::string> globs_;
237   std::vector<std::string> exclude_globs_;
238 
239   // URLPatterns that determine pages to inject the script into. These are
240   // only used with scripts that are part of extensions.
241   URLPatternSet url_set_;
242   URLPatternSet exclude_url_set_;
243 
244   // List of js scripts defined in content_scripts
245   FileList js_scripts_;
246 
247   // List of css scripts defined in content_scripts
248   FileList css_scripts_;
249 
250   // The ID of the extension this script is a part of, if any. Can be empty if
251   // the script is a "standlone" user script.
252   std::string extension_id_;
253 
254   // The globally-unique id associated with this user script. Defaults to
255   // -1 for invalid.
256   int64 user_script_id_;
257 
258   // Whether we should try to emulate Greasemonkey's APIs when running this
259   // script.
260   bool emulate_greasemonkey_;
261 
262   // Whether the user script should run in all frames, or only just the top one.
263   // Defaults to false.
264   bool match_all_frames_;
265 
266   // Whether the user script should run in about:blank and about:srcdoc as well.
267   // Defaults to false.
268   bool match_about_blank_;
269 
270   // True if the script should be injected into an incognito tab.
271   bool incognito_enabled_;
272 };
273 
274 typedef std::vector<UserScript> UserScriptList;
275 
276 }  // namespace extensions
277 
278 #endif  // EXTENSIONS_COMMON_USER_SCRIPT_H_
279