Move extension_messages.h to extensions/common.
[chromium-blink-merge.git] / chrome / renderer / extensions / runtime_custom_bindings.cc
blobac6fb400908a9ccfdbda20a428316650cae952da
1 // 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.
5 #include "chrome/renderer/extensions/runtime_custom_bindings.h"
7 #include "base/bind.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "base/values.h"
10 #include "chrome/renderer/extensions/api_activity_logger.h"
11 #include "chrome/renderer/extensions/chrome_v8_context.h"
12 #include "chrome/renderer/extensions/dispatcher.h"
13 #include "chrome/renderer/extensions/extension_helper.h"
14 #include "content/public/renderer/render_view.h"
15 #include "content/public/renderer/v8_value_converter.h"
16 #include "extensions/common/extension.h"
17 #include "extensions/common/extension_messages.h"
18 #include "extensions/common/features/feature.h"
19 #include "extensions/common/features/feature_provider.h"
20 #include "extensions/common/manifest.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(Dispatcher* dispatcher,
30 ChromeV8Context* context)
31 : ChromeV8Extension(dispatcher, context) {
32 RouteFunction("GetManifest",
33 base::Bind(&RuntimeCustomBindings::GetManifest,
34 base::Unretained(this)));
35 RouteFunction("OpenChannelToExtension",
36 base::Bind(&RuntimeCustomBindings::OpenChannelToExtension,
37 base::Unretained(this)));
38 RouteFunction("OpenChannelToNativeApp",
39 base::Bind(&RuntimeCustomBindings::OpenChannelToNativeApp,
40 base::Unretained(this)));
41 RouteFunction("GetExtensionViews",
42 base::Bind(&RuntimeCustomBindings::GetExtensionViews,
43 base::Unretained(this)));
46 RuntimeCustomBindings::~RuntimeCustomBindings() {}
48 void RuntimeCustomBindings::OpenChannelToExtension(
49 const v8::FunctionCallbackInfo<v8::Value>& args) {
50 // Get the current RenderView so that we can send a routed IPC message from
51 // the correct source.
52 content::RenderView* renderview = GetRenderView();
53 if (!renderview)
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]->ToString());
69 info.source_url = context()->GetURL();
70 std::string channel_name = *v8::String::Utf8Value(args[1]->ToString());
71 bool include_tls_channel_id =
72 args.Length() > 2 ? args[2]->BooleanValue() : false;
73 int port_id = -1;
74 renderview->Send(new ExtensionHostMsg_OpenChannelToExtension(
75 renderview->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")->IsAvailableToContext(
86 GetExtensionForRenderView(),
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 = GetRenderView();
95 if (!renderview)
96 return;
98 // The Javascript code should validate/fill the arguments.
99 CHECK(args.Length() >= 2 &&
100 args[0]->IsString() &&
101 args[1]->IsString());
103 std::string extension_id = *v8::String::Utf8Value(args[0]->ToString());
104 std::string native_app_name = *v8::String::Utf8Value(args[1]->ToString());
106 int port_id = -1;
107 renderview->Send(new ExtensionHostMsg_OpenChannelToNativeApp(
108 renderview->GetRoutingID(),
109 extension_id,
110 native_app_name,
111 &port_id));
112 args.GetReturnValue().Set(static_cast<int32_t>(port_id));
115 void RuntimeCustomBindings::GetManifest(
116 const v8::FunctionCallbackInfo<v8::Value>& args) {
117 CHECK(context()->extension());
119 scoped_ptr<V8ValueConverter> converter(V8ValueConverter::create());
120 args.GetReturnValue().Set(
121 converter->ToV8Value(context()->extension()->manifest()->value(),
122 context()->v8_context()));
125 void RuntimeCustomBindings::GetExtensionViews(
126 const v8::FunctionCallbackInfo<v8::Value>& args) {
127 if (args.Length() != 2)
128 return;
130 if (!args[0]->IsInt32() || !args[1]->IsString())
131 return;
133 // |browser_window_id| == extension_misc::kUnknownWindowId means getting
134 // all views for the current extension.
135 int browser_window_id = args[0]->Int32Value();
137 std::string view_type_string = *v8::String::Utf8Value(args[1]->ToString());
138 StringToUpperASCII(&view_type_string);
139 // |view_type| == VIEW_TYPE_INVALID means getting any type of
140 // views.
141 ViewType view_type = VIEW_TYPE_INVALID;
142 if (view_type_string == kViewTypeBackgroundPage) {
143 view_type = VIEW_TYPE_EXTENSION_BACKGROUND_PAGE;
144 } else if (view_type_string == kViewTypeInfobar) {
145 view_type = VIEW_TYPE_EXTENSION_INFOBAR;
146 } else if (view_type_string == kViewTypeNotification) {
147 view_type = VIEW_TYPE_NOTIFICATION;
148 } else if (view_type_string == kViewTypeTabContents) {
149 view_type = VIEW_TYPE_TAB_CONTENTS;
150 } else if (view_type_string == kViewTypePopup) {
151 view_type = VIEW_TYPE_EXTENSION_POPUP;
152 } else if (view_type_string == kViewTypeExtensionDialog) {
153 view_type = VIEW_TYPE_EXTENSION_DIALOG;
154 } else if (view_type_string == kViewTypeAppWindow) {
155 view_type = VIEW_TYPE_APP_WINDOW;
156 } else if (view_type_string == kViewTypePanel) {
157 view_type = VIEW_TYPE_PANEL;
158 } else if (view_type_string != kViewTypeAll) {
159 return;
162 std::string extension_id = context()->GetExtensionID();
163 if (extension_id.empty())
164 return;
166 std::vector<content::RenderView*> views = ExtensionHelper::GetExtensionViews(
167 extension_id, browser_window_id, view_type);
168 v8::Local<v8::Array> v8_views = v8::Array::New(args.GetIsolate());
169 int v8_index = 0;
170 for (size_t i = 0; i < views.size(); ++i) {
171 v8::Local<v8::Context> context =
172 views[i]->GetWebView()->mainFrame()->mainWorldScriptContext();
173 if (!context.IsEmpty()) {
174 v8::Local<v8::Value> window = context->Global();
175 DCHECK(!window.IsEmpty());
176 v8_views->Set(v8::Integer::New(args.GetIsolate(), v8_index++), window);
180 args.GetReturnValue().Set(v8_views);
183 } // namespace extensions