[email protected] | 1eea309 | 2012-05-25 04:12:18 | [diff] [blame] | 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. | ||||
4 | |||||
hashimoto | f784afd | 2014-09-05 02:38:31 | [diff] [blame] | 5 | #include "extensions/renderer/app_window_custom_bindings.h" |
[email protected] | 1eea309 | 2012-05-25 04:12:18 | [diff] [blame] | 6 | |
[email protected] | 1f4da9e | 2013-06-27 05:23:44 | [diff] [blame] | 7 | #include "base/command_line.h" |
mek | 87e0ab5 | 2015-02-13 01:18:26 | [diff] [blame] | 8 | #include "content/public/child/v8_value_converter.h" |
rdevlin.cronin | 25fcbdd6 | 2015-07-07 14:11:59 | [diff] [blame] | 9 | #include "content/public/renderer/render_frame.h" |
[email protected] | eb4bcac | 2012-06-27 01:32:08 | [diff] [blame] | 10 | #include "content/public/renderer/render_thread.h" |
[email protected] | 1eea309 | 2012-05-25 04:12:18 | [diff] [blame] | 11 | #include "content/public/renderer/render_view.h" |
[email protected] | fb820c0 | 2014-03-13 15:07:08 | [diff] [blame] | 12 | #include "extensions/common/extension_messages.h" |
hashimoto | f784afd | 2014-09-05 02:38:31 | [diff] [blame] | 13 | #include "extensions/common/switches.h" |
[email protected] | bcd9580f | 2014-04-17 19:17:59 | [diff] [blame] | 14 | #include "extensions/renderer/script_context.h" |
hashimoto | f784afd | 2014-09-05 02:38:31 | [diff] [blame] | 15 | #include "grit/extensions_renderer_resources.h" |
[email protected] | 8050465 | 2014-04-18 04:41:50 | [diff] [blame] | 16 | #include "third_party/WebKit/public/web/WebLocalFrame.h" |
[email protected] | 2255a933 | 2013-06-17 05:12:31 | [diff] [blame] | 17 | #include "third_party/WebKit/public/web/WebView.h" |
[email protected] | 1f4da9e | 2013-06-27 05:23:44 | [diff] [blame] | 18 | #include "ui/base/resource/resource_bundle.h" |
[email protected] | 2bcbf71 | 2012-09-05 06:58:49 | [diff] [blame] | 19 | #include "v8/include/v8.h" |
[email protected] | 1eea309 | 2012-05-25 04:12:18 | [diff] [blame] | 20 | |
21 | namespace extensions { | ||||
22 | |||||
rob | aefd358 | 2016-02-15 11:06:26 | [diff] [blame] | 23 | AppWindowCustomBindings::AppWindowCustomBindings(ScriptContext* context) |
24 | : ObjectBackedNativeHandler(context) { | ||||
rdevlin.cronin | c827ac2 | 2016-05-09 14:17:49 | [diff] [blame] | 25 | RouteFunction("GetFrame", base::Bind(&AppWindowCustomBindings::GetFrame, |
26 | base::Unretained(this))); | ||||
[email protected] | 1f4da9e | 2013-06-27 05:23:44 | [diff] [blame] | 27 | |
28 | RouteFunction("GetWindowControlsHtmlTemplate", | ||||
29 | base::Bind(&AppWindowCustomBindings::GetWindowControlsHtmlTemplate, | ||||
30 | base::Unretained(this))); | ||||
[email protected] | a443048 | 2012-10-23 07:31:07 | [diff] [blame] | 31 | } |
32 | |||||
rdevlin.cronin | c3d6ba1b1 | 2015-07-09 17:36:59 | [diff] [blame] | 33 | void AppWindowCustomBindings::GetFrame( |
[email protected] | d8c5fbb | 2013-06-14 11:35:25 | [diff] [blame] | 34 | const v8::FunctionCallbackInfo<v8::Value>& args) { |
[email protected] | 1eea309 | 2012-05-25 04:12:18 | [diff] [blame] | 35 | // TODO(jeremya): convert this to IDL nocompile to get validation, and turn |
36 | // these argument checks into CHECK(). | ||||
shuchen | 70a6b07 | 2016-02-23 10:09:00 | [diff] [blame] | 37 | if (args.Length() != 2) |
[email protected] | d8c5fbb | 2013-06-14 11:35:25 | [diff] [blame] | 38 | return; |
[email protected] | 1eea309 | 2012-05-25 04:12:18 | [diff] [blame] | 39 | |
shuchen | 70a6b07 | 2016-02-23 10:09:00 | [diff] [blame] | 40 | if (!args[0]->IsInt32() || !args[1]->IsBoolean()) |
[email protected] | d8c5fbb | 2013-06-14 11:35:25 | [diff] [blame] | 41 | return; |
[email protected] | 1eea309 | 2012-05-25 04:12:18 | [diff] [blame] | 42 | |
rdevlin.cronin | c3d6ba1b1 | 2015-07-09 17:36:59 | [diff] [blame] | 43 | int frame_id = args[0]->Int32Value(); |
shuchen | 70a6b07 | 2016-02-23 10:09:00 | [diff] [blame] | 44 | bool notify_browser = args[1]->BooleanValue(); |
[email protected] | 1eea309 | 2012-05-25 04:12:18 | [diff] [blame] | 45 | |
rdevlin.cronin | c3d6ba1b1 | 2015-07-09 17:36:59 | [diff] [blame] | 46 | if (frame_id == MSG_ROUTING_NONE) |
[email protected] | d8c5fbb | 2013-06-14 11:35:25 | [diff] [blame] | 47 | return; |
[email protected] | 1eea309 | 2012-05-25 04:12:18 | [diff] [blame] | 48 | |
rdevlin.cronin | c3d6ba1b1 | 2015-07-09 17:36:59 | [diff] [blame] | 49 | content::RenderFrame* app_frame = |
50 | content::RenderFrame::FromRoutingID(frame_id); | ||||
51 | if (!app_frame) | ||||
[email protected] | d8c5fbb | 2013-06-14 11:35:25 | [diff] [blame] | 52 | return; |
[email protected] | 1eea309 | 2012-05-25 04:12:18 | [diff] [blame] | 53 | |
shuchen | 70a6b07 | 2016-02-23 10:09:00 | [diff] [blame] | 54 | if (notify_browser) { |
55 | content::RenderThread::Get()->Send(new ExtensionHostMsg_AppWindowReady( | ||||
56 | app_frame->GetRenderView()->GetRoutingID())); | ||||
57 | } | ||||
rdevlin.cronin | c3d6ba1b1 | 2015-07-09 17:36:59 | [diff] [blame] | 58 | |
59 | v8::Local<v8::Value> window = | ||||
dcheng | 9e24bd35 | 2016-03-01 19:15:51 | [diff] [blame] | 60 | app_frame->GetWebFrame()->mainWorldScriptContext()->Global(); |
[email protected] | d8c5fbb | 2013-06-14 11:35:25 | [diff] [blame] | 61 | args.GetReturnValue().Set(window); |
[email protected] | 1eea309 | 2012-05-25 04:12:18 | [diff] [blame] | 62 | } |
63 | |||||
[email protected] | 1f4da9e | 2013-06-27 05:23:44 | [diff] [blame] | 64 | void AppWindowCustomBindings::GetWindowControlsHtmlTemplate( |
65 | const v8::FunctionCallbackInfo<v8::Value>& args) { | ||||
66 | CHECK_EQ(args.Length(), 0); | ||||
67 | |||||
tfarina | f85316f | 2015-04-29 17:03:40 | [diff] [blame] | 68 | v8::Local<v8::Value> result = v8::String::Empty(args.GetIsolate()); |
avi | 9ab03720 | 2014-12-22 23:49:53 | [diff] [blame] | 69 | if (base::CommandLine::ForCurrentProcess()->HasSwitch( |
70 | switches::kEnableAppWindowControls)) { | ||||
[email protected] | 1aea526 | 2014-07-09 04:55:33 | [diff] [blame] | 71 | base::StringValue value( |
72 | ResourceBundle::GetSharedInstance() | ||||
73 | .GetRawDataResource(IDR_WINDOW_CONTROLS_TEMPLATE_HTML) | ||||
74 | .as_string()); | ||||
dcheng | f6f8066 | 2016-04-20 20:26:04 | [diff] [blame] | 75 | std::unique_ptr<content::V8ValueConverter> converter( |
[email protected] | 1f4da9e | 2013-06-27 05:23:44 | [diff] [blame] | 76 | content::V8ValueConverter::create()); |
[email protected] | 1aea526 | 2014-07-09 04:55:33 | [diff] [blame] | 77 | result = converter->ToV8Value(&value, context()->v8_context()); |
[email protected] | 1f4da9e | 2013-06-27 05:23:44 | [diff] [blame] | 78 | } |
79 | args.GetReturnValue().Set(result); | ||||
80 | } | ||||
81 | |||||
[email protected] | 1eea309 | 2012-05-25 04:12:18 | [diff] [blame] | 82 | } // namespace extensions |