[email protected] | e689367 | 2014-05-01 17:29:13 | [diff] [blame] | 1 | // Copyright 2014 The Chromium Authors. All rights reserved. |
[email protected] | 120028b9c | 2012-07-03 01:32:24 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | e689367 | 2014-05-01 17:29:13 | [diff] [blame] | 5 | #include "extensions/renderer/runtime_custom_bindings.h" |
[email protected] | 120028b9c | 2012-07-03 01:32:24 | [diff] [blame] | 6 | |
avi | 2d124c0 | 2015-12-23 06:36:42 | [diff] [blame] | 7 | #include <stdint.h> |
| 8 | |
[email protected] | 120028b9c | 2012-07-03 01:32:24 | [diff] [blame] | 9 | #include "base/bind.h" |
| 10 | #include "base/memory/scoped_ptr.h" |
| 11 | #include "base/values.h" |
mek | 87e0ab5 | 2015-02-13 01:18:26 | [diff] [blame] | 12 | #include "content/public/child/v8_value_converter.h" |
rob | 248d6a8 | 2014-11-21 02:04:48 | [diff] [blame] | 13 | #include "content/public/renderer/render_frame.h" |
[email protected] | e4452d3 | 2013-11-15 23:07:41 | [diff] [blame] | 14 | #include "extensions/common/extension.h" |
[email protected] | fb820c0 | 2014-03-13 15:07:08 | [diff] [blame] | 15 | #include "extensions/common/extension_messages.h" |
[email protected] | d42c1115 | 2013-08-22 19:36:32 | [diff] [blame] | 16 | #include "extensions/common/features/feature.h" |
[email protected] | ad8b4ba | 2013-08-09 19:52:44 | [diff] [blame] | 17 | #include "extensions/common/features/feature_provider.h" |
[email protected] | d42c1115 | 2013-08-22 19:36:32 | [diff] [blame] | 18 | #include "extensions/common/manifest.h" |
[email protected] | e689367 | 2014-05-01 17:29:13 | [diff] [blame] | 19 | #include "extensions/renderer/api_activity_logger.h" |
rdevlin.cronin | 6f42c252 | 2015-06-19 18:58:51 | [diff] [blame] | 20 | #include "extensions/renderer/extension_frame_helper.h" |
[email protected] | bcd9580f | 2014-04-17 19:17:59 | [diff] [blame] | 21 | #include "extensions/renderer/script_context.h" |
[email protected] | 2255a933 | 2013-06-17 05:12:31 | [diff] [blame] | 22 | #include "third_party/WebKit/public/web/WebDocument.h" |
rdevlin.cronin | 6f42c252 | 2015-06-19 18:58:51 | [diff] [blame] | 23 | #include "third_party/WebKit/public/web/WebLocalFrame.h" |
[email protected] | 2255a933 | 2013-06-17 05:12:31 | [diff] [blame] | 24 | #include "third_party/WebKit/public/web/WebView.h" |
[email protected] | 120028b9c | 2012-07-03 01:32:24 | [diff] [blame] | 25 | |
| 26 | using content::V8ValueConverter; |
| 27 | |
| 28 | namespace extensions { |
| 29 | |
[email protected] | bcd9580f | 2014-04-17 19:17:59 | [diff] [blame] | 30 | RuntimeCustomBindings::RuntimeCustomBindings(ScriptContext* context) |
| 31 | : ObjectBackedNativeHandler(context) { |
[email protected] | e689367 | 2014-05-01 17:29:13 | [diff] [blame] | 32 | RouteFunction( |
| 33 | "GetManifest", |
| 34 | base::Bind(&RuntimeCustomBindings::GetManifest, base::Unretained(this))); |
rdevlin.cronin | c0569cc | 2016-04-15 21:51:21 | [diff] [blame^] | 35 | RouteFunction("OpenChannelToExtension", "runtime.connect", |
[email protected] | 4f1633f | 2013-03-09 14:26:24 | [diff] [blame] | 36 | base::Bind(&RuntimeCustomBindings::OpenChannelToExtension, |
| 37 | base::Unretained(this))); |
rdevlin.cronin | c0569cc | 2016-04-15 21:51:21 | [diff] [blame^] | 38 | RouteFunction("OpenChannelToNativeApp", "runtime.connectNative", |
[email protected] | 78b7518 | 2013-03-06 03:41:32 | [diff] [blame] | 39 | base::Bind(&RuntimeCustomBindings::OpenChannelToNativeApp, |
| 40 | base::Unretained(this))); |
[email protected] | c1e5fb1 | 2013-11-13 03:40:31 | [diff] [blame] | 41 | RouteFunction("GetExtensionViews", |
| 42 | base::Bind(&RuntimeCustomBindings::GetExtensionViews, |
| 43 | base::Unretained(this))); |
[email protected] | 120028b9c | 2012-07-03 01:32:24 | [diff] [blame] | 44 | } |
| 45 | |
[email protected] | e689367 | 2014-05-01 17:29:13 | [diff] [blame] | 46 | RuntimeCustomBindings::~RuntimeCustomBindings() { |
| 47 | } |
[email protected] | 120028b9c | 2012-07-03 01:32:24 | [diff] [blame] | 48 | |
[email protected] | d8c5fbb | 2013-06-14 11:35:25 | [diff] [blame] | 49 | void RuntimeCustomBindings::OpenChannelToExtension( |
| 50 | const v8::FunctionCallbackInfo<v8::Value>& args) { |
rob | 248d6a8 | 2014-11-21 02:04:48 | [diff] [blame] | 51 | // Get the current RenderFrame so that we can send a routed IPC message from |
[email protected] | 481a87e6 | 2012-12-19 23:00:27 | [diff] [blame] | 52 | // the correct source. |
rob | 248d6a8 | 2014-11-21 02:04:48 | [diff] [blame] | 53 | content::RenderFrame* renderframe = context()->GetRenderFrame(); |
| 54 | if (!renderframe) |
[email protected] | d8c5fbb | 2013-06-14 11:35:25 | [diff] [blame] | 55 | return; |
[email protected] | 481a87e6 | 2012-12-19 23:00:27 | [diff] [blame] | 56 | |
| 57 | // The Javascript code should validate/fill the arguments. |
[email protected] | 8ad95b7 | 2013-10-16 02:54:11 | [diff] [blame] | 58 | CHECK_EQ(args.Length(), 3); |
| 59 | CHECK(args[0]->IsString() && args[1]->IsString() && args[2]->IsBoolean()); |
[email protected] | 481a87e6 | 2012-12-19 23:00:27 | [diff] [blame] | 60 | |
[email protected] | 686914f | 2013-04-25 04:54:58 | [diff] [blame] | 61 | ExtensionMsg_ExternalConnectionInfo info; |
[email protected] | 9b6a633 | 2014-01-09 19:58:43 | [diff] [blame] | 62 | |
| 63 | // For messaging APIs, hosted apps should be considered a web page so hide |
| 64 | // its extension ID. |
| 65 | const Extension* extension = context()->extension(); |
| 66 | if (extension && !extension->is_hosted_app()) |
| 67 | info.source_id = extension->id(); |
| 68 | |
dcarney | a261b77 | 2014-11-20 17:55:07 | [diff] [blame] | 69 | info.target_id = *v8::String::Utf8Value(args[0]); |
kalman | 0475530 | 2015-09-14 18:52:11 | [diff] [blame] | 70 | info.source_url = context()->url(); |
dcarney | a261b77 | 2014-11-20 17:55:07 | [diff] [blame] | 71 | std::string channel_name = *v8::String::Utf8Value(args[1]); |
[email protected] | 8ad95b7 | 2013-10-16 02:54:11 | [diff] [blame] | 72 | bool include_tls_channel_id = |
| 73 | args.Length() > 2 ? args[2]->BooleanValue() : false; |
[email protected] | 481a87e6 | 2012-12-19 23:00:27 | [diff] [blame] | 74 | int port_id = -1; |
rob | 248d6a8 | 2014-11-21 02:04:48 | [diff] [blame] | 75 | renderframe->Send(new ExtensionHostMsg_OpenChannelToExtension( |
| 76 | renderframe->GetRoutingID(), info, channel_name, include_tls_channel_id, |
| 77 | &port_id)); |
[email protected] | d8c5fbb | 2013-06-14 11:35:25 | [diff] [blame] | 78 | args.GetReturnValue().Set(static_cast<int32_t>(port_id)); |
[email protected] | 481a87e6 | 2012-12-19 23:00:27 | [diff] [blame] | 79 | } |
| 80 | |
[email protected] | d8c5fbb | 2013-06-14 11:35:25 | [diff] [blame] | 81 | void RuntimeCustomBindings::OpenChannelToNativeApp( |
| 82 | const v8::FunctionCallbackInfo<v8::Value>& args) { |
[email protected] | 78b7518 | 2013-03-06 03:41:32 | [diff] [blame] | 83 | // Verify that the extension has permission to use native messaging. |
[email protected] | b6708d03 | 2013-07-14 08:18:22 | [diff] [blame] | 84 | Feature::Availability availability = |
[email protected] | bcd9580f | 2014-04-17 19:17:59 | [diff] [blame] | 85 | FeatureProvider::GetPermissionFeatures() |
| 86 | ->GetFeature("nativeMessaging") |
| 87 | ->IsAvailableToContext(context()->extension(), |
kalman | 0475530 | 2015-09-14 18:52:11 | [diff] [blame] | 88 | context()->context_type(), context()->url()); |
[email protected] | ecc854a | 2013-08-22 10:12:42 | [diff] [blame] | 89 | if (!availability.is_available()) |
[email protected] | d8c5fbb | 2013-06-14 11:35:25 | [diff] [blame] | 90 | return; |
[email protected] | 78b7518 | 2013-03-06 03:41:32 | [diff] [blame] | 91 | |
rdevlin.cronin | 25fcbdd6 | 2015-07-07 14:11:59 | [diff] [blame] | 92 | content::RenderFrame* render_frame = context()->GetRenderFrame(); |
| 93 | if (!render_frame) |
[email protected] | d8c5fbb | 2013-06-14 11:35:25 | [diff] [blame] | 94 | return; |
[email protected] | 481a87e6 | 2012-12-19 23:00:27 | [diff] [blame] | 95 | |
| 96 | // The Javascript code should validate/fill the arguments. |
[email protected] | e689367 | 2014-05-01 17:29:13 | [diff] [blame] | 97 | CHECK(args.Length() >= 2 && args[0]->IsString() && args[1]->IsString()); |
[email protected] | 481a87e6 | 2012-12-19 23:00:27 | [diff] [blame] | 98 | |
dcarney | a261b77 | 2014-11-20 17:55:07 | [diff] [blame] | 99 | std::string extension_id = *v8::String::Utf8Value(args[0]); |
| 100 | std::string native_app_name = *v8::String::Utf8Value(args[1]); |
[email protected] | 481a87e6 | 2012-12-19 23:00:27 | [diff] [blame] | 101 | |
| 102 | int port_id = -1; |
rdevlin.cronin | 25fcbdd6 | 2015-07-07 14:11:59 | [diff] [blame] | 103 | render_frame->Send(new ExtensionHostMsg_OpenChannelToNativeApp( |
| 104 | render_frame->GetRoutingID(), extension_id, native_app_name, &port_id)); |
[email protected] | d8c5fbb | 2013-06-14 11:35:25 | [diff] [blame] | 105 | args.GetReturnValue().Set(static_cast<int32_t>(port_id)); |
[email protected] | 481a87e6 | 2012-12-19 23:00:27 | [diff] [blame] | 106 | } |
| 107 | |
[email protected] | d8c5fbb | 2013-06-14 11:35:25 | [diff] [blame] | 108 | void RuntimeCustomBindings::GetManifest( |
| 109 | const v8::FunctionCallbackInfo<v8::Value>& args) { |
[email protected] | 9a59844 | 2013-06-04 16:39:12 | [diff] [blame] | 110 | CHECK(context()->extension()); |
[email protected] | 120028b9c | 2012-07-03 01:32:24 | [diff] [blame] | 111 | |
| 112 | scoped_ptr<V8ValueConverter> converter(V8ValueConverter::create()); |
[email protected] | e689367 | 2014-05-01 17:29:13 | [diff] [blame] | 113 | args.GetReturnValue().Set(converter->ToV8Value( |
| 114 | context()->extension()->manifest()->value(), context()->v8_context())); |
[email protected] | 120028b9c | 2012-07-03 01:32:24 | [diff] [blame] | 115 | } |
| 116 | |
[email protected] | c1e5fb1 | 2013-11-13 03:40:31 | [diff] [blame] | 117 | void RuntimeCustomBindings::GetExtensionViews( |
| 118 | const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 119 | if (args.Length() != 2) |
| 120 | return; |
| 121 | |
| 122 | if (!args[0]->IsInt32() || !args[1]->IsString()) |
| 123 | return; |
| 124 | |
| 125 | // |browser_window_id| == extension_misc::kUnknownWindowId means getting |
| 126 | // all views for the current extension. |
| 127 | int browser_window_id = args[0]->Int32Value(); |
| 128 | |
brettw | c15100c | 2015-08-06 22:54:16 | [diff] [blame] | 129 | std::string view_type_string = |
| 130 | base::ToUpperASCII(*v8::String::Utf8Value(args[1])); |
[email protected] | c1e5fb1 | 2013-11-13 03:40:31 | [diff] [blame] | 131 | // |view_type| == VIEW_TYPE_INVALID means getting any type of |
| 132 | // views. |
| 133 | ViewType view_type = VIEW_TYPE_INVALID; |
| 134 | if (view_type_string == kViewTypeBackgroundPage) { |
| 135 | view_type = VIEW_TYPE_EXTENSION_BACKGROUND_PAGE; |
[email protected] | c1e5fb1 | 2013-11-13 03:40:31 | [diff] [blame] | 136 | } else if (view_type_string == kViewTypeTabContents) { |
| 137 | view_type = VIEW_TYPE_TAB_CONTENTS; |
| 138 | } else if (view_type_string == kViewTypePopup) { |
| 139 | view_type = VIEW_TYPE_EXTENSION_POPUP; |
| 140 | } else if (view_type_string == kViewTypeExtensionDialog) { |
| 141 | view_type = VIEW_TYPE_EXTENSION_DIALOG; |
[email protected] | ddcf580 | 2014-02-20 23:21:32 | [diff] [blame] | 142 | } else if (view_type_string == kViewTypeAppWindow) { |
| 143 | view_type = VIEW_TYPE_APP_WINDOW; |
[email protected] | 9faae96 | 2014-08-11 07:59:19 | [diff] [blame] | 144 | } else if (view_type_string == kViewTypeLauncherPage) { |
| 145 | view_type = VIEW_TYPE_LAUNCHER_PAGE; |
[email protected] | c1e5fb1 | 2013-11-13 03:40:31 | [diff] [blame] | 146 | } else if (view_type_string == kViewTypePanel) { |
| 147 | view_type = VIEW_TYPE_PANEL; |
| 148 | } else if (view_type_string != kViewTypeAll) { |
| 149 | return; |
| 150 | } |
| 151 | |
| 152 | std::string extension_id = context()->GetExtensionID(); |
| 153 | if (extension_id.empty()) |
| 154 | return; |
| 155 | |
rdevlin.cronin | 6f42c252 | 2015-06-19 18:58:51 | [diff] [blame] | 156 | std::vector<content::RenderFrame*> frames = |
| 157 | ExtensionFrameHelper::GetExtensionFrames(extension_id, browser_window_id, |
| 158 | view_type); |
[email protected] | 95c6b301 | 2013-12-02 14:30:31 | [diff] [blame] | 159 | v8::Local<v8::Array> v8_views = v8::Array::New(args.GetIsolate()); |
[email protected] | c1e5fb1 | 2013-11-13 03:40:31 | [diff] [blame] | 160 | int v8_index = 0; |
rdevlin.cronin | 6f42c252 | 2015-06-19 18:58:51 | [diff] [blame] | 161 | for (content::RenderFrame* frame : frames) { |
rdevlin.cronin | 9e9f59a | 2015-06-24 23:49:07 | [diff] [blame] | 162 | // We filter out iframes here. GetExtensionViews should only return the |
| 163 | // main views, not any subframes. (Returning subframes can cause broken |
| 164 | // behavior by treating an app window's iframe as its main frame, and maybe |
| 165 | // other nastiness). |
| 166 | if (frame->GetWebFrame()->top() != frame->GetWebFrame()) |
| 167 | continue; |
| 168 | |
[email protected] | c1e5fb1 | 2013-11-13 03:40:31 | [diff] [blame] | 169 | v8::Local<v8::Context> context = |
rdevlin.cronin | 6f42c252 | 2015-06-19 18:58:51 | [diff] [blame] | 170 | frame->GetWebFrame()->mainWorldScriptContext(); |
[email protected] | c1e5fb1 | 2013-11-13 03:40:31 | [diff] [blame] | 171 | if (!context.IsEmpty()) { |
| 172 | v8::Local<v8::Value> window = context->Global(); |
| 173 | DCHECK(!window.IsEmpty()); |
[email protected] | fcb70570 | 2013-12-20 12:29:17 | [diff] [blame] | 174 | v8_views->Set(v8::Integer::New(args.GetIsolate(), v8_index++), window); |
[email protected] | c1e5fb1 | 2013-11-13 03:40:31 | [diff] [blame] | 175 | } |
| 176 | } |
| 177 | |
| 178 | args.GetReturnValue().Set(v8_views); |
| 179 | } |
| 180 | |
[email protected] | bcdd992f | 2013-06-09 12:58:03 | [diff] [blame] | 181 | } // namespace extensions |