blob: 96139f3a38746679a42a6640d6637733f65e0502 [file] [log] [blame]
[email protected]120028b9c2012-07-03 01:32:241// 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]481a87e62012-12-19 23:00:2711#include "chrome/common/extensions/extension_messages.h"
[email protected]120028b9c2012-07-03 01:32:2412#include "chrome/common/extensions/manifest.h"
[email protected]120028b9c2012-07-03 01:32:2413#include "chrome/renderer/extensions/chrome_v8_context.h"
[email protected]78b75182013-03-06 03:41:3214#include "chrome/renderer/extensions/dispatcher.h"
[email protected]481a87e62012-12-19 23:00:2715#include "content/public/renderer/render_view.h"
[email protected]120028b9c2012-07-03 01:32:2416#include "content/public/renderer/v8_value_converter.h"
17
18using content::V8ValueConverter;
19
20namespace extensions {
21
[email protected]78b75182013-03-06 03:41:3222RuntimeCustomBindings::RuntimeCustomBindings(Dispatcher* dispatcher,
23 ChromeV8Context* context)
[email protected]4f1633f2013-03-09 14:26:2424 : ChromeV8Extension(dispatcher, context->v8_context()),
25 context_(context) {
[email protected]120028b9c2012-07-03 01:32:2426 RouteFunction("GetManifest",
[email protected]78b75182013-03-06 03:41:3227 base::Bind(&RuntimeCustomBindings::GetManifest,
28 base::Unretained(this)));
[email protected]4f1633f2013-03-09 14:26:2429 RouteFunction("OpenChannelToExtension",
30 base::Bind(&RuntimeCustomBindings::OpenChannelToExtension,
31 base::Unretained(this)));
[email protected]78b75182013-03-06 03:41:3232 RouteFunction("OpenChannelToNativeApp",
33 base::Bind(&RuntimeCustomBindings::OpenChannelToNativeApp,
34 base::Unretained(this)));
[email protected]120028b9c2012-07-03 01:32:2435}
36
37RuntimeCustomBindings::~RuntimeCustomBindings() {}
38
[email protected]481a87e62012-12-19 23:00:2739v8::Handle<v8::Value> RuntimeCustomBindings::OpenChannelToExtension(
40 const v8::Arguments& args) {
41 // Get the current RenderView so that we can send a routed IPC message from
42 // the correct source.
[email protected]4f1633f2013-03-09 14:26:2443 content::RenderView* renderview = GetRenderView();
[email protected]481a87e62012-12-19 23:00:2744 if (!renderview)
45 return v8::Undefined();
46
47 // The Javascript code should validate/fill the arguments.
48 CHECK(args.Length() >= 3 &&
49 args[0]->IsString() &&
50 args[1]->IsString() &&
51 args[2]->IsString());
52
53 std::string source_id = *v8::String::Utf8Value(args[0]->ToString());
54 std::string target_id = *v8::String::Utf8Value(args[1]->ToString());
55 std::string channel_name = *v8::String::Utf8Value(args[2]->ToString());
56 int port_id = -1;
57 renderview->Send(new ExtensionHostMsg_OpenChannelToExtension(
58 renderview->GetRoutingID(),
59 source_id,
60 target_id,
61 channel_name,
62 &port_id));
63 return v8::Integer::New(port_id);
64}
65
[email protected]481a87e62012-12-19 23:00:2766v8::Handle<v8::Value> RuntimeCustomBindings::OpenChannelToNativeApp(
67 const v8::Arguments& args) {
[email protected]78b75182013-03-06 03:41:3268 // Verify that the extension has permission to use native messaging.
[email protected]4f1633f2013-03-09 14:26:2469 if (!dispatcher()->CheckContextAccessToExtensionAPI(
70 "nativeMessaging", context_)) {
[email protected]78b75182013-03-06 03:41:3271 return v8::Undefined();
72 }
73
[email protected]481a87e62012-12-19 23:00:2774 // Get the current RenderView so that we can send a routed IPC message from
75 // the correct source.
[email protected]4f1633f2013-03-09 14:26:2476 content::RenderView* renderview = GetRenderView();
[email protected]481a87e62012-12-19 23:00:2777 if (!renderview)
78 return v8::Undefined();
79
80 // The Javascript code should validate/fill the arguments.
[email protected]0c6417072013-01-23 00:20:5981 CHECK(args.Length() >= 2 &&
[email protected]481a87e62012-12-19 23:00:2782 args[0]->IsString() &&
[email protected]0c6417072013-01-23 00:20:5983 args[1]->IsString());
[email protected]481a87e62012-12-19 23:00:2784
85 std::string extension_id = *v8::String::Utf8Value(args[0]->ToString());
86 std::string native_app_name = *v8::String::Utf8Value(args[1]->ToString());
[email protected]481a87e62012-12-19 23:00:2787
88 int port_id = -1;
89 renderview->Send(new ExtensionHostMsg_OpenChannelToNativeApp(
90 renderview->GetRoutingID(),
91 extension_id,
92 native_app_name,
[email protected]481a87e62012-12-19 23:00:2793 &port_id));
94 return v8::Integer::New(port_id);
95}
96
[email protected]120028b9c2012-07-03 01:32:2497v8::Handle<v8::Value> RuntimeCustomBindings::GetManifest(
98 const v8::Arguments& args) {
99 CHECK(context_->extension());
100
101 scoped_ptr<V8ValueConverter> converter(V8ValueConverter::create());
102 return converter->ToV8Value(context_->extension()->manifest()->value(),
103 context_->v8_context());
104}
105
106} // extensions