[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] | e4452d3 | 2013-11-15 23:07:41 | [diff] [blame] | 12 | #include "extensions/common/extension.h" |
rdevlin.cronin | 6f42c252 | 2015-06-19 18:58:51 | [diff] [blame] | 13 | #include "extensions/renderer/extension_frame_helper.h" |
[email protected] | bcd9580f | 2014-04-17 19:17:59 | [diff] [blame] | 14 | #include "extensions/renderer/script_context.h" |
[email protected] | 120028b9c | 2012-07-03 01:32:24 | [diff] [blame] | 15 | |
| 16 | namespace extensions { |
| 17 | |
[email protected] | bcd9580f | 2014-04-17 19:17:59 | [diff] [blame] | 18 | RuntimeCustomBindings::RuntimeCustomBindings(ScriptContext* context) |
Devlin Cronin | d9ea834 | 2018-01-27 06:00:04 | [diff] [blame] | 19 | : ObjectBackedNativeHandler(context) {} |
| 20 | |
| 21 | RuntimeCustomBindings::~RuntimeCustomBindings() {} |
| 22 | |
| 23 | void RuntimeCustomBindings::AddRoutes() { |
| 24 | RouteHandlerFunction( |
Devlin Cronin | cc02a0c | 2019-01-03 22:15:07 | [diff] [blame] | 25 | "GetExtensionViews", |
| 26 | base::BindRepeating(&RuntimeCustomBindings::GetExtensionViews, |
| 27 | base::Unretained(this))); |
[email protected] | e689367 | 2014-05-01 17:29:13 | [diff] [blame] | 28 | } |
[email protected] | 120028b9c | 2012-07-03 01:32:24 | [diff] [blame] | 29 | |
[email protected] | c1e5fb1 | 2013-11-13 03:40:31 | [diff] [blame] | 30 | void RuntimeCustomBindings::GetExtensionViews( |
| 31 | const v8::FunctionCallbackInfo<v8::Value>& args) { |
catmullings | 15fd52b | 2016-07-14 23:46:59 | [diff] [blame] | 32 | CHECK_EQ(args.Length(), 3); |
rdevlin.cronin | c7ce4fc | 2016-05-10 01:41:37 | [diff] [blame] | 33 | CHECK(args[0]->IsInt32()); |
catmullings | 15fd52b | 2016-07-14 23:46:59 | [diff] [blame] | 34 | CHECK(args[1]->IsInt32()); |
| 35 | CHECK(args[2]->IsString()); |
[email protected] | c1e5fb1 | 2013-11-13 03:40:31 | [diff] [blame] | 36 | |
| 37 | // |browser_window_id| == extension_misc::kUnknownWindowId means getting |
| 38 | // all views for the current extension. |
Dan Elphick | d010a85a | 2018-08-03 11:32:26 | [diff] [blame] | 39 | int browser_window_id = args[0].As<v8::Int32>()->Value(); |
| 40 | int tab_id = args[1].As<v8::Int32>()->Value(); |
[email protected] | c1e5fb1 | 2013-11-13 03:40:31 | [diff] [blame] | 41 | |
brettw | c15100c | 2015-08-06 22:54:16 | [diff] [blame] | 42 | std::string view_type_string = |
Adam Klein | 686c0e5 | 2018-01-17 23:42:33 | [diff] [blame] | 43 | base::ToUpperASCII(*v8::String::Utf8Value(args.GetIsolate(), args[2])); |
[email protected] | c1e5fb1 | 2013-11-13 03:40:31 | [diff] [blame] | 44 | // |view_type| == VIEW_TYPE_INVALID means getting any type of |
| 45 | // views. |
| 46 | ViewType view_type = VIEW_TYPE_INVALID; |
Devlin Cronin | be3421d | 2017-11-10 20:32:07 | [diff] [blame] | 47 | bool parsed_view_type = GetViewTypeFromString(view_type_string, &view_type); |
| 48 | if (!parsed_view_type) |
| 49 | CHECK_EQ("ALL", view_type_string); |
[email protected] | c1e5fb1 | 2013-11-13 03:40:31 | [diff] [blame] | 50 | |
rdevlin.cronin | c7ce4fc | 2016-05-10 01:41:37 | [diff] [blame] | 51 | const std::string& extension_id = context()->GetExtensionID(); |
[email protected] | c1e5fb1 | 2013-11-13 03:40:31 | [diff] [blame] | 52 | if (extension_id.empty()) |
| 53 | return; |
| 54 | |
Devlin Cronin | 16cb0e7 | 2017-11-16 19:31:05 | [diff] [blame] | 55 | // We ignore iframes here. (Returning subframes can cause broken behavior by |
| 56 | // treating an app window's iframe as its main frame, and maybe other |
| 57 | // nastiness). |
| 58 | // TODO(devlin): Why wouldn't we just account for that? It seems like there |
| 59 | // can be reasons to want to access just a frame - especially with isolated |
| 60 | // extension frames in web pages. |
| 61 | v8::Local<v8::Array> v8_views = ExtensionFrameHelper::GetV8MainFrames( |
Dan Elphick | d010a85a | 2018-08-03 11:32:26 | [diff] [blame] | 62 | context()->v8_context(), extension_id, browser_window_id, tab_id, |
| 63 | view_type); |
[email protected] | c1e5fb1 | 2013-11-13 03:40:31 | [diff] [blame] | 64 | |
| 65 | args.GetReturnValue().Set(v8_views); |
| 66 | } |
| 67 | |
[email protected] | bcdd992f | 2013-06-09 12:58:03 | [diff] [blame] | 68 | } // namespace extensions |