blob: ac244b15a947e06744ebf986de45169c1a54a43d [file] [log] [blame]
[email protected]120028b9c2012-07-03 01:32:241// Copyright (c) 2012 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "chrome/renderer/extensions/runtime_custom_bindings.h"
6
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 "chrome/common/extensions/extension_messages.h"
[email protected]b6708d032013-07-14 08:18:2211#include "chrome/renderer/extensions/api_activity_logger.h"
[email protected]120028b9c2012-07-03 01:32:2412#include "chrome/renderer/extensions/chrome_v8_context.h"
[email protected]78b75182013-03-06 03:41:3213#include "chrome/renderer/extensions/dispatcher.h"
[email protected]c1e5fb12013-11-13 03:40:3114#include "chrome/renderer/extensions/extension_helper.h"
[email protected]481a87e62012-12-19 23:00:2715#include "content/public/renderer/render_view.h"
[email protected]120028b9c2012-07-03 01:32:2416#include "content/public/renderer/v8_value_converter.h"
[email protected]e4452d32013-11-15 23:07:4117#include "extensions/common/extension.h"
[email protected]d42c11152013-08-22 19:36:3218#include "extensions/common/features/feature.h"
[email protected]ad8b4ba2013-08-09 19:52:4419#include "extensions/common/features/feature_provider.h"
[email protected]d42c11152013-08-22 19:36:3220#include "extensions/common/manifest.h"
[email protected]2255a9332013-06-17 05:12:3121#include "third_party/WebKit/public/web/WebDocument.h"
22#include "third_party/WebKit/public/web/WebFrame.h"
23#include "third_party/WebKit/public/web/WebView.h"
[email protected]120028b9c2012-07-03 01:32:2424
25using content::V8ValueConverter;
26
27namespace extensions {
28
[email protected]78b75182013-03-06 03:41:3229RuntimeCustomBindings::RuntimeCustomBindings(Dispatcher* dispatcher,
30 ChromeV8Context* context)
[email protected]9a598442013-06-04 16:39:1231 : ChromeV8Extension(dispatcher, context) {
[email protected]120028b9c2012-07-03 01:32:2432 RouteFunction("GetManifest",
[email protected]78b75182013-03-06 03:41:3233 base::Bind(&RuntimeCustomBindings::GetManifest,
34 base::Unretained(this)));
[email protected]4f1633f2013-03-09 14:26:2435 RouteFunction("OpenChannelToExtension",
36 base::Bind(&RuntimeCustomBindings::OpenChannelToExtension,
37 base::Unretained(this)));
[email protected]78b75182013-03-06 03:41:3238 RouteFunction("OpenChannelToNativeApp",
39 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
46RuntimeCustomBindings::~RuntimeCustomBindings() {}
47
[email protected]d8c5fbb2013-06-14 11:35:2548void RuntimeCustomBindings::OpenChannelToExtension(
49 const v8::FunctionCallbackInfo<v8::Value>& args) {
[email protected]481a87e62012-12-19 23:00:2750 // Get the current RenderView so that we can send a routed IPC message from
51 // the correct source.
[email protected]4f1633f2013-03-09 14:26:2452 content::RenderView* renderview = GetRenderView();
[email protected]481a87e62012-12-19 23:00:2753 if (!renderview)
[email protected]d8c5fbb2013-06-14 11:35:2554 return;
[email protected]481a87e62012-12-19 23:00:2755
56 // The Javascript code should validate/fill the arguments.
[email protected]8ad95b72013-10-16 02:54:1157 CHECK_EQ(args.Length(), 3);
58 CHECK(args[0]->IsString() && args[1]->IsString() && args[2]->IsBoolean());
[email protected]481a87e62012-12-19 23:00:2759
[email protected]686914f2013-04-25 04:54:5860 ExtensionMsg_ExternalConnectionInfo info;
[email protected]9b6a6332014-01-09 19:58:4361
62 // For messaging APIs, hosted apps should be considered a web page so hide
63 // its extension ID.
64 const Extension* extension = context()->extension();
65 if (extension && !extension->is_hosted_app())
66 info.source_id = extension->id();
67
[email protected]bcdd992f2013-06-09 12:58:0368 info.target_id = *v8::String::Utf8Value(args[0]->ToString());
[email protected]83d26be2013-08-15 04:55:2769 info.source_url = context()->GetURL();
[email protected]bcdd992f2013-06-09 12:58:0370 std::string channel_name = *v8::String::Utf8Value(args[1]->ToString());
[email protected]8ad95b72013-10-16 02:54:1171 bool include_tls_channel_id =
72 args.Length() > 2 ? args[2]->BooleanValue() : false;
[email protected]481a87e62012-12-19 23:00:2773 int port_id = -1;
74 renderview->Send(new ExtensionHostMsg_OpenChannelToExtension(
[email protected]8ad95b72013-10-16 02:54:1175 renderview->GetRoutingID(), info, channel_name, include_tls_channel_id,
76 &port_id));
[email protected]d8c5fbb2013-06-14 11:35:2577 args.GetReturnValue().Set(static_cast<int32_t>(port_id));
[email protected]481a87e62012-12-19 23:00:2778}
79
[email protected]d8c5fbb2013-06-14 11:35:2580void RuntimeCustomBindings::OpenChannelToNativeApp(
81 const v8::FunctionCallbackInfo<v8::Value>& args) {
[email protected]78b75182013-03-06 03:41:3282 // Verify that the extension has permission to use native messaging.
[email protected]b6708d032013-07-14 08:18:2283 Feature::Availability availability =
[email protected]276f05aa2013-10-07 20:44:4184 FeatureProvider::GetPermissionFeatures()->
[email protected]b6708d032013-07-14 08:18:2285 GetFeature("nativeMessaging")->IsAvailableToContext(
86 GetExtensionForRenderView(),
87 context()->context_type(),
88 context()->GetURL());
[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
[email protected]481a87e62012-12-19 23:00:2792 // Get the current RenderView so that we can send a routed IPC message from
93 // the correct source.
[email protected]4f1633f2013-03-09 14:26:2494 content::RenderView* renderview = GetRenderView();
[email protected]481a87e62012-12-19 23:00:2795 if (!renderview)
[email protected]d8c5fbb2013-06-14 11:35:2596 return;
[email protected]481a87e62012-12-19 23:00:2797
98 // The Javascript code should validate/fill the arguments.
[email protected]0c6417072013-01-23 00:20:5999 CHECK(args.Length() >= 2 &&
[email protected]481a87e62012-12-19 23:00:27100 args[0]->IsString() &&
[email protected]0c6417072013-01-23 00:20:59101 args[1]->IsString());
[email protected]481a87e62012-12-19 23:00:27102
103 std::string extension_id = *v8::String::Utf8Value(args[0]->ToString());
104 std::string native_app_name = *v8::String::Utf8Value(args[1]->ToString());
[email protected]481a87e62012-12-19 23:00:27105
106 int port_id = -1;
107 renderview->Send(new ExtensionHostMsg_OpenChannelToNativeApp(
108 renderview->GetRoutingID(),
109 extension_id,
110 native_app_name,
[email protected]481a87e62012-12-19 23:00:27111 &port_id));
[email protected]d8c5fbb2013-06-14 11:35:25112 args.GetReturnValue().Set(static_cast<int32_t>(port_id));
[email protected]481a87e62012-12-19 23:00:27113}
114
[email protected]d8c5fbb2013-06-14 11:35:25115void RuntimeCustomBindings::GetManifest(
116 const v8::FunctionCallbackInfo<v8::Value>& args) {
[email protected]9a598442013-06-04 16:39:12117 CHECK(context()->extension());
[email protected]120028b9c2012-07-03 01:32:24118
119 scoped_ptr<V8ValueConverter> converter(V8ValueConverter::create());
[email protected]d8c5fbb2013-06-14 11:35:25120 args.GetReturnValue().Set(
121 converter->ToV8Value(context()->extension()->manifest()->value(),
122 context()->v8_context()));
[email protected]120028b9c2012-07-03 01:32:24123}
124
[email protected]c1e5fb12013-11-13 03:40:31125void RuntimeCustomBindings::GetExtensionViews(
126 const v8::FunctionCallbackInfo<v8::Value>& args) {
127 if (args.Length() != 2)
128 return;
129
130 if (!args[0]->IsInt32() || !args[1]->IsString())
131 return;
132
133 // |browser_window_id| == extension_misc::kUnknownWindowId means getting
134 // all views for the current extension.
135 int browser_window_id = args[0]->Int32Value();
136
137 std::string view_type_string = *v8::String::Utf8Value(args[1]->ToString());
138 StringToUpperASCII(&view_type_string);
139 // |view_type| == VIEW_TYPE_INVALID means getting any type of
140 // views.
141 ViewType view_type = VIEW_TYPE_INVALID;
142 if (view_type_string == kViewTypeBackgroundPage) {
143 view_type = VIEW_TYPE_EXTENSION_BACKGROUND_PAGE;
144 } else if (view_type_string == kViewTypeInfobar) {
145 view_type = VIEW_TYPE_EXTENSION_INFOBAR;
146 } else if (view_type_string == kViewTypeNotification) {
147 view_type = VIEW_TYPE_NOTIFICATION;
148 } else if (view_type_string == kViewTypeTabContents) {
149 view_type = VIEW_TYPE_TAB_CONTENTS;
150 } else if (view_type_string == kViewTypePopup) {
151 view_type = VIEW_TYPE_EXTENSION_POPUP;
152 } else if (view_type_string == kViewTypeExtensionDialog) {
153 view_type = VIEW_TYPE_EXTENSION_DIALOG;
154 } else if (view_type_string == kViewTypeAppShell) {
155 view_type = VIEW_TYPE_APP_SHELL;
156 } else if (view_type_string == kViewTypePanel) {
157 view_type = VIEW_TYPE_PANEL;
158 } else if (view_type_string != kViewTypeAll) {
159 return;
160 }
161
162 std::string extension_id = context()->GetExtensionID();
163 if (extension_id.empty())
164 return;
165
166 std::vector<content::RenderView*> views = ExtensionHelper::GetExtensionViews(
167 extension_id, browser_window_id, view_type);
[email protected]95c6b3012013-12-02 14:30:31168 v8::Local<v8::Array> v8_views = v8::Array::New(args.GetIsolate());
[email protected]c1e5fb12013-11-13 03:40:31169 int v8_index = 0;
170 for (size_t i = 0; i < views.size(); ++i) {
171 v8::Local<v8::Context> context =
172 views[i]->GetWebView()->mainFrame()->mainWorldScriptContext();
173 if (!context.IsEmpty()) {
174 v8::Local<v8::Value> window = context->Global();
175 DCHECK(!window.IsEmpty());
[email protected]fcb705702013-12-20 12:29:17176 v8_views->Set(v8::Integer::New(args.GetIsolate(), v8_index++), window);
[email protected]c1e5fb12013-11-13 03:40:31177 }
178 }
179
180 args.GetReturnValue().Set(v8_views);
181}
182
[email protected]bcdd992f2013-06-09 12:58:03183} // namespace extensions