Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / extensions / renderer / runtime_custom_bindings.cc
bloba7a5ff6cf4fd3b1262e4f56dae03e89506ed837d
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 "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) {
30 RouteFunction(
31 "GetManifest",
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();
52 if (!renderframe)
53 return;
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
62 // its extension ID.
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;
72 int port_id = -1;
73 renderframe->Send(new ExtensionHostMsg_OpenChannelToExtension(
74 renderframe->GetRoutingID(), info, channel_name, include_tls_channel_id,
75 &port_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(),
87 context()->GetURL());
88 if (!availability.is_available())
89 return;
91 content::RenderFrame* render_frame = context()->GetRenderFrame();
92 if (!render_frame)
93 return;
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]);
101 int port_id = -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)
119 return;
121 if (!args[0]->IsInt32() || !args[1]->IsString())
122 return;
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
131 // views.
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) {
148 return;
151 std::string extension_id = context()->GetExtensionID();
152 if (extension_id.empty())
153 return;
155 std::vector<content::RenderFrame*> frames =
156 ExtensionFrameHelper::GetExtensionFrames(extension_id, browser_window_id,
157 view_type);
158 v8::Local<v8::Array> v8_views = v8::Array::New(args.GetIsolate());
159 int v8_index = 0;
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
164 // other nastiness).
165 if (frame->GetWebFrame()->top() != frame->GetWebFrame())
166 continue;
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