• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2011 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 CHROME_COMMON_EXTENSIONS_USER_SCRIPT_H_
6 #define CHROME_COMMON_EXTENSIONS_USER_SCRIPT_H_
7 #pragma once
8 
9 #include <string>
10 #include <vector>
11 
12 #include "base/file_path.h"
13 #include "base/string_piece.h"
14 #include "googleurl/src/gurl.h"
15 #include "chrome/common/extensions/url_pattern.h"
16 
17 class Pickle;
18 class URLPattern;
19 
20 // Represents a user script, either a standalone one, or one that is part of an
21 // extension.
22 class UserScript {
23  public:
24   typedef std::vector<URLPattern> PatternList;
25 
26   // The file extension for standalone user scripts.
27   static const char kFileExtension[];
28 
29   // The bitmask for valid user script injectable schemes used by URLPattern.
30   static const int kValidUserScriptSchemes;
31 
32   // Check if a URL should be treated as a user script and converted to an
33   // extension.
34   static bool IsURLUserScript(const GURL& url, const std::string& mime_type);
35 
36   // Locations that user scripts can be run inside the document.
37   enum RunLocation {
38     DOCUMENT_START,  // After the documentElemnet is created, but before
39                      // anything else happens.
40     DOCUMENT_END,  // After the entire document is parsed. Same as
41                    // DOMContentLoaded.
42     DOCUMENT_IDLE,  // Sometime after DOMContentLoaded, as soon as the document
43                     // is "idle". Currently this uses the simple heuristic of:
44                     // min(DOM_CONTENT_LOADED + TIMEOUT, ONLOAD), but no
45                     // particular injection point is guaranteed.
46 
47     RUN_LOCATION_LAST  // Leave this as the last item.
48   };
49 
50   // Holds actual script file info.
51   class File {
52    public:
53     File(const FilePath& extension_root, const FilePath& relative_path,
54          const GURL& url);
55     File();
56     ~File();
57 
extension_root()58     const FilePath& extension_root() const { return extension_root_; }
relative_path()59     const FilePath& relative_path() const { return relative_path_; }
60 
url()61     const GURL& url() const { return url_; }
set_url(const GURL & url)62     void set_url(const GURL& url) { url_ = url; }
63 
64     // If external_content_ is set returns it as content otherwise it returns
65     // content_
GetContent()66     const base::StringPiece GetContent() const {
67       if (external_content_.data())
68         return external_content_;
69       else
70         return content_;
71     }
set_external_content(const base::StringPiece & content)72     void set_external_content(const base::StringPiece& content) {
73       external_content_ = content;
74     }
set_content(const base::StringPiece & content)75     void set_content(const base::StringPiece& content) {
76       content_.assign(content.begin(), content.end());
77     }
78 
79     // Serialization support. The content and FilePath members will not be
80     // serialized!
81     void Pickle(::Pickle* pickle) const;
82     void Unpickle(const ::Pickle& pickle, void** iter);
83 
84    private:
85     // Where the script file lives on the disk. We keep the path split so that
86     // it can be localized at will.
87     FilePath extension_root_;
88     FilePath relative_path_;
89 
90     // The url to this scipt file.
91     GURL url_;
92 
93     // The script content. It can be set to either loaded_content_ or
94     // externally allocated string.
95     base::StringPiece external_content_;
96 
97     // Set when the content is loaded by LoadContent
98     std::string content_;
99   };
100 
101   typedef std::vector<File> FileList;
102 
103   // Constructor. Default the run location to document end, which is like
104   // Greasemonkey and probably more useful for typical scripts.
105   UserScript();
106   ~UserScript();
107 
name_space()108   const std::string& name_space() const { return name_space_; }
set_name_space(const std::string & name_space)109   void set_name_space(const std::string& name_space) {
110     name_space_ = name_space;
111   }
112 
name()113   const std::string& name() const { return name_; }
set_name(const std::string & name)114   void set_name(const std::string& name) { name_ = name; }
115 
version()116   const std::string& version() const { return version_; }
set_version(const std::string & version)117   void set_version(const std::string& version) {
118     version_ = version;
119   }
120 
description()121   const std::string& description() const { return description_; }
set_description(const std::string & description)122   void set_description(const std::string& description) {
123     description_ = description;
124   }
125 
126   // The place in the document to run the script.
run_location()127   RunLocation run_location() const { return run_location_; }
set_run_location(RunLocation location)128   void set_run_location(RunLocation location) { run_location_ = location; }
129 
130   // Whether to emulate greasemonkey when running this script.
emulate_greasemonkey()131   bool emulate_greasemonkey() const { return emulate_greasemonkey_; }
set_emulate_greasemonkey(bool val)132   void set_emulate_greasemonkey(bool val) { emulate_greasemonkey_ = val; }
133 
134   // Whether to match all frames, or only the top one.
match_all_frames()135   bool match_all_frames() const { return match_all_frames_; }
set_match_all_frames(bool val)136   void set_match_all_frames(bool val) { match_all_frames_ = val; }
137 
138   // The globs, if any, that determine which pages this script runs against.
139   // These are only used with "standalone" Greasemonkey-like user scripts.
globs()140   const std::vector<std::string>& globs() const { return globs_; }
add_glob(const std::string & glob)141   void add_glob(const std::string& glob) { globs_.push_back(glob); }
clear_globs()142   void clear_globs() { globs_.clear(); }
exclude_globs()143   const std::vector<std::string>& exclude_globs() const {
144     return exclude_globs_;
145   }
add_exclude_glob(const std::string & glob)146   void add_exclude_glob(const std::string& glob) {
147     exclude_globs_.push_back(glob);
148   }
clear_exclude_globs()149   void clear_exclude_globs() { exclude_globs_.clear(); }
150 
151   // The URLPatterns, if any, that determine which pages this script runs
152   // against.
url_patterns()153   const PatternList& url_patterns() const { return url_patterns_; }
154   void add_url_pattern(const URLPattern& pattern);
155 
156   // List of js scripts for this user script
js_scripts()157   FileList& js_scripts() { return js_scripts_; }
js_scripts()158   const FileList& js_scripts() const { return js_scripts_; }
159 
160   // List of css scripts for this user script
css_scripts()161   FileList& css_scripts() { return css_scripts_; }
css_scripts()162   const FileList& css_scripts() const { return css_scripts_; }
163 
extension_id()164   const std::string& extension_id() const { return extension_id_; }
set_extension_id(const std::string & id)165   void set_extension_id(const std::string& id) { extension_id_ = id; }
166 
is_incognito_enabled()167   bool is_incognito_enabled() const { return incognito_enabled_; }
set_incognito_enabled(bool enabled)168   void set_incognito_enabled(bool enabled) { incognito_enabled_ = enabled; }
169 
is_standalone()170   bool is_standalone() const { return extension_id_.empty(); }
171 
172   // Returns true if the script should be applied to the specified URL, false
173   // otherwise.
174   bool MatchesUrl(const GURL& url) const;
175 
176   // Serialize the UserScript into a pickle. The content of the scripts and
177   // paths to UserScript::Files will not be serialized!
178   void Pickle(::Pickle* pickle) const;
179 
180   // Deserialize the script from a pickle. Note that this always succeeds
181   // because presumably we were the one that pickled it, and we did it
182   // correctly.
183   void Unpickle(const ::Pickle& pickle, void** iter);
184 
185  private:
186   // The location to run the script inside the document.
187   RunLocation run_location_;
188 
189   // The namespace of the script. This is used by Greasemonkey in the same way
190   // as XML namespaces. Only used when parsing Greasemonkey-style scripts.
191   std::string name_space_;
192 
193   // The script's name. Only used when parsing Greasemonkey-style scripts.
194   std::string name_;
195 
196   // A longer description. Only used when parsing Greasemonkey-style scripts.
197   std::string description_;
198 
199   // A version number of the script. Only used when parsing Greasemonkey-style
200   // scripts.
201   std::string version_;
202 
203   // Greasemonkey-style globs that determine pages to inject the script into.
204   // These are only used with standalone scripts.
205   std::vector<std::string> globs_;
206   std::vector<std::string> exclude_globs_;
207 
208   // URLPatterns that determine pages to inject the script into. These are
209   // only used with scripts that are part of extensions.
210   PatternList url_patterns_;
211 
212   // List of js scripts defined in content_scripts
213   FileList js_scripts_;
214 
215   // List of css scripts defined in content_scripts
216   FileList css_scripts_;
217 
218   // The ID of the extension this script is a part of, if any. Can be empty if
219   // the script is a "standlone" user script.
220   std::string extension_id_;
221 
222   // Whether we should try to emulate Greasemonkey's APIs when running this
223   // script.
224   bool emulate_greasemonkey_;
225 
226   // Whether the user script should run in all frames, or only just the top one.
227   // Defaults to false.
228   bool match_all_frames_;
229 
230   // True if the script should be injected into an incognito tab.
231   bool incognito_enabled_;
232 };
233 
234 typedef std::vector<UserScript> UserScriptList;
235 
236 #endif  // CHROME_COMMON_EXTENSIONS_USER_SCRIPT_H_
237