[email protected] | e689367 | 2014-05-01 17:29:13 | [diff] [blame] | 1 | // Copyright 2014 The Chromium Authors. All rights reserved. |
[email protected] | 120028b9c | 2012-07-03 01:32:24 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | e689367 | 2014-05-01 17:29:13 | [diff] [blame] | 5 | #include "extensions/renderer/runtime_custom_bindings.h" |
[email protected] | 120028b9c | 2012-07-03 01:32:24 | [diff] [blame] | 6 | |
avi | 2d124c0 | 2015-12-23 06:36:42 | [diff] [blame] | 7 | #include <stdint.h> |
| 8 | |
dcheng | f6f8066 | 2016-04-20 20:26:04 | [diff] [blame] | 9 | #include <memory> |
| 10 | |
[email protected] | 120028b9c | 2012-07-03 01:32:24 | [diff] [blame] | 11 | #include "base/bind.h" |
[email protected] | 120028b9c | 2012-07-03 01:32:24 | [diff] [blame] | 12 | #include "base/values.h" |
mek | 87e0ab5 | 2015-02-13 01:18:26 | [diff] [blame] | 13 | #include "content/public/child/v8_value_converter.h" |
rob | 248d6a8 | 2014-11-21 02:04:48 | [diff] [blame] | 14 | #include "content/public/renderer/render_frame.h" |
[email protected] | e4452d3 | 2013-11-15 23:07:41 | [diff] [blame] | 15 | #include "extensions/common/extension.h" |
[email protected] | fb820c0 | 2014-03-13 15:07:08 | [diff] [blame] | 16 | #include "extensions/common/extension_messages.h" |
[email protected] | d42c1115 | 2013-08-22 19:36:32 | [diff] [blame] | 17 | #include "extensions/common/manifest.h" |
rdevlin.cronin | 6f42c252 | 2015-06-19 18:58:51 | [diff] [blame] | 18 | #include "extensions/renderer/extension_frame_helper.h" |
[email protected] | bcd9580f | 2014-04-17 19:17:59 | [diff] [blame] | 19 | #include "extensions/renderer/script_context.h" |
rdevlin.cronin | 6f42c252 | 2015-06-19 18:58:51 | [diff] [blame] | 20 | #include "third_party/WebKit/public/web/WebLocalFrame.h" |
[email protected] | 120028b9c | 2012-07-03 01:32:24 | [diff] [blame] | 21 | |
| 22 | namespace extensions { |
| 23 | |
[email protected] | bcd9580f | 2014-04-17 19:17:59 | [diff] [blame] | 24 | RuntimeCustomBindings::RuntimeCustomBindings(ScriptContext* context) |
| 25 | : ObjectBackedNativeHandler(context) { |
[email protected] | e689367 | 2014-05-01 17:29:13 | [diff] [blame] | 26 | RouteFunction( |
| 27 | "GetManifest", |
| 28 | base::Bind(&RuntimeCustomBindings::GetManifest, base::Unretained(this))); |
[email protected] | c1e5fb1 | 2013-11-13 03:40:31 | [diff] [blame] | 29 | RouteFunction("GetExtensionViews", |
| 30 | base::Bind(&RuntimeCustomBindings::GetExtensionViews, |
| 31 | base::Unretained(this))); |
[email protected] | 120028b9c | 2012-07-03 01:32:24 | [diff] [blame] | 32 | } |
| 33 | |
[email protected] | e689367 | 2014-05-01 17:29:13 | [diff] [blame] | 34 | RuntimeCustomBindings::~RuntimeCustomBindings() { |
| 35 | } |
[email protected] | 120028b9c | 2012-07-03 01:32:24 | [diff] [blame] | 36 | |
[email protected] | d8c5fbb | 2013-06-14 11:35:25 | [diff] [blame] | 37 | void RuntimeCustomBindings::GetManifest( |
| 38 | const v8::FunctionCallbackInfo<v8::Value>& args) { |
[email protected] | 9a59844 | 2013-06-04 16:39:12 | [diff] [blame] | 39 | CHECK(context()->extension()); |
[email protected] | 120028b9c | 2012-07-03 01:32:24 | [diff] [blame] | 40 | |
rdevlin.cronin | c7ce4fc | 2016-05-10 01:41:37 | [diff] [blame] | 41 | std::unique_ptr<content::V8ValueConverter> converter( |
| 42 | content::V8ValueConverter::create()); |
[email protected] | e689367 | 2014-05-01 17:29:13 | [diff] [blame] | 43 | args.GetReturnValue().Set(converter->ToV8Value( |
| 44 | context()->extension()->manifest()->value(), context()->v8_context())); |
[email protected] | 120028b9c | 2012-07-03 01:32:24 | [diff] [blame] | 45 | } |
| 46 | |
[email protected] | c1e5fb1 | 2013-11-13 03:40:31 | [diff] [blame] | 47 | void RuntimeCustomBindings::GetExtensionViews( |
| 48 | const v8::FunctionCallbackInfo<v8::Value>& args) { |
catmullings | 15fd52b | 2016-07-14 23:46:59 | [diff] [blame] | 49 | CHECK_EQ(args.Length(), 3); |
rdevlin.cronin | c7ce4fc | 2016-05-10 01:41:37 | [diff] [blame] | 50 | CHECK(args[0]->IsInt32()); |
catmullings | 15fd52b | 2016-07-14 23:46:59 | [diff] [blame] | 51 | CHECK(args[1]->IsInt32()); |
| 52 | CHECK(args[2]->IsString()); |
[email protected] | c1e5fb1 | 2013-11-13 03:40:31 | [diff] [blame] | 53 | |
| 54 | // |browser_window_id| == extension_misc::kUnknownWindowId means getting |
| 55 | // all views for the current extension. |
| 56 | int browser_window_id = args[0]->Int32Value(); |
catmullings | 15fd52b | 2016-07-14 23:46:59 | [diff] [blame] | 57 | int tab_id = args[1]->Int32Value(); |
[email protected] | c1e5fb1 | 2013-11-13 03:40:31 | [diff] [blame] | 58 | |
brettw | c15100c | 2015-08-06 22:54:16 | [diff] [blame] | 59 | std::string view_type_string = |
catmullings | 15fd52b | 2016-07-14 23:46:59 | [diff] [blame] | 60 | base::ToUpperASCII(*v8::String::Utf8Value(args[2])); |
[email protected] | c1e5fb1 | 2013-11-13 03:40:31 | [diff] [blame] | 61 | // |view_type| == VIEW_TYPE_INVALID means getting any type of |
| 62 | // views. |
| 63 | ViewType view_type = VIEW_TYPE_INVALID; |
| 64 | if (view_type_string == kViewTypeBackgroundPage) { |
| 65 | view_type = VIEW_TYPE_EXTENSION_BACKGROUND_PAGE; |
[email protected] | c1e5fb1 | 2013-11-13 03:40:31 | [diff] [blame] | 66 | } else if (view_type_string == kViewTypeTabContents) { |
| 67 | view_type = VIEW_TYPE_TAB_CONTENTS; |
| 68 | } else if (view_type_string == kViewTypePopup) { |
| 69 | view_type = VIEW_TYPE_EXTENSION_POPUP; |
| 70 | } else if (view_type_string == kViewTypeExtensionDialog) { |
| 71 | view_type = VIEW_TYPE_EXTENSION_DIALOG; |
[email protected] | ddcf580 | 2014-02-20 23:21:32 | [diff] [blame] | 72 | } else if (view_type_string == kViewTypeAppWindow) { |
| 73 | view_type = VIEW_TYPE_APP_WINDOW; |
[email protected] | 9faae96 | 2014-08-11 07:59:19 | [diff] [blame] | 74 | } else if (view_type_string == kViewTypeLauncherPage) { |
| 75 | view_type = VIEW_TYPE_LAUNCHER_PAGE; |
[email protected] | c1e5fb1 | 2013-11-13 03:40:31 | [diff] [blame] | 76 | } else if (view_type_string == kViewTypePanel) { |
| 77 | view_type = VIEW_TYPE_PANEL; |
rdevlin.cronin | c7ce4fc | 2016-05-10 01:41:37 | [diff] [blame] | 78 | } else { |
| 79 | CHECK_EQ(view_type_string, kViewTypeAll); |
[email protected] | c1e5fb1 | 2013-11-13 03:40:31 | [diff] [blame] | 80 | } |
| 81 | |
rdevlin.cronin | c7ce4fc | 2016-05-10 01:41:37 | [diff] [blame] | 82 | const std::string& extension_id = context()->GetExtensionID(); |
[email protected] | c1e5fb1 | 2013-11-13 03:40:31 | [diff] [blame] | 83 | if (extension_id.empty()) |
| 84 | return; |
| 85 | |
rdevlin.cronin | 6f42c252 | 2015-06-19 18:58:51 | [diff] [blame] | 86 | std::vector<content::RenderFrame*> frames = |
| 87 | ExtensionFrameHelper::GetExtensionFrames(extension_id, browser_window_id, |
catmullings | 15fd52b | 2016-07-14 23:46:59 | [diff] [blame] | 88 | tab_id, view_type); |
rob | aa7a889 | 2016-05-02 16:18:37 | [diff] [blame] | 89 | v8::Local<v8::Context> v8_context = args.GetIsolate()->GetCurrentContext(); |
[email protected] | 95c6b301 | 2013-12-02 14:30:31 | [diff] [blame] | 90 | v8::Local<v8::Array> v8_views = v8::Array::New(args.GetIsolate()); |
[email protected] | c1e5fb1 | 2013-11-13 03:40:31 | [diff] [blame] | 91 | int v8_index = 0; |
rdevlin.cronin | 6f42c252 | 2015-06-19 18:58:51 | [diff] [blame] | 92 | for (content::RenderFrame* frame : frames) { |
rdevlin.cronin | 9e9f59a | 2015-06-24 23:49:07 | [diff] [blame] | 93 | // We filter out iframes here. GetExtensionViews should only return the |
| 94 | // main views, not any subframes. (Returning subframes can cause broken |
| 95 | // behavior by treating an app window's iframe as its main frame, and maybe |
| 96 | // other nastiness). |
rdevlin.cronin | c7ce4fc | 2016-05-10 01:41:37 | [diff] [blame] | 97 | blink::WebFrame* web_frame = frame->GetWebFrame(); |
| 98 | if (web_frame->top() != web_frame) |
rdevlin.cronin | 9e9f59a | 2015-06-24 23:49:07 | [diff] [blame] | 99 | continue; |
| 100 | |
rdevlin.cronin | c7ce4fc | 2016-05-10 01:41:37 | [diff] [blame] | 101 | if (!blink::WebFrame::scriptCanAccess(web_frame)) |
| 102 | continue; |
| 103 | |
| 104 | v8::Local<v8::Context> context = web_frame->mainWorldScriptContext(); |
[email protected] | c1e5fb1 | 2013-11-13 03:40:31 | [diff] [blame] | 105 | if (!context.IsEmpty()) { |
| 106 | v8::Local<v8::Value> window = context->Global(); |
rdevlin.cronin | c7ce4fc | 2016-05-10 01:41:37 | [diff] [blame] | 107 | CHECK(!window.IsEmpty()); |
rob | aa7a889 | 2016-05-02 16:18:37 | [diff] [blame] | 108 | v8::Maybe<bool> maybe = |
rdevlin.cronin | c7ce4fc | 2016-05-10 01:41:37 | [diff] [blame] | 109 | v8_views->CreateDataProperty(v8_context, v8_index++, window); |
| 110 | CHECK(maybe.IsJust() && maybe.FromJust()); |
[email protected] | c1e5fb1 | 2013-11-13 03:40:31 | [diff] [blame] | 111 | } |
| 112 | } |
| 113 | |
| 114 | args.GetReturnValue().Set(v8_views); |
| 115 | } |
| 116 | |
[email protected] | bcdd992f | 2013-06-09 12:58:03 | [diff] [blame] | 117 | } // namespace extensions |