summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/core/frame/web_remote_frame_impl.cc
blob: bd1307ca65b6caef60acc127a7c5605047ab8f99 (plain)
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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "third_party/blink/renderer/core/frame/web_remote_frame_impl.h"

#include <utility>

#include "third_party/blink/public/common/frame/frame_visual_properties.h"
#include "third_party/blink/public/common/permissions_policy/permissions_policy.h"
#include "third_party/blink/public/mojom/frame/tree_scope_type.mojom-blink.h"
#include "third_party/blink/public/mojom/security_context/insecure_request_policy.mojom-blink.h"
#include "third_party/blink/public/web/web_document.h"
#include "third_party/blink/public/web/web_frame_owner_properties.h"
#include "third_party/blink/public/web/web_performance.h"
#include "third_party/blink/public/web/web_range.h"
#include "third_party/blink/renderer/bindings/core/v8/window_proxy.h"
#include "third_party/blink/renderer/core/execution_context/remote_security_context.h"
#include "third_party/blink/renderer/core/execution_context/security_context.h"
#include "third_party/blink/renderer/core/exported/web_view_impl.h"
#include "third_party/blink/renderer/core/frame/csp/conversion_util.h"
#include "third_party/blink/renderer/core/frame/local_dom_window.h"
#include "third_party/blink/renderer/core/frame/local_frame_view.h"
#include "third_party/blink/renderer/core/frame/remote_frame_client_impl.h"
#include "third_party/blink/renderer/core/frame/remote_frame_owner.h"
#include "third_party/blink/renderer/core/frame/settings.h"
#include "third_party/blink/renderer/core/frame/web_frame_widget_impl.h"
#include "third_party/blink/renderer/core/frame/web_local_frame_impl.h"
#include "third_party/blink/renderer/core/html/fenced_frame/html_fenced_frame_element.h"
#include "third_party/blink/renderer/core/html/html_frame_owner_element.h"
#include "third_party/blink/renderer/core/html/portal/html_portal_element.h"
#include "third_party/blink/renderer/core/html_names.h"
#include "third_party/blink/renderer/core/layout/layout_object.h"
#include "third_party/blink/renderer/core/layout/layout_view.h"
#include "third_party/blink/renderer/core/page/chrome_client.h"
#include "third_party/blink/renderer/core/page/page.h"
#include "third_party/blink/renderer/platform/bindings/dom_wrapper_world.h"
#include "third_party/blink/renderer/platform/geometry/float_quad.h"
#include "third_party/blink/renderer/platform/geometry/layout_rect.h"
#include "third_party/blink/renderer/platform/heap/handle.h"
#include "third_party/blink/renderer/platform/heap/heap.h"
#include "third_party/blink/renderer/platform/wtf/text/atomic_string.h"
#include "v8/include/v8.h"

