| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/extensions/user_script_scheduler.h" | 5 #include "chrome/renderer/extensions/user_script_scheduler.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/message_loop.h" | 8 #include "base/message_loop.h" |
| 9 #include "chrome/common/extensions/extension_error_utils.h" | 9 #include "chrome/common/extensions/extension_error_utils.h" |
| 10 #include "chrome/common/extensions/extension_manifest_constants.h" | 10 #include "chrome/common/extensions/extension_manifest_constants.h" |
| (...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 154 return; | 154 return; |
| 155 } | 155 } |
| 156 | 156 |
| 157 std::vector<WebFrame*> frame_vector; | 157 std::vector<WebFrame*> frame_vector; |
| 158 frame_vector.push_back(frame_); | 158 frame_vector.push_back(frame_); |
| 159 if (params.all_frames) | 159 if (params.all_frames) |
| 160 GetAllChildFrames(frame_, &frame_vector); | 160 GetAllChildFrames(frame_, &frame_vector); |
| 161 | 161 |
| 162 for (std::vector<WebFrame*>::iterator frame_it = frame_vector.begin(); | 162 for (std::vector<WebFrame*>::iterator frame_it = frame_vector.begin(); |
| 163 frame_it != frame_vector.end(); ++frame_it) { | 163 frame_it != frame_vector.end(); ++frame_it) { |
| 164 WebFrame* frame = *frame_it; | 164 WebFrame* child_frame = *frame_it; |
| 165 if (params.is_javascript) { | 165 if (params.is_javascript) { |
| 166 // We recheck access here in the renderer for extra safety against races | 166 // We recheck access here in the renderer for extra safety against races |
| 167 // with navigation. | 167 // with navigation. |
| 168 // | 168 // |
| 169 // But different frames can have different URLs, and the extension might | 169 // But different frames can have different URLs, and the extension might |
| 170 // only have access to a subset of them. For the top frame, we can | 170 // only have access to a subset of them. For the top frame, we can |
| 171 // immediately send an error and stop because the browser process | 171 // immediately send an error and stop because the browser process |
| 172 // considers that an error too. | 172 // considers that an error too. |
| 173 // | 173 // |
| 174 // For child frames, we just skip ones the extension doesn't have access | 174 // For child frames, we just skip ones the extension doesn't have access |
| 175 // to and carry on. | 175 // to and carry on. |
| 176 if (!extension->CanExecuteScriptOnPage(frame->document().url(), | 176 if (!extension->CanExecuteScriptOnPage(child_frame->document().url(), |
| 177 frame_->document().url(), |
| 177 extension_helper->tab_id(), | 178 extension_helper->tab_id(), |
| 178 NULL, | 179 NULL, |
| 179 NULL)) { | 180 NULL)) { |
| 180 if (frame->parent()) { | 181 if (child_frame->parent()) { |
| 181 continue; | 182 continue; |
| 182 } else { | 183 } else { |
| 183 render_view->Send(new ExtensionHostMsg_ExecuteCodeFinished( | 184 render_view->Send(new ExtensionHostMsg_ExecuteCodeFinished( |
| 184 render_view->GetRoutingID(), | 185 render_view->GetRoutingID(), |
| 185 params.request_id, | 186 params.request_id, |
| 186 ExtensionErrorUtils::FormatErrorMessage( | 187 ExtensionErrorUtils::FormatErrorMessage( |
| 187 extension_manifest_errors::kCannotAccessPage, | 188 extension_manifest_errors::kCannotAccessPage, |
| 188 frame->document().url().spec()), | 189 child_frame->document().url().spec()), |
| 189 -1, | 190 -1, |
| 190 GURL(""), | 191 GURL(""), |
| 191 execution_results)); | 192 execution_results)); |
| 192 return; | 193 return; |
| 193 } | 194 } |
| 194 } | 195 } |
| 195 | 196 |
| 196 WebScriptSource source(WebString::fromUTF8(params.code)); | 197 WebScriptSource source(WebString::fromUTF8(params.code)); |
| 197 v8::HandleScope scope; | 198 v8::HandleScope scope; |
| 198 v8::Persistent<v8::Context> persistent_context = v8::Context::New(); | 199 v8::Persistent<v8::Context> persistent_context = v8::Context::New(); |
| 199 v8::Local<v8::Context> context = | 200 v8::Local<v8::Context> context = |
| 200 v8::Local<v8::Context>::New(persistent_context); | 201 v8::Local<v8::Context>::New(persistent_context); |
| 201 persistent_context.Dispose(); | 202 persistent_context.Dispose(); |
| 202 | 203 |
| 203 scoped_ptr<content::V8ValueConverter> v8_converter( | 204 scoped_ptr<content::V8ValueConverter> v8_converter( |
| 204 content::V8ValueConverter::create()); | 205 content::V8ValueConverter::create()); |
| 205 v8_converter->SetUndefinedAllowed(true); | 206 v8_converter->SetUndefinedAllowed(true); |
| 206 v8::Handle<v8::Value> script_value; | 207 v8::Handle<v8::Value> script_value; |
| 207 if (params.in_main_world) { | 208 if (params.in_main_world) { |
| 208 script_value = frame->executeScriptAndReturnValue(source); | 209 script_value = child_frame->executeScriptAndReturnValue(source); |
| 209 } else { | 210 } else { |
| 210 WebKit::WebVector<v8::Local<v8::Value> > results; | 211 WebKit::WebVector<v8::Local<v8::Value> > results; |
| 211 std::vector<WebScriptSource> sources; | 212 std::vector<WebScriptSource> sources; |
| 212 sources.push_back(source); | 213 sources.push_back(source); |
| 213 frame->executeScriptInIsolatedWorld( | 214 child_frame->executeScriptInIsolatedWorld( |
| 214 dispatcher_->user_script_slave()-> | 215 dispatcher_->user_script_slave()-> |
| 215 GetIsolatedWorldIdForExtension(extension, frame), | 216 GetIsolatedWorldIdForExtension(extension, child_frame), |
| 216 &sources.front(), sources.size(), EXTENSION_GROUP_CONTENT_SCRIPTS, | 217 &sources.front(), sources.size(), EXTENSION_GROUP_CONTENT_SCRIPTS, |
| 217 &results); | 218 &results); |
| 218 // We only expect one value back since we only pushed one source | 219 // We only expect one value back since we only pushed one source |
| 219 if (results.size() == 1 && !results[0].IsEmpty()) | 220 if (results.size() == 1 && !results[0].IsEmpty()) |
| 220 script_value = results[0]; | 221 script_value = results[0]; |
| 221 } | 222 } |
| 222 if (!script_value.IsEmpty()) { | 223 if (!script_value.IsEmpty()) { |
| 223 base::Value* base_val = | 224 base::Value* base_val = |
| 224 v8_converter->FromV8Value(script_value, context); | 225 v8_converter->FromV8Value(script_value, context); |
| 225 execution_results.Append(base_val); | 226 execution_results.Append(base_val); |
| 226 script_value.Clear(); | 227 script_value.Clear(); |
| 227 } | 228 } |
| 228 } else { | 229 } else { |
| 229 frame->document().insertUserStyleSheet( | 230 child_frame->document().insertUserStyleSheet( |
| 230 WebString::fromUTF8(params.code), | 231 WebString::fromUTF8(params.code), |
| 231 // Author level is consistent with WebView::addUserStyleSheet. | 232 // Author level is consistent with WebView::addUserStyleSheet. |
| 232 WebDocument::UserStyleAuthorLevel); | 233 WebDocument::UserStyleAuthorLevel); |
| 233 } | 234 } |
| 234 } | 235 } |
| 235 | 236 |
| 236 render_view->Send(new ExtensionHostMsg_ExecuteCodeFinished( | 237 render_view->Send(new ExtensionHostMsg_ExecuteCodeFinished( |
| 237 render_view->GetRoutingID(), | 238 render_view->GetRoutingID(), |
| 238 params.request_id, | 239 params.request_id, |
| 239 "", // no error | 240 "", // no error |
| (...skipping 10 matching lines...) Expand all Loading... |
| 250 | 251 |
| 251 for (WebFrame* child_frame = parent_frame->firstChild(); child_frame; | 252 for (WebFrame* child_frame = parent_frame->firstChild(); child_frame; |
| 252 child_frame = child_frame->nextSibling()) { | 253 child_frame = child_frame->nextSibling()) { |
| 253 frames_vector->push_back(child_frame); | 254 frames_vector->push_back(child_frame); |
| 254 GetAllChildFrames(child_frame, frames_vector); | 255 GetAllChildFrames(child_frame, frames_vector); |
| 255 } | 256 } |
| 256 return true; | 257 return true; |
| 257 } | 258 } |
| 258 | 259 |
| 259 } // namespace extensions | 260 } // namespace extensions |
| OLD | NEW |