blob: c6d3ba0113e9bf1af3097edd94132cc193963d68 [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
7#include "base/bind.h"
8#include "base/memory/scoped_ptr.h"
9#include "base/values.h"
mek87e0ab52015-02-13 01:18:2610#include "content/public/child/v8_value_converter.h"
rob248d6a82014-11-21 02:04:4811#include "content/public/renderer/render_frame.h"
[email protected]e4452d32013-11-15 23:07:4112#include "extensions/common/extension.h"
[email protected]fb820c02014-03-13 15:07:0813#include "extensions/common/extension_messages.h"
[email protected]d42c11152013-08-22 19:36:3214#include "extensions/common/features/feature.h"
[email protected]ad8b4ba2013-08-09 19:52:4415#include "extensions/common/features/feature_provider.h"
[email protected]d42c11152013-08-22 19:36:3216#include "extensions/common/manifest.h"
[email protected]e6893672014-05-01 17:29:1317#include "extensions/renderer/api_activity_logger.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"
[email protected]2255a9332013-06-17 05:12:3120#include "third_party/WebKit/public/web/WebDocument.h"
rdevlin.cronin6f42c2522015-06-19 18:58:5121#include "third_party/WebKit/public/web/WebLocalFrame.h"
[email protected]2255a9332013-06-17 05:12:3122#include "third_party/WebKit/public/web/WebView.h"
[email protected]120028b9c2012-07-03 01:32:2423
24using content::V8ValueConverter;
25
26namespace extensions {
27
[email protected]bcd9580f2014-04-17 19:17:5928RuntimeCustomBindings::RuntimeCustomBindings(ScriptContext* context)
29 : ObjectBackedNativeHandler(context) {
[email protected]e6893672014-05-01 17:29:1330 RouteFunction(
31 "GetManifest",
32 base::Bind(&RuntimeCustomBindings::GetManifest, base::Unretained(this)));
[email protected]4f1633f2013-03-09 14:26:2433 RouteFunction("OpenChannelToExtension",
34 base::Bind(&RuntimeCustomBindings::OpenChannelToExtension,
35 base::Unretained(this)));
[email protected]78b75182013-03-06 03:41:3236 RouteFunction("OpenChannelToNativeApp",
37 base::Bind(&RuntimeCustomBindings::OpenChannelToNativeApp,
38 base::Unretained(this)));
[email protected]c1e5fb12013-11-13 03:40:3139 RouteFunction("GetExtensionViews",
40 base::Bind(&RuntimeCustomBindings::GetExtensionViews,
41 base::Unretained(this)));
[email protected]120028b9c2012-07-03 01:32:2442}
43
[email protected]e6893672014-05-01 17:29:1344RuntimeCustomBindings::~RuntimeCustomBindings() {
45}
[email protected]120028b9c2012-07-03 01:32:2446
[email protected]d8c5fbb2013-06-14 11:35:2547void RuntimeCustomBindings::OpenChannelToExtension(
48 const v8::FunctionCallbackInfo<v8::Value>& args) {
rob248d6a82014-11-21 02:04:4849 // Get the current RenderFrame so that we can send a routed IPC message from
[email protected]481a87e62012-12-19 23:00:2750 // the correct source.
rob248d6a82014-11-21 02:04:4851 content::RenderFrame* renderframe = context()->GetRenderFrame();
52 if (!renderframe)
[email protected]d8c5fbb2013-06-14 11:35:2553 return;
[email protected]481a87e62012-12-19 23:00:2754
55 // The Javascript code should validate/fill the arguments.
[email protected]8ad95b72013-10-16 02:54:1156 CHECK_EQ(args.Length(), 3);
57 CHECK(args[0]->IsString() && args[1]->IsString() && args[2]->IsBoolean());
[email protected]481a87e62012-12-19 23:00:2758
[email protected]686914f2013-04-25 04:54:5859 ExtensionMsg_ExternalConnectionInfo info;
[email protected]9b6a6332014-01-09 19:58:4360
61 // For messaging APIs, hosted apps should be considered a web page so hide
62 // its extension ID.
63 const Extension* extension = context()->extension();
64 if (extension && !extension->is_hosted_app())
65 info.source_id = extension->id();
66
dcarneya261b772014-11-20 17:55:0767 info.target_id = *v8::String::Utf8Value(args[0]);
kalman04755302015-09-14 18:52:1168 info.source_url = context()->url();
dcarneya261b772014-11-20 17:55:0769 std::string channel_name = *v8::String::Utf8Value(args[1]);
[email protected]8ad95b72013-10-16 02:54:1170 bool include_tls_channel_id =
71 args.Length() > 2 ? args[2]->BooleanValue() : false;
[email protected]481a87e62012-12-19 23:00:2772 int port_id = -1;
rob248d6a82014-11-21 02:04:4873 renderframe->Send(new ExtensionHostMsg_OpenChannelToExtension(
74 renderframe->GetRoutingID(), info, channel_name, include_tls_channel_id,
75 &port_id));
[email protected]d8c5fbb2013-06-14 11:35:2576 args.GetReturnValue().Set(static_cast<int32_t>(port_id));
[email protected]481a87e62012-12-19 23:00:2777}
78
[email protected]d8c5fbb2013-06-14 11:35:2579void RuntimeCustomBindings::OpenChannelToNativeApp(
80 const v8::FunctionCallbackInfo<v8::Value>& args) {
[email protected]78b75182013-03-06 03:41:3281 // Verify that the extension has permission to use native messaging.
[email protected]b6708d032013-07-14 08:18:2282 Feature::Availability availability =
[email protected]bcd9580f2014-04-17 19:17:5983 FeatureProvider::GetPermissionFeatures()
84 ->GetFeature("nativeMessaging")
85 ->IsAvailableToContext(context()->extension(),
kalman04755302015-09-14 18:52:1186 context()->context_type(), context()->url());
[email protected]ecc854a2013-08-22 10:12:4287 if (!availability.is_available())
[email protected]d8c5fbb2013-06-14 11:35:2588 return;
[email protected]78b75182013-03-06 03:41:3289
rdevlin.cronin25fcbdd62015-07-07 14:11:5990 content::RenderFrame* render_frame = context()->GetRenderFrame();
91 if (!render_frame)
[email protected]d8c5fbb2013-06-14 11:35:2592 return;
[email protected]481a87e62012-12-19 23:00:2793
94 // The Javascript code should validate/fill the arguments.
[email protected]e6893672014-05-01 17:29:1395 CHECK(args.Length() >= 2 && args[0]->IsString() && args[1]->IsString());
[email protected]481a87e62012-12-19 23:00:2796
dcarneya261b772014-11-20 17:55:0797 std::string extension_id = *v8::String::Utf8Value(args[0]);
98 std::string native_app_name = *v8::String::Utf8Value(args[1]);
[email protected]481a87e62012-12-19 23:00:2799
100 int port_id = -1;
rdevlin.cronin25fcbdd62015-07-07 14:11:59101 render_frame->Send(new ExtensionHostMsg_OpenChannelToNativeApp(
102 render_frame->GetRoutingID(), extension_id, native_app_name, &port_id));
[email protected]d8c5fbb2013-06-14 11:35:25103 args.GetReturnValue().Set(static_cast<int32_t>(port_id));
[email protected]481a87e62012-12-19 23:00:27104}
105
[email protected]d8c5fbb2013-06-14 11:35:25106void RuntimeCustomBindings::GetManifest(
107 const v8::FunctionCallbackInfo<v8::Value>& args) {
[email protected]9a598442013-06-04 16:39:12108 CHECK(context()->extension());
[email protected]120028b9c2012-07-03 01:32:24109
110 scoped_ptr<V8ValueConverter> converter(V8ValueConverter::create());
[email protected]e6893672014-05-01 17:29:13111 args.GetReturnValue().Set(converter->ToV8Value(
112 context()->extension()->manifest()->value(), context()->v8_context()));
[email protected]120028b9c2012-07-03 01:32:24113}
114
[email protected]c1e5fb12013-11-13 03:40:31115void RuntimeCustomBindings::GetExtensionViews(
116 const v8::FunctionCallbackInfo<v8::Value>& args) {
117 if (args.Length() != 2)
118 return;
119
120 if (!args[0]->IsInt32() || !args[1]->IsString())
121 return;
122
123 // |browser_window_id| == extension_misc::kUnknownWindowId means getting
124 // all views for the current extension.
125 int browser_window_id = args[0]->Int32Value();
126
brettwc15100c2015-08-06 22:54:16127 std::string view_type_string =
128 base::ToUpperASCII(*v8::String::Utf8Value(args[1]));
[email protected]c1e5fb12013-11-13 03:40:31129 // |view_type| == VIEW_TYPE_INVALID means getting any type of
130 // views.
131 ViewType view_type = VIEW_TYPE_INVALID;
132 if (view_type_string == kViewTypeBackgroundPage) {
133 view_type = VIEW_TYPE_EXTENSION_BACKGROUND_PAGE;
[email protected]c1e5fb12013-11-13 03:40:31134 } else if (view_type_string == kViewTypeTabContents) {
135 view_type = VIEW_TYPE_TAB_CONTENTS;
136 } else if (view_type_string == kViewTypePopup) {
137 view_type = VIEW_TYPE_EXTENSION_POPUP;
138 } else if (view_type_string == kViewTypeExtensionDialog) {
139 view_type = VIEW_TYPE_EXTENSION_DIALOG;
[email protected]ddcf5802014-02-20 23:21:32140 } else if (view_type_string == kViewTypeAppWindow) {
141 view_type = VIEW_TYPE_APP_WINDOW;
[email protected]9faae962014-08-11 07:59:19142 } else if (view_type_string == kViewTypeLauncherPage) {
143 view_type = VIEW_TYPE_LAUNCHER_PAGE;
[email protected]c1e5fb12013-11-13 03:40:31144 } else if (view_type_string == kViewTypePanel) {
145 view_type = VIEW_TYPE_PANEL;
146 } else if (view_type_string != kViewTypeAll) {
147 return;
148 }
149
150 std::string extension_id = context()->GetExtensionID();
151 if (extension_id.empty())
152 return;
153
rdevlin.cronin6f42c2522015-06-19 18:58:51154 std::vector<content::RenderFrame*> frames =
155 ExtensionFrameHelper::GetExtensionFrames(extension_id, browser_window_id,
156 view_type);
[email protected]95c6b3012013-12-02 14:30:31157 v8::Local<v8::Array> v8_views = v8::Array::New(args.GetIsolate());
[email protected]c1e5fb12013-11-13 03:40:31158 int v8_index = 0;
rdevlin.cronin6f42c2522015-06-19 18:58:51159 for (content::RenderFrame* frame : frames) {
rdevlin.cronin9e9f59a2015-06-24 23:49:07160 // We filter out iframes here. GetExtensionViews should only return the
161 // main views, not any subframes. (Returning subframes can cause broken
162 // behavior by treating an app window's iframe as its main frame, and maybe
163 // other nastiness).
164 if (frame->GetWebFrame()->top() != frame->GetWebFrame())
165 continue;
166
[email protected]c1e5fb12013-11-13 03:40:31167 v8::Local<v8::Context> context =
rdevlin.cronin6f42c2522015-06-19 18:58:51168 frame->GetWebFrame()->mainWorldScriptContext();
[email protected]c1e5fb12013-11-13 03:40:31169 if (!context.IsEmpty()) {
170 v8::Local<v8::Value> window = context->Global();
171 DCHECK(!window.IsEmpty());
[email protected]fcb705702013-12-20 12:29:17172 v8_views->Set(v8::Integer::New(args.GetIsolate(), v8_index++), window);
[email protected]c1e5fb12013-11-13 03:40:31173 }
174 }
175
176 args.GetReturnValue().Set(v8_views);
177}
178
[email protected]bcdd992f2013-06-09 12:58:03179} // namespace extensions