[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] | 120028b9c | 2012-07-03 01:32:24 | [diff] [blame] | 12 | #include "chrome/common/extensions/manifest.h" |
[email protected] | 120028b9c | 2012-07-03 01:32:24 | [diff] [blame] | 13 | #include "chrome/renderer/extensions/chrome_v8_context.h" |
[email protected] | 78b7518 | 2013-03-06 03:41:32 | [diff] [blame^] | 14 | #include "chrome/renderer/extensions/dispatcher.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" |
| 17 | |
| 18 | using content::V8ValueConverter; |
| 19 | |
| 20 | namespace extensions { |
| 21 | |
[email protected] | 78b7518 | 2013-03-06 03:41:32 | [diff] [blame^] | 22 | RuntimeCustomBindings::RuntimeCustomBindings(Dispatcher* dispatcher, |
| 23 | ChromeV8Context* context) |
| 24 | : ChromeV8Extension(dispatcher), context_(context) { |
[email protected] | 120028b9c | 2012-07-03 01:32:24 | [diff] [blame] | 25 | RouteFunction("GetManifest", |
[email protected] | 78b7518 | 2013-03-06 03:41:32 | [diff] [blame^] | 26 | base::Bind(&RuntimeCustomBindings::GetManifest, |
| 27 | base::Unretained(this))); |
[email protected] | 481a87e6 | 2012-12-19 23:00:27 | [diff] [blame] | 28 | RouteStaticFunction("OpenChannelToExtension", &OpenChannelToExtension); |
[email protected] | 78b7518 | 2013-03-06 03:41:32 | [diff] [blame^] | 29 | RouteFunction("OpenChannelToNativeApp", |
| 30 | base::Bind(&RuntimeCustomBindings::OpenChannelToNativeApp, |
| 31 | base::Unretained(this))); |
[email protected] | 120028b9c | 2012-07-03 01:32:24 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | RuntimeCustomBindings::~RuntimeCustomBindings() {} |
| 35 | |
[email protected] | 481a87e6 | 2012-12-19 23:00:27 | [diff] [blame] | 36 | // static |
| 37 | v8::Handle<v8::Value> RuntimeCustomBindings::OpenChannelToExtension( |
| 38 | const v8::Arguments& args) { |
| 39 | // Get the current RenderView so that we can send a routed IPC message from |
| 40 | // the correct source. |
[email protected] | e3e77872 | 2013-03-03 23:28:03 | [diff] [blame] | 41 | content::RenderView* renderview = GetCurrentRenderView(); |
[email protected] | 481a87e6 | 2012-12-19 23:00:27 | [diff] [blame] | 42 | if (!renderview) |
| 43 | return v8::Undefined(); |
| 44 | |
| 45 | // The Javascript code should validate/fill the arguments. |
| 46 | CHECK(args.Length() >= 3 && |
| 47 | args[0]->IsString() && |
| 48 | args[1]->IsString() && |
| 49 | args[2]->IsString()); |
| 50 | |
| 51 | std::string source_id = *v8::String::Utf8Value(args[0]->ToString()); |
| 52 | std::string target_id = *v8::String::Utf8Value(args[1]->ToString()); |
| 53 | std::string channel_name = *v8::String::Utf8Value(args[2]->ToString()); |
| 54 | int port_id = -1; |
| 55 | renderview->Send(new ExtensionHostMsg_OpenChannelToExtension( |
| 56 | renderview->GetRoutingID(), |
| 57 | source_id, |
| 58 | target_id, |
| 59 | channel_name, |
| 60 | &port_id)); |
| 61 | return v8::Integer::New(port_id); |
| 62 | } |
| 63 | |
[email protected] | 481a87e6 | 2012-12-19 23:00:27 | [diff] [blame] | 64 | v8::Handle<v8::Value> RuntimeCustomBindings::OpenChannelToNativeApp( |
| 65 | const v8::Arguments& args) { |
[email protected] | 78b7518 | 2013-03-06 03:41:32 | [diff] [blame^] | 66 | // Verify that the extension has permission to use native messaging. |
| 67 | if (!dispatcher()->CheckCurrentContextAccessToExtensionAPI( |
| 68 | "nativeMessaging")) { |
| 69 | return v8::Undefined(); |
| 70 | } |
| 71 | |
[email protected] | 481a87e6 | 2012-12-19 23:00:27 | [diff] [blame] | 72 | // Get the current RenderView so that we can send a routed IPC message from |
| 73 | // the correct source. |
[email protected] | e3e77872 | 2013-03-03 23:28:03 | [diff] [blame] | 74 | content::RenderView* renderview = GetCurrentRenderView(); |
[email protected] | 481a87e6 | 2012-12-19 23:00:27 | [diff] [blame] | 75 | if (!renderview) |
| 76 | return v8::Undefined(); |
| 77 | |
| 78 | // The Javascript code should validate/fill the arguments. |
[email protected] | 0c641707 | 2013-01-23 00:20:59 | [diff] [blame] | 79 | CHECK(args.Length() >= 2 && |
[email protected] | 481a87e6 | 2012-12-19 23:00:27 | [diff] [blame] | 80 | args[0]->IsString() && |
[email protected] | 0c641707 | 2013-01-23 00:20:59 | [diff] [blame] | 81 | args[1]->IsString()); |
[email protected] | 481a87e6 | 2012-12-19 23:00:27 | [diff] [blame] | 82 | |
| 83 | std::string extension_id = *v8::String::Utf8Value(args[0]->ToString()); |
| 84 | std::string native_app_name = *v8::String::Utf8Value(args[1]->ToString()); |
[email protected] | 481a87e6 | 2012-12-19 23:00:27 | [diff] [blame] | 85 | |
| 86 | int port_id = -1; |
| 87 | renderview->Send(new ExtensionHostMsg_OpenChannelToNativeApp( |
| 88 | renderview->GetRoutingID(), |
| 89 | extension_id, |
| 90 | native_app_name, |
[email protected] | 481a87e6 | 2012-12-19 23:00:27 | [diff] [blame] | 91 | &port_id)); |
| 92 | return v8::Integer::New(port_id); |
| 93 | } |
| 94 | |
[email protected] | 120028b9c | 2012-07-03 01:32:24 | [diff] [blame] | 95 | v8::Handle<v8::Value> RuntimeCustomBindings::GetManifest( |
| 96 | const v8::Arguments& args) { |
| 97 | CHECK(context_->extension()); |
| 98 | |
| 99 | scoped_ptr<V8ValueConverter> converter(V8ValueConverter::create()); |
| 100 | return converter->ToV8Value(context_->extension()->manifest()->value(), |
| 101 | context_->v8_context()); |
| 102 | } |
| 103 | |
| 104 | } // extensions |