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()->GetURL();
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(),
88 if (!availability
.is_available())
91 content::RenderFrame
* render_frame
= context()->GetRenderFrame();
95 // The Javascript code should validate/fill the arguments.
96 CHECK(args
.Length() >= 2 && args
[0]->IsString() && args
[1]->IsString());
98 std::string extension_id
= *v8::String::Utf8Value(args
[0]);
99 std::string native_app_name
= *v8::String::Utf8Value(args
[1]);
102 render_frame
->Send(new ExtensionHostMsg_OpenChannelToNativeApp(
103 render_frame
->GetRoutingID(), extension_id
, native_app_name
, &port_id
));
104 args
.GetReturnValue().Set(static_cast<int32_t>(port_id
));
107 void RuntimeCustomBindings::GetManifest(
108 const v8::FunctionCallbackInfo
<v8::Value
>& args
) {
109 CHECK(context()->extension());
111 scoped_ptr
<V8ValueConverter
> converter(V8ValueConverter::create());
112 args
.GetReturnValue().Set(converter
->ToV8Value(
113 context()->extension()->manifest()->value(), context()->v8_context()));
116 void RuntimeCustomBindings::GetExtensionViews(
117 const v8::FunctionCallbackInfo
<v8::Value
>& args
) {
118 if (args
.Length() != 2)
121 if (!args
[0]->IsInt32() || !args
[1]->IsString())
124 // |browser_window_id| == extension_misc::kUnknownWindowId means getting
125 // all views for the current extension.
126 int browser_window_id
= args
[0]->Int32Value();
128 std::string view_type_string
= *v8::String::Utf8Value(args
[1]);
129 base::StringToUpperASCII(&view_type_string
);
130 // |view_type| == VIEW_TYPE_INVALID means getting any type of
132 ViewType view_type
= VIEW_TYPE_INVALID
;
133 if (view_type_string
== kViewTypeBackgroundPage
) {
134 view_type
= VIEW_TYPE_EXTENSION_BACKGROUND_PAGE
;
135 } else if (view_type_string
== kViewTypeTabContents
) {
136 view_type
= VIEW_TYPE_TAB_CONTENTS
;
137 } else if (view_type_string
== kViewTypePopup
) {
138 view_type
= VIEW_TYPE_EXTENSION_POPUP
;
139 } else if (view_type_string
== kViewTypeExtensionDialog
) {
140 view_type
= VIEW_TYPE_EXTENSION_DIALOG
;
141 } else if (view_type_string
== kViewTypeAppWindow
) {
142 view_type
= VIEW_TYPE_APP_WINDOW
;
143 } else if (view_type_string
== kViewTypeLauncherPage
) {
144 view_type
= VIEW_TYPE_LAUNCHER_PAGE
;
145 } else if (view_type_string
== kViewTypePanel
) {
146 view_type
= VIEW_TYPE_PANEL
;
147 } else if (view_type_string
!= kViewTypeAll
) {
151 std::string extension_id
= context()->GetExtensionID();
152 if (extension_id
.empty())
155 std::vector
<content::RenderFrame
*> frames
=
156 ExtensionFrameHelper::GetExtensionFrames(extension_id
, browser_window_id
,
158 v8::Local
<v8::Array
> v8_views
= v8::Array::New(args
.GetIsolate());
160 for (content::RenderFrame
* frame
: frames
) {
161 // We filter out iframes here. GetExtensionViews should only return the
162 // main views, not any subframes. (Returning subframes can cause broken
163 // behavior by treating an app window's iframe as its main frame, and maybe
165 if (frame
->GetWebFrame()->top() != frame
->GetWebFrame())
168 v8::Local
<v8::Context
> context
=
169 frame
->GetWebFrame()->mainWorldScriptContext();
170 if (!context
.IsEmpty()) {
171 v8::Local
<v8::Value
> window
= context
->Global();
172 DCHECK(!window
.IsEmpty());
173 v8_views
->Set(v8::Integer::New(args
.GetIsolate(), v8_index
++), window
);
177 args
.GetReturnValue().Set(v8_views
);
180 } // namespace extensions