| 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 "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/values.h" | 10 #include "base/values.h" |
| 11 #include "chrome/renderer/extensions/chrome_v8_extension.h" | |
| 12 #include "chrome/renderer/extensions/module_system.h" | |
| 13 #include "chrome/renderer/extensions/user_script_slave.h" | |
| 14 #include "content/public/renderer/render_view.h" | 11 #include "content/public/renderer/render_view.h" |
| 15 #include "content/public/renderer/v8_value_converter.h" | 12 #include "content/public/renderer/v8_value_converter.h" |
| 16 #include "extensions/common/extension.h" | 13 #include "extensions/common/extension.h" |
| 17 #include "extensions/common/extension_api.h" | 14 #include "extensions/common/extension_api.h" |
| 18 #include "extensions/common/extension_urls.h" | 15 #include "extensions/common/extension_urls.h" |
| 19 #include "extensions/common/features/base_feature_provider.h" | 16 #include "extensions/common/features/base_feature_provider.h" |
| 20 #include "third_party/WebKit/public/web/WebFrame.h" | 17 #include "third_party/WebKit/public/web/WebFrame.h" |
| 21 #include "third_party/WebKit/public/web/WebScopedMicrotaskSuppression.h" | 18 #include "third_party/WebKit/public/web/WebScopedMicrotaskSuppression.h" |
| 22 #include "third_party/WebKit/public/web/WebView.h" | 19 #include "third_party/WebKit/public/web/WebView.h" |
| 23 #include "v8/include/v8.h" | 20 #include "v8/include/v8.h" |
| 24 | 21 |
| 25 using content::V8ValueConverter; | 22 using content::V8ValueConverter; |
| 26 | 23 |
| 27 namespace extensions { | 24 namespace extensions { |
| 28 | 25 |
| 29 ChromeV8Context::ChromeV8Context(v8::Handle<v8::Context> v8_context, | 26 ScriptContext::ScriptContext(v8::Handle<v8::Context> v8_context, |
| 30 blink::WebFrame* web_frame, | 27 blink::WebFrame* web_frame, |
| 31 const Extension* extension, | 28 const Extension* extension, |
| 32 Feature::Context context_type) | 29 Feature::Context context_type) |
| 33 : v8_context_(v8_context), | 30 : v8_context_(v8_context), |
| 34 web_frame_(web_frame), | 31 web_frame_(web_frame), |
| 35 extension_(extension), | 32 extension_(extension), |
| 36 context_type_(context_type), | 33 context_type_(context_type), |
| 37 safe_builtins_(this), | 34 safe_builtins_(this), |
| 38 pepper_request_proxy_(this), | |
| 39 isolate_(v8_context->GetIsolate()) { | 35 isolate_(v8_context->GetIsolate()) { |
| 40 VLOG(1) << "Created context:\n" | 36 VLOG(1) << "Created context:\n" |
| 41 << " extension id: " << GetExtensionID() << "\n" | 37 << " extension id: " << GetExtensionID() << "\n" |
| 42 << " frame: " << web_frame_ << "\n" | 38 << " frame: " << web_frame_ << "\n" |
| 43 << " context type: " << GetContextTypeDescription(); | 39 << " context type: " << GetContextTypeDescription(); |
| 44 } | 40 } |
| 45 | 41 |
| 46 ChromeV8Context::~ChromeV8Context() { | 42 ScriptContext::~ScriptContext() { |
| 47 VLOG(1) << "Destroyed context for extension\n" | 43 VLOG(1) << "Destroyed context for extension\n" |
| 48 << " extension id: " << GetExtensionID(); | 44 << " extension id: " << GetExtensionID(); |
| 49 Invalidate(); | 45 Invalidate(); |
| 50 } | 46 } |
| 51 | 47 |
| 52 void ChromeV8Context::Invalidate() { | 48 void ScriptContext::Invalidate() { |
| 53 if (!is_valid()) | 49 if (!is_valid()) |
| 54 return; | 50 return; |
| 55 if (module_system_) | 51 if (module_system_) |
| 56 module_system_->Invalidate(); | 52 module_system_->Invalidate(); |
| 57 web_frame_ = NULL; | 53 web_frame_ = NULL; |
| 58 v8_context_.reset(); | 54 v8_context_.reset(); |
| 59 } | 55 } |
| 60 | 56 |
| 61 std::string ChromeV8Context::GetExtensionID() const { | 57 std::string ScriptContext::GetExtensionID() const { |
| 62 return extension_.get() ? extension_->id() : std::string(); | 58 return extension_.get() ? extension_->id() : std::string(); |
| 63 } | 59 } |
| 64 | 60 |
| 65 content::RenderView* ChromeV8Context::GetRenderView() const { | 61 content::RenderView* ScriptContext::GetRenderView() const { |
| 66 if (web_frame_ && web_frame_->view()) | 62 if (web_frame_ && web_frame_->view()) |
| 67 return content::RenderView::FromWebView(web_frame_->view()); | 63 return content::RenderView::FromWebView(web_frame_->view()); |
| 68 else | 64 else |
| 69 return NULL; | 65 return NULL; |
| 70 } | 66 } |
| 71 | 67 |
| 72 GURL ChromeV8Context::GetURL() const { | 68 v8::Local<v8::Value> ScriptContext::CallFunction( |
| 73 return web_frame_ ? | |
| 74 UserScriptSlave::GetDataSourceURLForFrame(web_frame_) : GURL(); | |
| 75 } | |
| 76 | |
| 77 v8::Local<v8::Value> ChromeV8Context::CallFunction( | |
| 78 v8::Handle<v8::Function> function, | 69 v8::Handle<v8::Function> function, |
| 79 int argc, | 70 int argc, |
| 80 v8::Handle<v8::Value> argv[]) const { | 71 v8::Handle<v8::Value> argv[]) const { |
| 81 v8::EscapableHandleScope handle_scope(isolate()); | 72 v8::EscapableHandleScope handle_scope(isolate()); |
| 82 v8::Context::Scope scope(v8_context()); | 73 v8::Context::Scope scope(v8_context()); |
| 83 | 74 |
| 84 blink::WebScopedMicrotaskSuppression suppression; | 75 blink::WebScopedMicrotaskSuppression suppression; |
| 85 if (!is_valid()) { | 76 if (!is_valid()) { |
| 86 return handle_scope.Escape( | 77 return handle_scope.Escape( |
| 87 v8::Local<v8::Primitive>(v8::Undefined(isolate()))); | 78 v8::Local<v8::Primitive>(v8::Undefined(isolate()))); |
| 88 } | 79 } |
| 89 | 80 |
| 90 v8::Handle<v8::Object> global = v8_context()->Global(); | 81 v8::Handle<v8::Object> global = v8_context()->Global(); |
| 91 if (!web_frame_) | 82 if (!web_frame_) |
| 92 return handle_scope.Escape(function->Call(global, argc, argv)); | 83 return handle_scope.Escape(function->Call(global, argc, argv)); |
| 93 return handle_scope.Escape( | 84 return handle_scope.Escape( |
| 94 v8::Local<v8::Value>(web_frame_->callFunctionEvenIfScriptDisabled( | 85 v8::Local<v8::Value>(web_frame_->callFunctionEvenIfScriptDisabled( |
| 95 function, global, argc, argv))); | 86 function, global, argc, argv))); |
| 96 } | 87 } |
| 97 | 88 |
| 98 bool ChromeV8Context::IsAnyFeatureAvailableToContext(const Feature& api) { | 89 Feature::Availability ScriptContext::GetAvailability( |
| 99 return ExtensionAPI::GetSharedInstance()->IsAnyFeatureAvailableToContext( | |
| 100 api, | |
| 101 extension_.get(), | |
| 102 context_type_, | |
| 103 UserScriptSlave::GetDataSourceURLForFrame(web_frame_)); | |
| 104 } | |
| 105 | |
| 106 Feature::Availability ChromeV8Context::GetAvailability( | |
| 107 const std::string& api_name) { | 90 const std::string& api_name) { |
| 108 // Hack: Hosted apps should have the availability of messaging APIs based on | 91 // Hack: Hosted apps should have the availability of messaging APIs based on |
| 109 // the URL of the page (which might have access depending on some extension | 92 // the URL of the page (which might have access depending on some extension |
| 110 // with externally_connectable), not whether the app has access to messaging | 93 // with externally_connectable), not whether the app has access to messaging |
| 111 // (which it won't). | 94 // (which it won't). |
| 112 const Extension* extension = extension_.get(); | 95 const Extension* extension = extension_.get(); |
| 113 if (extension && extension->is_hosted_app() && | 96 if (extension && extension->is_hosted_app() && |
| 114 (api_name == "runtime.connect" || api_name == "runtime.sendMessage")) { | 97 (api_name == "runtime.connect" || api_name == "runtime.sendMessage")) { |
| 115 extension = NULL; | 98 extension = NULL; |
| 116 } | 99 } |
| 117 return ExtensionAPI::GetSharedInstance()->IsAvailable(api_name, | 100 return ExtensionAPI::GetSharedInstance()->IsAvailable( |
| 118 extension, | 101 api_name, extension, context_type_, GetURL()); |
| 119 context_type_, | |
| 120 GetURL()); | |
| 121 } | 102 } |
| 122 | 103 |
| 123 void ChromeV8Context::DispatchEvent(const char* event_name, | 104 void ScriptContext::DispatchEvent(const char* event_name, |
| 124 v8::Handle<v8::Array> args) const { | 105 v8::Handle<v8::Array> args) const { |
| 125 v8::HandleScope handle_scope(isolate()); | 106 v8::HandleScope handle_scope(isolate()); |
| 126 v8::Context::Scope context_scope(v8_context()); | 107 v8::Context::Scope context_scope(v8_context()); |
| 127 | 108 |
| 128 v8::Handle<v8::Value> argv[] = { | 109 v8::Handle<v8::Value> argv[] = { |
| 129 v8::String::NewFromUtf8(isolate(), event_name), args}; | 110 v8::String::NewFromUtf8(isolate(), event_name), args}; |
| 130 module_system_->CallModuleMethod( | 111 module_system_->CallModuleMethod( |
| 131 kEventBindings, "dispatchEvent", arraysize(argv), argv); | 112 kEventBindings, "dispatchEvent", arraysize(argv), argv); |
| 132 } | 113 } |
| 133 | 114 |
| 134 void ChromeV8Context::DispatchOnUnloadEvent() { | 115 void ScriptContext::DispatchOnUnloadEvent() { |
| 135 module_system_->CallModuleMethod("unload_event", "dispatch"); | 116 module_system_->CallModuleMethod("unload_event", "dispatch"); |
| 136 } | 117 } |
| 137 | 118 |
| 138 std::string ChromeV8Context::GetContextTypeDescription() { | 119 std::string ScriptContext::GetContextTypeDescription() { |
| 139 switch (context_type_) { | 120 switch (context_type_) { |
| 140 case Feature::UNSPECIFIED_CONTEXT: return "UNSPECIFIED"; | 121 case Feature::UNSPECIFIED_CONTEXT: |
| 141 case Feature::BLESSED_EXTENSION_CONTEXT: return "BLESSED_EXTENSION"; | 122 return "UNSPECIFIED"; |
| 142 case Feature::UNBLESSED_EXTENSION_CONTEXT: return "UNBLESSED_EXTENSION"; | 123 case Feature::BLESSED_EXTENSION_CONTEXT: |
| 143 case Feature::CONTENT_SCRIPT_CONTEXT: return "CONTENT_SCRIPT"; | 124 return "BLESSED_EXTENSION"; |
| 144 case Feature::WEB_PAGE_CONTEXT: return "WEB_PAGE"; | 125 case Feature::UNBLESSED_EXTENSION_CONTEXT: |
| 145 case Feature::BLESSED_WEB_PAGE_CONTEXT: return "BLESSED_WEB_PAGE"; | 126 return "UNBLESSED_EXTENSION"; |
| 127 case Feature::CONTENT_SCRIPT_CONTEXT: |
| 128 return "CONTENT_SCRIPT"; |
| 129 case Feature::WEB_PAGE_CONTEXT: |
| 130 return "WEB_PAGE"; |
| 131 case Feature::BLESSED_WEB_PAGE_CONTEXT: |
| 132 return "BLESSED_WEB_PAGE"; |
| 146 } | 133 } |
| 147 NOTREACHED(); | 134 NOTREACHED(); |
| 148 return std::string(); | 135 return std::string(); |
| 149 } | 136 } |
| 150 | 137 |
| 151 ChromeV8Context* ChromeV8Context::GetContext() { | 138 ScriptContext* ScriptContext::GetContext() { return this; } |
| 152 return this; | |
| 153 } | |
| 154 | |
| 155 void ChromeV8Context::OnResponseReceived(const std::string& name, | |
| 156 int request_id, | |
| 157 bool success, | |
| 158 const base::ListValue& response, | |
| 159 const std::string& error) { | |
| 160 v8::HandleScope handle_scope(isolate()); | |
| 161 | |
| 162 scoped_ptr<V8ValueConverter> converter(V8ValueConverter::create()); | |
| 163 v8::Handle<v8::Value> argv[] = { | |
| 164 v8::Integer::New(isolate(), request_id), | |
| 165 v8::String::NewFromUtf8(isolate(), name.c_str()), | |
| 166 v8::Boolean::New(isolate(), success), | |
| 167 converter->ToV8Value(&response, v8_context_.NewHandle(isolate())), | |
| 168 v8::String::NewFromUtf8(isolate(), error.c_str()) | |
| 169 }; | |
| 170 | |
| 171 v8::Handle<v8::Value> retval = module_system_->CallModuleMethod( | |
| 172 "sendRequest", "handleResponse", arraysize(argv), argv); | |
| 173 | |
| 174 // In debug, the js will validate the callback parameters and return a | |
| 175 // string if a validation error has occured. | |
| 176 DCHECK(retval.IsEmpty() || retval->IsUndefined()) | |
| 177 << *v8::String::Utf8Value(retval); | |
| 178 } | |
| 179 | 139 |
| 180 } // namespace extensions | 140 } // namespace extensions |
| OLD | NEW |