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"
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 "extensions/common/extension.h"
13 #include "extensions/common/extension_messages.h"
14 #include "extensions/common/features/feature.h"
15 #include "extensions/common/features/feature_provider.h"
16 #include "extensions/common/manifest.h"
17 #include "extensions/renderer/api_activity_logger.h"
18 #include "extensions/renderer/extension_frame_helper.h"
19 #include "extensions/renderer/script_context.h"
20 #include "third_party/WebKit/public/web/WebDocument.h"
21 #include "third_party/WebKit/public/web/WebLocalFrame.h"
22 #include "third_party/WebKit/public/web/WebView.h"
24 using content::V8ValueConverter
;
26 namespace extensions
{
28 RuntimeCustomBindings::RuntimeCustomBindings(ScriptContext
* context
)
29 : ObjectBackedNativeHandler(context
) {
32 base::Bind(&RuntimeCustomBindings::GetManifest
, base::Unretained(this)));
33 RouteFunction("OpenChannelToExtension",
34 base::Bind(&RuntimeCustomBindings::OpenChannelToExtension
,
35 base::Unretained(this)));
36 RouteFunction("OpenChannelToNativeApp",
37 base::Bind(&RuntimeCustomBindings::OpenChannelToNativeApp
,
38 base::Unretained(this)));
39 RouteFunction("GetExtensionViews",
40 base::Bind(&RuntimeCustomBindings::GetExtensionViews
,
41 base::Unretained(this)));
44 RuntimeCustomBindings::~RuntimeCustomBindings() {
47 void RuntimeCustomBindings::OpenChannelToExtension(
48 const v8::FunctionCallbackInfo
<v8::Value
>& args
) {
49 // Get the current RenderFrame so that we can send a routed IPC message from
50 // the correct source.
51 content::RenderFrame
* renderframe
= context()->GetRenderFrame();
55 // The Javascript code should validate/fill the arguments.
56 CHECK_EQ(args
.Length(), 3);
57 CHECK(args
[0]->IsString() && args
[1]->IsString() && args
[2]->IsBoolean());
59 ExtensionMsg_ExternalConnectionInfo info
;
61 // For messaging APIs, hosted apps should be considered a web page so hide
63 const Extension
* extension
= context()->extension();
64 if (extension
&& !extension
->is_hosted_app())
65 info
.source_id
= extension
->id();
67 info
.target_id
= *v8::String::Utf8Value(args
[0]);
68 info
.source_url
= context()->url();
69 std::string channel_name
= *v8::String::Utf8Value(args
[1]);
70 bool include_tls_channel_id
=
71 args
.Length() > 2 ? args
[2]->BooleanValue() : false;
73 renderframe
->Send(new ExtensionHostMsg_OpenChannelToExtension(
74 renderframe
->GetRoutingID(), info
, channel_name
, include_tls_channel_id
,
76 args
.GetReturnValue().Set(static_cast<int32_t>(port_id
));
79 void RuntimeCustomBindings::OpenChannelToNativeApp(
80 const v8::FunctionCallbackInfo
<v8::Value
>& args
) {
81 // Verify that the extension has permission to use native messaging.
82 Feature::Availability availability
=
83 FeatureProvider::GetPermissionFeatures()
84 ->GetFeature("nativeMessaging")
85 ->IsAvailableToContext(context()->extension(),
86 context()->context_type(), context()->url());
87 if (!availability
.is_available())
90 content::RenderFrame
* render_frame
= context()->GetRenderFrame();
94 // The Javascript code should validate/fill the arguments.
95 CHECK(args
.Length() >= 2 && args
[0]->IsString() && args
[1]->IsString());
97 std::string extension_id
= *v8::String::Utf8Value(args
[0]);
98 std::string native_app_name
= *v8::String::Utf8Value(args
[1]);
101 render_frame
->Send(new ExtensionHostMsg_OpenChannelToNativeApp(
102 render_frame
->GetRoutingID(), extension_id
, native_app_name
, &port_id
));
103 args
.GetReturnValue().Set(static_cast<int32_t>(port_id
));
106 void RuntimeCustomBindings::GetManifest(
107 const v8::FunctionCallbackInfo
<v8::Value
>& args
) {
108 CHECK(context()->extension());
110 scoped_ptr
<V8ValueConverter
> converter(V8ValueConverter::create());
111 args
.GetReturnValue().Set(converter
->ToV8Value(
112 context()->extension()->manifest()->value(), context()->v8_context()));
115 void RuntimeCustomBindings::GetExtensionViews(
116 const v8::FunctionCallbackInfo
<v8::Value
>& args
) {
117 if (args
.Length() != 2)
120 if (!args
[0]->IsInt32() || !args
[1]->IsString())
123 // |browser_window_id| == extension_misc::kUnknownWindowId means getting
124 // all views for the current extension.
125 int browser_window_id
= args
[0]->Int32Value();
127 std::string view_type_string
=
128 base::ToUpperASCII(*v8::String::Utf8Value(args
[1]));
129 // |view_type| == VIEW_TYPE_INVALID means getting any type of
131 ViewType view_type
= VIEW_TYPE_INVALID
;
132 if (view_type_string
== kViewTypeBackgroundPage
) {
133 view_type
= VIEW_TYPE_EXTENSION_BACKGROUND_PAGE
;
134 } else if (view_type_string
== kViewTypeTabContents
) {
135 view_type
= VIEW_TYPE_TAB_CONTENTS
;
136 } else if (view_type_string
== kViewTypePopup
) {
137 view_type
= VIEW_TYPE_EXTENSION_POPUP
;
138 } else if (view_type_string
== kViewTypeExtensionDialog
) {
139 view_type
= VIEW_TYPE_EXTENSION_DIALOG
;
140 } else if (view_type_string
== kViewTypeAppWindow
) {
141 view_type
= VIEW_TYPE_APP_WINDOW
;
142 } else if (view_type_string
== kViewTypeLauncherPage
) {
143 view_type
= VIEW_TYPE_LAUNCHER_PAGE
;
144 } else if (view_type_string
== kViewTypePanel
) {
145 view_type
= VIEW_TYPE_PANEL
;
146 } else if (view_type_string
!= kViewTypeAll
) {
150 std::string extension_id
= context()->GetExtensionID();
151 if (extension_id
.empty())
154 std::vector
<content::RenderFrame
*> frames
=
155 ExtensionFrameHelper::GetExtensionFrames(extension_id
, browser_window_id
,
157 v8::Local
<v8::Array
> v8_views
= v8::Array::New(args
.GetIsolate());
159 for (content::RenderFrame
* frame
: frames
) {
160 // We filter out iframes here. GetExtensionViews should only return the
161 // main views, not any subframes. (Returning subframes can cause broken
162 // behavior by treating an app window's iframe as its main frame, and maybe
164 if (frame
->GetWebFrame()->top() != frame
->GetWebFrame())
167 v8::Local
<v8::Context
> context
=
168 frame
->GetWebFrame()->mainWorldScriptContext();
169 if (!context
.IsEmpty()) {
170 v8::Local
<v8::Value
> window
= context
->Global();
171 DCHECK(!window
.IsEmpty());
172 v8_views
->Set(v8::Integer::New(args
.GetIsolate(), v8_index
++), window
);
176 args
.GetReturnValue().Set(v8_views
);
179 } // namespace extensions