blob: 3c4847740919d48e5f7cb88fa6a70e237576b49c [file] [log] [blame]
[email protected]e6893672014-05-01 17:29:131// Copyright 2014 The Chromium Authors. All rights reserved.
[email protected]120028b9c2012-07-03 01:32:242// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]e6893672014-05-01 17:29:135#include "extensions/renderer/runtime_custom_bindings.h"
[email protected]120028b9c2012-07-03 01:32:246
avi2d124c02015-12-23 06:36:427#include <stdint.h>
8
dchengf6f80662016-04-20 20:26:049#include <memory>
10
[email protected]120028b9c2012-07-03 01:32:2411#include "base/bind.h"
[email protected]120028b9c2012-07-03 01:32:2412#include "base/values.h"
rob248d6a82014-11-21 02:04:4813#include "content/public/renderer/render_frame.h"
John Abd-El-Malek312a30bb2017-10-23 19:51:5214#include "content/public/renderer/v8_value_converter.h"
[email protected]e4452d32013-11-15 23:07:4115#include "extensions/common/extension.h"
[email protected]fb820c02014-03-13 15:07:0816#include "extensions/common/extension_messages.h"
[email protected]d42c11152013-08-22 19:36:3217#include "extensions/common/manifest.h"
rdevlin.cronin6f42c2522015-06-19 18:58:5118#include "extensions/renderer/extension_frame_helper.h"
[email protected]bcd9580f2014-04-17 19:17:5919#include "extensions/renderer/script_context.h"
[email protected]120028b9c2012-07-03 01:32:2420
21namespace extensions {
22
[email protected]bcd9580f2014-04-17 19:17:5923RuntimeCustomBindings::RuntimeCustomBindings(ScriptContext* context)
Devlin Cronind9ea8342018-01-27 06:00:0424 : ObjectBackedNativeHandler(context) {}
25
26RuntimeCustomBindings::~RuntimeCustomBindings() {}
27
28void RuntimeCustomBindings::AddRoutes() {
Devlin Cronincc02a0c2019-01-03 22:15:0729 RouteHandlerFunction("GetManifest",
30 base::BindRepeating(&RuntimeCustomBindings::GetManifest,
31 base::Unretained(this)));
Devlin Cronind9ea8342018-01-27 06:00:0432 RouteHandlerFunction(
Devlin Cronincc02a0c2019-01-03 22:15:0733 "GetExtensionViews",
34 base::BindRepeating(&RuntimeCustomBindings::GetExtensionViews,
35 base::Unretained(this)));
[email protected]e6893672014-05-01 17:29:1336}
[email protected]120028b9c2012-07-03 01:32:2437
[email protected]d8c5fbb2013-06-14 11:35:2538void RuntimeCustomBindings::GetManifest(
39 const v8::FunctionCallbackInfo<v8::Value>& args) {
[email protected]9a598442013-06-04 16:39:1240 CHECK(context()->extension());
[email protected]120028b9c2012-07-03 01:32:2441
rdevlin.cronin694c6052017-06-13 22:07:3542 args.GetReturnValue().Set(content::V8ValueConverter::Create()->ToV8Value(
[email protected]e6893672014-05-01 17:29:1343 context()->extension()->manifest()->value(), context()->v8_context()));
[email protected]120028b9c2012-07-03 01:32:2444}
45
[email protected]c1e5fb12013-11-13 03:40:3146void RuntimeCustomBindings::GetExtensionViews(
47 const v8::FunctionCallbackInfo<v8::Value>& args) {
catmullings15fd52b2016-07-14 23:46:5948 CHECK_EQ(args.Length(), 3);
rdevlin.croninc7ce4fc2016-05-10 01:41:3749 CHECK(args[0]->IsInt32());
catmullings15fd52b2016-07-14 23:46:5950 CHECK(args[1]->IsInt32());
51 CHECK(args[2]->IsString());
[email protected]c1e5fb12013-11-13 03:40:3152
53 // |browser_window_id| == extension_misc::kUnknownWindowId means getting
54 // all views for the current extension.
Dan Elphickd010a85a2018-08-03 11:32:2655 int browser_window_id = args[0].As<v8::Int32>()->Value();
56 int tab_id = args[1].As<v8::Int32>()->Value();
[email protected]c1e5fb12013-11-13 03:40:3157
brettwc15100c2015-08-06 22:54:1658 std::string view_type_string =
Adam Klein686c0e52018-01-17 23:42:3359 base::ToUpperASCII(*v8::String::Utf8Value(args.GetIsolate(), args[2]));
[email protected]c1e5fb12013-11-13 03:40:3160 // |view_type| == VIEW_TYPE_INVALID means getting any type of
61 // views.
62 ViewType view_type = VIEW_TYPE_INVALID;
Devlin Croninbe3421d2017-11-10 20:32:0763 bool parsed_view_type = GetViewTypeFromString(view_type_string, &view_type);
64 if (!parsed_view_type)
65 CHECK_EQ("ALL", view_type_string);
[email protected]c1e5fb12013-11-13 03:40:3166
rdevlin.croninc7ce4fc2016-05-10 01:41:3767 const std::string& extension_id = context()->GetExtensionID();
[email protected]c1e5fb12013-11-13 03:40:3168 if (extension_id.empty())
69 return;
70
Devlin Cronin16cb0e72017-11-16 19:31:0571 // 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 Elphickd010a85a2018-08-03 11:32:2678 context()->v8_context(), extension_id, browser_window_id, tab_id,
79 view_type);
[email protected]c1e5fb12013-11-13 03:40:3180
81 args.GetReturnValue().Set(v8_views);
82}
83
[email protected]bcdd992f2013-06-09 12:58:0384} // namespace extensions