1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379
|
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "extensions/renderer/guest_view/guest_view_internal_custom_bindings.h"
#include <string>
#include <utility>
#include "base/functional/bind.h"
#include "base/lazy_instance.h"
#include "components/guest_view/common/guest_view_constants.h"
#include "components/guest_view/renderer/guest_view_container.h"
#include "components/guest_view/renderer/guest_view_request.h"
#include "content/public/renderer/render_frame.h"
#include "content/public/renderer/render_frame_observer.h"
#include "content/public/renderer/render_thread.h"
#include "content/public/renderer/v8_value_converter.h"
#include "extensions/common/extension.h"
#include "extensions/renderer/script_context.h"
#include "ipc/ipc_sync_channel.h"
#include "third_party/blink/public/common/associated_interfaces/associated_interface_provider.h"
#include "third_party/blink/public/mojom/frame/user_activation_notification_type.mojom.h"
#include "third_party/blink/public/web/web_custom_element.h"
#include "third_party/blink/public/web/web_frame.h"
#include "third_party/blink/public/web/web_local_frame.h"
#include "third_party/blink/public/web/web_remote_frame.h"
#include "third_party/blink/public/web/web_view.h"
#include "v8/include/v8.h"
using content::V8ValueConverter;
namespace {
// A map from view instance ID to view object (stored via weak V8 reference).
// Views are registered into this map via
// GuestViewInternalCustomBindings::RegisterView(), and accessed via
// GuestViewInternalCustomBindings::GetViewFromID().
using ViewMap = std::map<int, v8::Global<v8::Object>*>;
static base::LazyInstance<ViewMap>::DestructorAtExit weak_view_map =
LAZY_INSTANCE_INITIALIZER;
} // namespace
namespace extensions {
namespace {
content::RenderFrame* GetRenderFrame(v8::Isolate* isolate,
v8::Local<v8::Value> value) {
v8::Local<v8::Context> context;
if (!v8::Local<v8::Object>::Cast(value)->GetCreationContext(isolate).ToLocal(
&context)) {
if (context.IsEmpty())
return nullptr;
}
blink::WebLocalFrame* frame = blink::WebLocalFrame::FrameForContext(context);
if (!frame)
return nullptr;
return content::RenderFrame::FromWebFrame(frame);
}
class RenderFrameStatus final : public content::RenderFrameObserver {
public:
explicit RenderFrameStatus(content::RenderFrame* render_frame)
: content::RenderFrameObserver(render_frame) {}
~RenderFrameStatus() final = default;
bool is_ok() { return render_frame() != nullptr; }
// RenderFrameObserver implementation.
void OnDestruct() final {}
};
} // namespace
struct GuestViewInternalCustomBindings::ViewHolder {
mojo::Remote<guest_view::mojom::ViewHandle> keep_alive_handle_remote;
int view_id;
};
GuestViewInternalCustomBindings::GuestViewInternalCustomBindings(
ScriptContext* context)
: ObjectBackedNativeHandler(context) {}
GuestViewInternalCustomBindings::~GuestViewInternalCustomBindings() = default;
void GuestViewInternalCustomBindings::AddRoutes() {
RouteHandlerFunction(
"AttachIframeGuest",
base::BindRepeating(&GuestViewInternalCustomBindings::AttachIframeGuest,
base::Unretained(this)));
RouteHandlerFunction(
"GetFrameToken",
base::BindRepeating(&GuestViewInternalCustomBindings::GetFrameToken,
base::Unretained(this)));
RouteHandlerFunction(
"DestroyContainer",
base::BindRepeating(&GuestViewInternalCustomBindings::DestroyContainer,
base::Unretained(this)));
RouteHandlerFunction(
"GetViewFromID",
base::BindRepeating(&GuestViewInternalCustomBindings::GetViewFromID,
base::Unretained(this)));
RouteHandlerFunction(
"RegisterDestructionCallback",
base::BindRepeating(
&GuestViewInternalCustomBindings::RegisterDestructionCallback,
base::Unretained(this)));
RouteHandlerFunction(
"RegisterView",
base::BindRepeating(&GuestViewInternalCustomBindings::RegisterView,
base::Unretained(this)));
RouteHandlerFunction(
"RunWithGesture",
base::BindRepeating(&GuestViewInternalCustomBindings::RunWithGesture,
base::Unretained(this)));
RouteHandlerFunction(
"AllowGuestViewElementDefinition",
base::BindRepeating(
&GuestViewInternalCustomBindings::AllowGuestViewElementDefinition,
base::Unretained(this)));
}
// static
void GuestViewInternalCustomBindings::ResetMapEntry(
const v8::WeakCallbackInfo<ViewHolder>& data) {
ViewHolder* param = data.GetParameter();
int view_instance_id = param->view_id;
delete param;
ViewMap& view_map = weak_view_map.Get();
auto entry = view_map.find(view_instance_id);
if (entry == view_map.end())
return;
// V8 says we need to explicitly reset weak handles from their callbacks.
// It is not implicit as one might expect.
entry->second->Reset();
delete entry->second;
view_map.erase(entry);
}
void GuestViewInternalCustomBindings::AttachIframeGuest(
const v8::FunctionCallbackInfo<v8::Value>& args) {
// Allow for an optional callback parameter.
const int num_required_params = 4;
CHECK(args.Length() >= num_required_params &&
args.Length() <= (num_required_params + 1));
// Element Instance ID.
CHECK(args[0]->IsInt32());
// Guest Instance ID.
CHECK(args[1]->IsInt32());
// Attach Parameters.
CHECK(args[2]->IsObject());
// <iframe>.contentWindow.
CHECK(args[3]->IsObject());
// Optional Callback Function.
CHECK(args.Length() <= num_required_params ||
args[num_required_params]->IsFunction());
int element_instance_id = args[0].As<v8::Int32>()->Value();
int guest_instance_id = args[1].As<v8::Int32>()->Value();
// Get the WebLocalFrame before (possibly) executing any user-space JS while
// getting the |params|. We track the status of the RenderFrame via an
// observer in case it is deleted during user code execution.
content::RenderFrame* render_frame =
GetRenderFrame(args.GetIsolate(), args[3]);
RenderFrameStatus render_frame_status(render_frame);
std::unique_ptr<base::Value> params =
content::V8ValueConverter::Create()->FromV8Value(args[2],
context()->v8_context());
CHECK(params);
CHECK(params->is_dict());
if (!render_frame_status.is_ok())
return;
blink::WebLocalFrame* frame = render_frame->GetWebFrame();
// Parent must exist.
blink::WebFrame* parent_frame = frame->Parent();
DCHECK(parent_frame);
DCHECK(parent_frame->IsWebLocalFrame());
// Add flag to |params| to indicate that the element size is specified in
// logical units.
params->GetDict().Set(guest_view::kElementSizeIsLogical, true);
content::RenderFrame* embedder_parent_frame =
content::RenderFrame::FromWebFrame(parent_frame->ToWebLocalFrame());
// Create a GuestViewContainer if it does not exist.
// An element instance ID uniquely identifies a GuestViewContainer
// within a RenderView.
auto* guest_view_container =
guest_view::GuestViewContainer::FromID(element_instance_id);
// This is the first time we hear about the |element_instance_id|.
DCHECK(!guest_view_container);
// The <webview> element's GC takes ownership of |guest_view_container|.
guest_view_container = new guest_view::GuestViewContainer(
embedder_parent_frame, element_instance_id);
std::unique_ptr<guest_view::GuestViewAttachRequest> request =
std::make_unique<guest_view::GuestViewAttachRequest>(
guest_view_container, render_frame, guest_instance_id,
std::move(*params).TakeDict(),
args.Length() == (num_required_params + 1)
? args[num_required_params].As<v8::Function>()
: v8::Local<v8::Function>(),
args.GetIsolate());
guest_view_container->IssueRequest(std::move(request));
args.GetReturnValue().Set(true);
}
void GuestViewInternalCustomBindings::GetFrameToken(
const v8::FunctionCallbackInfo<v8::Value>& args) {
CHECK_EQ(args.Length(), 1);
if (!args[0]->IsObject()) {
args.GetReturnValue().SetEmptyString();
return;
}
content::RenderFrame* render_frame =
GetRenderFrame(args.GetIsolate(), args[0]);
if (!render_frame) {
args.GetReturnValue().SetEmptyString();
return;
}
auto frame_token = render_frame->GetWebFrame()->GetLocalFrameToken();
std::string frame_token_string = frame_token.ToString();
auto return_object = v8::String::NewFromUtf8(
args.GetIsolate(), frame_token_string.data(), v8::NewStringType::kNormal,
frame_token_string.size());
args.GetReturnValue().Set(return_object.ToLocalChecked());
}
void GuestViewInternalCustomBindings::DestroyContainer(
const v8::FunctionCallbackInfo<v8::Value>& args) {
args.GetReturnValue().SetNull();
if (args.Length() != 1)
return;
// Element Instance ID.
if (!args[0]->IsInt32())
return;
int element_instance_id = args[0].As<v8::Int32>()->Value();
auto* guest_view_container =
guest_view::GuestViewContainer::FromID(element_instance_id);
if (!guest_view_container)
return;
// Note: |guest_view_container| is deleted.
// GuestViewContainer::DidDestroyElement() currently also destroys
// a GuestViewContainer. That won't be necessary once GuestViewContainer
// always runs w/o plugin.
guest_view_container->Destroy(false /* embedder_frame_destroyed */);
}
void GuestViewInternalCustomBindings::GetViewFromID(
const v8::FunctionCallbackInfo<v8::Value>& args) {
// Default to returning null.
args.GetReturnValue().SetNull();
// There is one argument.
CHECK(args.Length() == 1);
// The view ID.
CHECK(args[0]->IsInt32());
int view_id = args[0].As<v8::Int32>()->Value();
ViewMap& view_map = weak_view_map.Get();
auto map_entry = view_map.find(view_id);
if (map_entry == view_map.end())
return;
auto return_object =
v8::Local<v8::Object>::New(args.GetIsolate(), *map_entry->second);
args.GetReturnValue().Set(return_object);
}
void GuestViewInternalCustomBindings::RegisterDestructionCallback(
const v8::FunctionCallbackInfo<v8::Value>& args) {
// There are two parameters.
CHECK(args.Length() == 2);
// Element Instance ID.
CHECK(args[0]->IsInt32());
// Callback function.
CHECK(args[1]->IsFunction());
int element_instance_id = args[0].As<v8::Int32>()->Value();
// An element instance ID uniquely identifies a GuestViewContainer within a
// RenderView.
auto* guest_view_container =
guest_view::GuestViewContainer::FromID(element_instance_id);
if (!guest_view_container)
return;
guest_view_container->RegisterDestructionCallback(args[1].As<v8::Function>(),
args.GetIsolate());
args.GetReturnValue().Set(true);
}
void GuestViewInternalCustomBindings::RegisterView(
const v8::FunctionCallbackInfo<v8::Value>& args) {
// There are three parameters.
CHECK(args.Length() == 3);
// View Instance ID.
CHECK(args[0]->IsInt32());
// View element.
CHECK(args[1]->IsObject());
// View type (e.g. "webview").
CHECK(args[2]->IsString());
// A reference to the view object is stored in |weak_view_map| using its view
// ID as the key. The reference is made weak so that it will not extend the
// lifetime of the object.
int view_instance_id = args[0].As<v8::Int32>()->Value();
auto* object =
new v8::Global<v8::Object>(args.GetIsolate(), args[1].As<v8::Object>());
weak_view_map.Get().insert(std::make_pair(view_instance_id, object));
ViewHolder* view_holder = new ViewHolder();
view_holder->view_id = view_instance_id;
auto receiver =
view_holder->keep_alive_handle_remote.BindNewPipeAndPassReceiver();
// The `view_holder` is given to the SetWeak callback so that that view's
// entry in `weak_view_map` can be cleared when the view object is garbage
// collected. This will then also close the `keep_alive_handle_remote`
// indicating to the browser the object has been collected.
object->SetWeak(view_holder, &GuestViewInternalCustomBindings::ResetMapEntry,
v8::WeakCallbackType::kParameter);
// Let the GuestViewManager know that a GuestView has been created.
const std::string& view_type =
*v8::String::Utf8Value(args.GetIsolate(), args[2]);
GetGuestViewHost()->ViewCreated(view_instance_id, view_type,
std::move(receiver));
}
void GuestViewInternalCustomBindings::RunWithGesture(
const v8::FunctionCallbackInfo<v8::Value>& args) {
// Gesture is required to request fullscreen.
// TODO(devlin): All this needs to do is enter fullscreen. We should make this
// EnterFullscreen() and do it directly rather than having a generic "run with
// user gesture" function.
if (context()->web_frame()) {
context()->web_frame()->NotifyUserActivation(
blink::mojom::UserActivationNotificationType::kExtensionGuestView);
}
CHECK_EQ(args.Length(), 1);
CHECK(args[0]->IsFunction());
context()->SafeCallFunction(
v8::Local<v8::Function>::Cast(args[0]), 0, nullptr);
}
void GuestViewInternalCustomBindings::AllowGuestViewElementDefinition(
const v8::FunctionCallbackInfo<v8::Value>& args) {
blink::WebCustomElement::EmbedderNamesAllowedScope embedder_names_scope;
CHECK_EQ(args.Length(), 1);
CHECK(args[0]->IsFunction());
context()->SafeCallFunction(v8::Local<v8::Function>::Cast(args[0]), 0,
nullptr);
}
guest_view::mojom::GuestViewHost*
GuestViewInternalCustomBindings::GetGuestViewHost() {
if (!remote_.is_bound()) {
content::RenderFrame* render_frame = context()->GetRenderFrame();
CHECK(render_frame);
render_frame->GetRemoteAssociatedInterfaces()->GetInterface(&remote_);
}
return remote_.get();
}
} // namespace extensions
|