| 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/chrome_v8_context.h" | 5 #include "chrome/renderer/extensions/chrome_v8_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/values.h" | 10 #include "base/values.h" |
| (...skipping 16 matching lines...) Expand all Loading... |
| 27 namespace extensions { | 27 namespace extensions { |
| 28 | 28 |
| 29 ChromeV8Context::ChromeV8Context(v8::Handle<v8::Context> v8_context, | 29 ChromeV8Context::ChromeV8Context(v8::Handle<v8::Context> v8_context, |
| 30 WebKit::WebFrame* web_frame, | 30 WebKit::WebFrame* web_frame, |
| 31 const Extension* extension, | 31 const Extension* extension, |
| 32 Feature::Context context_type) | 32 Feature::Context context_type) |
| 33 : v8_context_(v8_context), | 33 : v8_context_(v8_context), |
| 34 web_frame_(web_frame), | 34 web_frame_(web_frame), |
| 35 extension_(extension), | 35 extension_(extension), |
| 36 context_type_(context_type), | 36 context_type_(context_type), |
| 37 safe_builtins_(this) { | 37 safe_builtins_(this), |
| 38 isolate_(v8_context->GetIsolate()) { |
| 38 VLOG(1) << "Created context:\n" | 39 VLOG(1) << "Created context:\n" |
| 39 << " extension id: " << GetExtensionID() << "\n" | 40 << " extension id: " << GetExtensionID() << "\n" |
| 40 << " frame: " << web_frame_ << "\n" | 41 << " frame: " << web_frame_ << "\n" |
| 41 << " context type: " << GetContextTypeDescription(); | 42 << " context type: " << GetContextTypeDescription(); |
| 42 } | 43 } |
| 43 | 44 |
| 44 ChromeV8Context::~ChromeV8Context() { | 45 ChromeV8Context::~ChromeV8Context() { |
| 45 VLOG(1) << "Destroyed context for extension\n" | 46 VLOG(1) << "Destroyed context for extension\n" |
| 46 << " extension id: " << GetExtensionID(); | 47 << " extension id: " << GetExtensionID(); |
| 47 Invalidate(); | 48 Invalidate(); |
| (...skipping 21 matching lines...) Expand all Loading... |
| 69 | 70 |
| 70 GURL ChromeV8Context::GetURL() const { | 71 GURL ChromeV8Context::GetURL() const { |
| 71 return web_frame_ ? | 72 return web_frame_ ? |
| 72 UserScriptSlave::GetDataSourceURLForFrame(web_frame_) : GURL(); | 73 UserScriptSlave::GetDataSourceURLForFrame(web_frame_) : GURL(); |
| 73 } | 74 } |
| 74 | 75 |
| 75 v8::Local<v8::Value> ChromeV8Context::CallFunction( | 76 v8::Local<v8::Value> ChromeV8Context::CallFunction( |
| 76 v8::Handle<v8::Function> function, | 77 v8::Handle<v8::Function> function, |
| 77 int argc, | 78 int argc, |
| 78 v8::Handle<v8::Value> argv[]) const { | 79 v8::Handle<v8::Value> argv[]) const { |
| 79 v8::HandleScope handle_scope; | 80 v8::HandleScope handle_scope(isolate()); |
| 80 v8::Context::Scope scope(v8_context()); | 81 v8::Context::Scope scope(v8_context()); |
| 81 | 82 |
| 82 WebKit::WebScopedMicrotaskSuppression suppression; | 83 WebKit::WebScopedMicrotaskSuppression suppression; |
| 83 if (!is_valid()) | 84 if (!is_valid()) |
| 84 return handle_scope.Close(v8::Undefined()); | 85 return handle_scope.Close(v8::Undefined()); |
| 85 | 86 |
| 86 v8::Handle<v8::Object> global = v8_context()->Global(); | 87 v8::Handle<v8::Object> global = v8_context()->Global(); |
| 87 if (!web_frame_) | 88 if (!web_frame_) |
| 88 return handle_scope.Close(function->Call(global, argc, argv)); | 89 return handle_scope.Close(function->Call(global, argc, argv)); |
| 89 return handle_scope.Close( | 90 return handle_scope.Close( |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 128 | 129 |
| 129 ChromeV8Context* ChromeV8Context::GetContext() { | 130 ChromeV8Context* ChromeV8Context::GetContext() { |
| 130 return this; | 131 return this; |
| 131 } | 132 } |
| 132 | 133 |
| 133 void ChromeV8Context::OnResponseReceived(const std::string& name, | 134 void ChromeV8Context::OnResponseReceived(const std::string& name, |
| 134 int request_id, | 135 int request_id, |
| 135 bool success, | 136 bool success, |
| 136 const base::ListValue& response, | 137 const base::ListValue& response, |
| 137 const std::string& error) { | 138 const std::string& error) { |
| 138 v8::HandleScope handle_scope; | 139 v8::HandleScope handle_scope(isolate()); |
| 139 | 140 |
| 140 scoped_ptr<V8ValueConverter> converter(V8ValueConverter::create()); | 141 scoped_ptr<V8ValueConverter> converter(V8ValueConverter::create()); |
| 141 v8::Handle<v8::Value> argv[] = { | 142 v8::Handle<v8::Value> argv[] = { |
| 142 v8::Integer::New(request_id), | 143 v8::Integer::New(request_id), |
| 143 v8::String::New(name.c_str()), | 144 v8::String::New(name.c_str()), |
| 144 v8::Boolean::New(success), | 145 v8::Boolean::New(success), |
| 145 converter->ToV8Value(&response, v8_context_.get()), | 146 converter->ToV8Value(&response, v8_context_.get()), |
| 146 v8::String::New(error.c_str()) | 147 v8::String::New(error.c_str()) |
| 147 }; | 148 }; |
| 148 | 149 |
| 149 v8::Handle<v8::Value> retval = module_system_->CallModuleMethod( | 150 v8::Handle<v8::Value> retval = module_system_->CallModuleMethod( |
| 150 "sendRequest", "handleResponse", arraysize(argv), argv); | 151 "sendRequest", "handleResponse", arraysize(argv), argv); |
| 151 | 152 |
| 152 // In debug, the js will validate the callback parameters and return a | 153 // In debug, the js will validate the callback parameters and return a |
| 153 // string if a validation error has occured. | 154 // string if a validation error has occured. |
| 154 if (DCHECK_IS_ON()) { | 155 if (DCHECK_IS_ON()) { |
| 155 if (!retval.IsEmpty() && !retval->IsUndefined()) { | 156 if (!retval.IsEmpty() && !retval->IsUndefined()) { |
| 156 std::string error = *v8::String::AsciiValue(retval); | 157 std::string error = *v8::String::AsciiValue(retval); |
| 157 DCHECK(false) << error; | 158 DCHECK(false) << error; |
| 158 } | 159 } |
| 159 } | 160 } |
| 160 } | 161 } |
| 161 | 162 |
| 162 } // namespace extensions | 163 } // namespace extensions |
| OLD | NEW |