| 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" |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 62 } // namespace | 62 } // namespace |
| 63 | 63 |
| 64 // A gin::Runner that delegates to its ScriptContext. | 64 // A gin::Runner that delegates to its ScriptContext. |
| 65 class ScriptContext::Runner : public gin::Runner { | 65 class ScriptContext::Runner : public gin::Runner { |
| 66 public: | 66 public: |
| 67 explicit Runner(ScriptContext* context); | 67 explicit Runner(ScriptContext* context); |
| 68 | 68 |
| 69 // gin::Runner overrides. | 69 // gin::Runner overrides. |
| 70 void Run(const std::string& source, | 70 void Run(const std::string& source, |
| 71 const std::string& resource_name) override; | 71 const std::string& resource_name) override; |
| 72 v8::Handle<v8::Value> Call(v8::Handle<v8::Function> function, | 72 v8::Local<v8::Value> Call(v8::Local<v8::Function> function, |
| 73 v8::Handle<v8::Value> receiver, | 73 v8::Local<v8::Value> receiver, |
| 74 int argc, | 74 int argc, |
| 75 v8::Handle<v8::Value> argv[]) override; | 75 v8::Local<v8::Value> argv[]) override; |
| 76 gin::ContextHolder* GetContextHolder() override; | 76 gin::ContextHolder* GetContextHolder() override; |
| 77 | 77 |
| 78 private: | 78 private: |
| 79 ScriptContext* context_; | 79 ScriptContext* context_; |
| 80 }; | 80 }; |
| 81 | 81 |
| 82 ScriptContext::ScriptContext(const v8::Handle<v8::Context>& v8_context, | 82 ScriptContext::ScriptContext(const v8::Local<v8::Context>& v8_context, |
| 83 blink::WebLocalFrame* web_frame, | 83 blink::WebLocalFrame* web_frame, |
| 84 const Extension* extension, | 84 const Extension* extension, |
| 85 Feature::Context context_type, | 85 Feature::Context context_type, |
| 86 const Extension* effective_extension, | 86 const Extension* effective_extension, |
| 87 Feature::Context effective_context_type) | 87 Feature::Context effective_context_type) |
| 88 : is_valid_(true), | 88 : is_valid_(true), |
| 89 v8_context_(v8_context->GetIsolate(), v8_context), | 89 v8_context_(v8_context->GetIsolate(), v8_context), |
| 90 web_frame_(web_frame), | 90 web_frame_(web_frame), |
| 91 extension_(extension), | 91 extension_(extension), |
| 92 context_type_(context_type), | 92 context_type_(context_type), |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 169 return NULL; | 169 return NULL; |
| 170 } | 170 } |
| 171 | 171 |
| 172 content::RenderFrame* ScriptContext::GetRenderFrame() const { | 172 content::RenderFrame* ScriptContext::GetRenderFrame() const { |
| 173 if (web_frame_) | 173 if (web_frame_) |
| 174 return content::RenderFrame::FromWebFrame(web_frame_); | 174 return content::RenderFrame::FromWebFrame(web_frame_); |
| 175 return NULL; | 175 return NULL; |
| 176 } | 176 } |
| 177 | 177 |
| 178 v8::Local<v8::Value> ScriptContext::CallFunction( | 178 v8::Local<v8::Value> ScriptContext::CallFunction( |
| 179 v8::Handle<v8::Function> function, | 179 v8::Local<v8::Function> function, |
| 180 int argc, | 180 int argc, |
| 181 v8::Handle<v8::Value> argv[]) const { | 181 v8::Local<v8::Value> argv[]) const { |
| 182 v8::EscapableHandleScope handle_scope(isolate()); | 182 v8::EscapableHandleScope handle_scope(isolate()); |
| 183 v8::Context::Scope scope(v8_context()); | 183 v8::Context::Scope scope(v8_context()); |
| 184 | 184 |
| 185 blink::WebScopedMicrotaskSuppression suppression; | 185 blink::WebScopedMicrotaskSuppression suppression; |
| 186 if (!is_valid_) { | 186 if (!is_valid_) { |
| 187 return handle_scope.Escape( | 187 return handle_scope.Escape( |
| 188 v8::Local<v8::Primitive>(v8::Undefined(isolate()))); | 188 v8::Local<v8::Primitive>(v8::Undefined(isolate()))); |
| 189 } | 189 } |
| 190 | 190 |
| 191 v8::Handle<v8::Object> global = v8_context()->Global(); | 191 v8::Local<v8::Object> global = v8_context()->Global(); |
| 192 if (!web_frame_) | 192 if (!web_frame_) |
| 193 return handle_scope.Escape(function->Call(global, argc, argv)); | 193 return handle_scope.Escape(function->Call(global, argc, argv)); |
| 194 return handle_scope.Escape( | 194 return handle_scope.Escape( |
| 195 v8::Local<v8::Value>(web_frame_->callFunctionEvenIfScriptDisabled( | 195 v8::Local<v8::Value>(web_frame_->callFunctionEvenIfScriptDisabled( |
| 196 function, global, argc, argv))); | 196 function, global, argc, argv))); |
| 197 } | 197 } |
| 198 | 198 |
| 199 Feature::Availability ScriptContext::GetAvailability( | 199 Feature::Availability ScriptContext::GetAvailability( |
| 200 const std::string& api_name) { | 200 const std::string& api_name) { |
| 201 // Hack: Hosted apps should have the availability of messaging APIs based on | 201 // Hack: Hosted apps should have the availability of messaging APIs based on |
| 202 // the URL of the page (which might have access depending on some extension | 202 // the URL of the page (which might have access depending on some extension |
| 203 // with externally_connectable), not whether the app has access to messaging | 203 // with externally_connectable), not whether the app has access to messaging |
| 204 // (which it won't). | 204 // (which it won't). |
| 205 const Extension* extension = extension_.get(); | 205 const Extension* extension = extension_.get(); |
| 206 if (extension && extension->is_hosted_app() && | 206 if (extension && extension->is_hosted_app() && |
| 207 (api_name == "runtime.connect" || api_name == "runtime.sendMessage")) { | 207 (api_name == "runtime.connect" || api_name == "runtime.sendMessage")) { |
| 208 extension = NULL; | 208 extension = NULL; |
| 209 } | 209 } |
| 210 return ExtensionAPI::GetSharedInstance()->IsAvailable( | 210 return ExtensionAPI::GetSharedInstance()->IsAvailable( |
| 211 api_name, extension, context_type_, GetURL()); | 211 api_name, extension, context_type_, GetURL()); |
| 212 } | 212 } |
| 213 | 213 |
| 214 void ScriptContext::DispatchEvent(const char* event_name, | 214 void ScriptContext::DispatchEvent(const char* event_name, |
| 215 v8::Handle<v8::Array> args) const { | 215 v8::Local<v8::Array> args) const { |
| 216 v8::HandleScope handle_scope(isolate()); | 216 v8::HandleScope handle_scope(isolate()); |
| 217 v8::Context::Scope context_scope(v8_context()); | 217 v8::Context::Scope context_scope(v8_context()); |
| 218 | 218 |
| 219 v8::Handle<v8::Value> argv[] = { | 219 v8::Local<v8::Value> argv[] = {v8::String::NewFromUtf8(isolate(), event_name), |
| 220 v8::String::NewFromUtf8(isolate(), event_name), args}; | 220 args}; |
| 221 module_system_->CallModuleMethod( | 221 module_system_->CallModuleMethod( |
| 222 kEventBindings, "dispatchEvent", arraysize(argv), argv); | 222 kEventBindings, "dispatchEvent", arraysize(argv), argv); |
| 223 } | 223 } |
| 224 | 224 |
| 225 void ScriptContext::DispatchOnUnloadEvent() { | 225 void ScriptContext::DispatchOnUnloadEvent() { |
| 226 v8::HandleScope handle_scope(isolate()); | 226 v8::HandleScope handle_scope(isolate()); |
| 227 v8::Context::Scope context_scope(v8_context()); | 227 v8::Context::Scope context_scope(v8_context()); |
| 228 module_system_->CallModuleMethod("unload_event", "dispatch"); | 228 module_system_->CallModuleMethod("unload_event", "dispatch"); |
| 229 } | 229 } |
| 230 | 230 |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 293 ScriptContext* ScriptContext::GetContext() { return this; } | 293 ScriptContext* ScriptContext::GetContext() { return this; } |
| 294 | 294 |
| 295 void ScriptContext::OnResponseReceived(const std::string& name, | 295 void ScriptContext::OnResponseReceived(const std::string& name, |
| 296 int request_id, | 296 int request_id, |
| 297 bool success, | 297 bool success, |
| 298 const base::ListValue& response, | 298 const base::ListValue& response, |
| 299 const std::string& error) { | 299 const std::string& error) { |
| 300 v8::HandleScope handle_scope(isolate()); | 300 v8::HandleScope handle_scope(isolate()); |
| 301 | 301 |
| 302 scoped_ptr<V8ValueConverter> converter(V8ValueConverter::create()); | 302 scoped_ptr<V8ValueConverter> converter(V8ValueConverter::create()); |
| 303 v8::Handle<v8::Value> argv[] = { | 303 v8::Local<v8::Value> argv[] = { |
| 304 v8::Integer::New(isolate(), request_id), | 304 v8::Integer::New(isolate(), request_id), |
| 305 v8::String::NewFromUtf8(isolate(), name.c_str()), | 305 v8::String::NewFromUtf8(isolate(), name.c_str()), |
| 306 v8::Boolean::New(isolate(), success), | 306 v8::Boolean::New(isolate(), success), |
| 307 converter->ToV8Value(&response, | 307 converter->ToV8Value(&response, |
| 308 v8::Local<v8::Context>::New(isolate(), v8_context_)), | 308 v8::Local<v8::Context>::New(isolate(), v8_context_)), |
| 309 v8::String::NewFromUtf8(isolate(), error.c_str())}; | 309 v8::String::NewFromUtf8(isolate(), error.c_str())}; |
| 310 | 310 |
| 311 v8::Handle<v8::Value> retval = module_system()->CallModuleMethod( | 311 v8::Local<v8::Value> retval = module_system()->CallModuleMethod( |
| 312 "sendRequest", "handleResponse", arraysize(argv), argv); | 312 "sendRequest", "handleResponse", arraysize(argv), argv); |
| 313 | 313 |
| 314 // In debug, the js will validate the callback parameters and return a | 314 // In debug, the js will validate the callback parameters and return a |
| 315 // string if a validation error has occured. | 315 // string if a validation error has occured. |
| 316 DCHECK(retval.IsEmpty() || retval->IsUndefined()) | 316 DCHECK(retval.IsEmpty() || retval->IsUndefined()) |
| 317 << *v8::String::Utf8Value(retval); | 317 << *v8::String::Utf8Value(retval); |
| 318 } | 318 } |
| 319 | 319 |
| 320 void ScriptContext::SetContentCapabilities( | 320 void ScriptContext::SetContentCapabilities( |
| 321 const APIPermissionSet& permissions) { | 321 const APIPermissionSet& permissions) { |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 369 } | 369 } |
| 370 | 370 |
| 371 ScriptContext::Runner::Runner(ScriptContext* context) : context_(context) { | 371 ScriptContext::Runner::Runner(ScriptContext* context) : context_(context) { |
| 372 } | 372 } |
| 373 | 373 |
| 374 void ScriptContext::Runner::Run(const std::string& source, | 374 void ScriptContext::Runner::Run(const std::string& source, |
| 375 const std::string& resource_name) { | 375 const std::string& resource_name) { |
| 376 context_->module_system()->RunString(source, resource_name); | 376 context_->module_system()->RunString(source, resource_name); |
| 377 } | 377 } |
| 378 | 378 |
| 379 v8::Handle<v8::Value> ScriptContext::Runner::Call( | 379 v8::Local<v8::Value> ScriptContext::Runner::Call( |
| 380 v8::Handle<v8::Function> function, | 380 v8::Local<v8::Function> function, |
| 381 v8::Handle<v8::Value> receiver, | 381 v8::Local<v8::Value> receiver, |
| 382 int argc, | 382 int argc, |
| 383 v8::Handle<v8::Value> argv[]) { | 383 v8::Local<v8::Value> argv[]) { |
| 384 return context_->CallFunction(function, argc, argv); | 384 return context_->CallFunction(function, argc, argv); |
| 385 } | 385 } |
| 386 | 386 |
| 387 gin::ContextHolder* ScriptContext::Runner::GetContextHolder() { | 387 gin::ContextHolder* ScriptContext::Runner::GetContextHolder() { |
| 388 v8::HandleScope handle_scope(context_->isolate()); | 388 v8::HandleScope handle_scope(context_->isolate()); |
| 389 return gin::PerContextData::From(context_->v8_context())->context_holder(); | 389 return gin::PerContextData::From(context_->v8_context())->context_holder(); |
| 390 } | 390 } |
| 391 | 391 |
| 392 } // namespace extensions | 392 } // namespace extensions |
| OLD | NEW |