[email protected] | 120028b9c | 2012-07-03 01:32:24 | [diff] [blame] | 1 | // 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] | 481a87e6 | 2012-12-19 23:00:27 | [diff] [blame] | 10 | #include "chrome/common/extensions/extension_messages.h" |
[email protected] | b6708d03 | 2013-07-14 08:18:22 | [diff] [blame] | 11 | #include "chrome/renderer/extensions/api_activity_logger.h" |
[email protected] | 120028b9c | 2012-07-03 01:32:24 | [diff] [blame] | 12 | #include "chrome/renderer/extensions/chrome_v8_context.h" |
[email protected] | 78b7518 | 2013-03-06 03:41:32 | [diff] [blame] | 13 | #include "chrome/renderer/extensions/dispatcher.h" |
[email protected] | c1e5fb1 | 2013-11-13 03:40:31 | [diff] [blame] | 14 | #include "chrome/renderer/extensions/extension_helper.h" |
[email protected] | 481a87e6 | 2012-12-19 23:00:27 | [diff] [blame] | 15 | #include "content/public/renderer/render_view.h" |
[email protected] | 120028b9c | 2012-07-03 01:32:24 | [diff] [blame] | 16 | #include "content/public/renderer/v8_value_converter.h" |
[email protected] | e4452d3 | 2013-11-15 23:07:41 | [diff] [blame^] | 17 | #include "extensions/common/extension.h" |
[email protected] | d42c1115 | 2013-08-22 19:36:32 | [diff] [blame] | 18 | #include "extensions/common/features/feature.h" |
[email protected] | ad8b4ba | 2013-08-09 19:52:44 | [diff] [blame] | 19 | #include "extensions/common/features/feature_provider.h" |
[email protected] | d42c1115 | 2013-08-22 19:36:32 | [diff] [blame] | 20 | #include "extensions/common/manifest.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] | 78b7518 | 2013-03-06 03:41:32 | [diff] [blame] | 29 | RuntimeCustomBindings::RuntimeCustomBindings(Dispatcher* dispatcher, |
| 30 | ChromeV8Context* context) |
[email protected] | 9a59844 | 2013-06-04 16:39:12 | [diff] [blame] | 31 | : ChromeV8Extension(dispatcher, context) { |
[email protected] | 120028b9c | 2012-07-03 01:32:24 | [diff] [blame] | 32 | RouteFunction("GetManifest", |
[email protected] | 78b7518 | 2013-03-06 03:41:32 | [diff] [blame] | 33 | base::Bind(&RuntimeCustomBindings::GetManifest, |
| 34 | base::Unretained(this))); |
[email protected] | 4f1633f | 2013-03-09 14:26:24 | [diff] [blame] | 35 | RouteFunction("OpenChannelToExtension", |
| 36 | base::Bind(&RuntimeCustomBindings::OpenChannelToExtension, |
| 37 | base::Unretained(this))); |
[email protected] | 78b7518 | 2013-03-06 03:41:32 | [diff] [blame] | 38 | RouteFunction("OpenChannelToNativeApp", |
| 39 | base::Bind(&RuntimeCustomBindings::OpenChannelToNativeApp, |
| 40 | base::Unretained(this))); |
[email protected] | c1e5fb1 | 2013-11-13 03:40:31 | [diff] [blame] | 41 | RouteFunction("GetExtensionViews", |
| 42 | base::Bind(&RuntimeCustomBindings::GetExtensionViews, |
| 43 | base::Unretained(this))); |
[email protected] | 120028b9c | 2012-07-03 01:32:24 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | RuntimeCustomBindings::~RuntimeCustomBindings() {} |
| 47 | |
[email protected] | d8c5fbb | 2013-06-14 11:35:25 | [diff] [blame] | 48 | void RuntimeCustomBindings::OpenChannelToExtension( |
| 49 | const v8::FunctionCallbackInfo<v8::Value>& args) { |
[email protected] | 481a87e6 | 2012-12-19 23:00:27 | [diff] [blame] | 50 | // Get the current RenderView so that we can send a routed IPC message from |
| 51 | // the correct source. |
[email protected] | 4f1633f | 2013-03-09 14:26:24 | [diff] [blame] | 52 | content::RenderView* renderview = GetRenderView(); |
[email protected] | 481a87e6 | 2012-12-19 23:00:27 | [diff] [blame] | 53 | if (!renderview) |
[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] | 83d26be | 2013-08-15 04:55:27 | [diff] [blame] | 61 | info.source_id = context()->GetExtensionID(); |
[email protected] | bcdd992f | 2013-06-09 12:58:03 | [diff] [blame] | 62 | info.target_id = *v8::String::Utf8Value(args[0]->ToString()); |
[email protected] | 83d26be | 2013-08-15 04:55:27 | [diff] [blame] | 63 | info.source_url = context()->GetURL(); |
[email protected] | bcdd992f | 2013-06-09 12:58:03 | [diff] [blame] | 64 | std::string channel_name = *v8::String::Utf8Value(args[1]->ToString()); |
[email protected] | 8ad95b7 | 2013-10-16 02:54:11 | [diff] [blame] | 65 | bool include_tls_channel_id = |
| 66 | args.Length() > 2 ? args[2]->BooleanValue() : false; |
[email protected] | 481a87e6 | 2012-12-19 23:00:27 | [diff] [blame] | 67 | int port_id = -1; |
| 68 | renderview->Send(new ExtensionHostMsg_OpenChannelToExtension( |
[email protected] | 8ad95b7 | 2013-10-16 02:54:11 | [diff] [blame] | 69 | renderview->GetRoutingID(), info, channel_name, include_tls_channel_id, |
| 70 | &port_id)); |
[email protected] | d8c5fbb | 2013-06-14 11:35:25 | [diff] [blame] | 71 | args.GetReturnValue().Set(static_cast<int32_t>(port_id)); |
[email protected] | 481a87e6 | 2012-12-19 23:00:27 | [diff] [blame] | 72 | } |
| 73 | |
[email protected] | d8c5fbb | 2013-06-14 11:35:25 | [diff] [blame] | 74 | void RuntimeCustomBindings::OpenChannelToNativeApp( |
| 75 | const v8::FunctionCallbackInfo<v8::Value>& args) { |
[email protected] | 78b7518 | 2013-03-06 03:41:32 | [diff] [blame] | 76 | // Verify that the extension has permission to use native messaging. |
[email protected] | b6708d03 | 2013-07-14 08:18:22 | [diff] [blame] | 77 | Feature::Availability availability = |
[email protected] | 276f05aa | 2013-10-07 20:44:41 | [diff] [blame] | 78 | FeatureProvider::GetPermissionFeatures()-> |
[email protected] | b6708d03 | 2013-07-14 08:18:22 | [diff] [blame] | 79 | GetFeature("nativeMessaging")->IsAvailableToContext( |
| 80 | GetExtensionForRenderView(), |
| 81 | context()->context_type(), |
| 82 | context()->GetURL()); |
[email protected] | ecc854a | 2013-08-22 10:12:42 | [diff] [blame] | 83 | if (!availability.is_available()) |
[email protected] | d8c5fbb | 2013-06-14 11:35:25 | [diff] [blame] | 84 | return; |
[email protected] | 78b7518 | 2013-03-06 03:41:32 | [diff] [blame] | 85 | |
[email protected] | 481a87e6 | 2012-12-19 23:00:27 | [diff] [blame] | 86 | // Get the current RenderView so that we can send a routed IPC message from |
| 87 | // the correct source. |
[email protected] | 4f1633f | 2013-03-09 14:26:24 | [diff] [blame] | 88 | content::RenderView* renderview = GetRenderView(); |
[email protected] | 481a87e6 | 2012-12-19 23:00:27 | [diff] [blame] | 89 | if (!renderview) |
[email protected] | d8c5fbb | 2013-06-14 11:35:25 | [diff] [blame] | 90 | return; |
[email protected] | 481a87e6 | 2012-12-19 23:00:27 | [diff] [blame] | 91 | |
| 92 | // The Javascript code should validate/fill the arguments. |
[email protected] | 0c641707 | 2013-01-23 00:20:59 | [diff] [blame] | 93 | CHECK(args.Length() >= 2 && |
[email protected] | 481a87e6 | 2012-12-19 23:00:27 | [diff] [blame] | 94 | args[0]->IsString() && |
[email protected] | 0c641707 | 2013-01-23 00:20:59 | [diff] [blame] | 95 | args[1]->IsString()); |
[email protected] | 481a87e6 | 2012-12-19 23:00:27 | [diff] [blame] | 96 | |
| 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] | 481a87e6 | 2012-12-19 23:00:27 | [diff] [blame] | 99 | |
| 100 | int port_id = -1; |
| 101 | renderview->Send(new ExtensionHostMsg_OpenChannelToNativeApp( |
| 102 | renderview->GetRoutingID(), |
| 103 | extension_id, |
| 104 | native_app_name, |
[email protected] | 481a87e6 | 2012-12-19 23:00:27 | [diff] [blame] | 105 | &port_id)); |
[email protected] | d8c5fbb | 2013-06-14 11:35:25 | [diff] [blame] | 106 | args.GetReturnValue().Set(static_cast<int32_t>(port_id)); |
[email protected] | 481a87e6 | 2012-12-19 23:00:27 | [diff] [blame] | 107 | } |
| 108 | |
[email protected] | d8c5fbb | 2013-06-14 11:35:25 | [diff] [blame] | 109 | void RuntimeCustomBindings::GetManifest( |
| 110 | const v8::FunctionCallbackInfo<v8::Value>& args) { |
[email protected] | 9a59844 | 2013-06-04 16:39:12 | [diff] [blame] | 111 | CHECK(context()->extension()); |
[email protected] | 120028b9c | 2012-07-03 01:32:24 | [diff] [blame] | 112 | |
| 113 | scoped_ptr<V8ValueConverter> converter(V8ValueConverter::create()); |
[email protected] | d8c5fbb | 2013-06-14 11:35:25 | [diff] [blame] | 114 | args.GetReturnValue().Set( |
| 115 | converter->ToV8Value(context()->extension()->manifest()->value(), |
| 116 | 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 | |
| 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] | bcdd992f | 2013-06-09 12:58:03 | [diff] [blame] | 177 | } // namespace extensions |