blob: 81c6e31bced1cc2cd6f36df4f4b51508ef38d6eb [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"
mek87e0ab52015-02-13 01:18:2613#include "content/public/child/v8_value_converter.h"
rob248d6a82014-11-21 02:04:4814#include "content/public/renderer/render_frame.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"
rdevlin.cronin6f42c2522015-06-19 18:58:5120#include "third_party/WebKit/public/web/WebLocalFrame.h"
[email protected]120028b9c2012-07-03 01:32:2421
22namespace extensions {
23
[email protected]bcd9580f2014-04-17 19:17:5924RuntimeCustomBindings::RuntimeCustomBindings(ScriptContext* context)
25 : ObjectBackedNativeHandler(context) {
[email protected]e6893672014-05-01 17:29:1326 RouteFunction(
27 "GetManifest",
28 base::Bind(&RuntimeCustomBindings::GetManifest, base::Unretained(this)));
[email protected]c1e5fb12013-11-13 03:40:3129 RouteFunction("GetExtensionViews",
30 base::Bind(&RuntimeCustomBindings::GetExtensionViews,
31 base::Unretained(this)));
[email protected]120028b9c2012-07-03 01:32:2432}
33
[email protected]e6893672014-05-01 17:29:1334RuntimeCustomBindings::~RuntimeCustomBindings() {
35}
[email protected]120028b9c2012-07-03 01:32:2436
[email protected]d8c5fbb2013-06-14 11:35:2537void RuntimeCustomBindings::GetManifest(
38 const v8::FunctionCallbackInfo<v8::Value>& args) {
[email protected]9a598442013-06-04 16:39:1239 CHECK(context()->extension());
[email protected]120028b9c2012-07-03 01:32:2440
rdevlin.croninc7ce4fc2016-05-10 01:41:3741 std::unique_ptr<content::V8ValueConverter> converter(
42 content::V8ValueConverter::create());
[email protected]e6893672014-05-01 17:29:1343 args.GetReturnValue().Set(converter->ToV8Value(
44 context()->extension()->manifest()->value(), context()->v8_context()));
[email protected]120028b9c2012-07-03 01:32:2445}
46
[email protected]c1e5fb12013-11-13 03:40:3147void RuntimeCustomBindings::GetExtensionViews(
48 const v8::FunctionCallbackInfo<v8::Value>& args) {
catmullings15fd52b2016-07-14 23:46:5949 CHECK_EQ(args.Length(), 3);
rdevlin.croninc7ce4fc2016-05-10 01:41:3750 CHECK(args[0]->IsInt32());
catmullings15fd52b2016-07-14 23:46:5951 CHECK(args[1]->IsInt32());
52 CHECK(args[2]->IsString());
[email protected]c1e5fb12013-11-13 03:40:3153
54 // |browser_window_id| == extension_misc::kUnknownWindowId means getting
55 // all views for the current extension.
56 int browser_window_id = args[0]->Int32Value();
catmullings15fd52b2016-07-14 23:46:5957 int tab_id = args[1]->Int32Value();
[email protected]c1e5fb12013-11-13 03:40:3158
brettwc15100c2015-08-06 22:54:1659 std::string view_type_string =
catmullings15fd52b2016-07-14 23:46:5960 base::ToUpperASCII(*v8::String::Utf8Value(args[2]));
[email protected]c1e5fb12013-11-13 03:40:3161 // |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]c1e5fb12013-11-13 03:40:3166 } 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]ddcf5802014-02-20 23:21:3272 } else if (view_type_string == kViewTypeAppWindow) {
73 view_type = VIEW_TYPE_APP_WINDOW;
[email protected]9faae962014-08-11 07:59:1974 } else if (view_type_string == kViewTypeLauncherPage) {
75 view_type = VIEW_TYPE_LAUNCHER_PAGE;
[email protected]c1e5fb12013-11-13 03:40:3176 } else if (view_type_string == kViewTypePanel) {
77 view_type = VIEW_TYPE_PANEL;
rdevlin.croninc7ce4fc2016-05-10 01:41:3778 } else {
79 CHECK_EQ(view_type_string, kViewTypeAll);
[email protected]c1e5fb12013-11-13 03:40:3180 }
81
rdevlin.croninc7ce4fc2016-05-10 01:41:3782 const std::string& extension_id = context()->GetExtensionID();
[email protected]c1e5fb12013-11-13 03:40:3183 if (extension_id.empty())
84 return;
85
rdevlin.cronin6f42c2522015-06-19 18:58:5186 std::vector<content::RenderFrame*> frames =
87 ExtensionFrameHelper::GetExtensionFrames(extension_id, browser_window_id,
catmullings15fd52b2016-07-14 23:46:5988 tab_id, view_type);
robaa7a8892016-05-02 16:18:3789 v8::Local<v8::Context> v8_context = args.GetIsolate()->GetCurrentContext();
[email protected]95c6b3012013-12-02 14:30:3190 v8::Local<v8::Array> v8_views = v8::Array::New(args.GetIsolate());
[email protected]c1e5fb12013-11-13 03:40:3191 int v8_index = 0;
rdevlin.cronin6f42c2522015-06-19 18:58:5192 for (content::RenderFrame* frame : frames) {
rdevlin.cronin9e9f59a2015-06-24 23:49:0793 // 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.croninc7ce4fc2016-05-10 01:41:3797 blink::WebFrame* web_frame = frame->GetWebFrame();
98 if (web_frame->top() != web_frame)
rdevlin.cronin9e9f59a2015-06-24 23:49:0799 continue;
100
rdevlin.croninc7ce4fc2016-05-10 01:41:37101 if (!blink::WebFrame::scriptCanAccess(web_frame))
102 continue;
103
104 v8::Local<v8::Context> context = web_frame->mainWorldScriptContext();
[email protected]c1e5fb12013-11-13 03:40:31105 if (!context.IsEmpty()) {
106 v8::Local<v8::Value> window = context->Global();
rdevlin.croninc7ce4fc2016-05-10 01:41:37107 CHECK(!window.IsEmpty());
robaa7a8892016-05-02 16:18:37108 v8::Maybe<bool> maybe =
rdevlin.croninc7ce4fc2016-05-10 01:41:37109 v8_views->CreateDataProperty(v8_context, v8_index++, window);
110 CHECK(maybe.IsJust() && maybe.FromJust());
[email protected]c1e5fb12013-11-13 03:40:31111 }
112 }
113
114 args.GetReturnValue().Set(v8_views);
115}
116
[email protected]bcdd992f2013-06-09 12:58:03117} // namespace extensions