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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
content / public / test / test_utils.cc [blame]
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "content/public/test/test_utils.h"
#include <memory>
#include <utility>
#include "base/base_switches.h"
#include "base/command_line.h"
#include "base/functional/bind.h"
#include "base/location.h"
#include "base/run_loop.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "base/task/current_thread.h"
#include "base/task/sequence_manager/sequence_manager.h"
#include "base/task/single_thread_task_runner.h"
#include "base/task/task_observer.h"
#include "base/task/thread_pool/thread_pool_instance.h"
#include "base/values.h"
#include "build/build_config.h"
#include "content/browser/child_process_security_policy_impl.h"
#include "content/browser/font_access/font_enumeration_cache.h"
#include "content/browser/origin_agent_cluster_isolation_state.h"
#include "content/browser/renderer_host/render_frame_host_delegate.h"
#include "content/browser/renderer_host/render_frame_host_impl.h"
#include "content/browser/site_info.h"
#include "content/browser/site_instance_impl.h"
#include "content/browser/web_contents/web_contents_impl.h"
#include "content/common/content_navigation_policy.h"
#include "content/common/features.h"
#include "content/public/browser/browser_child_process_host_iterator.h"
#include "content/public/browser/browser_task_traits.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/render_process_host.h"
#include "content/public/browser/site_isolation_policy.h"
#include "content/public/browser/web_contents.h"
#include "content/public/common/content_client.h"
#include "content/public/common/content_switches.h"
#include "content/public/common/process_type.h"
#include "content/public/common/url_constants.h"
#include "content/public/test/test_launcher.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/public/common/fetch/fetch_api_request_headers_map.h"
#include "third_party/blink/public/mojom/fetch/fetch_api_request.mojom.h"
#include "url/url_util.h"
namespace content {
namespace {
// Number of times to repost a Quit task so that the MessageLoop finishes up
// pending tasks and tasks posted by those pending tasks without risking the
// potential hang behavior of MessageLoop::QuitWhenIdle.
// The criteria for choosing this number: it should be high enough to make the
// quit act like QuitWhenIdle, while taking into account that any page which is
// animating may be rendering another frame for each quit deferral. For an
// animating page, the potential delay to quitting the RunLoop would be
// kNumQuitDeferrals * frame_render_time. Some perf tests run slow, such as
// 200ms/frame.
constexpr int kNumQuitDeferrals = 10;
void DeferredQuitRunLoop(base::OnceClosure quit_task, int num_quit_deferrals) {
if (num_quit_deferrals <= 0) {
std::move(quit_task).Run();
} else {
base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
FROM_HERE, base::BindOnce(&DeferredQuitRunLoop, std::move(quit_task),
num_quit_deferrals - 1));
}
}
// Monitors if any task is processed by the message loop.
class TaskObserver : public base::TaskObserver {
public:
TaskObserver() = default;
TaskObserver(const TaskObserver&) = delete;
TaskObserver& operator=(const TaskObserver&) = delete;
~TaskObserver() override = default;
// TaskObserver overrides.
void WillProcessTask(const base::PendingTask& pending_task,
bool was_blocked_or_low_priority) override {}
void DidProcessTask(const base::PendingTask& pending_task) override {
if (base::EndsWith(pending_task.posted_from.file_name(), "base/run_loop.cc",
base::CompareCase::SENSITIVE)) {
// Don't consider RunLoop internal tasks (i.e. QuitClosure() reposted by
// ProxyToTaskRunner() or RunLoop timeouts) as actual work.
return;
}
processed_ = true;
}
// Returns true if any task was processed.
bool processed() const { return processed_; }
private:
bool processed_ = false;
};
} // namespace
blink::mojom::FetchAPIRequestPtr CreateFetchAPIRequest(
const GURL& url,
const std::string& method,
const blink::FetchAPIRequestHeadersMap& headers,
blink::mojom::ReferrerPtr referrer,
bool is_reload) {
auto request = blink::mojom::FetchAPIRequest::New();
request->url = url;
request->method = method;
request->headers = headers;
request->referrer = std::move(referrer);
request->is_reload = is_reload;
return request;
}
void RunMessageLoop() {
base::RunLoop(base::RunLoop::Type::kNestableTasksAllowed).Run();
}
void RunAllPendingInMessageLoop() {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
RunAllPendingInMessageLoop(BrowserThread::UI);
}
void RunAllPendingInMessageLoop(BrowserThread::ID thread_id) {
// See comment for |kNumQuitDeferrals| for why this is needed.
for (int i = 0; i <= kNumQuitDeferrals; ++i) {
BrowserThread::RunAllPendingTasksOnThreadForTesting(thread_id);
}
}
void RunAllTasksUntilIdle() {
while (true) {
// Setup a task observer to determine if MessageLoop tasks run in the
// current loop iteration and loop in case the MessageLoop posts tasks to
// the Task Scheduler after the initial flush.
TaskObserver task_observer;
base::CurrentThread::Get()->AddTaskObserver(&task_observer);
// This must use RunLoop::Type::kNestableTasksAllowed in case this
// RunAllTasksUntilIdle() call is nested inside an existing Run(). Without
// it, the QuitWhenIdleClosure() below would never run if it's posted from
// another thread (i.e.. by run_loop.cc's ProxyToTaskRunner).
base::RunLoop run_loop(base::RunLoop::Type::kNestableTasksAllowed);
base::ThreadPoolInstance::Get()->FlushAsyncForTesting(
run_loop.QuitWhenIdleClosure());
run_loop.Run();
base::CurrentThread::Get()->RemoveTaskObserver(&task_observer);
if (!task_observer.processed())
break;
}
}
base::OnceClosure GetDeferredQuitTaskForRunLoop(base::RunLoop* run_loop) {
return base::BindOnce(&DeferredQuitRunLoop, run_loop->QuitClosure(),
kNumQuitDeferrals);
}
bool AreAllSitesIsolatedForTesting() {
return SiteIsolationPolicy::UseDedicatedProcessesForAllSites();
}
bool IsOriginAgentClusterEnabledForOrigin(SiteInstance* site_instance,
const url::Origin& origin) {
OriginAgentClusterIsolationState origin_requests_isolation(
OriginAgentClusterIsolationState::CreateNonIsolated());
return static_cast<ChildProcessSecurityPolicyImpl*>(
ChildProcessSecurityPolicy::GetInstance())
->DetermineOriginAgentClusterIsolation(
static_cast<SiteInstanceImpl*>(site_instance)->GetIsolationContext(),
origin, origin_requests_isolation)
.is_origin_agent_cluster();
}
bool AreDefaultSiteInstancesEnabled() {
return !AreAllSitesIsolatedForTesting() &&
base::FeatureList::IsEnabled(
features::kProcessSharingWithDefaultSiteInstances);
}
bool AreStrictSiteInstancesEnabled() {
return AreAllSitesIsolatedForTesting() ||
base::FeatureList::IsEnabled(
features::kProcessSharingWithStrictSiteInstances);
}
bool IsIsolatedOriginRequiredToGuaranteeDedicatedProcess() {
return AreDefaultSiteInstancesEnabled() ||
base::FeatureList::IsEnabled(
features::kProcessSharingWithStrictSiteInstances);
}
void IsolateAllSitesForTesting(base::CommandLine* command_line) {
command_line->AppendSwitch(switches::kSitePerProcess);
}
bool CanSameSiteMainFrameNavigationsChangeRenderFrameHosts() {
return ShouldCreateNewRenderFrameHostOnSameSiteNavigation(
/*is_main_frame=*/true, /*is_local_root=*/true) ||
CanSameSiteMainFrameNavigationsChangeSiteInstances();
}
bool WillSameSiteNavigationChangeRenderFrameHosts(bool is_main_frame,
bool is_local_root) {
return ShouldCreateNewRenderFrameHostOnSameSiteNavigation(is_main_frame,
is_local_root);
}
bool CanSameSiteMainFrameNavigationsChangeSiteInstances() {
return IsBackForwardCacheEnabled();
}
bool IsNavigationQueueingEnabled() {
return ShouldQueueNavigationsWhenPendingCommitRFHExists();
}
void DisableProactiveBrowsingInstanceSwapFor(RenderFrameHost* rfh) {
if (!CanSameSiteMainFrameNavigationsChangeSiteInstances())
return;
// If the RFH is not a primary main frame, navigations on it will never result
// in a proactive BrowsingInstance swap, so we shouldn't call this function on
// subframes.
DCHECK(rfh->IsInPrimaryMainFrame());
static_cast<RenderFrameHostImpl*>(rfh)
->DisableProactiveBrowsingInstanceSwapForTesting();
}
GURL GetWebUIURL(const std::string& host) {
return GURL(GetWebUIURLString(host));
}
std::string GetWebUIURLString(const std::string& host) {
return std::string(content::kChromeUIScheme) + url::kStandardSchemeSeparator +
host;
}
WebContents* CreateAndAttachInnerContents(RenderFrameHost* rfh) {
auto* outer_contents = WebContents::FromRenderFrameHost(rfh);
if (!outer_contents)
return nullptr;
WebContents::CreateParams inner_params(outer_contents->GetBrowserContext());
std::unique_ptr<WebContents> inner_contents_ptr =
WebContents::Create(inner_params);
// Attach. |inner_contents| becomes owned by |outer_contents|.
WebContents* inner_contents = inner_contents_ptr.get();
outer_contents->AttachInnerWebContents(std::move(inner_contents_ptr), rfh,
/*is_full_page=*/false);
return inner_contents;
}
void AwaitDocumentOnLoadCompleted(WebContents* web_contents) {
class Awaiter : public WebContentsObserver {
public:
explicit Awaiter(content::WebContents* web_contents)
: content::WebContentsObserver(web_contents),
observed_(
web_contents->IsDocumentOnLoadCompletedInPrimaryMainFrame()) {}
Awaiter(const Awaiter&) = delete;
Awaiter& operator=(const Awaiter&) = delete;
~Awaiter() override = default;
void Await() {
if (!observed_)
run_loop_.Run();
DCHECK(web_contents()->IsDocumentOnLoadCompletedInPrimaryMainFrame());
}
// WebContentsObserver:
void DocumentOnLoadCompletedInPrimaryMainFrame() override {
observed_ = true;
if (run_loop_.running())
run_loop_.Quit();
}
private:
bool observed_ = false;
base::RunLoop run_loop_;
};
Awaiter(web_contents).Await();
}
void FocusWebContentsOnFrame(WebContents* web_contents, RenderFrameHost* rfh) {
WebContentsImpl* contents = static_cast<WebContentsImpl*>(web_contents);
FrameTreeNode* node =
contents->GetPrimaryFrameTree().FindByID(rfh->GetFrameTreeNodeId());
CHECK(node);
CHECK_EQ(node->current_frame_host(), rfh);
contents->GetPrimaryFrameTree().SetFocusedFrame(
node, node->current_frame_host()->GetSiteInstance()->group());
}
MessageLoopRunner::MessageLoopRunner(QuitMode quit_mode)
: quit_mode_(quit_mode) {}
MessageLoopRunner::~MessageLoopRunner() = default;
void MessageLoopRunner::Run() {
DCHECK(thread_checker_.CalledOnValidThread());
// Do not run the message loop if our quit closure has already been called.
// This helps in scenarios where the closure has a chance to run before
// we Run explicitly.
if (quit_closure_called_)
return;
loop_running_ = true;
run_loop_.Run();
}
base::OnceClosure MessageLoopRunner::QuitClosure() {
return base::BindOnce(&MessageLoopRunner::Quit, this);
}
void MessageLoopRunner::Quit() {
DCHECK(thread_checker_.CalledOnValidThread());
quit_closure_called_ = true;
// Only run the quit task if we are running the message loop.
if (loop_running_) {
switch (quit_mode_) {
case QuitMode::DEFERRED:
DeferredQuitRunLoop(run_loop_.QuitClosure(), kNumQuitDeferrals);
break;
case QuitMode::IMMEDIATE:
run_loop_.Quit();
break;
}
loop_running_ = false;
}
}
LoadStopObserver::LoadStopObserver(WebContents* web_contents)
: WebContentsObserver(web_contents),
run_loop_(base::RunLoop::Type::kNestableTasksAllowed) {}
void LoadStopObserver::Wait() {
if (!seen_)
run_loop_.Run();
EXPECT_TRUE(seen_);
}
void LoadStopObserver::DidStopLoading() {
seen_ = true;
run_loop_.Quit();
}
InProcessUtilityThreadHelper::InProcessUtilityThreadHelper() {
RenderProcessHost::SetRunRendererInProcess(true);
}
InProcessUtilityThreadHelper::~InProcessUtilityThreadHelper() {
JoinAllUtilityThreads();
RenderProcessHost::SetRunRendererInProcess(false);
}
void InProcessUtilityThreadHelper::JoinAllUtilityThreads() {
ASSERT_FALSE(run_loop_);
run_loop_.emplace();
BrowserChildProcessObserver::Add(this);
CheckHasRunningChildProcess();
run_loop_->Run();
run_loop_.reset();
BrowserChildProcessObserver::Remove(this);
}
void InProcessUtilityThreadHelper::CheckHasRunningChildProcess() {
ASSERT_TRUE(run_loop_);
auto check_has_running_child_process_on_io =
[](base::OnceClosure quit_closure) {
BrowserChildProcessHostIterator it;
// If not Done(), we have some running child processes and need to wait.
if (it.Done())
std::move(quit_closure).Run();
};
GetUIThreadTaskRunner({})->PostTask(
FROM_HERE, base::BindOnce(check_has_running_child_process_on_io,
run_loop_->QuitClosure()));
}
void InProcessUtilityThreadHelper::BrowserChildProcessHostDisconnected(
const ChildProcessData& data) {
CheckHasRunningChildProcess();
}
RenderFrameDeletedObserver::RenderFrameDeletedObserver(RenderFrameHost* rfh)
: WebContentsObserver(WebContents::FromRenderFrameHost(rfh)),
rfh_id_(rfh->GetGlobalId()) {
DCHECK(rfh);
}
RenderFrameDeletedObserver::~RenderFrameDeletedObserver() = default;
void RenderFrameDeletedObserver::RenderFrameDeleted(
RenderFrameHost* render_frame_host) {
if (render_frame_host->GetGlobalId() == rfh_id_) {
rfh_id_ = GlobalRenderFrameHostId();
if (runner_.get())
runner_->Quit();
}
}
bool RenderFrameDeletedObserver::deleted() const {
return rfh_id_ == GlobalRenderFrameHostId();
}
bool RenderFrameDeletedObserver::WaitUntilDeleted() {
if (deleted())
return true;
runner_ = std::make_unique<base::RunLoop>();
runner_->Run();
runner_.reset();
return deleted();
}
RenderFrameHostWrapper::RenderFrameHostWrapper(RenderFrameHost* rfh)
: rfh_id_(rfh ? rfh->GetGlobalId() : GlobalRenderFrameHostId()),
deleted_observer_(rfh ? std::make_unique<RenderFrameDeletedObserver>(rfh)
: nullptr) {}
RenderFrameHostWrapper::RenderFrameHostWrapper(RenderFrameHostWrapper&& rfhft) =
default;
RenderFrameHostWrapper::~RenderFrameHostWrapper() = default;
RenderFrameHost* RenderFrameHostWrapper::get() const {
return RenderFrameHost::FromID(rfh_id_);
}
bool RenderFrameHostWrapper::IsDestroyed() const {
return get() == nullptr;
}
// See RenderFrameDeletedObserver for notes on the difference between
// RenderFrame being deleted and RenderFrameHost being destroyed.
bool RenderFrameHostWrapper::WaitUntilRenderFrameDeleted() const {
CHECK(deleted_observer_);
return deleted_observer_->WaitUntilDeleted();
}
bool RenderFrameHostWrapper::IsRenderFrameDeleted() const {
CHECK(deleted_observer_);
return deleted_observer_->deleted();
}
RenderFrameHost& RenderFrameHostWrapper::operator*() const {
DCHECK(get());
return *get();
}
RenderFrameHost* RenderFrameHostWrapper::operator->() const {
DCHECK(get());
return get();
}
WebContentsDestroyedWatcher::WebContentsDestroyedWatcher(
WebContents* web_contents)
: WebContentsObserver(web_contents) {
EXPECT_TRUE(web_contents != nullptr);
}
WebContentsDestroyedWatcher::~WebContentsDestroyedWatcher() = default;
void WebContentsDestroyedWatcher::Wait() {
run_loop_.Run();
}
void WebContentsDestroyedWatcher::WebContentsDestroyed() {
destroyed_ = true;
run_loop_.Quit();
}
TestPageScaleObserver::TestPageScaleObserver(WebContents* web_contents)
: WebContentsObserver(web_contents) {}
TestPageScaleObserver::~TestPageScaleObserver() = default;
void TestPageScaleObserver::OnPageScaleFactorChanged(float page_scale_factor) {
last_scale_ = page_scale_factor;
seen_page_scale_change_ = true;
if (done_callback_)
std::move(done_callback_).Run();
}
float TestPageScaleObserver::WaitForPageScaleUpdate() {
if (!seen_page_scale_change_) {
base::RunLoop run_loop;
done_callback_ = run_loop.QuitClosure();
run_loop.Run();
}
seen_page_scale_change_ = false;
return last_scale_;
}
EffectiveURLContentBrowserClientHelper::EffectiveURLContentBrowserClientHelper(
bool requires_dedicated_process)
: requires_dedicated_process_(requires_dedicated_process) {}
EffectiveURLContentBrowserClientHelper::
~EffectiveURLContentBrowserClientHelper() = default;
void EffectiveURLContentBrowserClientHelper::AddTranslation(
const GURL& url_to_modify,
const GURL& url_to_return) {
urls_to_modify_[url_to_modify] = url_to_return;
}
GURL EffectiveURLContentBrowserClientHelper::GetEffectiveURL(const GURL& url) {
auto it = urls_to_modify_.find(url);
if (it != urls_to_modify_.end()) {
return it->second;
}
return url;
}
bool EffectiveURLContentBrowserClientHelper::DoesSiteRequireDedicatedProcess(
BrowserContext* browser_context,
const GURL& effective_site_url) {
if (!requires_dedicated_process_) {
return false;
}
for (const auto& pair : urls_to_modify_) {
auto site_info = SiteInfo::CreateForTesting(
IsolationContext(browser_context), pair.first);
if (site_info.site_url() == effective_site_url) {
return true;
}
}
return false;
}
EffectiveURLContentBrowserClient::EffectiveURLContentBrowserClient(
bool requires_dedicated_process)
: helper_(requires_dedicated_process) {}
EffectiveURLContentBrowserClient::EffectiveURLContentBrowserClient(
const GURL& url_to_modify,
const GURL& url_to_return,
bool requires_dedicated_process)
: helper_(requires_dedicated_process) {
AddTranslation(url_to_modify, url_to_return);
}
EffectiveURLContentBrowserClient::~EffectiveURLContentBrowserClient() = default;
void EffectiveURLContentBrowserClient::AddTranslation(
const GURL& url_to_modify,
const GURL& url_to_return) {
helper_.AddTranslation(url_to_modify, url_to_return);
}
GURL EffectiveURLContentBrowserClient::GetEffectiveURL(
BrowserContext* browser_context,
const GURL& url) {
return helper_.GetEffectiveURL(url);
}
bool EffectiveURLContentBrowserClient::DoesSiteRequireDedicatedProcess(
BrowserContext* browser_context,
const GURL& effective_site_url) {
return helper_.DoesSiteRequireDedicatedProcess(browser_context,
effective_site_url);
}
ScopedContentBrowserClientSetting::ScopedContentBrowserClientSetting(
ContentBrowserClient* new_client)
: old_client_(SetBrowserClientForTesting(new_client)) {}
ScopedContentBrowserClientSetting::~ScopedContentBrowserClientSetting() {
SetBrowserClientForTesting(old_client_);
}
} // namespace content