blob: b41f43cc97c0d43a1e324e3dbc1d98bf93e702b9 [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"
[email protected]686914f2013-04-25 04:54:5817#include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h"
18#include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h"
19#include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h"
[email protected]120028b9c2012-07-03 01:32:2420
21using content::V8ValueConverter;
22
23namespace extensions {
24
[email protected]78b75182013-03-06 03:41:3225RuntimeCustomBindings::RuntimeCustomBindings(Dispatcher* dispatcher,
26 ChromeV8Context* context)
[email protected]4f1633f2013-03-09 14:26:2427 : ChromeV8Extension(dispatcher, context->v8_context()),
28 context_(context) {
[email protected]120028b9c2012-07-03 01:32:2429 RouteFunction("GetManifest",
[email protected]78b75182013-03-06 03:41:3230 base::Bind(&RuntimeCustomBindings::GetManifest,
31 base::Unretained(this)));
[email protected]4f1633f2013-03-09 14:26:2432 RouteFunction("OpenChannelToExtension",
33 base::Bind(&RuntimeCustomBindings::OpenChannelToExtension,
34 base::Unretained(this)));
[email protected]78b75182013-03-06 03:41:3235 RouteFunction("OpenChannelToNativeApp",
36 base::Bind(&RuntimeCustomBindings::OpenChannelToNativeApp,
37 base::Unretained(this)));
[email protected]120028b9c2012-07-03 01:32:2438}
39
40RuntimeCustomBindings::~RuntimeCustomBindings() {}
41
[email protected]481a87e62012-12-19 23:00:2742v8::Handle<v8::Value> RuntimeCustomBindings::OpenChannelToExtension(
43 const v8::Arguments& args) {
44 // Get the current RenderView so that we can send a routed IPC message from
45 // the correct source.
[email protected]4f1633f2013-03-09 14:26:2446 content::RenderView* renderview = GetRenderView();
[email protected]481a87e62012-12-19 23:00:2747 if (!renderview)
48 return v8::Undefined();
49
50 // The Javascript code should validate/fill the arguments.
51 CHECK(args.Length() >= 3 &&
52 args[0]->IsString() &&
53 args[1]->IsString() &&
54 args[2]->IsString());
55
[email protected]686914f2013-04-25 04:54:5856 ExtensionMsg_ExternalConnectionInfo info;
57 info.source_id = *v8::String::Utf8Value(args[0]->ToString());
58 info.target_id = *v8::String::Utf8Value(args[1]->ToString());
59 info.source_url = renderview->GetWebView()->mainFrame()->document().url();
[email protected]481a87e62012-12-19 23:00:2760 std::string channel_name = *v8::String::Utf8Value(args[2]->ToString());
61 int port_id = -1;
62 renderview->Send(new ExtensionHostMsg_OpenChannelToExtension(
[email protected]686914f2013-04-25 04:54:5863 renderview->GetRoutingID(), info, channel_name, &port_id));
[email protected]481a87e62012-12-19 23:00:2764 return v8::Integer::New(port_id);
65}
66
[email protected]481a87e62012-12-19 23:00:2767v8::Handle<v8::Value> RuntimeCustomBindings::OpenChannelToNativeApp(
68 const v8::Arguments& args) {
[email protected]78b75182013-03-06 03:41:3269 // Verify that the extension has permission to use native messaging.
[email protected]4f1633f2013-03-09 14:26:2470 if (!dispatcher()->CheckContextAccessToExtensionAPI(
71 "nativeMessaging", context_)) {
[email protected]78b75182013-03-06 03:41:3272 return v8::Undefined();
73 }
74
[email protected]481a87e62012-12-19 23:00:2775 // Get the current RenderView so that we can send a routed IPC message from
76 // the correct source.
[email protected]4f1633f2013-03-09 14:26:2477 content::RenderView* renderview = GetRenderView();
[email protected]481a87e62012-12-19 23:00:2778 if (!renderview)
79 return v8::Undefined();
80
81 // The Javascript code should validate/fill the arguments.
[email protected]0c6417072013-01-23 00:20:5982 CHECK(args.Length() >= 2 &&
[email protected]481a87e62012-12-19 23:00:2783 args[0]->IsString() &&
[email protected]0c6417072013-01-23 00:20:5984 args[1]->IsString());
[email protected]481a87e62012-12-19 23:00:2785
86 std::string extension_id = *v8::String::Utf8Value(args[0]->ToString());
87 std::string native_app_name = *v8::String::Utf8Value(args[1]->ToString());
[email protected]481a87e62012-12-19 23:00:2788
89 int port_id = -1;
90 renderview->Send(new ExtensionHostMsg_OpenChannelToNativeApp(
91 renderview->GetRoutingID(),
92 extension_id,
93 native_app_name,
[email protected]481a87e62012-12-19 23:00:2794 &port_id));
95 return v8::Integer::New(port_id);
96}
97
[email protected]120028b9c2012-07-03 01:32:2498v8::Handle<v8::Value> RuntimeCustomBindings::GetManifest(
99 const v8::Arguments& args) {
100 CHECK(context_->extension());
101
102 scoped_ptr<V8ValueConverter> converter(V8ValueConverter::create());
103 return converter->ToV8Value(context_->extension()->manifest()->value(),
104 context_->v8_context());
105}
106
107} // extensions