blob: 6e10e104c1abcb00a3b5249747198bf656ff928a [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]);
[email protected]83d26be2013-08-15 04:55:2768 info.source_url = context()->GetURL();
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(),
86 context()->context_type(),
87 context()->GetURL());
[email protected]ecc854a2013-08-22 10:12:4288 if (!availability.is_available())
[email protected]d8c5fbb2013-06-14 11:35:2589 return;
[email protected]78b75182013-03-06 03:41:3290
rdevlin.cronin25fcbdd62015-07-07 14:11:5991 content::RenderFrame* render_frame = context()->GetRenderFrame();
92 if (!render_frame)
[email protected]d8c5fbb2013-06-14 11:35:2593 return;
[email protected]481a87e62012-12-19 23:00:2794
95 // The Javascript code should validate/fill the arguments.
[email protected]e6893672014-05-01 17:29:1396 CHECK(args.Length() >= 2 && args[0]->IsString() && args[1]->IsString());
[email protected]481a87e62012-12-19 23:00:2797
dcarneya261b772014-11-20 17:55:0798 std::string extension_id = *v8::String::Utf8Value(args[0]);
99 std::string native_app_name = *v8::String::Utf8Value(args[1]);
[email protected]481a87e62012-12-19 23:00:27100
101 int port_id = -1;
rdevlin.cronin25fcbdd62015-07-07 14:11:59102 render_frame->Send(new ExtensionHostMsg_OpenChannelToNativeApp(
103 render_frame->GetRoutingID(), extension_id, native_app_name, &port_id));
[email protected]d8c5fbb2013-06-14 11:35:25104 args.GetReturnValue().Set(static_cast<int32_t>(port_id));
[email protected]481a87e62012-12-19 23:00:27105}
106
[email protected]d8c5fbb2013-06-14 11:35:25107void RuntimeCustomBindings::GetManifest(
108 const v8::FunctionCallbackInfo<v8::Value>& args) {
[email protected]9a598442013-06-04 16:39:12109 CHECK(context()->extension());
[email protected]120028b9c2012-07-03 01:32:24110
111 scoped_ptr<V8ValueConverter> converter(V8ValueConverter::create());
[email protected]e6893672014-05-01 17:29:13112 args.GetReturnValue().Set(converter->ToV8Value(
113 context()->extension()->manifest()->value(), context()->v8_context()));
[email protected]120028b9c2012-07-03 01:32:24114}
115
[email protected]c1e5fb12013-11-13 03:40:31116void RuntimeCustomBindings::GetExtensionViews(
117 const v8::FunctionCallbackInfo<v8::Value>& args) {
118 if (args.Length() != 2)
119 return;
120
121 if (!args[0]->IsInt32() || !args[1]->IsString())
122 return;
123
124 // |browser_window_id| == extension_misc::kUnknownWindowId means getting
125 // all views for the current extension.
126 int browser_window_id = args[0]->Int32Value();
127
brettwc15100c2015-08-06 22:54:16128 std::string view_type_string =
129 base::ToUpperASCII(*v8::String::Utf8Value(args[1]));
[email protected]c1e5fb12013-11-13 03:40:31130 // |view_type| == VIEW_TYPE_INVALID means getting any type of
131 // views.
132 ViewType view_type = VIEW_TYPE_INVALID;
133 if (view_type_string == kViewTypeBackgroundPage) {
134 view_type = VIEW_TYPE_EXTENSION_BACKGROUND_PAGE;
[email protected]c1e5fb12013-11-13 03:40:31135 } else if (view_type_string == kViewTypeTabContents) {
136 view_type = VIEW_TYPE_TAB_CONTENTS;
137 } else if (view_type_string == kViewTypePopup) {
138 view_type = VIEW_TYPE_EXTENSION_POPUP;
139 } else if (view_type_string == kViewTypeExtensionDialog) {
140 view_type = VIEW_TYPE_EXTENSION_DIALOG;
[email protected]ddcf5802014-02-20 23:21:32141 } else if (view_type_string == kViewTypeAppWindow) {
142 view_type = VIEW_TYPE_APP_WINDOW;
[email protected]9faae962014-08-11 07:59:19143 } else if (view_type_string == kViewTypeLauncherPage) {
144 view_type = VIEW_TYPE_LAUNCHER_PAGE;
[email protected]c1e5fb12013-11-13 03:40:31145 } else if (view_type_string == kViewTypePanel) {
146 view_type = VIEW_TYPE_PANEL;
147 } else if (view_type_string != kViewTypeAll) {
148 return;
149 }
150
151 std::string extension_id = context()->GetExtensionID();
152 if (extension_id.empty())
153 return;
154
rdevlin.cronin6f42c2522015-06-19 18:58:51155 std::vector<content::RenderFrame*> frames =
156 ExtensionFrameHelper::GetExtensionFrames(extension_id, browser_window_id,
157 view_type);
[email protected]95c6b3012013-12-02 14:30:31158 v8::Local<v8::Array> v8_views = v8::Array::New(args.GetIsolate());
[email protected]c1e5fb12013-11-13 03:40:31159 int v8_index = 0;
rdevlin.cronin6f42c2522015-06-19 18:58:51160 for (content::RenderFrame* frame : frames) {
rdevlin.cronin9e9f59a2015-06-24 23:49:07161 // We filter out iframes here. GetExtensionViews should only return the
162 // main views, not any subframes. (Returning subframes can cause broken
163 // behavior by treating an app window's iframe as its main frame, and maybe
164 // other nastiness).
165 if (frame->GetWebFrame()->top() != frame->GetWebFrame())
166 continue;
167
[email protected]c1e5fb12013-11-13 03:40:31168 v8::Local<v8::Context> context =
rdevlin.cronin6f42c2522015-06-19 18:58:51169 frame->GetWebFrame()->mainWorldScriptContext();
[email protected]c1e5fb12013-11-13 03:40:31170 if (!context.IsEmpty()) {
171 v8::Local<v8::Value> window = context->Global();
172 DCHECK(!window.IsEmpty());
[email protected]fcb705702013-12-20 12:29:17173 v8_views->Set(v8::Integer::New(args.GetIsolate(), v8_index++), window);
[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