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