blob: 5a0915e548690c0592534802044d4f56c9d1472a [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]83d26be2013-08-15 04:55:2761 info.source_id = context()->GetExtensionID();
[email protected]bcdd992f2013-06-09 12:58:0362 info.target_id = *v8::String::Utf8Value(args[0]->ToString());
[email protected]83d26be2013-08-15 04:55:2763 info.source_url = context()->GetURL();
[email protected]bcdd992f2013-06-09 12:58:0364 std::string channel_name = *v8::String::Utf8Value(args[1]->ToString());
[email protected]8ad95b72013-10-16 02:54:1165 bool include_tls_channel_id =
66 args.Length() > 2 ? args[2]->BooleanValue() : false;
[email protected]481a87e62012-12-19 23:00:2767 int port_id = -1;
68 renderview->Send(new ExtensionHostMsg_OpenChannelToExtension(
[email protected]8ad95b72013-10-16 02:54:1169 renderview->GetRoutingID(), info, channel_name, include_tls_channel_id,
70 &port_id));
[email protected]d8c5fbb2013-06-14 11:35:2571 args.GetReturnValue().Set(static_cast<int32_t>(port_id));
[email protected]481a87e62012-12-19 23:00:2772}
73
[email protected]d8c5fbb2013-06-14 11:35:2574void RuntimeCustomBindings::OpenChannelToNativeApp(
75 const v8::FunctionCallbackInfo<v8::Value>& args) {
[email protected]78b75182013-03-06 03:41:3276 // Verify that the extension has permission to use native messaging.
[email protected]b6708d032013-07-14 08:18:2277 Feature::Availability availability =
[email protected]276f05aa2013-10-07 20:44:4178 FeatureProvider::GetPermissionFeatures()->
[email protected]b6708d032013-07-14 08:18:2279 GetFeature("nativeMessaging")->IsAvailableToContext(
80 GetExtensionForRenderView(),
81 context()->context_type(),
82 context()->GetURL());
[email protected]ecc854a2013-08-22 10:12:4283 if (!availability.is_available())
[email protected]d8c5fbb2013-06-14 11:35:2584 return;
[email protected]78b75182013-03-06 03:41:3285
[email protected]481a87e62012-12-19 23:00:2786 // Get the current RenderView so that we can send a routed IPC message from
87 // the correct source.
[email protected]4f1633f2013-03-09 14:26:2488 content::RenderView* renderview = GetRenderView();
[email protected]481a87e62012-12-19 23:00:2789 if (!renderview)
[email protected]d8c5fbb2013-06-14 11:35:2590 return;
[email protected]481a87e62012-12-19 23:00:2791
92 // The Javascript code should validate/fill the arguments.
[email protected]0c6417072013-01-23 00:20:5993 CHECK(args.Length() >= 2 &&
[email protected]481a87e62012-12-19 23:00:2794 args[0]->IsString() &&
[email protected]0c6417072013-01-23 00:20:5995 args[1]->IsString());
[email protected]481a87e62012-12-19 23:00:2796
97 std::string extension_id = *v8::String::Utf8Value(args[0]->ToString());
98 std::string native_app_name = *v8::String::Utf8Value(args[1]->ToString());
[email protected]481a87e62012-12-19 23:00:2799
100 int port_id = -1;
101 renderview->Send(new ExtensionHostMsg_OpenChannelToNativeApp(
102 renderview->GetRoutingID(),
103 extension_id,
104 native_app_name,
[email protected]481a87e62012-12-19 23:00:27105 &port_id));
[email protected]d8c5fbb2013-06-14 11:35:25106 args.GetReturnValue().Set(static_cast<int32_t>(port_id));
[email protected]481a87e62012-12-19 23:00:27107}
108
[email protected]d8c5fbb2013-06-14 11:35:25109void RuntimeCustomBindings::GetManifest(
110 const v8::FunctionCallbackInfo<v8::Value>& args) {
[email protected]9a598442013-06-04 16:39:12111 CHECK(context()->extension());
[email protected]120028b9c2012-07-03 01:32:24112
113 scoped_ptr<V8ValueConverter> converter(V8ValueConverter::create());
[email protected]d8c5fbb2013-06-14 11:35:25114 args.GetReturnValue().Set(
115 converter->ToV8Value(context()->extension()->manifest()->value(),
116 context()->v8_context()));
[email protected]120028b9c2012-07-03 01:32:24117}
118
[email protected]c1e5fb12013-11-13 03:40:31119void RuntimeCustomBindings::GetExtensionViews(
120 const v8::FunctionCallbackInfo<v8::Value>& args) {
121 if (args.Length() != 2)
122 return;
123
124 if (!args[0]->IsInt32() || !args[1]->IsString())
125 return;
126
127 // |browser_window_id| == extension_misc::kUnknownWindowId means getting
128 // all views for the current extension.
129 int browser_window_id = args[0]->Int32Value();
130
131 std::string view_type_string = *v8::String::Utf8Value(args[1]->ToString());
132 StringToUpperASCII(&view_type_string);
133 // |view_type| == VIEW_TYPE_INVALID means getting any type of
134 // views.
135 ViewType view_type = VIEW_TYPE_INVALID;
136 if (view_type_string == kViewTypeBackgroundPage) {
137 view_type = VIEW_TYPE_EXTENSION_BACKGROUND_PAGE;
138 } else if (view_type_string == kViewTypeInfobar) {
139 view_type = VIEW_TYPE_EXTENSION_INFOBAR;
140 } else if (view_type_string == kViewTypeNotification) {
141 view_type = VIEW_TYPE_NOTIFICATION;
142 } 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;
148 } else if (view_type_string == kViewTypeAppShell) {
149 view_type = VIEW_TYPE_APP_SHELL;
150 } else if (view_type_string == kViewTypePanel) {
151 view_type = VIEW_TYPE_PANEL;
152 } else if (view_type_string != kViewTypeAll) {
153 return;
154 }
155
156 std::string extension_id = context()->GetExtensionID();
157 if (extension_id.empty())
158 return;
159
160 std::vector<content::RenderView*> views = ExtensionHelper::GetExtensionViews(
161 extension_id, browser_window_id, view_type);
162 v8::Local<v8::Array> v8_views = v8::Array::New();
163 int v8_index = 0;
164 for (size_t i = 0; i < views.size(); ++i) {
165 v8::Local<v8::Context> context =
166 views[i]->GetWebView()->mainFrame()->mainWorldScriptContext();
167 if (!context.IsEmpty()) {
168 v8::Local<v8::Value> window = context->Global();
169 DCHECK(!window.IsEmpty());
170 v8_views->Set(v8::Integer::New(v8_index++), window);
171 }
172 }
173
174 args.GetReturnValue().Set(v8_views);
175}
176
[email protected]bcdd992f2013-06-09 12:58:03177} // namespace extensions