[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" |
| 10 | #include "chrome/common/extensions/extension.h" |
[email protected] | 481a87e6 | 2012-12-19 23:00:27 | [diff] [blame] | 11 | #include "chrome/common/extensions/extension_messages.h" |
[email protected] | 6876bb9 | 2013-06-14 06:03:51 | [diff] [blame] | 12 | #include "chrome/common/extensions/features/base_feature_provider.h" |
[email protected] | 120028b9c | 2012-07-03 01:32:24 | [diff] [blame] | 13 | #include "chrome/common/extensions/manifest.h" |
[email protected] | 120028b9c | 2012-07-03 01:32:24 | [diff] [blame] | 14 | #include "chrome/renderer/extensions/chrome_v8_context.h" |
[email protected] | 78b7518 | 2013-03-06 03:41:32 | [diff] [blame] | 15 | #include "chrome/renderer/extensions/dispatcher.h" |
[email protected] | 481a87e6 | 2012-12-19 23:00:27 | [diff] [blame] | 16 | #include "content/public/renderer/render_view.h" |
[email protected] | 120028b9c | 2012-07-03 01:32:24 | [diff] [blame] | 17 | #include "content/public/renderer/v8_value_converter.h" |
[email protected] | 2255a933 | 2013-06-17 05:12:31 | [diff] [blame^] | 18 | #include "third_party/WebKit/public/web/WebDocument.h" |
| 19 | #include "third_party/WebKit/public/web/WebFrame.h" |
| 20 | #include "third_party/WebKit/public/web/WebView.h" |
[email protected] | 120028b9c | 2012-07-03 01:32:24 | [diff] [blame] | 21 | |
| 22 | using content::V8ValueConverter; |
| 23 | |
| 24 | namespace extensions { |
| 25 | |
[email protected] | 78b7518 | 2013-03-06 03:41:32 | [diff] [blame] | 26 | RuntimeCustomBindings::RuntimeCustomBindings(Dispatcher* dispatcher, |
| 27 | ChromeV8Context* context) |
[email protected] | 9a59844 | 2013-06-04 16:39:12 | [diff] [blame] | 28 | : ChromeV8Extension(dispatcher, context) { |
[email protected] | 120028b9c | 2012-07-03 01:32:24 | [diff] [blame] | 29 | RouteFunction("GetManifest", |
[email protected] | 78b7518 | 2013-03-06 03:41:32 | [diff] [blame] | 30 | base::Bind(&RuntimeCustomBindings::GetManifest, |
| 31 | base::Unretained(this))); |
[email protected] | 4f1633f | 2013-03-09 14:26:24 | [diff] [blame] | 32 | RouteFunction("OpenChannelToExtension", |
| 33 | base::Bind(&RuntimeCustomBindings::OpenChannelToExtension, |
| 34 | base::Unretained(this))); |
[email protected] | 78b7518 | 2013-03-06 03:41:32 | [diff] [blame] | 35 | RouteFunction("OpenChannelToNativeApp", |
| 36 | base::Bind(&RuntimeCustomBindings::OpenChannelToNativeApp, |
| 37 | base::Unretained(this))); |
[email protected] | 120028b9c | 2012-07-03 01:32:24 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | RuntimeCustomBindings::~RuntimeCustomBindings() {} |
| 41 | |
[email protected] | d8c5fbb | 2013-06-14 11:35:25 | [diff] [blame] | 42 | void RuntimeCustomBindings::OpenChannelToExtension( |
| 43 | const v8::FunctionCallbackInfo<v8::Value>& args) { |
[email protected] | 481a87e6 | 2012-12-19 23:00:27 | [diff] [blame] | 44 | // Get the current RenderView so that we can send a routed IPC message from |
| 45 | // the correct source. |
[email protected] | 4f1633f | 2013-03-09 14:26:24 | [diff] [blame] | 46 | content::RenderView* renderview = GetRenderView(); |
[email protected] | 481a87e6 | 2012-12-19 23:00:27 | [diff] [blame] | 47 | if (!renderview) |
[email protected] | d8c5fbb | 2013-06-14 11:35:25 | [diff] [blame] | 48 | return; |
[email protected] | 481a87e6 | 2012-12-19 23:00:27 | [diff] [blame] | 49 | |
| 50 | // The Javascript code should validate/fill the arguments. |
[email protected] | bcdd992f | 2013-06-09 12:58:03 | [diff] [blame] | 51 | CHECK_EQ(2, args.Length()); |
| 52 | CHECK(args[0]->IsString() && args[1]->IsString()); |
[email protected] | 481a87e6 | 2012-12-19 23:00:27 | [diff] [blame] | 53 | |
[email protected] | 686914f | 2013-04-25 04:54:58 | [diff] [blame] | 54 | ExtensionMsg_ExternalConnectionInfo info; |
[email protected] | bcdd992f | 2013-06-09 12:58:03 | [diff] [blame] | 55 | info.source_id = context()->extension() ? context()->extension()->id() : ""; |
| 56 | info.target_id = *v8::String::Utf8Value(args[0]->ToString()); |
[email protected] | 686914f | 2013-04-25 04:54:58 | [diff] [blame] | 57 | info.source_url = renderview->GetWebView()->mainFrame()->document().url(); |
[email protected] | bcdd992f | 2013-06-09 12:58:03 | [diff] [blame] | 58 | std::string channel_name = *v8::String::Utf8Value(args[1]->ToString()); |
[email protected] | 481a87e6 | 2012-12-19 23:00:27 | [diff] [blame] | 59 | int port_id = -1; |
| 60 | renderview->Send(new ExtensionHostMsg_OpenChannelToExtension( |
[email protected] | 686914f | 2013-04-25 04:54:58 | [diff] [blame] | 61 | renderview->GetRoutingID(), info, channel_name, &port_id)); |
[email protected] | d8c5fbb | 2013-06-14 11:35:25 | [diff] [blame] | 62 | args.GetReturnValue().Set(static_cast<int32_t>(port_id)); |
[email protected] | 481a87e6 | 2012-12-19 23:00:27 | [diff] [blame] | 63 | } |
| 64 | |
[email protected] | d8c5fbb | 2013-06-14 11:35:25 | [diff] [blame] | 65 | void RuntimeCustomBindings::OpenChannelToNativeApp( |
| 66 | const v8::FunctionCallbackInfo<v8::Value>& args) { |
[email protected] | 78b7518 | 2013-03-06 03:41:32 | [diff] [blame] | 67 | // Verify that the extension has permission to use native messaging. |
[email protected] | 6876bb9 | 2013-06-14 06:03:51 | [diff] [blame] | 68 | if (!BaseFeatureProvider::GetByName("permission")->GetFeature( |
| 69 | "nativeMessaging")->IsAvailableToContext( |
| 70 | GetExtensionForRenderView(), |
| 71 | context()->context_type(), |
| 72 | context()->GetURL()).is_available()) { |
[email protected] | d8c5fbb | 2013-06-14 11:35:25 | [diff] [blame] | 73 | return; |
[email protected] | 78b7518 | 2013-03-06 03:41:32 | [diff] [blame] | 74 | } |
| 75 | |
[email protected] | 481a87e6 | 2012-12-19 23:00:27 | [diff] [blame] | 76 | // Get the current RenderView so that we can send a routed IPC message from |
| 77 | // the correct source. |
[email protected] | 4f1633f | 2013-03-09 14:26:24 | [diff] [blame] | 78 | content::RenderView* renderview = GetRenderView(); |
[email protected] | 481a87e6 | 2012-12-19 23:00:27 | [diff] [blame] | 79 | if (!renderview) |
[email protected] | d8c5fbb | 2013-06-14 11:35:25 | [diff] [blame] | 80 | return; |
[email protected] | 481a87e6 | 2012-12-19 23:00:27 | [diff] [blame] | 81 | |
| 82 | // The Javascript code should validate/fill the arguments. |
[email protected] | 0c641707 | 2013-01-23 00:20:59 | [diff] [blame] | 83 | CHECK(args.Length() >= 2 && |
[email protected] | 481a87e6 | 2012-12-19 23:00:27 | [diff] [blame] | 84 | args[0]->IsString() && |
[email protected] | 0c641707 | 2013-01-23 00:20:59 | [diff] [blame] | 85 | args[1]->IsString()); |
[email protected] | 481a87e6 | 2012-12-19 23:00:27 | [diff] [blame] | 86 | |
| 87 | std::string extension_id = *v8::String::Utf8Value(args[0]->ToString()); |
| 88 | std::string native_app_name = *v8::String::Utf8Value(args[1]->ToString()); |
[email protected] | 481a87e6 | 2012-12-19 23:00:27 | [diff] [blame] | 89 | |
| 90 | int port_id = -1; |
| 91 | renderview->Send(new ExtensionHostMsg_OpenChannelToNativeApp( |
| 92 | renderview->GetRoutingID(), |
| 93 | extension_id, |
| 94 | native_app_name, |
[email protected] | 481a87e6 | 2012-12-19 23:00:27 | [diff] [blame] | 95 | &port_id)); |
[email protected] | d8c5fbb | 2013-06-14 11:35:25 | [diff] [blame] | 96 | args.GetReturnValue().Set(static_cast<int32_t>(port_id)); |
[email protected] | 481a87e6 | 2012-12-19 23:00:27 | [diff] [blame] | 97 | } |
| 98 | |
[email protected] | d8c5fbb | 2013-06-14 11:35:25 | [diff] [blame] | 99 | void RuntimeCustomBindings::GetManifest( |
| 100 | const v8::FunctionCallbackInfo<v8::Value>& args) { |
[email protected] | 9a59844 | 2013-06-04 16:39:12 | [diff] [blame] | 101 | CHECK(context()->extension()); |
[email protected] | 120028b9c | 2012-07-03 01:32:24 | [diff] [blame] | 102 | |
| 103 | scoped_ptr<V8ValueConverter> converter(V8ValueConverter::create()); |
[email protected] | d8c5fbb | 2013-06-14 11:35:25 | [diff] [blame] | 104 | args.GetReturnValue().Set( |
| 105 | converter->ToV8Value(context()->extension()->manifest()->value(), |
| 106 | context()->v8_context())); |
[email protected] | 120028b9c | 2012-07-03 01:32:24 | [diff] [blame] | 107 | } |
| 108 | |
[email protected] | bcdd992f | 2013-06-09 12:58:03 | [diff] [blame] | 109 | } // namespace extensions |