namespace blink {

WebRemoteFrame* WebRemoteFrame::Create(
    mojom::blink::TreeScopeType scope,
    WebRemoteFrameClient* client,
    InterfaceRegistry* interface_registry,
    AssociatedInterfaceProvider* associated_interface_provider,
    const RemoteFrameToken& frame_token) {
  return MakeGarbageCollected<WebRemoteFrameImpl>(
      scope, client, interface_registry, associated_interface_provider,
      frame_token);
}

// static
WebRemoteFrame* WebRemoteFrame::CreateMainFrame(
    WebView* web_view,
    WebRemoteFrameClient* client,
    InterfaceRegistry* interface_registry,
    AssociatedInterfaceProvider* associated_interface_provider,
    const RemoteFrameToken& frame_token,
    const base::UnguessableToken& devtools_frame_token,
    WebFrame* opener) {
  return WebRemoteFrameImpl::CreateMainFrame(
      web_view, client, interface_registry, associated_interface_provider,
      frame_token, devtools_frame_token, opener);
}

// static
WebRemoteFrame* WebRemoteFrame::CreateForPortalOrFencedFrame(
    mojom::blink::TreeScopeType scope,
    WebRemoteFrameClient* client,
    InterfaceRegistry* interface_registry,
    AssociatedInterfaceProvider* associated_interface_provider,
    const RemoteFrameToken& frame_token,
    const base::UnguessableToken& devtools_frame_token,
    const WebElement& frame_owner) {
  return WebRemoteFrameImpl::CreateForPortalOrFencedFrame(
      scope, client, interface_registry, associated_interface_provider,
      frame_token, devtools_frame_token, frame_owner);
}

// static
WebRemoteFrameImpl* WebRemoteFrameImpl::CreateMainFrame(
    WebView* web_view,
    WebRemoteFrameClient* client,
    InterfaceRegistry* interface_registry,
    AssociatedInterfaceProvider* associated_interface_provider,
    const RemoteFrameToken& frame_token,
    const base::UnguessableToken& devtools_frame_token,
    WebFrame* opener) {
  WebRemoteFrameImpl* frame = MakeGarbageCollected<WebRemoteFrameImpl>(
      mojom::blink::TreeScopeType::kDocument, client, interface_registry,
      associated_interface_provider, frame_token);
  Page& page = *To<WebViewImpl>(web_view)->GetPage();
  // It would be nice to DCHECK that the main frame is not set yet here.
  // Unfortunately, there is an edge case with a pending RenderFrameHost that
  // violates this: the embedder may create a pending RenderFrameHost for
  // navigating to a new page in a popup. If the navigation ends up redirecting
  // to a site that requires a process swap, it doesn't go through the standard
  // swapping path and instead directly overwrites the main frame.
  // TODO(dcheng): Remove the need for this and strongly enforce this condition
  // with a DCHECK.
  frame->InitializeCoreFrame(
      page, nullptr, nullptr, nullptr, FrameInsertType::kInsertInConstructor,
      g_null_atom,
      opener ? &ToCoreFrame(*opener)->window_agent_factory() : nullptr,
      devtools_frame_token);
  Frame* opener_frame = opener ? ToCoreFrame(*opener) : nullptr;
  ToCoreFrame(*frame)->SetOpenerDoNotNotify(opener_frame);
  return frame;
}

WebRemoteFrameImpl* WebRemoteFrameImpl::CreateForPortalOrFencedFrame(
    mojom::blink::TreeScopeType scope,
    WebRemoteFrameClient* client,
    InterfaceRegistry* interface_registry,
    AssociatedInterfaceProvider* associated_interface_provider,
    const RemoteFrameToken& frame_token,
    const base::UnguessableToken& devtools_frame_token,
    const WebElement& frame_owner) {
  auto* frame = MakeGarbageCollected<WebRemoteFrameImpl>(
      scope, client, interface_registry, associated_interface_provider,
      frame_token);

  // We first convert this to a raw blink::Element*, and manually convert this
  // to an HTMLElement*. That is the only way the IsA<> and To<> casts below
  // will work.
  Element* element = frame_owner;
  DCHECK(IsA<HTMLPortalElement>(element) ||
         IsA<HTMLFencedFrameElement>(element));
  ExecutionContext* execution_context = element->GetExecutionContext();
  DCHECK(RuntimeEnabledFeatures::PortalsEnabled(execution_context) ||
         RuntimeEnabledFeatures::FencedFramesEnabled(execution_context));
  HTMLFrameOwnerElement* frame_owner_element =
      To<HTMLFrameOwnerElement>(element);
  LocalFrame* host_frame = frame_owner_element->GetDocument().GetFrame();
  frame->InitializeCoreFrame(
      *host_frame->GetPage(), frame_owner_element, nullptr, nullptr,
      FrameInsertType::kInsertInConstructor, g_null_atom,
      &host_frame->window_agent_factory(), devtools_frame_token);

  return frame;
}

WebRemoteFrameImpl::~WebRemoteFrameImpl() = default;

void WebRemoteFrameImpl::Trace(Visitor* visitor) const {
  visitor->Trace(frame_client_);
  visitor->Trace(frame_);
}

bool WebRemoteFrameImpl::IsWebLocalFrame() const {
  return false;
}

WebLocalFrame* WebRemoteFrameImpl::ToWebLocalFrame() {
  NOTREACHED();
  return nullptr;
}

const WebLocalFrame* WebRemoteFrameImpl::ToWebLocalFrame() const {
  NOTREACHED();
  return nullptr;
}

bool WebRemoteFrameImpl::IsWebRemoteFrame() const {
  return true;
}

WebRemoteFrame* WebRemoteFrameImpl::ToWebRemoteFrame() {
  return this;
}

const WebRemoteFrame* WebRemoteFrameImpl::ToWebRemoteFrame() const {
  return this;
}

void WebRemoteFrameImpl::Close() {
  WebRemoteFrame::Close();

  self_keep_alive_.Clear();
}

WebView* WebRemoteFrameImpl::View() const {
  if (!GetFrame()) {
    return nullptr;
  }
  DCHECK(GetFrame()->GetPage());
  return GetFrame()->GetPage()->GetChromeClient().GetWebView();
}

WebLocalFrame* WebRemoteFrameImpl::CreateLocalChild(
    mojom::blink::TreeScopeType scope,
    const WebString& name,
    const FramePolicy& frame_policy,
    WebLocalFrameClient* client,
    InterfaceRegistry* interface_registry,
    WebFrame* previous_sibling,
    const WebFrameOwnerProperties& frame_owner_properties,
    const LocalFrameToken& frame_token,
    WebFrame* opener,
    std::unique_ptr<WebPolicyContainer> policy_container) {
  auto* child = MakeGarbageCollected<WebLocalFrameImpl>(
      base::PassKey<WebRemoteFrameImpl>(), scope, client, interface_registry,
      frame_token);
  auto* owner = MakeGarbageCollected<RemoteFrameOwner>(frame_policy,
                                                       frame_owner_properties);

  WindowAgentFactory* window_agent_factory = nullptr;
  if (opener) {
    window_agent_factory = &ToCoreFrame(*opener)->window_agent_factory();
  } else {
    window_agent_factory = &GetFrame()->window_agent_factory();
  }

  child->InitializeCoreFrame(
      *GetFrame()->GetPage(), owner, this, previous_sibling,
      FrameInsertType::kInsertInConstructor, name, window_agent_factory, opener,
      std::move(policy_container));
  DCHECK(child->GetFrame());
  return child;
}

void WebRemoteFrameImpl::InitializeCoreFrame(
    Page& page,
    FrameOwner* owner,
    WebFrame* parent,
    WebFrame* previous_sibling,
    FrameInsertType insert_type,
    const AtomicString& name,
    WindowAgentFactory* window_agent_factory,
    const base::UnguessableToken& devtools_frame_token) {
  Frame* parent_frame = parent ? ToCoreFrame(*parent) : nullptr;
  Frame* previous_sibling_frame =
      previous_sibling ? ToCoreFrame(*previous_sibling) : nullptr;

  // If this is not a top-level frame, we need to send FrameVisualProperties to
  // the remote renderer process. Some of the properties are inherited from the
  // WebFrameWidget containing this frame, and this is true for regular frames
  // in the frame tree as well as for portals, which are not in the frame tree;
  // hence the code to traverse up through FrameOwner.
  WebFrameWidget* ancestor_widget = nullptr;
  if (parent) {
    if (parent->IsWebLocalFrame()) {
      ancestor_widget =
          To<WebLocalFrameImpl>(parent)->LocalRoot()->FrameWidget();
    }
  } else if (owner && owner->IsLocal()) {
    // Never gets to this point unless |owner| is a <portal> or <fencedframe>
    // element.
    HTMLFrameOwnerElement* owner_element = To<HTMLFrameOwnerElement>(owner);
    DCHECK(owner_element->IsHTMLPortalElement() ||
           owner_element->IsHTMLFencedFrameElement());
    LocalFrame& local_frame =
        owner_element->GetDocument().GetFrame()->LocalFrameRoot();
    ancestor_widget = WebLocalFrameImpl::FromFrame(local_frame)->FrameWidget();
  }

  SetCoreFrame(MakeGarbageCollected<RemoteFrame>(
      frame_client_.Get(), page, owner, parent_frame, previous_sibling_frame,
      insert_type, GetRemoteFrameToken(), window_agent_factory,
      interface_registry_, associated_interface_provider_, ancestor_widget,
      devtools_frame_token));

  if (ancestor_widget)
    InitializeFrameVisualProperties(ancestor_widget, View());

  GetFrame()->CreateView();
  frame_->Tree().SetName(name);
}

WebRemoteFrame* WebRemoteFrameImpl::CreateRemoteChild(
    mojom::blink::TreeScopeType scope,
    const WebString& name,
    const FramePolicy& frame_policy,
    WebRemoteFrameClient* client,
    InterfaceRegistry* interface_registry,
    AssociatedInterfaceProvider* associated_interface_provider,
    const RemoteFrameToken& frame_token,
    const base::UnguessableToken& devtools_frame_token,
    WebFrame* opener) {
  auto* child = MakeGarbageCollected<WebRemoteFrameImpl>(
      scope, client, interface_registry, associated_interface_provider,
      frame_token);
  auto* owner = MakeGarbageCollected<RemoteFrameOwner>(
      frame_policy, WebFrameOwnerProperties());
  WindowAgentFactory* window_agent_factory = nullptr;
  if (opener) {
    window_agent_factory = &ToCoreFrame(*opener)->window_agent_factory();
  } else {
    window_agent_factory = &GetFrame()->window_agent_factory();
  }

  child->InitializeCoreFrame(*GetFrame()->GetPage(), owner, this, LastChild(),
                             FrameInsertType::kInsertInConstructor, name,
                             window_agent_factory, devtools_frame_token);
  Frame* opener_frame = opener ? ToCoreFrame(*opener) : nullptr;
  ToCoreFrame(*child)->SetOpenerDoNotNotify(opener_frame);
  return child;
}

void WebRemoteFrameImpl::SetCoreFrame(RemoteFrame* frame) {
  frame_ = frame;
}

void WebRemoteFrameImpl::InitializeFrameVisualProperties(
    WebFrameWidget* ancestor_widget,
    WebView* web_view) {
  FrameVisualProperties visual_properties;
  visual_properties.zoom_level = web_view->ZoomLevel();
  visual_properties.page_scale_factor = ancestor_widget->PageScaleInMainFrame();
  visual_properties.is_pinch_gesture_active =
      ancestor_widget->PinchGestureActiveInMainFrame();
  visual_properties.screen_info = ancestor_widget->GetOriginalScreenInfo();
  visual_properties.visible_viewport_size =
      ancestor_widget->VisibleViewportSizeInDIPs();
  const WebVector<gfx::Rect>& window_segments =
      ancestor_widget->WindowSegments();
  visual_properties.root_widget_window_segments.assign(window_segments.begin(),
                                                       window_segments.end());
  GetFrame()->InitializeFrameVisualProperties(visual_properties);
}

WebRemoteFrameImpl* WebRemoteFrameImpl::FromFrame(RemoteFrame& frame) {
  if (!frame.Client())
    return nullptr;
  RemoteFrameClientImpl* client =
      static_cast<RemoteFrameClientImpl*>(frame.Client());
  return client->GetWebFrame();
}

void WebRemoteFrameImpl::SetReplicatedOrigin(
    const WebSecurityOrigin& origin,
    bool is_potentially_trustworthy_opaque_origin) {
  DCHECK(GetFrame());
  GetFrame()->SetReplicatedOrigin(origin,
                                  is_potentially_trustworthy_opaque_origin);
}

void WebRemoteFrameImpl::SetReplicatedSandboxFlags(
    network::mojom::blink::WebSandboxFlags flags) {
  DCHECK(GetFrame());
  GetFrame()->SetReplicatedSandboxFlags(flags);
}

void WebRemoteFrameImpl::SetReplicatedName(const WebString& name,
                                           const WebString& unique_name) {
  DCHECK(GetFrame());
  GetFrame()->SetReplicatedName(name, unique_name);
}

void WebRemoteFrameImpl::SetReplicatedPermissionsPolicyHeader(
    const ParsedPermissionsPolicy& parsed_header) {
  DCHECK(GetFrame());
  GetFrame()->SetReplicatedPermissionsPolicyHeader(parsed_header);
}

void WebRemoteFrameImpl::SetReplicatedInsecureRequestPolicy(
    mojom::blink::InsecureRequestPolicy policy) {
  DCHECK(GetFrame());
  GetFrame()->SetInsecureRequestPolicy(policy);
}

void WebRemoteFrameImpl::SetReplicatedInsecureNavigationsSet(
    const WebVector<unsigned>& set) {
  DCHECK(GetFrame());
  GetFrame()->SetInsecureNavigationsSet(set);
}

void WebRemoteFrameImpl::SetReplicatedIsAdSubframe(bool is_ad_subframe) {
  DCHECK(GetFrame());
  GetFrame()->SetReplicatedIsAdSubframe(is_ad_subframe);
}

void WebRemoteFrameImpl::DidStartLoading() {
  GetFrame()->DidStartLoading();
}

void WebRemoteFrameImpl::UpdateUserActivationState(
    mojom::blink::UserActivationUpdateType update_type,
    mojom::blink::UserActivationNotificationType notification_type) {
  GetFrame()->UpdateUserActivationState(update_type, notification_type);
}

void WebRemoteFrameImpl::SetHadStickyUserActivationBeforeNavigation(
    bool value) {
  GetFrame()->SetHadStickyUserActivationBeforeNavigation(value);
}

v8::Local<v8::Object> WebRemoteFrameImpl::GlobalProxy() const {
  return GetFrame()
      ->GetWindowProxy(DOMWrapperWorld::MainWorld())
      ->GlobalProxyIfNotDetached();
}

gfx::Rect WebRemoteFrameImpl::GetCompositingRect() {
  return GetFrame()->View()->GetCompositingRect();
}

WebString WebRemoteFrameImpl::UniqueName() const {
  return GetFrame()->UniqueName();
}

const FrameVisualProperties&
WebRemoteFrameImpl::GetPendingVisualPropertiesForTesting() const {
  return GetFrame()->GetPendingVisualPropertiesForTesting();
}

bool WebRemoteFrameImpl::IsAdSubframe() const {
  return GetFrame()->IsAdSubframe();
}

WebRemoteFrameImpl::WebRemoteFrameImpl(
    mojom::blink::TreeScopeType scope,
    WebRemoteFrameClient* client,
    InterfaceRegistry* interface_registry,
    AssociatedInterfaceProvider* associated_interface_provider,
    const RemoteFrameToken& frame_token)
    : WebRemoteFrame(scope, frame_token),
      client_(client),
      frame_client_(MakeGarbageCollected<RemoteFrameClientImpl>(this)),
      interface_registry_(interface_registry),
      associated_interface_provider_(associated_interface_provider),
      self_keep_alive_(PERSISTENT_FROM_HERE, this) {
  DCHECK(client);
}

}  // namespace blink