OLD | NEW |
1 // Copyright 2009 The Chromium Authors. All rights reserved. | 1 // Copyright 2009 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CHROME_COMMON_EXTENSIONS_USER_SCRIPT_H_ | 5 #ifndef CHROME_COMMON_EXTENSIONS_USER_SCRIPT_H_ |
6 #define CHROME_COMMON_EXTENSIONS_USER_SCRIPT_H_ | 6 #define CHROME_COMMON_EXTENSIONS_USER_SCRIPT_H_ |
7 | 7 |
8 #include <vector> | 8 #include <vector> |
9 #include <string> | 9 #include <string> |
10 | 10 |
11 #include "base/file_path.h" | 11 #include "base/file_path.h" |
12 #include "base/string_piece.h" | 12 #include "base/string_piece.h" |
13 #include "chrome/common/extensions/extension_resource.h" | 13 #include "chrome/common/extensions/extension_resource.h" |
14 #include "chrome/common/extensions/url_pattern.h" | 14 #include "chrome/common/extensions/url_pattern.h" |
15 #include "googleurl/src/gurl.h" | 15 #include "googleurl/src/gurl.h" |
16 | 16 |
17 class Pickle; | 17 class Pickle; |
18 | 18 |
19 // Represents a user script, either a standalone one, or one that is part of an | 19 // Represents a user script, either a standalone one, or one that is part of an |
20 // extension. | 20 // extension. |
21 class UserScript { | 21 class UserScript { |
22 public: | 22 public: |
23 typedef std::vector<URLPattern> PatternList; | 23 typedef std::vector<URLPattern> PatternList; |
24 | 24 |
| 25 // The file extension for standalone user scripts. |
| 26 static const char kFileExtension[]; |
| 27 |
| 28 // Check if a file or URL has the user script file extension. |
| 29 static bool HasUserScriptFileExtension(const GURL& url); |
| 30 static bool HasUserScriptFileExtension(const FilePath& path); |
| 31 |
25 // Locations that user scripts can be run inside the document. | 32 // Locations that user scripts can be run inside the document. |
26 enum RunLocation { | 33 enum RunLocation { |
27 DOCUMENT_START, // After the documentElemnet is created, but before | 34 DOCUMENT_START, // After the documentElemnet is created, but before |
28 // anything else happens. | 35 // anything else happens. |
29 DOCUMENT_END, // After the entire document is parsed. Same as | 36 DOCUMENT_END, // After the entire document is parsed. Same as |
30 // DOMContentLoaded. | 37 // DOMContentLoaded. |
31 | 38 |
32 RUN_LOCATION_LAST // Leave this as the last item. | 39 RUN_LOCATION_LAST // Leave this as the last item. |
33 }; | 40 }; |
34 | 41 |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
83 base::StringPiece external_content_; | 90 base::StringPiece external_content_; |
84 | 91 |
85 // Set when the content is loaded by LoadContent | 92 // Set when the content is loaded by LoadContent |
86 std::string content_; | 93 std::string content_; |
87 }; | 94 }; |
88 | 95 |
89 typedef std::vector<File> FileList; | 96 typedef std::vector<File> FileList; |
90 | 97 |
91 // Constructor. Default the run location to document end, which is like | 98 // Constructor. Default the run location to document end, which is like |
92 // Greasemonkey and probably more useful for typical scripts. | 99 // Greasemonkey and probably more useful for typical scripts. |
93 UserScript() : run_location_(DOCUMENT_END) {} | 100 UserScript() |
| 101 : run_location_(DOCUMENT_END), emulate_greasemonkey_(false) { |
| 102 } |
| 103 |
| 104 const std::string& name_space() const { return name_space_; } |
| 105 void set_name_space(const std::string& name_space) { |
| 106 name_space_ = name_space; |
| 107 } |
| 108 |
| 109 const std::string& name() const { return name_; } |
| 110 void set_name(const std::string& name) { name_ = name; } |
| 111 |
| 112 const std::string& description() const { return description_; } |
| 113 void set_description(const std::string& description) { |
| 114 description_ = description; |
| 115 } |
94 | 116 |
95 // The place in the document to run the script. | 117 // The place in the document to run the script. |
96 RunLocation run_location() const { return run_location_; } | 118 RunLocation run_location() const { return run_location_; } |
97 void set_run_location(RunLocation location) { run_location_ = location; } | 119 void set_run_location(RunLocation location) { run_location_ = location; } |
98 | 120 |
| 121 // Whether to emulate greasemonkey when running this script. |
| 122 bool emulate_greasemonkey() const { return emulate_greasemonkey_; } |
| 123 void set_emulate_greasemonkey(bool val) { emulate_greasemonkey_ = val; } |
| 124 |
99 // The globs, if any, that determine which pages this script runs against. | 125 // The globs, if any, that determine which pages this script runs against. |
100 // These are only used with "standalone" Greasemonkey-like user scripts. | 126 // These are only used with "standalone" Greasemonkey-like user scripts. |
101 const std::vector<std::string>& globs() const { return globs_; } | 127 const std::vector<std::string>& globs() const { return globs_; } |
102 void add_glob(const std::string& glob) { globs_.push_back(glob); } | 128 void add_glob(const std::string& glob) { globs_.push_back(glob); } |
103 void clear_globs() { globs_.clear(); } | 129 void clear_globs() { globs_.clear(); } |
| 130 const std::vector<std::string>& exclude_globs() const { |
| 131 return exclude_globs_; |
| 132 } |
| 133 void add_exclude_glob(const std::string& glob) { |
| 134 exclude_globs_.push_back(glob); |
| 135 } |
| 136 void clear_exclude_globs() { exclude_globs_.clear(); } |
104 | 137 |
105 // The URLPatterns, if any, that determine which pages this script runs | 138 // The URLPatterns, if any, that determine which pages this script runs |
106 // against. | 139 // against. |
107 const PatternList& url_patterns() const { return url_patterns_; } | 140 const PatternList& url_patterns() const { return url_patterns_; } |
108 void add_url_pattern(const URLPattern& pattern) { | 141 void add_url_pattern(const URLPattern& pattern) { |
109 url_patterns_.push_back(pattern); | 142 url_patterns_.push_back(pattern); |
110 } | 143 } |
111 void clear_url_patterns() { url_patterns_.clear(); } | 144 void clear_url_patterns() { url_patterns_.clear(); } |
112 | 145 |
113 // List of js scripts for this user script | 146 // List of js scripts for this user script |
(...skipping 19 matching lines...) Expand all Loading... |
133 | 166 |
134 // Deserialize the script from a pickle. Note that this always succeeds | 167 // Deserialize the script from a pickle. Note that this always succeeds |
135 // because presumably we were the one that pickled it, and we did it | 168 // because presumably we were the one that pickled it, and we did it |
136 // correctly. | 169 // correctly. |
137 void Unpickle(const ::Pickle& pickle, void** iter); | 170 void Unpickle(const ::Pickle& pickle, void** iter); |
138 | 171 |
139 private: | 172 private: |
140 // The location to run the script inside the document. | 173 // The location to run the script inside the document. |
141 RunLocation run_location_; | 174 RunLocation run_location_; |
142 | 175 |
| 176 // The namespace of the script. This is used by Greasemonkey in the same way |
| 177 // as XML namespaces. Only used when parsing Greasemonkey-style scripts. |
| 178 std::string name_space_; |
| 179 |
| 180 // The script's name. Only used when parsing Greasemonkey-style scripts. |
| 181 std::string name_; |
| 182 |
| 183 // A longer description. Only used when parsing Greasemonkey-style scripts. |
| 184 std::string description_; |
| 185 |
143 // Greasemonkey-style globs that determine pages to inject the script into. | 186 // Greasemonkey-style globs that determine pages to inject the script into. |
144 // These are only used with standalone scripts. | 187 // These are only used with standalone scripts. |
145 std::vector<std::string> globs_; | 188 std::vector<std::string> globs_; |
| 189 std::vector<std::string> exclude_globs_; |
146 | 190 |
147 // URLPatterns that determine pages to inject the script into. These are | 191 // URLPatterns that determine pages to inject the script into. These are |
148 // only used with scripts that are part of extensions. | 192 // only used with scripts that are part of extensions. |
149 PatternList url_patterns_; | 193 PatternList url_patterns_; |
150 | 194 |
151 // List of js scripts defined in content_scripts | 195 // List of js scripts defined in content_scripts |
152 FileList js_scripts_; | 196 FileList js_scripts_; |
153 | 197 |
154 // List of css scripts defined in content_scripts | 198 // List of css scripts defined in content_scripts |
155 FileList css_scripts_; | 199 FileList css_scripts_; |
156 | 200 |
157 // The ID of the extension this script is a part of, if any. Can be empty if | 201 // The ID of the extension this script is a part of, if any. Can be empty if |
158 // the script is a "standlone" user script. | 202 // the script is a "standlone" user script. |
159 std::string extension_id_; | 203 std::string extension_id_; |
| 204 |
| 205 // Whether we should try to emulate Greasemonkey's APIs when running this |
| 206 // script. |
| 207 bool emulate_greasemonkey_; |
160 }; | 208 }; |
161 | 209 |
162 typedef std::vector<UserScript> UserScriptList; | 210 typedef std::vector<UserScript> UserScriptList; |
163 | 211 |
164 #endif // CHROME_COMMON_EXTENSIONS_USER_SCRIPT_H_ | 212 #endif // CHROME_COMMON_EXTENSIONS_USER_SCRIPT_H_ |
OLD | NEW |