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