blob: a6131e7b7787b69500e451690a32592cc5f9aba2 [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"
rob248d6a82014-11-21 02:04:4810#include "content/public/renderer/render_frame.h"
[email protected]481a87e62012-12-19 23:00:2711#include "content/public/renderer/render_view.h"
[email protected]120028b9c2012-07-03 01:32:2412#include "content/public/renderer/v8_value_converter.h"
[email protected]e4452d32013-11-15 23:07:4113#include "extensions/common/extension.h"
[email protected]fb820c02014-03-13 15:07:0814#include "extensions/common/extension_messages.h"
[email protected]d42c11152013-08-22 19:36:3215#include "extensions/common/features/feature.h"
[email protected]ad8b4ba2013-08-09 19:52:4416#include "extensions/common/features/feature_provider.h"
[email protected]d42c11152013-08-22 19:36:3217#include "extensions/common/manifest.h"
[email protected]e6893672014-05-01 17:29:1318#include "extensions/renderer/api_activity_logger.h"
[email protected]b6cd4722014-05-01 22:04:0619#include "extensions/renderer/extension_helper.h"
[email protected]bcd9580f2014-04-17 19:17:5920#include "extensions/renderer/script_context.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]bcd9580f2014-04-17 19:17:5929RuntimeCustomBindings::RuntimeCustomBindings(ScriptContext* context)
30 : ObjectBackedNativeHandler(context) {
[email protected]e6893672014-05-01 17:29:1331 RouteFunction(
32 "GetManifest",
33 base::Bind(&RuntimeCustomBindings::GetManifest, base::Unretained(this)));
[email protected]4f1633f2013-03-09 14:26:2434 RouteFunction("OpenChannelToExtension",
35 base::Bind(&RuntimeCustomBindings::OpenChannelToExtension,
36 base::Unretained(this)));
[email protected]78b75182013-03-06 03:41:3237 RouteFunction("OpenChannelToNativeApp",
38 base::Bind(&RuntimeCustomBindings::OpenChannelToNativeApp,
39 base::Unretained(this)));
[email protected]c1e5fb12013-11-13 03:40:3140 RouteFunction("GetExtensionViews",
41 base::Bind(&RuntimeCustomBindings::GetExtensionViews,
42 base::Unretained(this)));
[email protected]120028b9c2012-07-03 01:32:2443}
44
[email protected]e6893672014-05-01 17:29:1345RuntimeCustomBindings::~RuntimeCustomBindings() {
46}
[email protected]120028b9c2012-07-03 01:32:2447
[email protected]d8c5fbb2013-06-14 11:35:2548void RuntimeCustomBindings::OpenChannelToExtension(
49 const v8::FunctionCallbackInfo<v8::Value>& args) {
rob248d6a82014-11-21 02:04:4850 // Get the current RenderFrame so that we can send a routed IPC message from
[email protected]481a87e62012-12-19 23:00:2751 // the correct source.
rob248d6a82014-11-21 02:04:4852 content::RenderFrame* renderframe = context()->GetRenderFrame();
53 if (!renderframe)
[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
dcarneya261b772014-11-20 17:55:0768 info.target_id = *v8::String::Utf8Value(args[0]);
[email protected]83d26be2013-08-15 04:55:2769 info.source_url = context()->GetURL();
dcarneya261b772014-11-20 17:55:0770 std::string channel_name = *v8::String::Utf8Value(args[1]);
[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;
rob248d6a82014-11-21 02:04:4874 renderframe->Send(new ExtensionHostMsg_OpenChannelToExtension(
75 renderframe->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]bcd9580f2014-04-17 19:17:5984 FeatureProvider::GetPermissionFeatures()
85 ->GetFeature("nativeMessaging")
86 ->IsAvailableToContext(context()->extension(),
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]bcd9580f2014-04-17 19:17:5994 content::RenderView* renderview = context()->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]e6893672014-05-01 17:29:1399 CHECK(args.Length() >= 2 && args[0]->IsString() && args[1]->IsString());
[email protected]481a87e62012-12-19 23:00:27100
dcarneya261b772014-11-20 17:55:07101 std::string extension_id = *v8::String::Utf8Value(args[0]);
102 std::string native_app_name = *v8::String::Utf8Value(args[1]);
[email protected]481a87e62012-12-19 23:00:27103
104 int port_id = -1;
105 renderview->Send(new ExtensionHostMsg_OpenChannelToNativeApp(
[email protected]e6893672014-05-01 17:29:13106 renderview->GetRoutingID(), extension_id, native_app_name, &port_id));
[email protected]d8c5fbb2013-06-14 11:35:25107 args.GetReturnValue().Set(static_cast<int32_t>(port_id));
[email protected]481a87e62012-12-19 23:00:27108}
109
[email protected]d8c5fbb2013-06-14 11:35:25110void RuntimeCustomBindings::GetManifest(
111 const v8::FunctionCallbackInfo<v8::Value>& args) {
[email protected]9a598442013-06-04 16:39:12112 CHECK(context()->extension());
[email protected]120028b9c2012-07-03 01:32:24113
114 scoped_ptr<V8ValueConverter> converter(V8ValueConverter::create());
[email protected]e6893672014-05-01 17:29:13115 args.GetReturnValue().Set(converter->ToV8Value(
116 context()->extension()->manifest()->value(), 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
dcarneya261b772014-11-20 17:55:07131 std::string view_type_string = *v8::String::Utf8Value(args[1]);
[email protected]df807042014-08-13 16:48:41132 StringToUpperASCII(&view_type_string);
[email protected]c1e5fb12013-11-13 03:40:31133 // |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;
[email protected]c1e5fb12013-11-13 03:40:31140 } else if (view_type_string == kViewTypeTabContents) {
141 view_type = VIEW_TYPE_TAB_CONTENTS;
142 } else if (view_type_string == kViewTypePopup) {
143 view_type = VIEW_TYPE_EXTENSION_POPUP;
144 } else if (view_type_string == kViewTypeExtensionDialog) {
145 view_type = VIEW_TYPE_EXTENSION_DIALOG;
[email protected]ddcf5802014-02-20 23:21:32146 } else if (view_type_string == kViewTypeAppWindow) {
147 view_type = VIEW_TYPE_APP_WINDOW;
[email protected]9faae962014-08-11 07:59:19148 } else if (view_type_string == kViewTypeLauncherPage) {
149 view_type = VIEW_TYPE_LAUNCHER_PAGE;
[email protected]c1e5fb12013-11-13 03:40:31150 } 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);
[email protected]95c6b3012013-12-02 14:30:31162 v8::Local<v8::Array> v8_views = v8::Array::New(args.GetIsolate());
[email protected]c1e5fb12013-11-13 03:40:31163 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());
[email protected]fcb705702013-12-20 12:29:17170 v8_views->Set(v8::Integer::New(args.GetIsolate(), v8_index++), window);
[email protected]c1e5fb12013-11-13 03:40:31171 }
172 }
173
174 args.GetReturnValue().Set(v8_views);
175}
176
[email protected]bcdd992f2013-06-09 12:58:03177} // namespace extensions