| 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/runtime_custom_bindings.h" | 5 #include "extensions/renderer/runtime_custom_bindings.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 | 8 |
| 9 #include <memory> | 9 #include <memory> |
| 10 | 10 |
| 11 #include "base/bind.h" | 11 #include "base/bind.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/renderer/render_frame.h" | 14 #include "content/public/renderer/render_frame.h" |
| 15 #include "extensions/common/extension.h" | 15 #include "extensions/common/extension.h" |
| 16 #include "extensions/common/extension_messages.h" | 16 #include "extensions/common/extension_messages.h" |
| 17 #include "extensions/common/features/feature.h" | |
| 18 #include "extensions/common/features/feature_provider.h" | |
| 19 #include "extensions/common/manifest.h" | 17 #include "extensions/common/manifest.h" |
| 20 #include "extensions/renderer/api_activity_logger.h" | |
| 21 #include "extensions/renderer/extension_frame_helper.h" | 18 #include "extensions/renderer/extension_frame_helper.h" |
| 22 #include "extensions/renderer/script_context.h" | 19 #include "extensions/renderer/script_context.h" |
| 23 #include "third_party/WebKit/public/web/WebDocument.h" | |
| 24 #include "third_party/WebKit/public/web/WebLocalFrame.h" | 20 #include "third_party/WebKit/public/web/WebLocalFrame.h" |
| 25 #include "third_party/WebKit/public/web/WebView.h" | |
| 26 | |
| 27 using content::V8ValueConverter; | |
| 28 | 21 |
| 29 namespace extensions { | 22 namespace extensions { |
| 30 | 23 |
| 31 RuntimeCustomBindings::RuntimeCustomBindings(ScriptContext* context) | 24 RuntimeCustomBindings::RuntimeCustomBindings(ScriptContext* context) |
| 32 : ObjectBackedNativeHandler(context) { | 25 : ObjectBackedNativeHandler(context) { |
| 33 RouteFunction( | 26 RouteFunction( |
| 34 "GetManifest", | 27 "GetManifest", |
| 35 base::Bind(&RuntimeCustomBindings::GetManifest, base::Unretained(this))); | 28 base::Bind(&RuntimeCustomBindings::GetManifest, base::Unretained(this))); |
| 36 RouteFunction("OpenChannelToExtension", "runtime.connect", | 29 RouteFunction("OpenChannelToExtension", "runtime.connect", |
| 37 base::Bind(&RuntimeCustomBindings::OpenChannelToExtension, | 30 base::Bind(&RuntimeCustomBindings::OpenChannelToExtension, |
| (...skipping 28 matching lines...) Expand all Loading... |
| 66 const Extension* extension = context()->extension(); | 59 const Extension* extension = context()->extension(); |
| 67 if (extension && !extension->is_hosted_app()) | 60 if (extension && !extension->is_hosted_app()) |
| 68 info.source_id = extension->id(); | 61 info.source_id = extension->id(); |
| 69 | 62 |
| 70 info.target_id = *v8::String::Utf8Value(args[0]); | 63 info.target_id = *v8::String::Utf8Value(args[0]); |
| 71 info.source_url = context()->url(); | 64 info.source_url = context()->url(); |
| 72 std::string channel_name = *v8::String::Utf8Value(args[1]); | 65 std::string channel_name = *v8::String::Utf8Value(args[1]); |
| 73 bool include_tls_channel_id = | 66 bool include_tls_channel_id = |
| 74 args.Length() > 2 ? args[2]->BooleanValue() : false; | 67 args.Length() > 2 ? args[2]->BooleanValue() : false; |
| 75 int port_id = -1; | 68 int port_id = -1; |
| 69 // TODO(devlin): This file is littered with sync IPCs. Yuck. |
| 76 renderframe->Send(new ExtensionHostMsg_OpenChannelToExtension( | 70 renderframe->Send(new ExtensionHostMsg_OpenChannelToExtension( |
| 77 renderframe->GetRoutingID(), info, channel_name, include_tls_channel_id, | 71 renderframe->GetRoutingID(), info, channel_name, include_tls_channel_id, |
| 78 &port_id)); | 72 &port_id)); |
| 79 args.GetReturnValue().Set(static_cast<int32_t>(port_id)); | 73 args.GetReturnValue().Set(static_cast<int32_t>(port_id)); |
| 80 } | 74 } |
| 81 | 75 |
| 82 void RuntimeCustomBindings::OpenChannelToNativeApp( | 76 void RuntimeCustomBindings::OpenChannelToNativeApp( |
| 83 const v8::FunctionCallbackInfo<v8::Value>& args) { | 77 const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 78 // The Javascript code should validate/fill the arguments. |
| 79 CHECK_EQ(args.Length(), 1); |
| 80 CHECK(args[0]->IsString()); |
| 81 |
| 84 // Verify that the extension has permission to use native messaging. | 82 // Verify that the extension has permission to use native messaging. |
| 85 Feature::Availability availability = | 83 if (!context()->GetAvailability("runtime.connectNative").is_available()) |
| 86 FeatureProvider::GetPermissionFeatures() | |
| 87 ->GetFeature("nativeMessaging") | |
| 88 ->IsAvailableToContext(context()->extension(), | |
| 89 context()->context_type(), context()->url()); | |
| 90 if (!availability.is_available()) | |
| 91 return; | 84 return; |
| 92 | 85 |
| 93 content::RenderFrame* render_frame = context()->GetRenderFrame(); | 86 content::RenderFrame* render_frame = context()->GetRenderFrame(); |
| 94 if (!render_frame) | 87 if (!render_frame) |
| 95 return; | 88 return; |
| 96 | 89 |
| 97 // The Javascript code should validate/fill the arguments. | 90 const std::string& extension_id = context()->GetExtensionID(); |
| 98 CHECK(args.Length() >= 2 && args[0]->IsString() && args[1]->IsString()); | 91 // Should be caught by JS. |
| 92 CHECK(!extension_id.empty()); |
| 99 | 93 |
| 100 std::string extension_id = *v8::String::Utf8Value(args[0]); | 94 std::string native_app_name = *v8::String::Utf8Value(args[0]); |
| 101 std::string native_app_name = *v8::String::Utf8Value(args[1]); | |
| 102 | 95 |
| 103 int port_id = -1; | 96 int port_id = -1; |
| 104 render_frame->Send(new ExtensionHostMsg_OpenChannelToNativeApp( | 97 render_frame->Send(new ExtensionHostMsg_OpenChannelToNativeApp( |
| 105 render_frame->GetRoutingID(), extension_id, native_app_name, &port_id)); | 98 render_frame->GetRoutingID(), extension_id, native_app_name, &port_id)); |
| 106 args.GetReturnValue().Set(static_cast<int32_t>(port_id)); | 99 args.GetReturnValue().Set(static_cast<int32_t>(port_id)); |
| 107 } | 100 } |
| 108 | 101 |
| 109 void RuntimeCustomBindings::GetManifest( | 102 void RuntimeCustomBindings::GetManifest( |
| 110 const v8::FunctionCallbackInfo<v8::Value>& args) { | 103 const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 111 CHECK(context()->extension()); | 104 CHECK(context()->extension()); |
| 112 | 105 |
| 113 std::unique_ptr<V8ValueConverter> converter(V8ValueConverter::create()); | 106 std::unique_ptr<content::V8ValueConverter> converter( |
| 107 content::V8ValueConverter::create()); |
| 114 args.GetReturnValue().Set(converter->ToV8Value( | 108 args.GetReturnValue().Set(converter->ToV8Value( |
| 115 context()->extension()->manifest()->value(), context()->v8_context())); | 109 context()->extension()->manifest()->value(), context()->v8_context())); |
| 116 } | 110 } |
| 117 | 111 |
| 118 void RuntimeCustomBindings::GetExtensionViews( | 112 void RuntimeCustomBindings::GetExtensionViews( |
| 119 const v8::FunctionCallbackInfo<v8::Value>& args) { | 113 const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 120 if (args.Length() != 2) | 114 CHECK_EQ(args.Length(), 2); |
| 121 return; | 115 CHECK(args[0]->IsInt32()); |
| 122 | 116 CHECK(args[1]->IsString()); |
| 123 if (!args[0]->IsInt32() || !args[1]->IsString()) | |
| 124 return; | |
| 125 | 117 |
| 126 // |browser_window_id| == extension_misc::kUnknownWindowId means getting | 118 // |browser_window_id| == extension_misc::kUnknownWindowId means getting |
| 127 // all views for the current extension. | 119 // all views for the current extension. |
| 128 int browser_window_id = args[0]->Int32Value(); | 120 int browser_window_id = args[0]->Int32Value(); |
| 129 | 121 |
| 130 std::string view_type_string = | 122 std::string view_type_string = |
| 131 base::ToUpperASCII(*v8::String::Utf8Value(args[1])); | 123 base::ToUpperASCII(*v8::String::Utf8Value(args[1])); |
| 132 // |view_type| == VIEW_TYPE_INVALID means getting any type of | 124 // |view_type| == VIEW_TYPE_INVALID means getting any type of |
| 133 // views. | 125 // views. |
| 134 ViewType view_type = VIEW_TYPE_INVALID; | 126 ViewType view_type = VIEW_TYPE_INVALID; |
| 135 if (view_type_string == kViewTypeBackgroundPage) { | 127 if (view_type_string == kViewTypeBackgroundPage) { |
| 136 view_type = VIEW_TYPE_EXTENSION_BACKGROUND_PAGE; | 128 view_type = VIEW_TYPE_EXTENSION_BACKGROUND_PAGE; |
| 137 } else if (view_type_string == kViewTypeTabContents) { | 129 } else if (view_type_string == kViewTypeTabContents) { |
| 138 view_type = VIEW_TYPE_TAB_CONTENTS; | 130 view_type = VIEW_TYPE_TAB_CONTENTS; |
| 139 } else if (view_type_string == kViewTypePopup) { | 131 } else if (view_type_string == kViewTypePopup) { |
| 140 view_type = VIEW_TYPE_EXTENSION_POPUP; | 132 view_type = VIEW_TYPE_EXTENSION_POPUP; |
| 141 } else if (view_type_string == kViewTypeExtensionDialog) { | 133 } else if (view_type_string == kViewTypeExtensionDialog) { |
| 142 view_type = VIEW_TYPE_EXTENSION_DIALOG; | 134 view_type = VIEW_TYPE_EXTENSION_DIALOG; |
| 143 } else if (view_type_string == kViewTypeAppWindow) { | 135 } else if (view_type_string == kViewTypeAppWindow) { |
| 144 view_type = VIEW_TYPE_APP_WINDOW; | 136 view_type = VIEW_TYPE_APP_WINDOW; |
| 145 } else if (view_type_string == kViewTypeLauncherPage) { | 137 } else if (view_type_string == kViewTypeLauncherPage) { |
| 146 view_type = VIEW_TYPE_LAUNCHER_PAGE; | 138 view_type = VIEW_TYPE_LAUNCHER_PAGE; |
| 147 } else if (view_type_string == kViewTypePanel) { | 139 } else if (view_type_string == kViewTypePanel) { |
| 148 view_type = VIEW_TYPE_PANEL; | 140 view_type = VIEW_TYPE_PANEL; |
| 149 } else if (view_type_string != kViewTypeAll) { | 141 } else { |
| 150 return; | 142 CHECK_EQ(view_type_string, kViewTypeAll); |
| 151 } | 143 } |
| 152 | 144 |
| 153 std::string extension_id = context()->GetExtensionID(); | 145 const std::string& extension_id = context()->GetExtensionID(); |
| 154 if (extension_id.empty()) | 146 if (extension_id.empty()) |
| 155 return; | 147 return; |
| 156 | 148 |
| 157 std::vector<content::RenderFrame*> frames = | 149 std::vector<content::RenderFrame*> frames = |
| 158 ExtensionFrameHelper::GetExtensionFrames(extension_id, browser_window_id, | 150 ExtensionFrameHelper::GetExtensionFrames(extension_id, browser_window_id, |
| 159 view_type); | 151 view_type); |
| 160 v8::Local<v8::Context> v8_context = args.GetIsolate()->GetCurrentContext(); | 152 v8::Local<v8::Context> v8_context = args.GetIsolate()->GetCurrentContext(); |
| 161 v8::Local<v8::Array> v8_views = v8::Array::New(args.GetIsolate()); | 153 v8::Local<v8::Array> v8_views = v8::Array::New(args.GetIsolate()); |
| 162 int v8_index = 0; | 154 int v8_index = 0; |
| 163 for (content::RenderFrame* frame : frames) { | 155 for (content::RenderFrame* frame : frames) { |
| 164 // We filter out iframes here. GetExtensionViews should only return the | 156 // We filter out iframes here. GetExtensionViews should only return the |
| 165 // main views, not any subframes. (Returning subframes can cause broken | 157 // main views, not any subframes. (Returning subframes can cause broken |
| 166 // behavior by treating an app window's iframe as its main frame, and maybe | 158 // behavior by treating an app window's iframe as its main frame, and maybe |
| 167 // other nastiness). | 159 // other nastiness). |
| 168 if (frame->GetWebFrame()->top() != frame->GetWebFrame()) | 160 blink::WebFrame* web_frame = frame->GetWebFrame(); |
| 161 if (web_frame->top() != web_frame) |
| 169 continue; | 162 continue; |
| 170 | 163 |
| 171 v8::Local<v8::Context> context = | 164 if (!blink::WebFrame::scriptCanAccess(web_frame)) |
| 172 frame->GetWebFrame()->mainWorldScriptContext(); | 165 continue; |
| 166 |
| 167 v8::Local<v8::Context> context = web_frame->mainWorldScriptContext(); |
| 173 if (!context.IsEmpty()) { | 168 if (!context.IsEmpty()) { |
| 174 v8::Local<v8::Value> window = context->Global(); | 169 v8::Local<v8::Value> window = context->Global(); |
| 175 DCHECK(!window.IsEmpty()); | 170 CHECK(!window.IsEmpty()); |
| 176 v8::Maybe<bool> maybe = | 171 v8::Maybe<bool> maybe = |
| 177 v8_views->CreateDataProperty(v8_context, v8_index++, window); | 172 v8_views->CreateDataProperty(v8_context, v8_index++, window); |
| 178 DCHECK(maybe.IsJust() && maybe.FromJust()); | 173 CHECK(maybe.IsJust() && maybe.FromJust()); |
| 179 } | 174 } |
| 180 } | 175 } |
| 181 | 176 |
| 182 args.GetReturnValue().Set(v8_views); | 177 args.GetReturnValue().Set(v8_views); |
| 183 } | 178 } |
| 184 | 179 |
| 185 } // namespace extensions | 180 } // namespace extensions |
| OLD | NEW |