blob: 3d1d3c24dd85a88c86a07a45b6c525a6ffa635d1 [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"
[email protected]481a87e62012-12-19 23:00:2710#include "content/public/renderer/render_view.h"
[email protected]120028b9c2012-07-03 01:32:2411#include "content/public/renderer/v8_value_converter.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"
[email protected]b6cd4722014-05-01 22:04:0618#include "extensions/renderer/extension_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"
21#include "third_party/WebKit/public/web/WebFrame.h"
22#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) {
[email protected]481a87e62012-12-19 23:00:2749 // Get the current RenderView so that we can send a routed IPC message from
50 // the correct source.
[email protected]bcd9580f2014-04-17 19:17:5951 content::RenderView* renderview = context()->GetRenderView();
[email protected]481a87e62012-12-19 23:00:2752 if (!renderview)
[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;
[email protected]e6893672014-05-01 17:29:1373 renderview->Send(
74 new ExtensionHostMsg_OpenChannelToExtension(renderview->GetRoutingID(),
75 info,
76 channel_name,
77 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(),
89 context()->context_type(),
90 context()->GetURL());
[email protected]ecc854a2013-08-22 10:12:4291 if (!availability.is_available())
[email protected]d8c5fbb2013-06-14 11:35:2592 return;
[email protected]78b75182013-03-06 03:41:3293
[email protected]481a87e62012-12-19 23:00:2794 // Get the current RenderView so that we can send a routed IPC message from
95 // the correct source.
[email protected]bcd9580f2014-04-17 19:17:5996 content::RenderView* renderview = context()->GetRenderView();
[email protected]481a87e62012-12-19 23:00:2797 if (!renderview)
[email protected]d8c5fbb2013-06-14 11:35:2598 return;
[email protected]481a87e62012-12-19 23:00:2799
100 // The Javascript code should validate/fill the arguments.
[email protected]e6893672014-05-01 17:29:13101 CHECK(args.Length() >= 2 && args[0]->IsString() && args[1]->IsString());
[email protected]481a87e62012-12-19 23:00:27102
dcarneya261b772014-11-20 17:55:07103 std::string extension_id = *v8::String::Utf8Value(args[0]);
104 std::string native_app_name = *v8::String::Utf8Value(args[1]);
[email protected]481a87e62012-12-19 23:00:27105
106 int port_id = -1;
107 renderview->Send(new ExtensionHostMsg_OpenChannelToNativeApp(
[email protected]e6893672014-05-01 17:29:13108 renderview->GetRoutingID(), extension_id, native_app_name, &port_id));
[email protected]d8c5fbb2013-06-14 11:35:25109 args.GetReturnValue().Set(static_cast<int32_t>(port_id));
[email protected]481a87e62012-12-19 23:00:27110}
111
[email protected]d8c5fbb2013-06-14 11:35:25112void RuntimeCustomBindings::GetManifest(
113 const v8::FunctionCallbackInfo<v8::Value>& args) {
[email protected]9a598442013-06-04 16:39:12114 CHECK(context()->extension());
[email protected]120028b9c2012-07-03 01:32:24115
116 scoped_ptr<V8ValueConverter> converter(V8ValueConverter::create());
[email protected]e6893672014-05-01 17:29:13117 args.GetReturnValue().Set(converter->ToV8Value(
118 context()->extension()->manifest()->value(), context()->v8_context()));
[email protected]120028b9c2012-07-03 01:32:24119}
120
[email protected]c1e5fb12013-11-13 03:40:31121void RuntimeCustomBindings::GetExtensionViews(
122 const v8::FunctionCallbackInfo<v8::Value>& args) {
123 if (args.Length() != 2)
124 return;
125
126 if (!args[0]->IsInt32() || !args[1]->IsString())
127 return;
128
129 // |browser_window_id| == extension_misc::kUnknownWindowId means getting
130 // all views for the current extension.
131 int browser_window_id = args[0]->Int32Value();
132
dcarneya261b772014-11-20 17:55:07133 std::string view_type_string = *v8::String::Utf8Value(args[1]);
[email protected]df807042014-08-13 16:48:41134 StringToUpperASCII(&view_type_string);
[email protected]c1e5fb12013-11-13 03:40:31135 // |view_type| == VIEW_TYPE_INVALID means getting any type of
136 // views.
137 ViewType view_type = VIEW_TYPE_INVALID;
138 if (view_type_string == kViewTypeBackgroundPage) {
139 view_type = VIEW_TYPE_EXTENSION_BACKGROUND_PAGE;
140 } else if (view_type_string == kViewTypeInfobar) {
141 view_type = VIEW_TYPE_EXTENSION_INFOBAR;
[email protected]c1e5fb12013-11-13 03:40:31142 } else if (view_type_string == kViewTypeTabContents) {
143 view_type = VIEW_TYPE_TAB_CONTENTS;
144 } else if (view_type_string == kViewTypePopup) {
145 view_type = VIEW_TYPE_EXTENSION_POPUP;
146 } else if (view_type_string == kViewTypeExtensionDialog) {
147 view_type = VIEW_TYPE_EXTENSION_DIALOG;
[email protected]ddcf5802014-02-20 23:21:32148 } else if (view_type_string == kViewTypeAppWindow) {
149 view_type = VIEW_TYPE_APP_WINDOW;
[email protected]9faae962014-08-11 07:59:19150 } else if (view_type_string == kViewTypeLauncherPage) {
151 view_type = VIEW_TYPE_LAUNCHER_PAGE;
[email protected]c1e5fb12013-11-13 03:40:31152 } else if (view_type_string == kViewTypePanel) {
153 view_type = VIEW_TYPE_PANEL;
154 } else if (view_type_string != kViewTypeAll) {
155 return;
156 }
157
158 std::string extension_id = context()->GetExtensionID();
159 if (extension_id.empty())
160 return;
161
162 std::vector<content::RenderView*> views = ExtensionHelper::GetExtensionViews(
163 extension_id, browser_window_id, view_type);
[email protected]95c6b3012013-12-02 14:30:31164 v8::Local<v8::Array> v8_views = v8::Array::New(args.GetIsolate());
[email protected]c1e5fb12013-11-13 03:40:31165 int v8_index = 0;
166 for (size_t i = 0; i < views.size(); ++i) {
167 v8::Local<v8::Context> context =
168 views[i]->GetWebView()->mainFrame()->mainWorldScriptContext();
169 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