blob: 8cdc10383562f1b35dce9036bdbc2eebfe2f2bbf [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]6876bb92013-06-14 06:03:5112#include "chrome/common/extensions/features/base_feature_provider.h"
[email protected]120028b9c2012-07-03 01:32:2413#include "chrome/common/extensions/manifest.h"
[email protected]120028b9c2012-07-03 01:32:2414#include "chrome/renderer/extensions/chrome_v8_context.h"
[email protected]78b75182013-03-06 03:41:3215#include "chrome/renderer/extensions/dispatcher.h"
[email protected]481a87e62012-12-19 23:00:2716#include "content/public/renderer/render_view.h"
[email protected]120028b9c2012-07-03 01:32:2417#include "content/public/renderer/v8_value_converter.h"
[email protected]2255a9332013-06-17 05:12:3118#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]120028b9c2012-07-03 01:32:2421
22using content::V8ValueConverter;
23
24namespace extensions {
25
[email protected]78b75182013-03-06 03:41:3226RuntimeCustomBindings::RuntimeCustomBindings(Dispatcher* dispatcher,
27 ChromeV8Context* context)
[email protected]9a598442013-06-04 16:39:1228 : ChromeV8Extension(dispatcher, 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]d8c5fbb2013-06-14 11:35:2542void RuntimeCustomBindings::OpenChannelToExtension(
43 const v8::FunctionCallbackInfo<v8::Value>& args) {
[email protected]481a87e62012-12-19 23:00:2744 // 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)
[email protected]d8c5fbb2013-06-14 11:35:2548 return;
[email protected]481a87e62012-12-19 23:00:2749
50 // The Javascript code should validate/fill the arguments.
[email protected]bcdd992f2013-06-09 12:58:0351 CHECK_EQ(2, args.Length());
52 CHECK(args[0]->IsString() && args[1]->IsString());
[email protected]481a87e62012-12-19 23:00:2753
[email protected]686914f2013-04-25 04:54:5854 ExtensionMsg_ExternalConnectionInfo info;
[email protected]bcdd992f2013-06-09 12:58:0355 info.source_id = context()->extension() ? context()->extension()->id() : "";
56 info.target_id = *v8::String::Utf8Value(args[0]->ToString());
[email protected]686914f2013-04-25 04:54:5857 info.source_url = renderview->GetWebView()->mainFrame()->document().url();
[email protected]bcdd992f2013-06-09 12:58:0358 std::string channel_name = *v8::String::Utf8Value(args[1]->ToString());
[email protected]481a87e62012-12-19 23:00:2759 int port_id = -1;
60 renderview->Send(new ExtensionHostMsg_OpenChannelToExtension(
[email protected]686914f2013-04-25 04:54:5861 renderview->GetRoutingID(), info, channel_name, &port_id));
[email protected]d8c5fbb2013-06-14 11:35:2562 args.GetReturnValue().Set(static_cast<int32_t>(port_id));
[email protected]481a87e62012-12-19 23:00:2763}
64
[email protected]d8c5fbb2013-06-14 11:35:2565void RuntimeCustomBindings::OpenChannelToNativeApp(
66 const v8::FunctionCallbackInfo<v8::Value>& args) {
[email protected]78b75182013-03-06 03:41:3267 // Verify that the extension has permission to use native messaging.
[email protected]6876bb92013-06-14 06:03:5168 if (!BaseFeatureProvider::GetByName("permission")->GetFeature(
69 "nativeMessaging")->IsAvailableToContext(
70 GetExtensionForRenderView(),
71 context()->context_type(),
72 context()->GetURL()).is_available()) {
[email protected]d8c5fbb2013-06-14 11:35:2573 return;
[email protected]78b75182013-03-06 03:41:3274 }
75
[email protected]481a87e62012-12-19 23:00:2776 // Get the current RenderView so that we can send a routed IPC message from
77 // the correct source.
[email protected]4f1633f2013-03-09 14:26:2478 content::RenderView* renderview = GetRenderView();
[email protected]481a87e62012-12-19 23:00:2779 if (!renderview)
[email protected]d8c5fbb2013-06-14 11:35:2580 return;
[email protected]481a87e62012-12-19 23:00:2781
82 // The Javascript code should validate/fill the arguments.
[email protected]0c6417072013-01-23 00:20:5983 CHECK(args.Length() >= 2 &&
[email protected]481a87e62012-12-19 23:00:2784 args[0]->IsString() &&
[email protected]0c6417072013-01-23 00:20:5985 args[1]->IsString());
[email protected]481a87e62012-12-19 23:00:2786
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]481a87e62012-12-19 23:00:2789
90 int port_id = -1;
91 renderview->Send(new ExtensionHostMsg_OpenChannelToNativeApp(
92 renderview->GetRoutingID(),
93 extension_id,
94 native_app_name,
[email protected]481a87e62012-12-19 23:00:2795 &port_id));
[email protected]d8c5fbb2013-06-14 11:35:2596 args.GetReturnValue().Set(static_cast<int32_t>(port_id));
[email protected]481a87e62012-12-19 23:00:2797}
98
[email protected]d8c5fbb2013-06-14 11:35:2599void RuntimeCustomBindings::GetManifest(
100 const v8::FunctionCallbackInfo<v8::Value>& args) {
[email protected]9a598442013-06-04 16:39:12101 CHECK(context()->extension());
[email protected]120028b9c2012-07-03 01:32:24102
103 scoped_ptr<V8ValueConverter> converter(V8ValueConverter::create());
[email protected]d8c5fbb2013-06-14 11:35:25104 args.GetReturnValue().Set(
105 converter->ToV8Value(context()->extension()->manifest()->value(),
106 context()->v8_context()));
[email protected]120028b9c2012-07-03 01:32:24107}
108
[email protected]bcdd992f2013-06-09 12:58:03109} // namespace extensions