blob: 3e292139124fbce80314c8a5acb987dbca11878c [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
[email protected]120028b9c2012-07-03 01:32:249#include "base/bind.h"
10#include "base/memory/scoped_ptr.h"
11#include "base/values.h"
mek87e0ab52015-02-13 01:18:2612#include "content/public/child/v8_value_converter.h"
rob248d6a82014-11-21 02:04:4813#include "content/public/renderer/render_frame.h"
[email protected]e4452d32013-11-15 23:07:4114#include "extensions/common/extension.h"
[email protected]fb820c02014-03-13 15:07:0815#include "extensions/common/extension_messages.h"
[email protected]d42c11152013-08-22 19:36:3216#include "extensions/common/features/feature.h"
[email protected]ad8b4ba2013-08-09 19:52:4417#include "extensions/common/features/feature_provider.h"
[email protected]d42c11152013-08-22 19:36:3218#include "extensions/common/manifest.h"
[email protected]e6893672014-05-01 17:29:1319#include "extensions/renderer/api_activity_logger.h"
rdevlin.cronin6f42c2522015-06-19 18:58:5120#include "extensions/renderer/extension_frame_helper.h"
[email protected]bcd9580f2014-04-17 19:17:5921#include "extensions/renderer/script_context.h"
[email protected]2255a9332013-06-17 05:12:3122#include "third_party/WebKit/public/web/WebDocument.h"
rdevlin.cronin6f42c2522015-06-19 18:58:5123#include "third_party/WebKit/public/web/WebLocalFrame.h"
[email protected]2255a9332013-06-17 05:12:3124#include "third_party/WebKit/public/web/WebView.h"
[email protected]120028b9c2012-07-03 01:32:2425
26using content::V8ValueConverter;
27
28namespace extensions {
29
[email protected]bcd9580f2014-04-17 19:17:5930RuntimeCustomBindings::RuntimeCustomBindings(ScriptContext* context)
31 : ObjectBackedNativeHandler(context) {
[email protected]e6893672014-05-01 17:29:1332 RouteFunction(
33 "GetManifest",
34 base::Bind(&RuntimeCustomBindings::GetManifest, base::Unretained(this)));
rdevlin.croninc0569cc2016-04-15 21:51:2135 RouteFunction("OpenChannelToExtension", "runtime.connect",
[email protected]4f1633f2013-03-09 14:26:2436 base::Bind(&RuntimeCustomBindings::OpenChannelToExtension,
37 base::Unretained(this)));
rdevlin.croninc0569cc2016-04-15 21:51:2138 RouteFunction("OpenChannelToNativeApp", "runtime.connectNative",
[email protected]78b75182013-03-06 03:41:3239 base::Bind(&RuntimeCustomBindings::OpenChannelToNativeApp,
40 base::Unretained(this)));
[email protected]c1e5fb12013-11-13 03:40:3141 RouteFunction("GetExtensionViews",
42 base::Bind(&RuntimeCustomBindings::GetExtensionViews,
43 base::Unretained(this)));
[email protected]120028b9c2012-07-03 01:32:2444}
45
[email protected]e6893672014-05-01 17:29:1346RuntimeCustomBindings::~RuntimeCustomBindings() {
47}
[email protected]120028b9c2012-07-03 01:32:2448
[email protected]d8c5fbb2013-06-14 11:35:2549void RuntimeCustomBindings::OpenChannelToExtension(
50 const v8::FunctionCallbackInfo<v8::Value>& args) {
rob248d6a82014-11-21 02:04:4851 // Get the current RenderFrame so that we can send a routed IPC message from
[email protected]481a87e62012-12-19 23:00:2752 // the correct source.
rob248d6a82014-11-21 02:04:4853 content::RenderFrame* renderframe = context()->GetRenderFrame();
54 if (!renderframe)
[email protected]d8c5fbb2013-06-14 11:35:2555 return;
[email protected]481a87e62012-12-19 23:00:2756
57 // The Javascript code should validate/fill the arguments.
[email protected]8ad95b72013-10-16 02:54:1158 CHECK_EQ(args.Length(), 3);
59 CHECK(args[0]->IsString() && args[1]->IsString() && args[2]->IsBoolean());
[email protected]481a87e62012-12-19 23:00:2760
[email protected]686914f2013-04-25 04:54:5861 ExtensionMsg_ExternalConnectionInfo info;
[email protected]9b6a6332014-01-09 19:58:4362
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
dcarneya261b772014-11-20 17:55:0769 info.target_id = *v8::String::Utf8Value(args[0]);
kalman04755302015-09-14 18:52:1170 info.source_url = context()->url();
dcarneya261b772014-11-20 17:55:0771 std::string channel_name = *v8::String::Utf8Value(args[1]);
[email protected]8ad95b72013-10-16 02:54:1172 bool include_tls_channel_id =
73 args.Length() > 2 ? args[2]->BooleanValue() : false;
[email protected]481a87e62012-12-19 23:00:2774 int port_id = -1;
rob248d6a82014-11-21 02:04:4875 renderframe->Send(new ExtensionHostMsg_OpenChannelToExtension(
76 renderframe->GetRoutingID(), info, channel_name, include_tls_channel_id,
77 &port_id));
[email protected]d8c5fbb2013-06-14 11:35:2578 args.GetReturnValue().Set(static_cast<int32_t>(port_id));
[email protected]481a87e62012-12-19 23:00:2779}
80
[email protected]d8c5fbb2013-06-14 11:35:2581void RuntimeCustomBindings::OpenChannelToNativeApp(
82 const v8::FunctionCallbackInfo<v8::Value>& args) {
[email protected]78b75182013-03-06 03:41:3283 // Verify that the extension has permission to use native messaging.
[email protected]b6708d032013-07-14 08:18:2284 Feature::Availability availability =
[email protected]bcd9580f2014-04-17 19:17:5985 FeatureProvider::GetPermissionFeatures()
86 ->GetFeature("nativeMessaging")
87 ->IsAvailableToContext(context()->extension(),
kalman04755302015-09-14 18:52:1188 context()->context_type(), context()->url());
[email protected]ecc854a2013-08-22 10:12:4289 if (!availability.is_available())
[email protected]d8c5fbb2013-06-14 11:35:2590 return;
[email protected]78b75182013-03-06 03:41:3291
rdevlin.cronin25fcbdd62015-07-07 14:11:5992 content::RenderFrame* render_frame = context()->GetRenderFrame();
93 if (!render_frame)
[email protected]d8c5fbb2013-06-14 11:35:2594 return;
[email protected]481a87e62012-12-19 23:00:2795
96 // The Javascript code should validate/fill the arguments.
[email protected]e6893672014-05-01 17:29:1397 CHECK(args.Length() >= 2 && args[0]->IsString() && args[1]->IsString());
[email protected]481a87e62012-12-19 23:00:2798
dcarneya261b772014-11-20 17:55:0799 std::string extension_id = *v8::String::Utf8Value(args[0]);
100 std::string native_app_name = *v8::String::Utf8Value(args[1]);
[email protected]481a87e62012-12-19 23:00:27101
102 int port_id = -1;
rdevlin.cronin25fcbdd62015-07-07 14:11:59103 render_frame->Send(new ExtensionHostMsg_OpenChannelToNativeApp(
104 render_frame->GetRoutingID(), extension_id, native_app_name, &port_id));
[email protected]d8c5fbb2013-06-14 11:35:25105 args.GetReturnValue().Set(static_cast<int32_t>(port_id));
[email protected]481a87e62012-12-19 23:00:27106}
107
[email protected]d8c5fbb2013-06-14 11:35:25108void RuntimeCustomBindings::GetManifest(
109 const v8::FunctionCallbackInfo<v8::Value>& args) {
[email protected]9a598442013-06-04 16:39:12110 CHECK(context()->extension());
[email protected]120028b9c2012-07-03 01:32:24111
112 scoped_ptr<V8ValueConverter> converter(V8ValueConverter::create());
[email protected]e6893672014-05-01 17:29:13113 args.GetReturnValue().Set(converter->ToV8Value(
114 context()->extension()->manifest()->value(), context()->v8_context()));
[email protected]120028b9c2012-07-03 01:32:24115}
116
[email protected]c1e5fb12013-11-13 03:40:31117void 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
brettwc15100c2015-08-06 22:54:16129 std::string view_type_string =
130 base::ToUpperASCII(*v8::String::Utf8Value(args[1]));
[email protected]c1e5fb12013-11-13 03:40:31131 // |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]c1e5fb12013-11-13 03:40:31136 } 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]ddcf5802014-02-20 23:21:32142 } else if (view_type_string == kViewTypeAppWindow) {
143 view_type = VIEW_TYPE_APP_WINDOW;
[email protected]9faae962014-08-11 07:59:19144 } else if (view_type_string == kViewTypeLauncherPage) {
145 view_type = VIEW_TYPE_LAUNCHER_PAGE;
[email protected]c1e5fb12013-11-13 03:40:31146 } 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.cronin6f42c2522015-06-19 18:58:51156 std::vector<content::RenderFrame*> frames =
157 ExtensionFrameHelper::GetExtensionFrames(extension_id, browser_window_id,
158 view_type);
[email protected]95c6b3012013-12-02 14:30:31159 v8::Local<v8::Array> v8_views = v8::Array::New(args.GetIsolate());
[email protected]c1e5fb12013-11-13 03:40:31160 int v8_index = 0;
rdevlin.cronin6f42c2522015-06-19 18:58:51161 for (content::RenderFrame* frame : frames) {
rdevlin.cronin9e9f59a2015-06-24 23:49:07162 // 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]c1e5fb12013-11-13 03:40:31169 v8::Local<v8::Context> context =
rdevlin.cronin6f42c2522015-06-19 18:58:51170 frame->GetWebFrame()->mainWorldScriptContext();
[email protected]c1e5fb12013-11-13 03:40:31171 if (!context.IsEmpty()) {
172 v8::Local<v8::Value> window = context->Global();
173 DCHECK(!window.IsEmpty());
[email protected]fcb705702013-12-20 12:29:17174 v8_views->Set(v8::Integer::New(args.GetIsolate(), v8_index++), window);
[email protected]c1e5fb12013-11-13 03:40:31175 }
176 }
177
178 args.GetReturnValue().Set(v8_views);
179}
180
[email protected]bcdd992f2013-06-09 12:58:03181} // namespace extensions