Remove unused parameter.
[chromium-blink-merge.git] / extensions / renderer / runtime_custom_bindings.cc
blob483f08b7796d2f2c2f80634cf70a6d3ce9b1f680
1 // Copyright 2014 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.
5 #include "extensions/renderer/runtime_custom_bindings.h"
7 #include "base/bind.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "base/values.h"
10 #include "content/public/child/v8_value_converter.h"
11 #include "content/public/renderer/render_frame.h"
12 #include "content/public/renderer/render_view.h"
13 #include "extensions/common/extension.h"
14 #include "extensions/common/extension_messages.h"
15 #include "extensions/common/features/feature.h"
16 #include "extensions/common/features/feature_provider.h"
17 #include "extensions/common/manifest.h"
18 #include "extensions/renderer/api_activity_logger.h"
19 #include "extensions/renderer/extension_helper.h"
20 #include "extensions/renderer/script_context.h"
21 #include "third_party/WebKit/public/web/WebDocument.h"
22 #include "third_party/WebKit/public/web/WebFrame.h"
23 #include "third_party/WebKit/public/web/WebView.h"
25 using content::V8ValueConverter;
27 namespace extensions {
29 RuntimeCustomBindings::RuntimeCustomBindings(ScriptContext* context)
30 : ObjectBackedNativeHandler(context) {
31 RouteFunction(
32 "GetManifest",
33 base::Bind(&RuntimeCustomBindings::GetManifest, base::Unretained(this)));
34 RouteFunction("OpenChannelToExtension",
35 base::Bind(&RuntimeCustomBindings::OpenChannelToExtension,
36 base::Unretained(this)));
37 RouteFunction("OpenChannelToNativeApp",
38 base::Bind(&RuntimeCustomBindings::OpenChannelToNativeApp,
39 base::Unretained(this)));
40 RouteFunction("GetExtensionViews",
41 base::Bind(&RuntimeCustomBindings::GetExtensionViews,
42 base::Unretained(this)));
45 RuntimeCustomBindings::~RuntimeCustomBindings() {
48 void RuntimeCustomBindings::OpenChannelToExtension(
49 const v8::FunctionCallbackInfo<v8::Value>& args) {
50 // Get the current RenderFrame so that we can send a routed IPC message from
51 // the correct source.
52 content::RenderFrame* renderframe = context()->GetRenderFrame();
53 if (!renderframe)
54 return;
56 // The Javascript code should validate/fill the arguments.
57 CHECK_EQ(args.Length(), 3);
58 CHECK(args[0]->IsString() && args[1]->IsString() && args[2]->IsBoolean());
60 ExtensionMsg_ExternalConnectionInfo info;
62 // For messaging APIs, hosted apps should be considered a web page so hide
63 // its extension ID.
64 const Extension* extension = context()->extension();
65 if (extension && !extension->is_hosted_app())
66 info.source_id = extension->id();
68 info.target_id = *v8::String::Utf8Value(args[0]);
69 info.source_url = context()->GetURL();
70 std::string channel_name = *v8::String::Utf8Value(args[1]);
71 bool include_tls_channel_id =
72 args.Length() > 2 ? args[2]->BooleanValue() : false;
73 int port_id = -1;
74 renderframe->Send(new ExtensionHostMsg_OpenChannelToExtension(
75 renderframe->GetRoutingID(), info, channel_name, include_tls_channel_id,
76 &port_id));
77 args.GetReturnValue().Set(static_cast<int32_t>(port_id));
80 void RuntimeCustomBindings::OpenChannelToNativeApp(
81 const v8::FunctionCallbackInfo<v8::Value>& args) {
82 // Verify that the extension has permission to use native messaging.
83 Feature::Availability availability =
84 FeatureProvider::GetPermissionFeatures()
85 ->GetFeature("nativeMessaging")
86 ->IsAvailableToContext(context()->extension(),
87 context()->context_type(),
88 context()->GetURL());
89 if (!availability.is_available())
90 return;
92 // Get the current RenderView so that we can send a routed IPC message from
93 // the correct source.
94 content::RenderView* renderview = context()->GetRenderView();
95 if (!renderview)
96 return;
98 // The Javascript code should validate/fill the arguments.
99 CHECK(args.Length() >= 2 && args[0]->IsString() && args[1]->IsString());
101 std::string extension_id = *v8::String::Utf8Value(args[0]);
102 std::string native_app_name = *v8::String::Utf8Value(args[1]);
104 int port_id = -1;
105 renderview->Send(new ExtensionHostMsg_OpenChannelToNativeApp(
106 renderview->GetRoutingID(), extension_id, native_app_name, &port_id));
107 args.GetReturnValue().Set(static_cast<int32_t>(port_id));
110 void RuntimeCustomBindings::GetManifest(
111 const v8::FunctionCallbackInfo<v8::Value>& args) {
112 CHECK(context()->extension());
114 scoped_ptr<V8ValueConverter> converter(V8ValueConverter::create());
115 args.GetReturnValue().Set(converter->ToV8Value(
116 context()->extension()->manifest()->value(), context()->v8_context()));
119 void RuntimeCustomBindings::GetExtensionViews(
120 const v8::FunctionCallbackInfo<v8::Value>& args) {
121 if (args.Length() != 2)
122 return;
124 if (!args[0]->IsInt32() || !args[1]->IsString())
125 return;
127 // |browser_window_id| == extension_misc::kUnknownWindowId means getting
128 // all views for the current extension.
129 int browser_window_id = args[0]->Int32Value();
131 std::string view_type_string = *v8::String::Utf8Value(args[1]);
132 StringToUpperASCII(&view_type_string);
133 // |view_type| == VIEW_TYPE_INVALID means getting any type of
134 // views.
135 ViewType view_type = VIEW_TYPE_INVALID;
136 if (view_type_string == kViewTypeBackgroundPage) {
137 view_type = VIEW_TYPE_EXTENSION_BACKGROUND_PAGE;
138 } else if (view_type_string == kViewTypeTabContents) {
139 view_type = VIEW_TYPE_TAB_CONTENTS;
140 } else if (view_type_string == kViewTypePopup) {
141 view_type = VIEW_TYPE_EXTENSION_POPUP;
142 } else if (view_type_string == kViewTypeExtensionDialog) {
143 view_type = VIEW_TYPE_EXTENSION_DIALOG;
144 } else if (view_type_string == kViewTypeAppWindow) {
145 view_type = VIEW_TYPE_APP_WINDOW;
146 } else if (view_type_string == kViewTypeLauncherPage) {
147 view_type = VIEW_TYPE_LAUNCHER_PAGE;
148 } else if (view_type_string == kViewTypePanel) {
149 view_type = VIEW_TYPE_PANEL;
150 } else if (view_type_string != kViewTypeAll) {
151 return;
154 std::string extension_id = context()->GetExtensionID();
155 if (extension_id.empty())
156 return;
158 std::vector<content::RenderView*> views = ExtensionHelper::GetExtensionViews(
159 extension_id, browser_window_id, view_type);
160 v8::Local<v8::Array> v8_views = v8::Array::New(args.GetIsolate());
161 int v8_index = 0;
162 for (size_t i = 0; i < views.size(); ++i) {
163 v8::Local<v8::Context> context =
164 views[i]->GetWebView()->mainFrame()->mainWorldScriptContext();
165 if (!context.IsEmpty()) {
166 v8::Local<v8::Value> window = context->Global();
167 DCHECK(!window.IsEmpty());
168 v8_views->Set(v8::Integer::New(args.GetIsolate(), v8_index++), window);
172 args.GetReturnValue().Set(v8_views);
175 } // namespace extensions