blob: 1e540f30d5fd4d5f62107d293be90f63eba3c5a0 [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/features/feature.h"
[email protected]ad8b4ba2013-08-09 19:52:4418#include "extensions/common/features/feature_provider.h"
[email protected]d42c11152013-08-22 19:36:3219#include "extensions/common/manifest.h"
[email protected]e6893672014-05-01 17:29:1320#include "extensions/renderer/api_activity_logger.h"
rdevlin.cronin6f42c2522015-06-19 18:58:5121#include "extensions/renderer/extension_frame_helper.h"
[email protected]bcd9580f2014-04-17 19:17:5922#include "extensions/renderer/script_context.h"
[email protected]2255a9332013-06-17 05:12:3123#include "third_party/WebKit/public/web/WebDocument.h"
rdevlin.cronin6f42c2522015-06-19 18:58:5124#include "third_party/WebKit/public/web/WebLocalFrame.h"
[email protected]2255a9332013-06-17 05:12:3125#include "third_party/WebKit/public/web/WebView.h"
[email protected]120028b9c2012-07-03 01:32:2426
27using content::V8ValueConverter;
28
29namespace extensions {
30
[email protected]bcd9580f2014-04-17 19:17:5931RuntimeCustomBindings::RuntimeCustomBindings(ScriptContext* context)
32 : ObjectBackedNativeHandler(context) {
[email protected]e6893672014-05-01 17:29:1333 RouteFunction(
34 "GetManifest",
35 base::Bind(&RuntimeCustomBindings::GetManifest, base::Unretained(this)));
rdevlin.croninc0569cc2016-04-15 21:51:2136 RouteFunction("OpenChannelToExtension", "runtime.connect",
[email protected]4f1633f2013-03-09 14:26:2437 base::Bind(&RuntimeCustomBindings::OpenChannelToExtension,
38 base::Unretained(this)));
rdevlin.croninc0569cc2016-04-15 21:51:2139 RouteFunction("OpenChannelToNativeApp", "runtime.connectNative",
[email protected]78b75182013-03-06 03:41:3240 base::Bind(&RuntimeCustomBindings::OpenChannelToNativeApp,
41 base::Unretained(this)));
[email protected]c1e5fb12013-11-13 03:40:3142 RouteFunction("GetExtensionViews",
43 base::Bind(&RuntimeCustomBindings::GetExtensionViews,
44 base::Unretained(this)));
[email protected]120028b9c2012-07-03 01:32:2445}
46
[email protected]e6893672014-05-01 17:29:1347RuntimeCustomBindings::~RuntimeCustomBindings() {
48}
[email protected]120028b9c2012-07-03 01:32:2449
[email protected]d8c5fbb2013-06-14 11:35:2550void RuntimeCustomBindings::OpenChannelToExtension(
51 const v8::FunctionCallbackInfo<v8::Value>& args) {
rob248d6a82014-11-21 02:04:4852 // Get the current RenderFrame so that we can send a routed IPC message from
[email protected]481a87e62012-12-19 23:00:2753 // the correct source.
rob248d6a82014-11-21 02:04:4854 content::RenderFrame* renderframe = context()->GetRenderFrame();
55 if (!renderframe)
[email protected]d8c5fbb2013-06-14 11:35:2556 return;
[email protected]481a87e62012-12-19 23:00:2757
58 // The Javascript code should validate/fill the arguments.
[email protected]8ad95b72013-10-16 02:54:1159 CHECK_EQ(args.Length(), 3);
60 CHECK(args[0]->IsString() && args[1]->IsString() && args[2]->IsBoolean());
[email protected]481a87e62012-12-19 23:00:2761
[email protected]686914f2013-04-25 04:54:5862 ExtensionMsg_ExternalConnectionInfo info;
[email protected]9b6a6332014-01-09 19:58:4363
64 // For messaging APIs, hosted apps should be considered a web page so hide
65 // its extension ID.
66 const Extension* extension = context()->extension();
67 if (extension && !extension->is_hosted_app())
68 info.source_id = extension->id();
69
dcarneya261b772014-11-20 17:55:0770 info.target_id = *v8::String::Utf8Value(args[0]);
kalman04755302015-09-14 18:52:1171 info.source_url = context()->url();
dcarneya261b772014-11-20 17:55:0772 std::string channel_name = *v8::String::Utf8Value(args[1]);
[email protected]8ad95b72013-10-16 02:54:1173 bool include_tls_channel_id =
74 args.Length() > 2 ? args[2]->BooleanValue() : false;
[email protected]481a87e62012-12-19 23:00:2775 int port_id = -1;
rob248d6a82014-11-21 02:04:4876 renderframe->Send(new ExtensionHostMsg_OpenChannelToExtension(
77 renderframe->GetRoutingID(), info, channel_name, include_tls_channel_id,
78 &port_id));
[email protected]d8c5fbb2013-06-14 11:35:2579 args.GetReturnValue().Set(static_cast<int32_t>(port_id));
[email protected]481a87e62012-12-19 23:00:2780}
81
[email protected]d8c5fbb2013-06-14 11:35:2582void RuntimeCustomBindings::OpenChannelToNativeApp(
83 const v8::FunctionCallbackInfo<v8::Value>& args) {
[email protected]78b75182013-03-06 03:41:3284 // Verify that the extension has permission to use native messaging.
[email protected]b6708d032013-07-14 08:18:2285 Feature::Availability availability =
[email protected]bcd9580f2014-04-17 19:17:5986 FeatureProvider::GetPermissionFeatures()
87 ->GetFeature("nativeMessaging")
88 ->IsAvailableToContext(context()->extension(),
kalman04755302015-09-14 18:52:1189 context()->context_type(), context()->url());
[email protected]ecc854a2013-08-22 10:12:4290 if (!availability.is_available())
[email protected]d8c5fbb2013-06-14 11:35:2591 return;
[email protected]78b75182013-03-06 03:41:3292
rdevlin.cronin25fcbdd62015-07-07 14:11:5993 content::RenderFrame* render_frame = context()->GetRenderFrame();
94 if (!render_frame)
[email protected]d8c5fbb2013-06-14 11:35:2595 return;
[email protected]481a87e62012-12-19 23:00:2796
97 // The Javascript code should validate/fill the arguments.
[email protected]e6893672014-05-01 17:29:1398 CHECK(args.Length() >= 2 && args[0]->IsString() && args[1]->IsString());
[email protected]481a87e62012-12-19 23:00:2799
dcarneya261b772014-11-20 17:55:07100 std::string extension_id = *v8::String::Utf8Value(args[0]);
101 std::string native_app_name = *v8::String::Utf8Value(args[1]);
[email protected]481a87e62012-12-19 23:00:27102
103 int port_id = -1;
rdevlin.cronin25fcbdd62015-07-07 14:11:59104 render_frame->Send(new ExtensionHostMsg_OpenChannelToNativeApp(
105 render_frame->GetRoutingID(), extension_id, native_app_name, &port_id));
[email protected]d8c5fbb2013-06-14 11:35:25106 args.GetReturnValue().Set(static_cast<int32_t>(port_id));
[email protected]481a87e62012-12-19 23:00:27107}
108
[email protected]d8c5fbb2013-06-14 11:35:25109void RuntimeCustomBindings::GetManifest(
110 const v8::FunctionCallbackInfo<v8::Value>& args) {
[email protected]9a598442013-06-04 16:39:12111 CHECK(context()->extension());
[email protected]120028b9c2012-07-03 01:32:24112
dchengf6f80662016-04-20 20:26:04113 std::unique_ptr<V8ValueConverter> converter(V8ValueConverter::create());
[email protected]e6893672014-05-01 17:29:13114 args.GetReturnValue().Set(converter->ToV8Value(
115 context()->extension()->manifest()->value(), context()->v8_context()));
[email protected]120028b9c2012-07-03 01:32:24116}
117
[email protected]c1e5fb12013-11-13 03:40:31118void RuntimeCustomBindings::GetExtensionViews(
119 const v8::FunctionCallbackInfo<v8::Value>& args) {
120 if (args.Length() != 2)
121 return;
122
123 if (!args[0]->IsInt32() || !args[1]->IsString())
124 return;
125
126 // |browser_window_id| == extension_misc::kUnknownWindowId means getting
127 // all views for the current extension.
128 int browser_window_id = args[0]->Int32Value();
129
brettwc15100c2015-08-06 22:54:16130 std::string view_type_string =
131 base::ToUpperASCII(*v8::String::Utf8Value(args[1]));
[email protected]c1e5fb12013-11-13 03:40:31132 // |view_type| == VIEW_TYPE_INVALID means getting any type of
133 // views.
134 ViewType view_type = VIEW_TYPE_INVALID;
135 if (view_type_string == kViewTypeBackgroundPage) {
136 view_type = VIEW_TYPE_EXTENSION_BACKGROUND_PAGE;
[email protected]c1e5fb12013-11-13 03:40:31137 } else if (view_type_string == kViewTypeTabContents) {
138 view_type = VIEW_TYPE_TAB_CONTENTS;
139 } else if (view_type_string == kViewTypePopup) {
140 view_type = VIEW_TYPE_EXTENSION_POPUP;
141 } else if (view_type_string == kViewTypeExtensionDialog) {
142 view_type = VIEW_TYPE_EXTENSION_DIALOG;
[email protected]ddcf5802014-02-20 23:21:32143 } else if (view_type_string == kViewTypeAppWindow) {
144 view_type = VIEW_TYPE_APP_WINDOW;
[email protected]9faae962014-08-11 07:59:19145 } else if (view_type_string == kViewTypeLauncherPage) {
146 view_type = VIEW_TYPE_LAUNCHER_PAGE;
[email protected]c1e5fb12013-11-13 03:40:31147 } else if (view_type_string == kViewTypePanel) {
148 view_type = VIEW_TYPE_PANEL;
149 } else if (view_type_string != kViewTypeAll) {
150 return;
151 }
152
153 std::string extension_id = context()->GetExtensionID();
154 if (extension_id.empty())
155 return;
156
rdevlin.cronin6f42c2522015-06-19 18:58:51157 std::vector<content::RenderFrame*> frames =
158 ExtensionFrameHelper::GetExtensionFrames(extension_id, browser_window_id,
159 view_type);
robaa7a8892016-05-02 16:18:37160 v8::Local<v8::Context> v8_context = args.GetIsolate()->GetCurrentContext();
[email protected]95c6b3012013-12-02 14:30:31161 v8::Local<v8::Array> v8_views = v8::Array::New(args.GetIsolate());
[email protected]c1e5fb12013-11-13 03:40:31162 int v8_index = 0;
rdevlin.cronin6f42c2522015-06-19 18:58:51163 for (content::RenderFrame* frame : frames) {
rdevlin.cronin9e9f59a2015-06-24 23:49:07164 // We filter out iframes here. GetExtensionViews should only return the
165 // 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
167 // other nastiness).
168 if (frame->GetWebFrame()->top() != frame->GetWebFrame())
169 continue;
170
[email protected]c1e5fb12013-11-13 03:40:31171 v8::Local<v8::Context> context =
rdevlin.cronin6f42c2522015-06-19 18:58:51172 frame->GetWebFrame()->mainWorldScriptContext();
[email protected]c1e5fb12013-11-13 03:40:31173 if (!context.IsEmpty()) {
174 v8::Local<v8::Value> window = context->Global();
175 DCHECK(!window.IsEmpty());
robaa7a8892016-05-02 16:18:37176 v8::Maybe<bool> maybe =
177 v8_views->CreateDataProperty(v8_context, v8_index++, window);
178 DCHECK(maybe.IsJust() && maybe.FromJust());
[email protected]c1e5fb12013-11-13 03:40:31179 }
180 }
181
182 args.GetReturnValue().Set(v8_views);
183}
184
[email protected]bcdd992f2013-06-09 12:58:03185} // namespace extensions