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

Side by Side Diff: chrome/renderer/user_script_slave.cc

Issue 18183: First pass as implementing the greasemonkey API. This patch (Closed)
Patch Set: Created 11 years, 11 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
« no previous file with comments | « chrome/renderer/user_script_slave.h ('k') | webkit/glue/webframe.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2008 The Chromium Authors. All rights reserved. 1 // Copyright (c) 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/renderer/user_script_slave.h" 5 #include "chrome/renderer/user_script_slave.h"
6 6
7 #include "base/logging.h" 7 #include "base/logging.h"
8 #include "base/pickle.h" 8 #include "base/pickle.h"
9 #include "base/shared_memory.h" 9 #include "base/shared_memory.h"
10 #include "chrome/common/resource_bundle.h"
11 #include "chrome/renderer/renderer_resources.h"
10 #include "googleurl/src/gurl.h" 12 #include "googleurl/src/gurl.h"
11 13
14 // These two strings are injected before and after the Greasemonkey API and
15 // user script to wrap it in an anonymous scope.
16 static const char kUserScriptHead[] = "(function (unsafeWindow) {";
17 static const char kUserScriptTail[] = "\n})(window);";
12 18
13 // UserScript 19 // UserScript
14 20
15 void UserScript::Parse(const StringPiece& script_text) { 21 void UserScript::Parse(const StringPiece& script_text) {
16 ParseMetadata(script_text); 22 ParseMetadata(script_text);
17 23
18 // TODO(aa): Set body to just the part after the metadata block? This would 24 // TODO(aa): Set body to just the part after the metadata block? This would
19 // significantly cut down on the size of the injected script in some cases. 25 // significantly cut down on the size of the injected script in some cases.
20 // Would require remembering the line number the body begins at, for correct 26 // Would require remembering the line number the body begins at, for correct
21 // error line number reporting. 27 // error line number reporting.
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after
103 default: 109 default:
104 output_pattern += input_pattern[i]; 110 output_pattern += input_pattern[i];
105 } 111 }
106 } 112 }
107 113
108 return output_pattern; 114 return output_pattern;
109 } 115 }
110 116
111 117
112 // UserScriptSlave 118 // UserScriptSlave
113 UserScriptSlave::UserScriptSlave() : shared_memory_(NULL) { 119 UserScriptSlave::UserScriptSlave()
120 : shared_memory_(NULL),
121 user_script_start_line_(0) {
122 // TODO: Only windows supports resources and only windows supports user
123 // scrips, so only load the Greasemonkey API on windows. Fix this when
124 // better cross platofrm support is available.
125 #if defined(OS_WIN)
126 api_js_ = ResourceBundle::GetSharedInstance().GetRawDataResource(
127 IDR_GREASEMONKEY_API_JS);
128 #endif
129
130 // Count the number of lines that will be injected before the user script.
131 StringPiece::size_type pos = 0;
132 while ((pos = api_js_.find('\n', pos)) != StringPiece::npos) {
133 user_script_start_line_++;
134 pos++;
135 }
136
137 // Add one more line to account for the function that wraps everything.
138 user_script_start_line_++;
114 } 139 }
115 140
116 bool UserScriptSlave::UpdateScripts(base::SharedMemoryHandle shared_memory) { 141 bool UserScriptSlave::UpdateScripts(base::SharedMemoryHandle shared_memory) {
117 scripts_.clear(); 142 scripts_.clear();
118 143
119 // Create the shared memory object (read only). 144 // Create the shared memory object (read only).
120 shared_memory_.reset(new base::SharedMemory(shared_memory, true)); 145 shared_memory_.reset(new base::SharedMemory(shared_memory, true));
121 if (!shared_memory_.get()) 146 if (!shared_memory_.get())
122 return false; 147 return false;
123 148
(...skipping 30 matching lines...) Expand all
154 script.Parse(StringPiece(body, body_length)); 179 script.Parse(StringPiece(body, body_length));
155 } 180 }
156 181
157 return true; 182 return true;
158 } 183 }
159 184
160 bool UserScriptSlave::InjectScripts(WebFrame* frame) { 185 bool UserScriptSlave::InjectScripts(WebFrame* frame) {
161 for (std::vector<UserScript>::iterator script = scripts_.begin(); 186 for (std::vector<UserScript>::iterator script = scripts_.begin();
162 script != scripts_.end(); ++script) { 187 script != scripts_.end(); ++script) {
163 if (script->MatchesUrl(frame->GetURL())) { 188 if (script->MatchesUrl(frame->GetURL())) {
164 frame->ExecuteJavaScript(script->GetBody().as_string(), 189 std::string inject(kUserScriptHead);
165 GURL(script->GetURL().as_string())); 190 inject.append(api_js_.as_string());
191 inject.append(script->GetBody().as_string());
192 inject.append(kUserScriptTail);
193 frame->ExecuteJavaScript(inject,
194 GURL(script->GetURL().as_string()),
195 -user_script_start_line_);
166 } 196 }
167 } 197 }
168 198
169 return true; 199 return true;
170 } 200 }
OLDNEW
« no previous file with comments | « chrome/renderer/user_script_slave.h ('k') | webkit/glue/webframe.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698