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
|
// Copyright (c) 2012 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 "content/renderer/render_view_impl.h"
#include <map>
#include <memory>
#include "base/bind.h"
#include "base/command_line.h"
#include "base/feature_list.h"
#include "base/lazy_instance.h"
#include "base/location.h"
#include "base/single_thread_task_runner.h"
#include "base/strings/string_piece.h"
#include "cc/trees/ukm_manager.h"
#include "content/common/agent_scheduling_group.mojom.h"
#include "content/public/common/content_client.h"
#include "content/public/common/content_constants.h"
#include "content/public/common/content_features.h"
#include "content/public/common/content_switches.h"
#include "content/public/renderer/content_renderer_client.h"
#include "content/public/renderer/render_view_visitor.h"
#include "content/public/renderer/window_features_converter.h"
#include "content/renderer/agent_scheduling_group.h"
#include "content/renderer/render_frame_impl.h"
#include "content/renderer/render_frame_proxy.h"
#include "content/renderer/render_thread_impl.h"
#include "third_party/blink/public/mojom/page/page.mojom.h"
#include "third_party/blink/public/platform/impression_conversions.h"
#include "third_party/blink/public/platform/modules/video_capture/web_video_capture_impl_manager.h"
#include "third_party/blink/public/platform/url_conversion.h"
#include "third_party/blink/public/web/modules/mediastream/web_media_stream_device_observer.h"
#include "third_party/blink/public/web/web_frame_widget.h"
#include "third_party/blink/public/web/web_local_frame.h"
#include "third_party/blink/public/web/web_page_popup.h"
#include "third_party/blink/public/web/web_view.h"
#include "third_party/blink/public/web/web_window_features.h"
#include "ui/base/ui_base_features.h"
using blink::WebFrame;
using blink::WebLocalFrame;
using blink::WebNavigationPolicy;
using blink::WebString;
using blink::WebURLRequest;
using blink::WebView;
using blink::WebWindowFeatures;
namespace content {
//-----------------------------------------------------------------------------
typedef std::map<blink::WebView*, RenderViewImpl*> ViewMap;
static base::LazyInstance<ViewMap>::Leaky g_view_map =
LAZY_INSTANCE_INITIALIZER;
typedef std::map<int32_t, RenderViewImpl*> RoutingIDViewMap;
static base::LazyInstance<RoutingIDViewMap>::Leaky g_routing_id_view_map =
LAZY_INSTANCE_INITIALIZER;
// static
WindowOpenDisposition RenderViewImpl::NavigationPolicyToDisposition(
WebNavigationPolicy policy) {
switch (policy) {
case blink::kWebNavigationPolicyDownload:
return WindowOpenDisposition::SAVE_TO_DISK;
case blink::kWebNavigationPolicyCurrentTab:
return WindowOpenDisposition::CURRENT_TAB;
case blink::kWebNavigationPolicyNewBackgroundTab:
return WindowOpenDisposition::NEW_BACKGROUND_TAB;
case blink::kWebNavigationPolicyNewForegroundTab:
return WindowOpenDisposition::NEW_FOREGROUND_TAB;
case blink::kWebNavigationPolicyNewWindow:
return WindowOpenDisposition::NEW_WINDOW;
case blink::kWebNavigationPolicyNewPopup:
return WindowOpenDisposition::NEW_POPUP;
default:
NOTREACHED() << "Unexpected WebNavigationPolicy";
return WindowOpenDisposition::IGNORE_ACTION;
}
}
///////////////////////////////////////////////////////////////////////////////
namespace {
content::mojom::WindowContainerType WindowFeaturesToContainerType(
const blink::WebWindowFeatures& window_features) {
if (window_features.background) {
if (window_features.persistent)
return content::mojom::WindowContainerType::PERSISTENT;
else
return content::mojom::WindowContainerType::BACKGROUND;
} else {
return content::mojom::WindowContainerType::NORMAL;
}
}
} // namespace
RenderViewImpl::RenderViewImpl(AgentSchedulingGroup& agent_scheduling_group,
const mojom::CreateViewParams& params)
: routing_id_(params.view_id),
renderer_wide_named_frame_lookup_(
params.renderer_wide_named_frame_lookup),
agent_scheduling_group_(agent_scheduling_group) {
// Please put all logic in RenderViewImpl::Initialize().
}
void RenderViewImpl::Initialize(
mojom::CreateViewParamsPtr params,
bool was_created_by_renderer,
scoped_refptr<base::SingleThreadTaskRunner> task_runner) {
DCHECK(RenderThread::IsMainThread());
WebFrame* opener_frame = nullptr;
if (params->opener_frame_token)
opener_frame = WebFrame::FromFrameToken(params->opener_frame_token.value());
// The newly created webview_ is owned by this instance.
webview_ = WebView::Create(
this, params->hidden, params->is_prerendering,
params->type == mojom::ViewWidgetType::kPortal ? true : false,
/*compositing_enabled=*/true, params->never_composited,
opener_frame ? opener_frame->View() : nullptr,
std::move(params->blink_page_broadcast),
agent_scheduling_group_.agent_group_scheduler(),
params->session_storage_namespace_id, params->base_background_color);
g_view_map.Get().insert(std::make_pair(GetWebView(), this));
g_routing_id_view_map.Get().insert(std::make_pair(GetRoutingID(), this));
bool local_main_frame = params->main_frame->is_local_params();
webview_->SetWebPreferences(params->web_preferences);
if (local_main_frame) {
RenderFrameImpl::CreateMainFrame(
agent_scheduling_group_, this, opener_frame,
params->type != mojom::ViewWidgetType::kTopLevel,
std::move(params->replication_state), params->devtools_main_frame_token,
std::move(params->main_frame->get_local_params()));
} else {
RenderFrameProxy::CreateFrameProxy(
agent_scheduling_group_, params->main_frame->get_remote_params()->token,
params->main_frame->get_remote_params()->routing_id,
params->opener_frame_token, GetRoutingID(), MSG_ROUTING_NONE,
blink::mojom::TreeScopeType::kDocument /* ignored for main frames */,
std::move(params->replication_state), params->devtools_main_frame_token,
std::move(
params->main_frame->get_remote_params()->main_frame_interfaces));
}
// TODO(davidben): Move this state from Blink into content.
if (params->window_was_created_with_opener)
GetWebView()->SetOpenedByDOM();
webview_->SetRendererPreferences(params->renderer_preferences);
GetContentClient()->renderer()->WebViewCreated(webview_);
#if defined(OS_ANDROID)
// TODO(sgurun): crbug.com/325351 Needed only for android webview's deprecated
// HandleNavigation codepath.
was_created_by_renderer_ = was_created_by_renderer;
#endif
}
RenderViewImpl::~RenderViewImpl() {
DCHECK(destroying_); // Always deleted through Destroy().
g_routing_id_view_map.Get().erase(routing_id_);
#ifndef NDEBUG
// Make sure we are no longer referenced by the ViewMap or RoutingIDViewMap.
ViewMap* views = g_view_map.Pointer();
for (ViewMap::iterator it = views->begin(); it != views->end(); ++it)
DCHECK_NE(this, it->second) << "Failed to call Close?";
RoutingIDViewMap* routing_id_views = g_routing_id_view_map.Pointer();
for (RoutingIDViewMap::iterator it = routing_id_views->begin();
it != routing_id_views->end(); ++it)
DCHECK_NE(this, it->second) << "Failed to call Close?";
#endif
}
/*static*/
RenderView* RenderView::FromWebView(blink::WebView* webview) {
DCHECK(RenderThread::IsMainThread());
ViewMap* views = g_view_map.Pointer();
auto it = views->find(webview);
return it == views->end() ? NULL : it->second;
}
/*static*/
RenderViewImpl* RenderViewImpl::FromRoutingID(int32_t routing_id) {
DCHECK(RenderThread::IsMainThread());
RoutingIDViewMap* views = g_routing_id_view_map.Pointer();
auto it = views->find(routing_id);
return it == views->end() ? NULL : it->second;
}
/*static*/
void RenderView::ForEach(RenderViewVisitor* visitor) {
DCHECK(RenderThread::IsMainThread());
ViewMap* views = g_view_map.Pointer();
for (auto it = views->begin(); it != views->end(); ++it) {
if (!visitor->Visit(it->second))
return;
}
}
/*static*/
RenderViewImpl* RenderViewImpl::Create(
AgentSchedulingGroup& agent_scheduling_group,
mojom::CreateViewParamsPtr params,
bool was_created_by_renderer,
scoped_refptr<base::SingleThreadTaskRunner> task_runner) {
DCHECK(params->view_id != MSG_ROUTING_NONE);
DCHECK(!params->session_storage_namespace_id.empty())
<< "Session storage namespace must be populated.";
RenderViewImpl* render_view =
new RenderViewImpl(agent_scheduling_group, *params);
render_view->Initialize(std::move(params), was_created_by_renderer,
std::move(task_runner));
return render_view;
}
void RenderViewImpl::Destroy() {
destroying_ = true;
webview_->Close();
// The webview_ is already destroyed by the time we get here, remove any
// references to it.
g_view_map.Get().erase(webview_);
webview_ = nullptr;
delete this;
}
// blink::WebViewClient ------------------------------------------------------
// TODO(csharrison): Migrate this method to WebLocalFrameClient /
// RenderFrameImpl, as it is now serviced by a mojo interface scoped to the
// opener frame.
WebView* RenderViewImpl::CreateView(
WebLocalFrame* creator,
const WebURLRequest& request,
const WebWindowFeatures& features,
const WebString& frame_name,
WebNavigationPolicy policy,
network::mojom::WebSandboxFlags sandbox_flags,
const blink::SessionStorageNamespaceId& session_storage_namespace_id,
bool& consumed_user_gesture,
const absl::optional<blink::WebImpression>& impression) {
consumed_user_gesture = false;
RenderFrameImpl* creator_frame = RenderFrameImpl::FromWebFrame(creator);
mojom::CreateNewWindowParamsPtr params = mojom::CreateNewWindowParams::New();
// The user activation check is done at the browser process through
// |frame_host->CreateNewWindow()| call below. But the extensions case
// handled through the following |if| is an exception.
params->allow_popup = false;
if (GetContentClient()->renderer()->AllowPopup())
params->allow_popup = true;
params->window_container_type = WindowFeaturesToContainerType(features);
params->session_storage_namespace_id = session_storage_namespace_id;
if (!features.noopener) {
params->clone_from_session_storage_namespace_id =
GetWebView()->GetSessionStorageNamespaceId();
}
const std::string& frame_name_utf8 = frame_name.Utf8(
WebString::UTF8ConversionMode::kStrictReplacingErrorsWithFFFD);
params->frame_name = frame_name_utf8;
params->opener_suppressed = features.noopener;
params->disposition = NavigationPolicyToDisposition(policy);
if (!request.IsNull()) {
params->target_url = request.Url();
params->referrer = blink::mojom::Referrer::New(
blink::WebStringToGURL(request.ReferrerString()),
request.GetReferrerPolicy());
}
params->features = ConvertWebWindowFeaturesToMojoWindowFeatures(features);
if (impression) {
params->impression = blink::ConvertWebImpressionToImpression(*impression);
}
params->download_policy.ApplyDownloadFramePolicy(
/*is_opener_navigation=*/false, request.HasUserGesture(),
// `openee_can_access_opener_origin` only matters for opener navigations,
// so its value here is irrelevant.
/*openee_can_access_opener_origin=*/true, !creator->IsAllowedToDownload(),
creator->IsAdSubframe());
// We preserve this information before sending the message since |params| is
// moved on send.
bool is_background_tab =
params->disposition == WindowOpenDisposition::NEW_BACKGROUND_TAB;
mojom::CreateNewWindowStatus status;
mojom::CreateNewWindowReplyPtr reply;
auto* frame_host = creator_frame->GetFrameHost();
bool err = !frame_host->CreateNewWindow(std::move(params), &status, &reply);
// If creation of the window was blocked, return before consuming user
// activation. A frame that isn't itself activated shouldn't be able to
// consume the activation for the rest of the frame tree.
if (status == mojom::CreateNewWindowStatus::kBlocked)
return nullptr;
consumed_user_gesture = creator->ConsumeTransientUserActivation(
blink::UserActivationUpdateSource::kBrowser);
// If there was an error or we should ignore the new window (e.g. because of
// `noopener`), return now that user activation was consumed.
if (err || status == mojom::CreateNewWindowStatus::kIgnore)
return nullptr;
// For Android WebView, we support a pop-up like behavior for window.open()
// even if the embedding app doesn't support multiple windows. In this case,
// window.open() will return "window" and navigate it to whatever URL was
// passed. We also don't need to consume user gestures to protect against
// multiple windows being opened, because, well, the app doesn't support
// multiple windows.
// TODO(dcheng): It's awkward that this is plumbed into Blink but not really
// used much in Blink, except to enable web testing... perhaps this should
// be checked directly in the browser side.
if (status == mojom::CreateNewWindowStatus::kReuse)
return GetWebView();
DCHECK(reply);
DCHECK_NE(MSG_ROUTING_NONE, reply->route_id);
DCHECK_NE(MSG_ROUTING_NONE, reply->main_frame_route_id);
DCHECK_NE(MSG_ROUTING_NONE, reply->widget_params->routing_id);
// While this view may be a background extension page, it can spawn a visible
// render view. So we just assume that the new one is not another background
// page instead of passing on our own value.
// TODO(vangelis): Can we tell if the new view will be a background page?
bool never_composited = false;
// The initial hidden state for the RenderViewImpl here has to match what the
// browser will eventually decide for the given disposition. Since we have to
// return from this call synchronously, we just have to make our best guess
// and rely on the browser sending a WasHidden / WasShown message if it
// disagrees.
mojom::CreateViewParamsPtr view_params = mojom::CreateViewParams::New();
view_params->opener_frame_token = creator->GetFrameToken();
DCHECK_EQ(GetRoutingID(), creator_frame->render_view()->GetRoutingID());
view_params->window_was_created_with_opener = true;
view_params->renderer_preferences = webview_->GetRendererPreferences();
view_params->web_preferences = webview_->GetWebPreferences();
view_params->view_id = reply->route_id;
view_params->replication_state = blink::mojom::FrameReplicationState::New();
view_params->replication_state->frame_policy.sandbox_flags = sandbox_flags;
view_params->replication_state->name = frame_name_utf8;
view_params->devtools_main_frame_token = reply->devtools_main_frame_token;
auto main_frame_params = mojom::CreateLocalMainFrameParams::New();
main_frame_params->token = reply->main_frame_token;
main_frame_params->routing_id = reply->main_frame_route_id;
main_frame_params->frame = std::move(reply->frame);
main_frame_params->interface_broker =
std::move(reply->main_frame_interface_broker);
main_frame_params->policy_container = std::move(reply->policy_container);
main_frame_params->widget_params = std::move(reply->widget_params);
main_frame_params->subresource_loader_factories =
base::WrapUnique(static_cast<blink::PendingURLLoaderFactoryBundle*>(
creator_frame->CloneLoaderFactories()->Clone().release()));
view_params->main_frame =
mojom::CreateMainFrameUnion::NewLocalParams(std::move(main_frame_params));
view_params->blink_page_broadcast = std::move(reply->page_broadcast);
view_params->session_storage_namespace_id =
reply->cloned_session_storage_namespace_id;
DCHECK(!view_params->session_storage_namespace_id.empty())
<< "Session storage namespace must be populated.";
view_params->hidden = is_background_tab;
view_params->never_composited = never_composited;
RenderViewImpl* view = RenderViewImpl::Create(
agent_scheduling_group_, std::move(view_params),
/*was_created_by_renderer=*/true,
creator->GetTaskRunner(blink::TaskType::kInternalDefault));
if (reply->wait_for_debugger) {
blink::WebFrameWidget* frame_widget = view->GetWebView()
->MainFrame()
->ToWebLocalFrame()
->LocalRoot()
->FrameWidget();
frame_widget->WaitForDebuggerWhenShown();
}
return view->GetWebView();
}
// RenderView implementation ---------------------------------------------------
int RenderViewImpl::GetRoutingID() {
return routing_id_;
}
blink::WebView* RenderViewImpl::GetWebView() {
return webview_;
}
} // namespace content
|