Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(204)

Side by Side Diff: chrome/browser/web_contents.cc

Issue 6274: Greasemonkey support. (Closed)
Patch Set: Created 12 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2006-2008 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 #include "chrome/browser/web_contents.h" 5 #include "chrome/browser/web_contents.h"
6 6
7 #include "base/command_line.h" 7 #include "base/command_line.h"
8 #include "base/compiler_specific.h" 8 #include "base/compiler_specific.h"
9 #include "base/path_service.h"
10 #include "base/singleton.h"
11 #include "base/string_util.h"
12 #include "net/base/escape.h"
13 #include "chrome/common/chrome_paths.h"
14 #include "base/file_util.h"
9 #include "base/file_version_info.h" 15 #include "base/file_version_info.h"
10 #include "chrome/app/locales/locale_settings.h" 16 #include "chrome/app/locales/locale_settings.h"
11 #include "chrome/browser/bookmarks/bookmark_model.h" 17 #include "chrome/browser/bookmarks/bookmark_model.h"
12 #include "chrome/browser/browser.h" 18 #include "chrome/browser/browser.h"
13 #include "chrome/browser/cache_manager_host.h" 19 #include "chrome/browser/cache_manager_host.h"
14 #include "chrome/browser/character_encoding.h" 20 #include "chrome/browser/character_encoding.h"
15 #include "chrome/browser/dom_operation_notification_details.h" 21 #include "chrome/browser/dom_operation_notification_details.h"
16 #include "chrome/browser/download/download_manager.h" 22 #include "chrome/browser/download/download_manager.h"
17 #include "chrome/browser/find_in_page_controller.h" 23 #include "chrome/browser/find_in_page_controller.h"
18 #include "chrome/browser/find_notification_details.h" 24 #include "chrome/browser/find_notification_details.h"
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after
146 } 152 }
147 153
148 // Returns true if the entry's transition type is FORM_SUBMIT. 154 // Returns true if the entry's transition type is FORM_SUBMIT.
149 bool IsFormSubmit(const NavigationEntry* entry) { 155 bool IsFormSubmit(const NavigationEntry* entry) {
150 return (PageTransition::StripQualifier(entry->transition_type()) == 156 return (PageTransition::StripQualifier(entry->transition_type()) ==
151 PageTransition::FORM_SUBMIT); 157 PageTransition::FORM_SUBMIT);
152 } 158 }
153 159
154 } // namespace 160 } // namespace
155 161
162 class GreaseMonkeyScript {
163 public:
164 GreaseMonkeyScript(const std::wstring& path)
165 : path_(path) {}
166
167 bool Parse(const std::string& content) {
168 // http://wiki.greasespot.net/Metadata_block
169 std::map<std::string, std::string> keyvals;
170 std::vector<std::string> lines;
171 SplitString(content, '\n', &lines);
172 bool in_metadata = false;
173 for (std::vector<std::string>::iterator i = lines.begin(); i != lines.end(); ++i) {
174 std::string line;
175 TrimWhitespace(*i, TRIM_ALL, &line);
176 if (line == "// ==UserScript==") {
177 in_metadata = true;
178 } else if (in_metadata) {
179 if (line == "// ==/UserScript==")
180 break;
181 std::vector<std::string> parts;
182 SplitString(line, ' ', &parts);
183 if (parts.size() > 3) {
184 for (size_t i = 3; i < parts.size(); ++i) {
185 parts[2] += " " + parts[i];
186 }
187 parts.resize(3);
188 }
189 if (parts.size() == 3 && parts[0] == "//") {
190 std::string key = parts[1];
191 if (key.length() > 0 && key[0] == '@')
192 key = key.substr(1);
193 std::string value;
194 TrimWhitespace(parts[2], TRIM_ALL, &value);
195 if (key == "include") {
196 include_globs_.push_back(value);
197 } else {
198 //NOTREACHED();
199 }
200 }
201 }
202 }
203 script_ = content;
204 return true;
205 }
206
207 bool AffectsURL(const GURL& url) {
208 const std::string& spec = url.spec();
209 for (std::vector<std::string>::const_iterator glob = include_globs_.begin();
210 glob != include_globs_.end(); ++glob) {
211 for (size_t i = 0; i < glob->size(); ++i) {
212 char glob_char = (*glob)[i];
213 if (glob_char == '*' && i == glob->size() - 1)
214 return true;
215 if (i > spec.size() || spec[i] != glob_char)
216 return false;
217 }
218 }
219 return true;
220 }
221
222 std::string script() const {
223 return script_;
224 }
225
226 std::wstring path() const {
227 return path_;
228 }
229
230 std::vector<std::string> include_globs_;
231 std::string script_;
232 std::wstring path_;
233 };
234
235 class ScriptInjector {
236 public:
237 ScriptInjector() {
238 LoadUserScripts();
239 }
240
241 ~ScriptInjector() {
242 ClearScripts();
243 }
244
245 void ClearScripts() {
246 for (std::vector<GreaseMonkeyScript*>::iterator script = scripts_.begin();
247 script != scripts_.end(); ++script) {
248 delete *script;
249 }
250 scripts_.clear();
251 }
252
253 void LoadUserScripts() {
254 ClearScripts();
255 std::wstring path;
256 DCHECK(PathService::Get(chrome::DIR_USER_SCRIPTS, &path));
257 file_util::FileEnumerator enumerator(path, false, file_util::FileEnumerator: :FILES,
258 L"*.user.js");
259 for (std::wstring file = enumerator.Next(); !file.empty(); file = enumerator .Next()) {
260 std::string contents;
261 DCHECK(file_util::ReadFileToString(file, &contents));
262 GreaseMonkeyScript* script = new GreaseMonkeyScript(file);
263 DCHECK(script->Parse(contents));
264 scripts_.push_back(script);
265 //url_js_["http://" + WideToUTF8(file_util::GetFilenameFromPath(file)) + " /"] = UTF8ToWide(contents);
266 }
267 }
268
269 void LoadCompleted(WebContents* web_contents) {
270 LoadUserScripts();
271 NavigationEntry* nav_entry = web_contents->controller()->GetActiveEntry();
272 DCHECK(nav_entry);
273 for (std::vector<GreaseMonkeyScript*>::const_iterator script = scripts_.begi n();
274 script != scripts_.end(); ++script) {
275 if ((*script)->AffectsURL(nav_entry->url())) {
276 RunJavaScript(web_contents, (*script)->script(), (*script)->path());
277 }
278 }
279 }
280
281 void RunJavaScript(WebContents* web_contents, const std::string& javascript,
282 const std::wstring& path) {
283 web_contents->render_view_host()->ExecuteJavascriptInWebFrame(std::wstring() ,
284 UTF8ToWide(jav ascript),
285 path);
286 }
287
288 std::vector<GreaseMonkeyScript*> scripts_;
289 };
290 Singleton<ScriptInjector> script_injector;
291
292
156 class WebContents::GearsCreateShortcutCallbackFunctor { 293 class WebContents::GearsCreateShortcutCallbackFunctor {
157 public: 294 public:
158 explicit GearsCreateShortcutCallbackFunctor(WebContents* contents) 295 explicit GearsCreateShortcutCallbackFunctor(WebContents* contents)
159 : contents_(contents) {} 296 : contents_(contents) {}
160 297
161 void Run(const GearsShortcutData& shortcut_data, bool success) { 298 void Run(const GearsShortcutData& shortcut_data, bool success) {
162 if (contents_) 299 if (contents_)
163 contents_->OnGearsCreateShortcutDone(shortcut_data, success); 300 contents_->OnGearsCreateShortcutDone(shortcut_data, success);
164 delete this; 301 delete this;
165 } 302 }
(...skipping 947 matching lines...) Expand 10 before | Expand all | Expand 10 after
1113 "available in OnMsgDidStopLoading unless we are loading the " 1250 "available in OnMsgDidStopLoading unless we are loading the "
1114 "initial blank page."; 1251 "initial blank page.";
1115 } 1252 }
1116 } 1253 }
1117 1254
1118 // Tell PasswordManager we've finished a page load, which serves as a 1255 // Tell PasswordManager we've finished a page load, which serves as a
1119 // green light to save pending passwords and reset itself. 1256 // green light to save pending passwords and reset itself.
1120 GetPasswordManager()->DidStopLoading(); 1257 GetPasswordManager()->DidStopLoading();
1121 1258
1122 SetIsLoading(false, details.get()); 1259 SetIsLoading(false, details.get());
1260 script_injector->LoadCompleted(this);
1123 } 1261 }
1124 1262
1125 void WebContents::DidStartProvisionalLoadForFrame( 1263 void WebContents::DidStartProvisionalLoadForFrame(
1126 RenderViewHost* render_view_host, 1264 RenderViewHost* render_view_host,
1127 bool is_main_frame, 1265 bool is_main_frame,
1128 const GURL& url) { 1266 const GURL& url) {
1129 ProvisionalLoadDetails details( 1267 ProvisionalLoadDetails details(
1130 is_main_frame, 1268 is_main_frame,
1131 render_manager_.IsRenderViewInterstitial(render_view_host), 1269 render_manager_.IsRenderViewInterstitial(render_view_host),
1132 controller()->IsURLInPageNavigation(url), 1270 controller()->IsURLInPageNavigation(url),
(...skipping 1176 matching lines...) Expand 10 before | Expand all | Expand 10 after
2309 // The favicon url isn't valid. This means there really isn't a favicon, 2447 // The favicon url isn't valid. This means there really isn't a favicon,
2310 // or the favicon url wasn't obtained before the load started. This assumes 2448 // or the favicon url wasn't obtained before the load started. This assumes
2311 // the later. 2449 // the later.
2312 // TODO(sky): Need a way to set the favicon that doesn't involve generating 2450 // TODO(sky): Need a way to set the favicon that doesn't involve generating
2313 // its url. 2451 // its url.
2314 new_url->SetFavIconURL(TemplateURL::GenerateFaviconURL(params.referrer)); 2452 new_url->SetFavIconURL(TemplateURL::GenerateFaviconURL(params.referrer));
2315 } 2453 }
2316 new_url->set_safe_for_autoreplace(true); 2454 new_url->set_safe_for_autoreplace(true);
2317 url_model->Add(new_url); 2455 url_model->Add(new_url);
2318 } 2456 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698