[email protected] | e689367 | 2014-05-01 17:29:13 | [diff] [blame] | 1 | // Copyright 2014 The Chromium Authors. All rights reserved. |
[email protected] | 120028b9c | 2012-07-03 01:32:24 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | e689367 | 2014-05-01 17:29:13 | [diff] [blame] | 5 | #include "extensions/renderer/runtime_custom_bindings.h" |
[email protected] | 120028b9c | 2012-07-03 01:32:24 | [diff] [blame] | 6 | |
| 7 | #include "base/bind.h" |
| 8 | #include "base/memory/scoped_ptr.h" |
| 9 | #include "base/values.h" |
rob | 248d6a8 | 2014-11-21 02:04:48 | [diff] [blame^] | 10 | #include "content/public/renderer/render_frame.h" |
[email protected] | 481a87e6 | 2012-12-19 23:00:27 | [diff] [blame] | 11 | #include "content/public/renderer/render_view.h" |
[email protected] | 120028b9c | 2012-07-03 01:32:24 | [diff] [blame] | 12 | #include "content/public/renderer/v8_value_converter.h" |
[email protected] | e4452d3 | 2013-11-15 23:07:41 | [diff] [blame] | 13 | #include "extensions/common/extension.h" |
[email protected] | fb820c0 | 2014-03-13 15:07:08 | [diff] [blame] | 14 | #include "extensions/common/extension_messages.h" |
[email protected] | d42c1115 | 2013-08-22 19:36:32 | [diff] [blame] | 15 | #include "extensions/common/features/feature.h" |
[email protected] | ad8b4ba | 2013-08-09 19:52:44 | [diff] [blame] | 16 | #include "extensions/common/features/feature_provider.h" |
[email protected] | d42c1115 | 2013-08-22 19:36:32 | [diff] [blame] | 17 | #include "extensions/common/manifest.h" |
[email protected] | e689367 | 2014-05-01 17:29:13 | [diff] [blame] | 18 | #include "extensions/renderer/api_activity_logger.h" |
[email protected] | b6cd472 | 2014-05-01 22:04:06 | [diff] [blame] | 19 | #include "extensions/renderer/extension_helper.h" |
[email protected] | bcd9580f | 2014-04-17 19:17:59 | [diff] [blame] | 20 | #include "extensions/renderer/script_context.h" |
[email protected] | 2255a933 | 2013-06-17 05:12:31 | [diff] [blame] | 21 | #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] | 120028b9c | 2012-07-03 01:32:24 | [diff] [blame] | 24 | |
| 25 | using content::V8ValueConverter; |
| 26 | |
| 27 | namespace extensions { |
| 28 | |
[email protected] | bcd9580f | 2014-04-17 19:17:59 | [diff] [blame] | 29 | RuntimeCustomBindings::RuntimeCustomBindings(ScriptContext* context) |
| 30 | : ObjectBackedNativeHandler(context) { |
[email protected] | e689367 | 2014-05-01 17:29:13 | [diff] [blame] | 31 | RouteFunction( |
| 32 | "GetManifest", |
| 33 | base::Bind(&RuntimeCustomBindings::GetManifest, base::Unretained(this))); |
[email protected] | 4f1633f | 2013-03-09 14:26:24 | [diff] [blame] | 34 | RouteFunction("OpenChannelToExtension", |
| 35 | base::Bind(&RuntimeCustomBindings::OpenChannelToExtension, |
| 36 | base::Unretained(this))); |
[email protected] | 78b7518 | 2013-03-06 03:41:32 | [diff] [blame] | 37 | RouteFunction("OpenChannelToNativeApp", |
| 38 | base::Bind(&RuntimeCustomBindings::OpenChannelToNativeApp, |
| 39 | base::Unretained(this))); |
[email protected] | c1e5fb1 | 2013-11-13 03:40:31 | [diff] [blame] | 40 | RouteFunction("GetExtensionViews", |
| 41 | base::Bind(&RuntimeCustomBindings::GetExtensionViews, |
| 42 | base::Unretained(this))); |
[email protected] | 120028b9c | 2012-07-03 01:32:24 | [diff] [blame] | 43 | } |
| 44 | |
[email protected] | e689367 | 2014-05-01 17:29:13 | [diff] [blame] | 45 | RuntimeCustomBindings::~RuntimeCustomBindings() { |
| 46 | } |
[email protected] | 120028b9c | 2012-07-03 01:32:24 | [diff] [blame] | 47 | |
[email protected] | d8c5fbb | 2013-06-14 11:35:25 | [diff] [blame] | 48 | void RuntimeCustomBindings::OpenChannelToExtension( |
| 49 | const v8::FunctionCallbackInfo<v8::Value>& args) { |
rob | 248d6a8 | 2014-11-21 02:04:48 | [diff] [blame^] | 50 | // Get the current RenderFrame so that we can send a routed IPC message from |
[email protected] | 481a87e6 | 2012-12-19 23:00:27 | [diff] [blame] | 51 | // the correct source. |
rob | 248d6a8 | 2014-11-21 02:04:48 | [diff] [blame^] | 52 | content::RenderFrame* renderframe = context()->GetRenderFrame(); |
| 53 | if (!renderframe) |
[email protected] | d8c5fbb | 2013-06-14 11:35:25 | [diff] [blame] | 54 | return; |
[email protected] | 481a87e6 | 2012-12-19 23:00:27 | [diff] [blame] | 55 | |
| 56 | // The Javascript code should validate/fill the arguments. |
[email protected] | 8ad95b7 | 2013-10-16 02:54:11 | [diff] [blame] | 57 | CHECK_EQ(args.Length(), 3); |
| 58 | CHECK(args[0]->IsString() && args[1]->IsString() && args[2]->IsBoolean()); |
[email protected] | 481a87e6 | 2012-12-19 23:00:27 | [diff] [blame] | 59 | |
[email protected] | 686914f | 2013-04-25 04:54:58 | [diff] [blame] | 60 | ExtensionMsg_ExternalConnectionInfo info; |
[email protected] | 9b6a633 | 2014-01-09 19:58:43 | [diff] [blame] | 61 | |
| 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 | |
dcarney | a261b77 | 2014-11-20 17:55:07 | [diff] [blame] | 68 | info.target_id = *v8::String::Utf8Value(args[0]); |
[email protected] | 83d26be | 2013-08-15 04:55:27 | [diff] [blame] | 69 | info.source_url = context()->GetURL(); |
dcarney | a261b77 | 2014-11-20 17:55:07 | [diff] [blame] | 70 | std::string channel_name = *v8::String::Utf8Value(args[1]); |
[email protected] | 8ad95b7 | 2013-10-16 02:54:11 | [diff] [blame] | 71 | bool include_tls_channel_id = |
| 72 | args.Length() > 2 ? args[2]->BooleanValue() : false; |
[email protected] | 481a87e6 | 2012-12-19 23:00:27 | [diff] [blame] | 73 | int port_id = -1; |
rob | 248d6a8 | 2014-11-21 02:04:48 | [diff] [blame^] | 74 | renderframe->Send(new ExtensionHostMsg_OpenChannelToExtension( |
| 75 | renderframe->GetRoutingID(), info, channel_name, include_tls_channel_id, |
| 76 | &port_id)); |
[email protected] | d8c5fbb | 2013-06-14 11:35:25 | [diff] [blame] | 77 | args.GetReturnValue().Set(static_cast<int32_t>(port_id)); |
[email protected] | 481a87e6 | 2012-12-19 23:00:27 | [diff] [blame] | 78 | } |
| 79 | |
[email protected] | d8c5fbb | 2013-06-14 11:35:25 | [diff] [blame] | 80 | void RuntimeCustomBindings::OpenChannelToNativeApp( |
| 81 | const v8::FunctionCallbackInfo<v8::Value>& args) { |
[email protected] | 78b7518 | 2013-03-06 03:41:32 | [diff] [blame] | 82 | // Verify that the extension has permission to use native messaging. |
[email protected] | b6708d03 | 2013-07-14 08:18:22 | [diff] [blame] | 83 | Feature::Availability availability = |
[email protected] | bcd9580f | 2014-04-17 19:17:59 | [diff] [blame] | 84 | FeatureProvider::GetPermissionFeatures() |
| 85 | ->GetFeature("nativeMessaging") |
| 86 | ->IsAvailableToContext(context()->extension(), |
| 87 | context()->context_type(), |
| 88 | context()->GetURL()); |
[email protected] | ecc854a | 2013-08-22 10:12:42 | [diff] [blame] | 89 | if (!availability.is_available()) |
[email protected] | d8c5fbb | 2013-06-14 11:35:25 | [diff] [blame] | 90 | return; |
[email protected] | 78b7518 | 2013-03-06 03:41:32 | [diff] [blame] | 91 | |
[email protected] | 481a87e6 | 2012-12-19 23:00:27 | [diff] [blame] | 92 | // Get the current RenderView so that we can send a routed IPC message from |
| 93 | // the correct source. |
[email protected] | bcd9580f | 2014-04-17 19:17:59 | [diff] [blame] | 94 | content::RenderView* renderview = context()->GetRenderView(); |
[email protected] | 481a87e6 | 2012-12-19 23:00:27 | [diff] [blame] | 95 | if (!renderview) |
[email protected] | d8c5fbb | 2013-06-14 11:35:25 | [diff] [blame] | 96 | return; |
[email protected] | 481a87e6 | 2012-12-19 23:00:27 | [diff] [blame] | 97 | |
| 98 | // The Javascript code should validate/fill the arguments. |
[email protected] | e689367 | 2014-05-01 17:29:13 | [diff] [blame] | 99 | CHECK(args.Length() >= 2 && args[0]->IsString() && args[1]->IsString()); |
[email protected] | 481a87e6 | 2012-12-19 23:00:27 | [diff] [blame] | 100 | |
dcarney | a261b77 | 2014-11-20 17:55:07 | [diff] [blame] | 101 | std::string extension_id = *v8::String::Utf8Value(args[0]); |
| 102 | std::string native_app_name = *v8::String::Utf8Value(args[1]); |
[email protected] | 481a87e6 | 2012-12-19 23:00:27 | [diff] [blame] | 103 | |
| 104 | int port_id = -1; |
| 105 | renderview->Send(new ExtensionHostMsg_OpenChannelToNativeApp( |
[email protected] | e689367 | 2014-05-01 17:29:13 | [diff] [blame] | 106 | renderview->GetRoutingID(), extension_id, native_app_name, &port_id)); |
[email protected] | d8c5fbb | 2013-06-14 11:35:25 | [diff] [blame] | 107 | args.GetReturnValue().Set(static_cast<int32_t>(port_id)); |
[email protected] | 481a87e6 | 2012-12-19 23:00:27 | [diff] [blame] | 108 | } |
| 109 | |
[email protected] | d8c5fbb | 2013-06-14 11:35:25 | [diff] [blame] | 110 | void RuntimeCustomBindings::GetManifest( |
| 111 | const v8::FunctionCallbackInfo<v8::Value>& args) { |
[email protected] | 9a59844 | 2013-06-04 16:39:12 | [diff] [blame] | 112 | CHECK(context()->extension()); |
[email protected] | 120028b9c | 2012-07-03 01:32:24 | [diff] [blame] | 113 | |
| 114 | scoped_ptr<V8ValueConverter> converter(V8ValueConverter::create()); |
[email protected] | e689367 | 2014-05-01 17:29:13 | [diff] [blame] | 115 | args.GetReturnValue().Set(converter->ToV8Value( |
| 116 | context()->extension()->manifest()->value(), context()->v8_context())); |
[email protected] | 120028b9c | 2012-07-03 01:32:24 | [diff] [blame] | 117 | } |
| 118 | |
[email protected] | c1e5fb1 | 2013-11-13 03:40:31 | [diff] [blame] | 119 | void 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 | |
dcarney | a261b77 | 2014-11-20 17:55:07 | [diff] [blame] | 131 | std::string view_type_string = *v8::String::Utf8Value(args[1]); |
[email protected] | df80704 | 2014-08-13 16:48:41 | [diff] [blame] | 132 | StringToUpperASCII(&view_type_string); |
[email protected] | c1e5fb1 | 2013-11-13 03:40:31 | [diff] [blame] | 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; |
[email protected] | c1e5fb1 | 2013-11-13 03:40:31 | [diff] [blame] | 140 | } 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] | ddcf580 | 2014-02-20 23:21:32 | [diff] [blame] | 146 | } else if (view_type_string == kViewTypeAppWindow) { |
| 147 | view_type = VIEW_TYPE_APP_WINDOW; |
[email protected] | 9faae96 | 2014-08-11 07:59:19 | [diff] [blame] | 148 | } else if (view_type_string == kViewTypeLauncherPage) { |
| 149 | view_type = VIEW_TYPE_LAUNCHER_PAGE; |
[email protected] | c1e5fb1 | 2013-11-13 03:40:31 | [diff] [blame] | 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); |
[email protected] | 95c6b301 | 2013-12-02 14:30:31 | [diff] [blame] | 162 | v8::Local<v8::Array> v8_views = v8::Array::New(args.GetIsolate()); |
[email protected] | c1e5fb1 | 2013-11-13 03:40:31 | [diff] [blame] | 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()); |
[email protected] | fcb70570 | 2013-12-20 12:29:17 | [diff] [blame] | 170 | v8_views->Set(v8::Integer::New(args.GetIsolate(), v8_index++), window); |
[email protected] | c1e5fb1 | 2013-11-13 03:40:31 | [diff] [blame] | 171 | } |
| 172 | } |
| 173 | |
| 174 | args.GetReturnValue().Set(v8_views); |
| 175 | } |
| 176 | |
[email protected] | bcdd992f | 2013-06-09 12:58:03 | [diff] [blame] | 177 | } // namespace extensions |