OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "extensions/renderer/script_context.h" | 5 #include "extensions/renderer/script_context.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
9 #include "base/strings/string_split.h" | 9 #include "base/strings/string_split.h" |
10 #include "base/strings/string_util.h" | 10 #include "base/strings/string_util.h" |
11 #include "base/strings/stringprintf.h" | 11 #include "base/strings/stringprintf.h" |
12 #include "base/values.h" | 12 #include "base/values.h" |
13 #include "content/public/child/v8_value_converter.h" | 13 #include "content/public/child/v8_value_converter.h" |
14 #include "content/public/common/url_constants.h" | 14 #include "content/public/common/url_constants.h" |
15 #include "content/public/renderer/render_frame.h" | 15 #include "content/public/renderer/render_frame.h" |
16 #include "content/public/renderer/render_view.h" | 16 #include "content/public/renderer/render_view.h" |
17 #include "extensions/common/constants.h" | 17 #include "extensions/common/constants.h" |
18 #include "extensions/common/extension.h" | 18 #include "extensions/common/extension.h" |
19 #include "extensions/common/extension_api.h" | 19 #include "extensions/common/extension_api.h" |
20 #include "extensions/common/extension_set.h" | 20 #include "extensions/common/extension_set.h" |
21 #include "extensions/common/extension_urls.h" | 21 #include "extensions/common/extension_urls.h" |
22 #include "extensions/common/features/base_feature_provider.h" | 22 #include "extensions/common/features/base_feature_provider.h" |
23 #include "extensions/common/manifest_handlers/sandboxed_page_info.h" | 23 #include "extensions/common/manifest_handlers/sandboxed_page_info.h" |
24 #include "extensions/common/permissions/permissions_data.h" | 24 #include "extensions/common/permissions/permissions_data.h" |
| 25 #include "extensions/renderer/v8_maybe_helpers.h" |
25 #include "gin/per_context_data.h" | 26 #include "gin/per_context_data.h" |
26 #include "third_party/WebKit/public/web/WebDataSource.h" | 27 #include "third_party/WebKit/public/web/WebDataSource.h" |
27 #include "third_party/WebKit/public/web/WebDocument.h" | 28 #include "third_party/WebKit/public/web/WebDocument.h" |
28 #include "third_party/WebKit/public/web/WebFrame.h" | 29 #include "third_party/WebKit/public/web/WebFrame.h" |
29 #include "third_party/WebKit/public/web/WebLocalFrame.h" | 30 #include "third_party/WebKit/public/web/WebLocalFrame.h" |
30 #include "third_party/WebKit/public/web/WebScopedMicrotaskSuppression.h" | 31 #include "third_party/WebKit/public/web/WebScopedMicrotaskSuppression.h" |
31 #include "third_party/WebKit/public/web/WebSecurityOrigin.h" | 32 #include "third_party/WebKit/public/web/WebSecurityOrigin.h" |
32 #include "third_party/WebKit/public/web/WebView.h" | 33 #include "third_party/WebKit/public/web/WebView.h" |
33 #include "v8/include/v8.h" | 34 #include "v8/include/v8.h" |
34 | 35 |
(...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
174 v8::EscapableHandleScope handle_scope(isolate()); | 175 v8::EscapableHandleScope handle_scope(isolate()); |
175 v8::Context::Scope scope(v8_context()); | 176 v8::Context::Scope scope(v8_context()); |
176 | 177 |
177 blink::WebScopedMicrotaskSuppression suppression; | 178 blink::WebScopedMicrotaskSuppression suppression; |
178 if (!is_valid_) { | 179 if (!is_valid_) { |
179 return handle_scope.Escape( | 180 return handle_scope.Escape( |
180 v8::Local<v8::Primitive>(v8::Undefined(isolate()))); | 181 v8::Local<v8::Primitive>(v8::Undefined(isolate()))); |
181 } | 182 } |
182 | 183 |
183 v8::Local<v8::Object> global = v8_context()->Global(); | 184 v8::Local<v8::Object> global = v8_context()->Global(); |
184 if (!web_frame_) | 185 if (!web_frame_) { |
185 return handle_scope.Escape(function->Call(global, argc, argv)); | 186 v8::Local<v8::Value> result; |
| 187 if (function->Call(v8_context(), global, argc, argv).ToLocal(&result)) |
| 188 return handle_scope.Escape(result); |
| 189 return handle_scope.Escape( |
| 190 v8::Local<v8::Primitive>(v8::Undefined(isolate()))); |
| 191 } |
186 return handle_scope.Escape( | 192 return handle_scope.Escape( |
187 v8::Local<v8::Value>(web_frame_->callFunctionEvenIfScriptDisabled( | 193 v8::Local<v8::Value>(web_frame_->callFunctionEvenIfScriptDisabled( |
188 function, global, argc, argv))); | 194 function, global, argc, argv))); |
189 } | 195 } |
190 | 196 |
191 v8::Local<v8::Value> ScriptContext::CallFunction( | 197 v8::Local<v8::Value> ScriptContext::CallFunction( |
192 const v8::Local<v8::Function>& function) const { | 198 const v8::Local<v8::Function>& function) const { |
193 return CallFunction(function, 0, nullptr); | 199 return CallFunction(function, 0, nullptr); |
194 } | 200 } |
195 | 201 |
(...skipping 10 matching lines...) Expand all Loading... |
206 } | 212 } |
207 return ExtensionAPI::GetSharedInstance()->IsAvailable( | 213 return ExtensionAPI::GetSharedInstance()->IsAvailable( |
208 api_name, extension, context_type_, GetURL()); | 214 api_name, extension, context_type_, GetURL()); |
209 } | 215 } |
210 | 216 |
211 void ScriptContext::DispatchEvent(const char* event_name, | 217 void ScriptContext::DispatchEvent(const char* event_name, |
212 v8::Local<v8::Array> args) const { | 218 v8::Local<v8::Array> args) const { |
213 v8::HandleScope handle_scope(isolate()); | 219 v8::HandleScope handle_scope(isolate()); |
214 v8::Context::Scope context_scope(v8_context()); | 220 v8::Context::Scope context_scope(v8_context()); |
215 | 221 |
216 v8::Local<v8::Value> argv[] = {v8::String::NewFromUtf8(isolate(), event_name), | 222 v8::Local<v8::Value> argv[] = {ToV8String(isolate(), event_name), |
217 args}; | 223 args}; |
218 module_system_->CallModuleMethod( | 224 module_system_->CallModuleMethod( |
219 kEventBindings, "dispatchEvent", arraysize(argv), argv); | 225 kEventBindings, "dispatchEvent", arraysize(argv), argv); |
220 } | 226 } |
221 | 227 |
222 void ScriptContext::DispatchOnUnloadEvent() { | 228 void ScriptContext::DispatchOnUnloadEvent() { |
223 v8::HandleScope handle_scope(isolate()); | 229 v8::HandleScope handle_scope(isolate()); |
224 v8::Context::Scope context_scope(v8_context()); | 230 v8::Context::Scope context_scope(v8_context()); |
225 module_system_->CallModuleMethod("unload_event", "dispatch"); | 231 module_system_->CallModuleMethod("unload_event", "dispatch"); |
226 } | 232 } |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
289 | 295 |
290 ScriptContext* ScriptContext::GetContext() { return this; } | 296 ScriptContext* ScriptContext::GetContext() { return this; } |
291 | 297 |
292 void ScriptContext::OnResponseReceived(const std::string& name, | 298 void ScriptContext::OnResponseReceived(const std::string& name, |
293 int request_id, | 299 int request_id, |
294 bool success, | 300 bool success, |
295 const base::ListValue& response, | 301 const base::ListValue& response, |
296 const std::string& error) { | 302 const std::string& error) { |
297 v8::HandleScope handle_scope(isolate()); | 303 v8::HandleScope handle_scope(isolate()); |
298 | 304 |
| 305 CHECK(name.size() < v8::String::kMaxLength); |
| 306 CHECK(error.size() < v8::String::kMaxLength); |
299 scoped_ptr<V8ValueConverter> converter(V8ValueConverter::create()); | 307 scoped_ptr<V8ValueConverter> converter(V8ValueConverter::create()); |
300 v8::Local<v8::Value> argv[] = { | 308 v8::Local<v8::Value> argv[] = { |
301 v8::Integer::New(isolate(), request_id), | 309 v8::Integer::New(isolate(), request_id), |
302 v8::String::NewFromUtf8(isolate(), name.c_str()), | 310 ToV8String(isolate(), name.c_str()), |
303 v8::Boolean::New(isolate(), success), | 311 v8::Boolean::New(isolate(), success), |
304 converter->ToV8Value(&response, | 312 converter->ToV8Value(&response, |
305 v8::Local<v8::Context>::New(isolate(), v8_context_)), | 313 v8::Local<v8::Context>::New(isolate(), v8_context_)), |
306 v8::String::NewFromUtf8(isolate(), error.c_str())}; | 314 ToV8String(isolate(), error.c_str())}; |
307 | 315 |
308 v8::Local<v8::Value> retval = module_system()->CallModuleMethod( | 316 v8::Local<v8::Value> retval = module_system()->CallModuleMethod( |
309 "sendRequest", "handleResponse", arraysize(argv), argv); | 317 "sendRequest", "handleResponse", arraysize(argv), argv); |
310 | 318 |
311 // In debug, the js will validate the callback parameters and return a | 319 // In debug, the js will validate the callback parameters and return a |
312 // string if a validation error has occured. | 320 // string if a validation error has occured. |
313 DCHECK(retval.IsEmpty() || retval->IsUndefined()) | 321 DCHECK(retval.IsEmpty() || retval->IsUndefined()) |
314 << *v8::String::Utf8Value(retval); | 322 << *v8::String::Utf8Value(retval); |
315 } | 323 } |
316 | 324 |
(...skipping 26 matching lines...) Expand all Loading... |
343 // crbug.com/466373 is fixed, we don't know the security origin up-front and | 351 // crbug.com/466373 is fixed, we don't know the security origin up-front and |
344 // may not know it here, either. | 352 // may not know it here, either. |
345 // | 353 // |
346 // [1] citation needed. This ScriptContext should already be in a state that | 354 // [1] citation needed. This ScriptContext should already be in a state that |
347 // doesn't allow this, from ScriptContextSet::ClassifyJavaScriptContext. | 355 // doesn't allow this, from ScriptContextSet::ClassifyJavaScriptContext. |
348 if (extension() && | 356 if (extension() && |
349 SandboxedPageInfo::IsSandboxedPage(extension(), url_.path())) { | 357 SandboxedPageInfo::IsSandboxedPage(extension(), url_.path())) { |
350 static const char kMessage[] = | 358 static const char kMessage[] = |
351 "%s cannot be used within a sandboxed frame."; | 359 "%s cannot be used within a sandboxed frame."; |
352 std::string error_msg = base::StringPrintf(kMessage, name.c_str()); | 360 std::string error_msg = base::StringPrintf(kMessage, name.c_str()); |
| 361 CHECK(error_msg.size() < v8::String::kMaxLength); |
353 isolate()->ThrowException(v8::Exception::Error( | 362 isolate()->ThrowException(v8::Exception::Error( |
354 v8::String::NewFromUtf8(isolate(), error_msg.c_str()))); | 363 ToV8String(isolate(), error_msg.c_str()))); |
355 return false; | 364 return false; |
356 } | 365 } |
357 | 366 |
358 Feature::Availability availability = GetAvailability(name); | 367 Feature::Availability availability = GetAvailability(name); |
359 if (!availability.is_available()) { | 368 if (!availability.is_available()) { |
| 369 CHECK(availability.message().size() < v8::String::kMaxLength); |
360 isolate()->ThrowException(v8::Exception::Error( | 370 isolate()->ThrowException(v8::Exception::Error( |
361 v8::String::NewFromUtf8(isolate(), availability.message().c_str()))); | 371 ToV8String(isolate(), availability.message().c_str()))); |
362 return false; | 372 return false; |
363 } | 373 } |
364 | 374 |
365 return true; | 375 return true; |
366 } | 376 } |
367 | 377 |
368 std::string ScriptContext::GetDebugString() const { | 378 std::string ScriptContext::GetDebugString() const { |
369 return base::StringPrintf( | 379 return base::StringPrintf( |
370 " extension id: %s\n" | 380 " extension id: %s\n" |
371 " frame: %p\n" | 381 " frame: %p\n" |
(...skipping 23 matching lines...) Expand all Loading... |
395 v8::Local<v8::Value> argv[]) { | 405 v8::Local<v8::Value> argv[]) { |
396 return context_->CallFunction(function, argc, argv); | 406 return context_->CallFunction(function, argc, argv); |
397 } | 407 } |
398 | 408 |
399 gin::ContextHolder* ScriptContext::Runner::GetContextHolder() { | 409 gin::ContextHolder* ScriptContext::Runner::GetContextHolder() { |
400 v8::HandleScope handle_scope(context_->isolate()); | 410 v8::HandleScope handle_scope(context_->isolate()); |
401 return gin::PerContextData::From(context_->v8_context())->context_holder(); | 411 return gin::PerContextData::From(context_->v8_context())->context_holder(); |
402 } | 412 } |
403 | 413 |
404 } // namespace extensions | 414 } // namespace extensions |
OLD | NEW |