blob: c7f9e279fde28af65d6d72a3525a5c6860393f8c [file] [log] [blame]
[email protected]e6893672014-05-01 17:29:131// Copyright 2014 The Chromium Authors. All rights reserved.
[email protected]120028b9c2012-07-03 01:32:242// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]e6893672014-05-01 17:29:135#include "extensions/renderer/runtime_custom_bindings.h"
[email protected]120028b9c2012-07-03 01:32:246
avi2d124c02015-12-23 06:36:427#include <stdint.h>
8
dchengf6f80662016-04-20 20:26:049#include <memory>
10
[email protected]120028b9c2012-07-03 01:32:2411#include "base/bind.h"
[email protected]120028b9c2012-07-03 01:32:2412#include "base/values.h"
mek87e0ab52015-02-13 01:18:2613#include "content/public/child/v8_value_converter.h"
rob248d6a82014-11-21 02:04:4814#include "content/public/renderer/render_frame.h"
[email protected]e4452d32013-11-15 23:07:4115#include "extensions/common/extension.h"
[email protected]fb820c02014-03-13 15:07:0816#include "extensions/common/extension_messages.h"
[email protected]d42c11152013-08-22 19:36:3217#include "extensions/common/manifest.h"
rdevlin.cronin6f42c2522015-06-19 18:58:5118#include "extensions/renderer/extension_frame_helper.h"
[email protected]bcd9580f2014-04-17 19:17:5919#include "extensions/renderer/script_context.h"
rdevlin.cronin6f42c2522015-06-19 18:58:5120#include "third_party/WebKit/public/web/WebLocalFrame.h"
[email protected]120028b9c2012-07-03 01:32:2421
22namespace extensions {
23
[email protected]bcd9580f2014-04-17 19:17:5924RuntimeCustomBindings::RuntimeCustomBindings(ScriptContext* context)
25 : ObjectBackedNativeHandler(context) {
[email protected]e6893672014-05-01 17:29:1326 RouteFunction(
27 "GetManifest",
28 base::Bind(&RuntimeCustomBindings::GetManifest, base::Unretained(this)));
rdevlin.croninc0569cc2016-04-15 21:51:2129 RouteFunction("OpenChannelToExtension", "runtime.connect",
[email protected]4f1633f2013-03-09 14:26:2430 base::Bind(&RuntimeCustomBindings::OpenChannelToExtension,
31 base::Unretained(this)));
rdevlin.croninc0569cc2016-04-15 21:51:2132 RouteFunction("OpenChannelToNativeApp", "runtime.connectNative",
[email protected]78b75182013-03-06 03:41:3233 base::Bind(&RuntimeCustomBindings::OpenChannelToNativeApp,
34 base::Unretained(this)));
[email protected]c1e5fb12013-11-13 03:40:3135 RouteFunction("GetExtensionViews",
36 base::Bind(&RuntimeCustomBindings::GetExtensionViews,
37 base::Unretained(this)));
[email protected]120028b9c2012-07-03 01:32:2438}
39
[email protected]e6893672014-05-01 17:29:1340RuntimeCustomBindings::~RuntimeCustomBindings() {
41}
[email protected]120028b9c2012-07-03 01:32:2442
[email protected]d8c5fbb2013-06-14 11:35:2543void RuntimeCustomBindings::OpenChannelToExtension(
44 const v8::FunctionCallbackInfo<v8::Value>& args) {
rob248d6a82014-11-21 02:04:4845 // Get the current RenderFrame so that we can send a routed IPC message from
[email protected]481a87e62012-12-19 23:00:2746 // the correct source.
rob248d6a82014-11-21 02:04:4847 content::RenderFrame* renderframe = context()->GetRenderFrame();
48 if (!renderframe)
[email protected]d8c5fbb2013-06-14 11:35:2549 return;
[email protected]481a87e62012-12-19 23:00:2750
51 // The Javascript code should validate/fill the arguments.
[email protected]8ad95b72013-10-16 02:54:1152 CHECK_EQ(args.Length(), 3);
53 CHECK(args[0]->IsString() && args[1]->IsString() && args[2]->IsBoolean());
[email protected]481a87e62012-12-19 23:00:2754
[email protected]686914f2013-04-25 04:54:5855 ExtensionMsg_ExternalConnectionInfo info;
[email protected]9b6a6332014-01-09 19:58:4356
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
dcarneya261b772014-11-20 17:55:0763 info.target_id = *v8::String::Utf8Value(args[0]);
kalman04755302015-09-14 18:52:1164 info.source_url = context()->url();
dcarneya261b772014-11-20 17:55:0765 std::string channel_name = *v8::String::Utf8Value(args[1]);
[email protected]8ad95b72013-10-16 02:54:1166 bool include_tls_channel_id =
67 args.Length() > 2 ? args[2]->BooleanValue() : false;
[email protected]481a87e62012-12-19 23:00:2768 int port_id = -1;
rdevlin.croninc7ce4fc2016-05-10 01:41:3769 // TODO(devlin): This file is littered with sync IPCs. Yuck.
rob248d6a82014-11-21 02:04:4870 renderframe->Send(new ExtensionHostMsg_OpenChannelToExtension(
71 renderframe->GetRoutingID(), info, channel_name, include_tls_channel_id,
72 &port_id));
[email protected]d8c5fbb2013-06-14 11:35:2573 args.GetReturnValue().Set(static_cast<int32_t>(port_id));
[email protected]481a87e62012-12-19 23:00:2774}
75
[email protected]d8c5fbb2013-06-14 11:35:2576void RuntimeCustomBindings::OpenChannelToNativeApp(
77 const v8::FunctionCallbackInfo<v8::Value>& args) {
rdevlin.croninc7ce4fc2016-05-10 01:41:3778 // The Javascript code should validate/fill the arguments.
79 CHECK_EQ(args.Length(), 1);
80 CHECK(args[0]->IsString());
81
[email protected]78b75182013-03-06 03:41:3282 // Verify that the extension has permission to use native messaging.
rdevlin.croninc7ce4fc2016-05-10 01:41:3783 if (!context()->GetAvailability("runtime.connectNative").is_available())
[email protected]d8c5fbb2013-06-14 11:35:2584 return;
[email protected]78b75182013-03-06 03:41:3285
rdevlin.cronin25fcbdd62015-07-07 14:11:5986 content::RenderFrame* render_frame = context()->GetRenderFrame();
87 if (!render_frame)
[email protected]d8c5fbb2013-06-14 11:35:2588 return;
[email protected]481a87e62012-12-19 23:00:2789
rdevlin.croninc7ce4fc2016-05-10 01:41:3790 const std::string& extension_id = context()->GetExtensionID();
91 // Should be caught by JS.
92 CHECK(!extension_id.empty());
[email protected]481a87e62012-12-19 23:00:2793
rdevlin.croninc7ce4fc2016-05-10 01:41:3794 std::string native_app_name = *v8::String::Utf8Value(args[0]);
[email protected]481a87e62012-12-19 23:00:2795
96 int port_id = -1;
rdevlin.cronin25fcbdd62015-07-07 14:11:5997 render_frame->Send(new ExtensionHostMsg_OpenChannelToNativeApp(
98 render_frame->GetRoutingID(), extension_id, native_app_name, &port_id));
[email protected]d8c5fbb2013-06-14 11:35:2599 args.GetReturnValue().Set(static_cast<int32_t>(port_id));
[email protected]481a87e62012-12-19 23:00:27100}
101
[email protected]d8c5fbb2013-06-14 11:35:25102void RuntimeCustomBindings::GetManifest(
103 const v8::FunctionCallbackInfo<v8::Value>& args) {
[email protected]9a598442013-06-04 16:39:12104 CHECK(context()->extension());
[email protected]120028b9c2012-07-03 01:32:24105
rdevlin.croninc7ce4fc2016-05-10 01:41:37106 std::unique_ptr<content::V8ValueConverter> converter(
107 content::V8ValueConverter::create());
[email protected]e6893672014-05-01 17:29:13108 args.GetReturnValue().Set(converter->ToV8Value(
109 context()->extension()->manifest()->value(), context()->v8_context()));
[email protected]120028b9c2012-07-03 01:32:24110}
111
[email protected]c1e5fb12013-11-13 03:40:31112void RuntimeCustomBindings::GetExtensionViews(
113 const v8::FunctionCallbackInfo<v8::Value>& args) {
rdevlin.croninc7ce4fc2016-05-10 01:41:37114 CHECK_EQ(args.Length(), 2);
115 CHECK(args[0]->IsInt32());
116 CHECK(args[1]->IsString());
[email protected]c1e5fb12013-11-13 03:40:31117
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
brettwc15100c2015-08-06 22:54:16122 std::string view_type_string =
123 base::ToUpperASCII(*v8::String::Utf8Value(args[1]));
[email protected]c1e5fb12013-11-13 03:40:31124 // |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]c1e5fb12013-11-13 03:40:31129 } 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]ddcf5802014-02-20 23:21:32135 } else if (view_type_string == kViewTypeAppWindow) {
136 view_type = VIEW_TYPE_APP_WINDOW;
[email protected]9faae962014-08-11 07:59:19137 } else if (view_type_string == kViewTypeLauncherPage) {
138 view_type = VIEW_TYPE_LAUNCHER_PAGE;
[email protected]c1e5fb12013-11-13 03:40:31139 } else if (view_type_string == kViewTypePanel) {
140 view_type = VIEW_TYPE_PANEL;
rdevlin.croninc7ce4fc2016-05-10 01:41:37141 } else {
142 CHECK_EQ(view_type_string, kViewTypeAll);
[email protected]c1e5fb12013-11-13 03:40:31143 }
144
rdevlin.croninc7ce4fc2016-05-10 01:41:37145 const std::string& extension_id = context()->GetExtensionID();
[email protected]c1e5fb12013-11-13 03:40:31146 if (extension_id.empty())
147 return;
148
rdevlin.cronin6f42c2522015-06-19 18:58:51149 std::vector<content::RenderFrame*> frames =
150 ExtensionFrameHelper::GetExtensionFrames(extension_id, browser_window_id,
151 view_type);
robaa7a8892016-05-02 16:18:37152 v8::Local<v8::Context> v8_context = args.GetIsolate()->GetCurrentContext();
[email protected]95c6b3012013-12-02 14:30:31153 v8::Local<v8::Array> v8_views = v8::Array::New(args.GetIsolate());
[email protected]c1e5fb12013-11-13 03:40:31154 int v8_index = 0;
rdevlin.cronin6f42c2522015-06-19 18:58:51155 for (content::RenderFrame* frame : frames) {
rdevlin.cronin9e9f59a2015-06-24 23:49:07156 // 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.croninc7ce4fc2016-05-10 01:41:37160 blink::WebFrame* web_frame = frame->GetWebFrame();
161 if (web_frame->top() != web_frame)
rdevlin.cronin9e9f59a2015-06-24 23:49:07162 continue;
163
rdevlin.croninc7ce4fc2016-05-10 01:41:37164 if (!blink::WebFrame::scriptCanAccess(web_frame))
165 continue;
166
167 v8::Local<v8::Context> context = web_frame->mainWorldScriptContext();
[email protected]c1e5fb12013-11-13 03:40:31168 if (!context.IsEmpty()) {
169 v8::Local<v8::Value> window = context->Global();
rdevlin.croninc7ce4fc2016-05-10 01:41:37170 CHECK(!window.IsEmpty());
robaa7a8892016-05-02 16:18:37171 v8::Maybe<bool> maybe =
rdevlin.croninc7ce4fc2016-05-10 01:41:37172 v8_views->CreateDataProperty(v8_context, v8_index++, window);
173 CHECK(maybe.IsJust() && maybe.FromJust());
[email protected]c1e5fb12013-11-13 03:40:31174 }
175 }
176
177 args.GetReturnValue().Set(v8_views);
178}
179
[email protected]bcdd992f2013-06-09 12:58:03180} // namespace extensions