forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontent_browser_test_utils_internal.cc
1250 lines (1076 loc) · 45.3 KB
/
content_browser_test_utils_internal.cc
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
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// 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 "content/test/content_browser_test_utils_internal.h"
#include <stddef.h>
#include <map>
#include <memory>
#include <set>
#include <utility>
#include <vector>
#include "base/containers/stack.h"
#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/ranges/algorithm.h"
#include "base/strings/escape.h"
#include "base/strings/stringprintf.h"
#include "base/task/thread_pool.h"
#include "base/test/bind.h"
#include "base/test/test_future.h"
#include "base/test/test_timeouts.h"
#include "build/build_config.h"
#include "content/browser/preloading/prerender/prerender_host_registry.h"
#include "content/browser/renderer_host/delegated_frame_host.h"
#include "content/browser/renderer_host/frame_tree_node.h"
#include "content/browser/renderer_host/navigation_request.h"
#include "content/browser/renderer_host/navigator.h"
#include "content/browser/renderer_host/render_frame_host_delegate.h"
#include "content/browser/renderer_host/render_frame_proxy_host.h"
#include "content/browser/renderer_host/render_widget_host_factory.h"
#include "content/browser/site_instance_impl.h"
#include "content/browser/web_contents/web_contents_impl.h"
#include "content/common/frame_messages.mojom.h"
#include "content/public/browser/browser_task_traits.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/file_select_listener.h"
#include "content/public/browser/navigation_handle.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/site_isolation_policy.h"
#include "content/public/browser/web_contents.h"
#include "content/public/test/browser_test_utils.h"
#include "content/public/test/content_browser_test_utils.h"
#include "content/public/test/test_frame_navigation_observer.h"
#include "content/public/test/test_navigation_observer.h"
#include "content/shell/browser/shell.h"
#include "content/shell/browser/shell_javascript_dialog_manager.h"
#include "net/test/embedded_test_server/http_request.h"
#include "net/test/embedded_test_server/http_response.h"
#include "net/test/embedded_test_server/request_handler_util.h"
#include "third_party/blink/public/common/frame/frame_visual_properties.h"
#if BUILDFLAG(IS_ANDROID)
#include "cc/slim/layer_tree.h"
#include "content/browser/renderer_host/compositor_impl_android.h"
#include "ui/android/window_android.h"
#include "ui/android/window_android_compositor.h"
#endif // BUILDFLAG(IS_ANDROID)
#if BUILDFLAG(IS_MAC)
#include "content/browser/renderer_host/browser_compositor_view_mac.h"
#include "content/browser/renderer_host/test_render_widget_host_view_mac_factory.h"
#endif // BUILDFLAG(IS_MAC)
#if BUILDFLAG(IS_IOS)
#include "content/browser/renderer_host/browser_compositor_ios.h"
#include "content/browser/renderer_host/test_render_widget_host_view_ios_factory.h"
#endif // BUILDFLAG(IS_IOS)
#if defined(USE_AURA)
#include "content/browser/renderer_host/render_widget_host_view_aura.h"
#endif // defined(USE_AURA)
namespace content {
bool NavigateFrameToURL(FrameTreeNode* node, const GURL& url) {
TestFrameNavigationObserver observer(node);
NavigationController::LoadURLParams params(url);
params.transition_type = ui::PAGE_TRANSITION_LINK;
params.frame_tree_node_id = node->frame_tree_node_id();
FrameTree& frame_tree = node->frame_tree();
node->navigator().controller().LoadURLWithParams(params);
observer.Wait();
if (!observer.last_navigation_succeeded()) {
DLOG(WARNING) << "Navigation did not succeed: " << url;
return false;
}
// It's possible for JS handlers triggered during the navigation to remove
// the node, so retrieve it by ID again to check if that occurred.
node = frame_tree.FindByID(params.frame_tree_node_id);
if (node && url != node->current_url()) {
DLOG(WARNING) << "Expected URL " << url << " but observed "
<< node->current_url();
return false;
}
return true;
}
void SetShouldProceedOnBeforeUnload(Shell* shell, bool proceed, bool success) {
ShellJavaScriptDialogManager* manager =
static_cast<ShellJavaScriptDialogManager*>(
shell->GetJavaScriptDialogManager(shell->web_contents()));
manager->set_should_proceed_on_beforeunload(proceed, success);
}
RenderFrameHost* ConvertToRenderFrameHost(FrameTreeNode* frame_tree_node) {
return frame_tree_node->current_frame_host();
}
bool NavigateToURLInSameBrowsingInstance(Shell* window, const GURL& url) {
TestNavigationObserver observer(window->web_contents());
// Using a PAGE_TRANSITION_LINK transition with a browser-initiated
// navigation forces it to stay in the current BrowsingInstance, as normally
// that transition is used by renderer-initiated navigations.
window->LoadURLForFrame(url, std::string(),
ui::PageTransitionFromInt(ui::PAGE_TRANSITION_LINK));
observer.Wait();
if (!IsLastCommittedEntryOfPageType(window->web_contents(),
PAGE_TYPE_NORMAL)) {
NavigationEntry* last_entry =
window->web_contents()->GetController().GetLastCommittedEntry();
DLOG(WARNING) << "last_entry->GetPageType() = "
<< (last_entry ? last_entry->GetPageType() : -1);
return false;
}
if (window->web_contents()->GetLastCommittedURL() != url) {
DLOG(WARNING) << "window->web_contents()->GetLastCommittedURL() = "
<< window->web_contents()->GetLastCommittedURL()
<< "; url = " << url;
return false;
}
return true;
}
bool IsExpectedSubframeErrorTransition(SiteInstance* start_site_instance,
SiteInstance* end_site_instance) {
bool site_instances_are_equal = (start_site_instance == end_site_instance);
// AgentClusterKey mismatch will trigger a SiteInstance switch.
if (static_cast<SiteInstanceImpl*>(start_site_instance)
->GetSiteInfo()
.agent_cluster_key() !=
static_cast<SiteInstanceImpl*>(end_site_instance)
->GetSiteInfo()
.agent_cluster_key() &&
!site_instances_are_equal) {
return true;
}
bool is_error_page_site_instance =
(static_cast<SiteInstanceImpl*>(end_site_instance)
->GetSiteInfo()
.is_error_page());
if (!SiteIsolationPolicy::IsErrorPageIsolationEnabled(
/*in_main_frame=*/false)) {
return site_instances_are_equal && !is_error_page_site_instance;
} else {
return !site_instances_are_equal && is_error_page_site_instance;
}
}
RenderFrameHost* CreateSubframe(WebContentsImpl* web_contents,
std::string frame_id,
const GURL& url,
bool wait_for_navigation) {
RenderFrameHostCreatedObserver subframe_created_observer(web_contents);
TestNavigationObserver subframe_nav_observer(web_contents);
if (url.is_empty()) {
EXPECT_TRUE(ExecJs(web_contents, JsReplace(R"(
var iframe = document.createElement('iframe');
iframe.id = $1;
document.body.appendChild(iframe);
)",
frame_id)));
} else {
EXPECT_TRUE(ExecJs(web_contents, JsReplace(R"(
var iframe = document.createElement('iframe');
iframe.id = $1;
iframe.src = $2;
document.body.appendChild(iframe);
)",
frame_id, url)));
}
subframe_created_observer.Wait();
if (wait_for_navigation)
subframe_nav_observer.Wait();
FrameTreeNode* root = web_contents->GetPrimaryFrameTree().root();
return root->child_at(root->child_count() - 1)->current_frame_host();
}
std::vector<RenderFrameHostImpl*> CollectAllRenderFrameHosts(
RenderFrameHostImpl* starting_rfh) {
std::vector<RenderFrameHostImpl*> visited_frames;
starting_rfh->ForEachRenderFrameHost(
[&](RenderFrameHostImpl* rfh) { visited_frames.push_back(rfh); });
return visited_frames;
}
std::vector<RenderFrameHostImpl*>
CollectAllRenderFrameHostsIncludingSpeculative(
RenderFrameHostImpl* starting_rfh) {
std::vector<RenderFrameHostImpl*> visited_frames;
starting_rfh->ForEachRenderFrameHostIncludingSpeculative(
[&](RenderFrameHostImpl* rfh) { visited_frames.push_back(rfh); });
return visited_frames;
}
std::vector<RenderFrameHostImpl*> CollectAllRenderFrameHosts(
WebContentsImpl* web_contents) {
std::vector<RenderFrameHostImpl*> visited_frames;
web_contents->ForEachRenderFrameHost(
[&](RenderFrameHostImpl* rfh) { visited_frames.push_back(rfh); });
return visited_frames;
}
std::vector<RenderFrameHostImpl*>
CollectAllRenderFrameHostsIncludingSpeculative(WebContentsImpl* web_contents) {
std::vector<RenderFrameHostImpl*> visited_frames;
web_contents->ForEachRenderFrameHostIncludingSpeculative(
[&](RenderFrameHostImpl* rfh) { visited_frames.push_back(rfh); });
return visited_frames;
}
Shell* OpenBlankWindow(WebContentsImpl* web_contents) {
FrameTreeNode* root = web_contents->GetPrimaryFrameTree().root();
ShellAddedObserver new_shell_observer;
EXPECT_TRUE(ExecJs(root, "last_opened_window = window.open()"));
Shell* new_shell = new_shell_observer.GetShell();
EXPECT_NE(new_shell->web_contents(), web_contents);
EXPECT_TRUE(new_shell->web_contents()
->GetController()
.GetLastCommittedEntry()
->IsInitialEntry());
EXPECT_EQ(1, new_shell->web_contents()->GetController().GetEntryCount());
return new_shell;
}
Shell* OpenWindow(WebContentsImpl* web_contents, const GURL& url) {
FrameTreeNode* root = web_contents->GetPrimaryFrameTree().root();
ShellAddedObserver new_shell_observer;
EXPECT_TRUE(
ExecJs(root, JsReplace("last_opened_window = window.open($1)", url)));
Shell* new_shell = new_shell_observer.GetShell();
EXPECT_NE(new_shell->web_contents(), web_contents);
return new_shell;
}
FrameTreeVisualizer::FrameTreeVisualizer() = default;
FrameTreeVisualizer::~FrameTreeVisualizer() = default;
std::string FrameTreeVisualizer::DepictFrameTree(FrameTreeNode* root) {
// Tracks the sites actually used in this depiction.
std::map<std::string, SiteInstance*> legend;
// Traversal 1: Assign names to current frames. This ensures that the first
// call to the pretty-printer will result in a naming of the site instances
// that feels natural and stable.
base::stack<FrameTreeNode*> to_explore;
for (to_explore.push(root); !to_explore.empty();) {
FrameTreeNode* node = to_explore.top();
to_explore.pop();
for (size_t i = node->child_count(); i-- != 0;) {
to_explore.push(node->child_at(i));
}
RenderFrameHost* current = node->render_manager()->current_frame_host();
legend[GetName(current->GetSiteInstance())] = current->GetSiteInstance();
}
// Traversal 2: Assign names to the pending/speculative frames. For stability
// of assigned names it's important to do this before trying to name the
// proxies, which have a less well defined order.
for (to_explore.push(root); !to_explore.empty();) {
FrameTreeNode* node = to_explore.top();
to_explore.pop();
for (size_t i = node->child_count(); i-- != 0;) {
to_explore.push(node->child_at(i));
}
RenderFrameHost* spec = node->render_manager()->speculative_frame_host();
if (spec)
legend[GetName(spec->GetSiteInstance())] = spec->GetSiteInstance();
}
// Traversal 3: Assign names to the SiteInstances within each group's proxies
// (which are associated with SiteInstanceGroups instead of SiteInstances) and
// add them to |legend| too. Typically, only openers should have their names
// assigned this way.
for (to_explore.push(root); !to_explore.empty();) {
FrameTreeNode* node = to_explore.top();
to_explore.pop();
for (size_t i = node->child_count(); i-- != 0;) {
to_explore.push(node->child_at(i));
}
// Sort the proxies by SiteInstanceGroup ID to avoid unordered_map ordering.
std::vector<SiteInstance*> site_instances;
for (const auto& proxy_pair :
node->render_manager()->GetAllProxyHostsForTesting()) {
SiteInstanceGroup* group = proxy_pair.second->site_instance_group();
for (raw_ptr<SiteInstanceImpl> instance :
group->site_instances_for_testing()) {
site_instances.push_back(instance);
}
}
std::sort(site_instances.begin(), site_instances.end(),
[](SiteInstance* lhs, SiteInstance* rhs) {
return lhs->GetId() < rhs->GetId();
});
for (SiteInstance* site_instance : site_instances)
legend[GetName(site_instance)] = site_instance;
}
// Traversal 4: Now that all names are assigned, make a big loop to pretty-
// print the tree. Each iteration produces exactly one line of format.
std::string result;
for (to_explore.push(root); !to_explore.empty();) {
FrameTreeNode* node = to_explore.top();
to_explore.pop();
for (size_t i = node->child_count(); i-- != 0;) {
to_explore.push(node->child_at(i));
}
// Draw the feeler line tree graphics by walking up to the root. A feeler
// line is needed for each ancestor that is the last child of its parent.
// This creates the ASCII art that looks like:
// Foo
// |--Foo
// |--Foo
// | |--Foo
// | +--Foo
// | +--Foo
// +--Foo
// +--Foo
//
// TODO(nick): Make this more elegant.
std::string line;
if (node != root) {
if (node->parent()->child_at(node->parent()->child_count() - 1) != node)
line = " |--";
else
line = " +--";
for (RenderFrameHostImpl* up = node->parent();
up != root->current_frame_host(); up = up->GetParent()) {
if (up->GetParent()
->child_at(up->GetParent()->child_count() - 1)
->current_frame_host() != up)
line = " | " + line;
else
line = " " + line;
}
}
// Prefix one extra space of padding for two reasons. First, this helps the
// diagram aligns nicely with the legend. Second, this makes it easier to
// read the diffs that gtest spits out on EXPECT_EQ failure.
line = " " + line;
// Summarize the FrameTreeNode's state. Always show the site of the current
// RenderFrameHost, and show any exceptional state of the node, like a
// pending or speculative RenderFrameHost.
RenderFrameHost* current = node->render_manager()->current_frame_host();
RenderFrameHost* spec = node->render_manager()->speculative_frame_host();
base::StringAppendF(&line, "Site %s",
GetName(current->GetSiteInstance()).c_str());
if (spec) {
base::StringAppendF(&line, " (%s speculative)",
GetName(spec->GetSiteInstance()).c_str());
}
// Show the SiteInstances of the RenderFrameProxyHosts of this node.
const auto& proxy_host_map =
node->render_manager()->GetAllProxyHostsForTesting();
if (!proxy_host_map.empty()) {
// Show a dashed line of variable length before the proxy list. Always at
// least two dashes.
line.append(" --");
// To make proxy lists align vertically for the first three tree levels,
// pad with dashes up to a first tab stop at column 19 (which works out to
// text editor column 28 in the typical diagram fed to EXPECT_EQ as a
// string literal). Lining the lists up vertically makes differences in
// the proxy sets easier to spot visually. We choose not to use the
// *actual* tree height here, because that would make the diagram's
// appearance less stable as the tree's shape evolves.
while (line.length() < 20) {
line.append("-");
}
line.append(" proxies for");
// Sort these alphabetically, to avoid hash_map ordering dependency.
std::vector<std::string> sorted_proxy_hosts;
for (const auto& proxy_pair : proxy_host_map) {
sorted_proxy_hosts.push_back(
GetGroupName(proxy_pair.second->site_instance_group()));
}
std::sort(sorted_proxy_hosts.begin(), sorted_proxy_hosts.end());
for (std::string& proxy_name : sorted_proxy_hosts) {
base::StringAppendF(&line, " %s", proxy_name.c_str());
}
}
if (node != root)
result.append("\n");
result.append(line);
}
// Finally, show a legend with details of the site instances.
const char* prefix = "Where ";
for (auto& legend_entry : legend) {
SiteInstanceImpl* site_instance =
static_cast<SiteInstanceImpl*>(legend_entry.second);
std::string description =
GetUrlWithoutPort(site_instance->GetSiteURL()).spec();
// data: URLs have site URLs of the form data:nonce, where the nonce is an
// UnguessableToken. Make these deterministic for testing by using the
// abbreviated letter for the site in the nonce. For example,
// "data:nonce_A".
if (site_instance->GetSiteURL().SchemeIs(url::kDataScheme)) {
description =
base::StringPrintf("data:nonce_%s", legend_entry.first.c_str());
}
base::StringAppendF(&result, "\n%s%s = %s", prefix,
legend_entry.first.c_str(), description.c_str());
// Highlight some exceptionable conditions.
if (site_instance->GetSiteInfo().is_sandboxed()) {
result.append(" (sandboxed)");
}
if (site_instance->group()->active_frame_count() == 0)
result.append(" (active_frame_count == 0)");
if (!site_instance->GetProcess()->IsInitializedAndNotDead())
result.append(" (no process)");
prefix = " ";
}
return result;
}
std::string FrameTreeVisualizer::GetName(SiteInstance* site_instance) {
// Indices into the vector correspond to letters of the alphabet.
size_t index =
base::ranges::find(seen_site_instance_ids_, site_instance->GetId()) -
seen_site_instance_ids_.begin();
if (index == seen_site_instance_ids_.size())
seen_site_instance_ids_.push_back(site_instance->GetId());
// Whosoever writes a test using >=26 site instances shall be a lucky ducky.
if (index < 25)
return base::StringPrintf("%c", 'A' + static_cast<char>(index));
else
return base::StringPrintf("Z%d", static_cast<int>(index - 25));
}
std::string FrameTreeVisualizer::GetGroupName(SiteInstanceGroup* group) {
// If there's only one SiteInstance in `group`, get the name of the
// SiteInstance directly. This preserves test expectations for DepictFrameTree
// uses that predate SiteInstanceGroup.
if (group->site_instances_for_testing().size() == 1) {
return GetName(*group->site_instances_for_testing().begin());
}
// Alphabetically sort the SiteInstances within the group.
std::vector<std::string> sorted_instance_names;
for (auto& site_instance : group->site_instances_for_testing()) {
sorted_instance_names.push_back(GetName(site_instance));
}
std::sort(sorted_instance_names.begin(), sorted_instance_names.end());
// Name the group using set notation.
CHECK(sorted_instance_names.size() >= 1u);
std::string result = "{";
for (auto& site_instance_name : sorted_instance_names) {
base::StringAppendF(&result, "%s,", site_instance_name.c_str());
}
result.resize(result.length() - 1);
result.append("}");
return result;
}
GURL FrameTreeVisualizer::GetUrlWithoutPort(const GURL& url) {
GURL::Replacements replacements;
replacements.ClearPort();
return url.ReplaceComponents(replacements);
}
std::string DepictFrameTree(FrameTreeNode& root) {
return FrameTreeVisualizer().DepictFrameTree(&root);
}
Shell* OpenPopup(const ToRenderFrameHost& opener,
const GURL& url,
const std::string& name) {
return OpenPopup(opener, url, name, "", true);
}
Shell* OpenPopup(const ToRenderFrameHost& opener,
const GURL& url,
const std::string& name,
const std::string& features,
bool expect_return_from_window_open) {
TestNavigationObserver observer(url);
observer.StartWatchingNewWebContents();
ShellAddedObserver new_shell_observer;
std::string popup_script = "!!window.open('" + url.spec() + "', '" + name +
"', '" + features + "');";
bool did_create_popup = EvalJs(opener, popup_script).ExtractBool();
if (!(did_create_popup || !expect_return_from_window_open)) {
return nullptr;
}
observer.Wait();
Shell* new_shell = new_shell_observer.GetShell();
EXPECT_EQ(
url,
new_shell->web_contents()->GetPrimaryMainFrame()->GetLastCommittedURL());
return new_shell_observer.GetShell();
}
FileChooserDelegate::FileChooserDelegate(std::vector<base::FilePath> files,
const base::FilePath& base_dir,
base::OnceClosure callback)
: files_(std::move(files)),
base_dir_(base_dir),
callback_(std::move(callback)) {}
FileChooserDelegate::FileChooserDelegate(const base::FilePath& file,
base::OnceClosure callback)
: FileChooserDelegate(std::vector<base::FilePath>(1, file),
base::FilePath(),
std::move(callback)) {}
FileChooserDelegate::~FileChooserDelegate() = default;
void FileChooserDelegate::RunFileChooser(
RenderFrameHost* render_frame_host,
scoped_refptr<content::FileSelectListener> listener,
const blink::mojom::FileChooserParams& params) {
// |base_dir_| should be set for and only for |kUploadFolder| mode.
DCHECK(base_dir_.empty() ==
(params.mode != blink::mojom::FileChooserParams::Mode::kUploadFolder));
// Send the selected files to the renderer process.
std::vector<blink::mojom::FileChooserFileInfoPtr> files;
for (const auto& file : files_) {
auto file_info = blink::mojom::FileChooserFileInfo::NewNativeFile(
blink::mojom::NativeFileInfo::New(file, std::u16string()));
files.push_back(std::move(file_info));
}
listener->FileSelected(std::move(files), base_dir_, params.mode);
params_ = params.Clone();
if (callback_)
std::move(callback_).Run();
}
FrameTestNavigationManager::FrameTestNavigationManager(
int filtering_frame_tree_node_id,
WebContents* web_contents,
const GURL& url)
: TestNavigationManager(web_contents, url),
filtering_frame_tree_node_id_(filtering_frame_tree_node_id) {}
bool FrameTestNavigationManager::ShouldMonitorNavigation(
NavigationHandle* handle) {
return TestNavigationManager::ShouldMonitorNavigation(handle) &&
handle->GetFrameTreeNodeId() == filtering_frame_tree_node_id_;
}
UrlCommitObserver::UrlCommitObserver(FrameTreeNode* frame_tree_node,
const GURL& url)
: content::WebContentsObserver(WebContents::FromRenderFrameHost(
frame_tree_node->current_frame_host())),
frame_tree_node_id_(frame_tree_node->frame_tree_node_id()),
url_(url) {}
UrlCommitObserver::~UrlCommitObserver() {}
void UrlCommitObserver::Wait() {
run_loop_.Run();
}
void UrlCommitObserver::DidFinishNavigation(
NavigationHandle* navigation_handle) {
if (navigation_handle->HasCommitted() &&
!navigation_handle->IsErrorPage() &&
navigation_handle->GetURL() == url_ &&
navigation_handle->GetFrameTreeNodeId() == frame_tree_node_id_) {
run_loop_.Quit();
}
}
RenderProcessHostBadIpcMessageWaiter::RenderProcessHostBadIpcMessageWaiter(
RenderProcessHost* render_process_host)
: internal_waiter_(render_process_host,
"Stability.BadMessageTerminated.Content") {}
std::optional<bad_message::BadMessageReason>
RenderProcessHostBadIpcMessageWaiter::Wait() {
std::optional<int> internal_result = internal_waiter_.Wait();
if (!internal_result.has_value())
return std::nullopt;
return static_cast<bad_message::BadMessageReason>(internal_result.value());
}
CreateNewPopupWidgetInterceptor::CreateNewPopupWidgetInterceptor(
RenderFrameHostImpl* rfh,
base::OnceCallback<void(RenderWidgetHostImpl*)> did_create_callback)
: swapped_impl_(rfh->local_frame_host_receiver_for_testing(), this),
did_create_callback_(std::move(did_create_callback)) {}
CreateNewPopupWidgetInterceptor::~CreateNewPopupWidgetInterceptor() = default;
void CreateNewPopupWidgetInterceptor::CreateNewPopupWidget(
mojo::PendingAssociatedReceiver<blink::mojom::PopupWidgetHost>
blink_popup_widget_host,
mojo::PendingAssociatedReceiver<blink::mojom::WidgetHost> blink_widget_host,
mojo::PendingAssociatedRemote<blink::mojom::Widget> blink_widget) {
class PopupWidgetCreationObserver : public RenderWidgetHostFactory {
public:
PopupWidgetCreationObserver() { RegisterFactory(this); }
~PopupWidgetCreationObserver() override { UnregisterFactory(); }
// RenderWidgetHostFactory overrides:
RenderWidgetHostImpl* CreateSelfOwnedRenderWidgetHost(
FrameTree* frame_tree,
RenderWidgetHostDelegate* delegate,
base::SafeRef<SiteInstanceGroup> site_instance_group,
int32_t routing_id,
bool hidden) override {
CHECK(!last_created_widget_);
last_created_widget_ =
RenderWidgetHostFactory::CreateSelfOwnedRenderWidgetHost(
frame_tree, delegate, std::move(site_instance_group), routing_id,
hidden);
return last_created_widget_;
}
RenderWidgetHostImpl* TakeLastCreatedWidget() {
return std::exchange(last_created_widget_, nullptr);
}
private:
raw_ptr<RenderWidgetHostImpl> last_created_widget_;
};
PopupWidgetCreationObserver creation_observer;
GetForwardingInterface()->CreateNewPopupWidget(
std::move(blink_popup_widget_host), std::move(blink_widget_host),
std::move(blink_widget));
if (!did_create_callback_) {
return;
}
if (auto* widget = creation_observer.TakeLastCreatedWidget(); widget) {
std::move(did_create_callback_).Run(widget);
}
}
blink::mojom::LocalFrameHost*
CreateNewPopupWidgetInterceptor::GetForwardingInterface() {
return swapped_impl_.old_impl();
}
#if BUILDFLAG(IS_MAC) || BUILDFLAG(IS_ANDROID)
ShowPopupWidgetWaiter::ShowPopupMenuInterceptor::ShowPopupMenuInterceptor(
RenderFrameHostImpl* rfh,
base::OnceCallback<void(const gfx::Rect&)> did_show_popup_menu_callback)
: swapped_impl_(rfh->local_frame_host_receiver_for_testing(), this),
did_show_popup_menu_callback_(std::move(did_show_popup_menu_callback)) {}
ShowPopupWidgetWaiter::ShowPopupMenuInterceptor::~ShowPopupMenuInterceptor() =
default;
void ShowPopupWidgetWaiter::ShowPopupMenuInterceptor::ShowPopupMenu(
mojo::PendingRemote<blink::mojom::PopupMenuClient> popup_client,
const gfx::Rect& bounds,
int32_t item_height,
double font_size,
int32_t selected_item,
std::vector<blink::mojom::MenuItemPtr> menu_items,
bool right_aligned,
bool allow_multiple_selection) {
if (did_show_popup_menu_callback_) {
std::move(did_show_popup_menu_callback_).Run(bounds);
mojo::Remote<blink::mojom::PopupMenuClient>(std::move(popup_client))
->DidCancel();
return;
}
GetForwardingInterface()->ShowPopupMenu(
std::move(popup_client), bounds, item_height, font_size, selected_item,
std::move(menu_items), right_aligned, allow_multiple_selection);
}
blink::mojom::LocalFrameHost*
ShowPopupWidgetWaiter::ShowPopupMenuInterceptor::GetForwardingInterface() {
return swapped_impl_.old_impl();
}
#endif
ShowPopupWidgetWaiter::ShowPopupWidgetWaiter(WebContentsImpl* web_contents,
RenderFrameHostImpl* frame_host)
: create_new_popup_widget_interceptor_(
frame_host,
base::BindOnce(&ShowPopupWidgetWaiter::DidCreatePopupWidget,
base::Unretained(this))),
#if BUILDFLAG(IS_MAC) || BUILDFLAG(IS_ANDROID)
show_popup_menu_interceptor_(
frame_host,
base::BindOnce(&ShowPopupWidgetWaiter::DidShowPopupMenu,
base::Unretained(this))),
#endif
frame_host_(frame_host) {
}
ShowPopupWidgetWaiter::~ShowPopupWidgetWaiter() {
if (auto* rwhi = RenderWidgetHostImpl::FromID(process_id_, routing_id_)) {
std::ignore =
rwhi->popup_widget_host_receiver_for_testing().SwapImplForTesting(rwhi);
}
}
void ShowPopupWidgetWaiter::Wait() {
run_loop_.Run();
}
blink::mojom::PopupWidgetHost* ShowPopupWidgetWaiter::GetForwardingInterface() {
DCHECK_NE(MSG_ROUTING_NONE, routing_id_);
return RenderWidgetHostImpl::FromID(process_id_, routing_id_);
}
void ShowPopupWidgetWaiter::ShowPopup(const gfx::Rect& initial_rect,
const gfx::Rect& initial_anchor_rect,
ShowPopupCallback callback) {
GetForwardingInterface()->ShowPopup(initial_rect, initial_anchor_rect,
std::move(callback));
initial_rect_ = initial_rect;
run_loop_.Quit();
}
void ShowPopupWidgetWaiter::DidCreatePopupWidget(
RenderWidgetHostImpl* render_widget_host) {
process_id_ = render_widget_host->GetProcess()->GetID();
routing_id_ = render_widget_host->GetRoutingID();
// Swapped back in destructor from process_id_ and routing_id_ lookup.
std::ignore = render_widget_host->popup_widget_host_receiver_for_testing()
.SwapImplForTesting(this);
}
#if BUILDFLAG(IS_MAC) || BUILDFLAG(IS_ANDROID)
void ShowPopupWidgetWaiter::DidShowPopupMenu(const gfx::Rect& bounds) {
initial_rect_ = bounds;
run_loop_.Quit();
}
#endif
UnresponsiveRendererObserver::UnresponsiveRendererObserver(
WebContents* web_contents)
: WebContentsObserver(web_contents) {}
UnresponsiveRendererObserver::~UnresponsiveRendererObserver() = default;
RenderProcessHost* UnresponsiveRendererObserver::Wait(base::TimeDelta timeout) {
if (!captured_render_process_host_) {
base::OneShotTimer timer;
timer.Start(FROM_HERE, timeout, run_loop_.QuitClosure());
run_loop_.Run();
timer.Stop();
}
return captured_render_process_host_;
}
void UnresponsiveRendererObserver::OnRendererUnresponsive(
RenderProcessHost* render_process_host) {
captured_render_process_host_ = render_process_host;
run_loop_.Quit();
}
BeforeUnloadBlockingDelegate::BeforeUnloadBlockingDelegate(
WebContentsImpl* web_contents)
: web_contents_(web_contents) {
web_contents_->SetDelegate(this);
}
BeforeUnloadBlockingDelegate::~BeforeUnloadBlockingDelegate() {
if (!callback_.is_null())
std::move(callback_).Run(true, std::u16string());
web_contents_->SetDelegate(nullptr);
web_contents_->SetJavaScriptDialogManagerForTesting(nullptr);
}
void BeforeUnloadBlockingDelegate::Wait() {
run_loop_->Run();
run_loop_ = std::make_unique<base::RunLoop>();
}
JavaScriptDialogManager*
BeforeUnloadBlockingDelegate::GetJavaScriptDialogManager(WebContents* source) {
return this;
}
bool BeforeUnloadBlockingDelegate::IsBackForwardCacheSupported(
WebContents& web_contents) {
return true;
}
void BeforeUnloadBlockingDelegate::RunJavaScriptDialog(
WebContents* web_contents,
RenderFrameHost* render_frame_host,
JavaScriptDialogType dialog_type,
const std::u16string& message_text,
const std::u16string& default_prompt_text,
DialogClosedCallback callback,
bool* did_suppress_message) {
NOTREACHED_IN_MIGRATION();
}
void BeforeUnloadBlockingDelegate::RunBeforeUnloadDialog(
WebContents* web_contents,
RenderFrameHost* render_frame_host,
bool is_reload,
DialogClosedCallback callback) {
callback_ = std::move(callback);
run_loop_->Quit();
}
bool BeforeUnloadBlockingDelegate::HandleJavaScriptDialog(
WebContents* web_contents,
bool accept,
const std::u16string* prompt_override) {
NOTREACHED_IN_MIGRATION();
return true;
}
FrameNavigateParamsCapturer::FrameNavigateParamsCapturer(WebContents* contents)
: WebContentsObserver(contents) {}
FrameNavigateParamsCapturer::FrameNavigateParamsCapturer(FrameTreeNode* node)
: WebContentsObserver(
WebContents::FromRenderFrameHost(node->current_frame_host())),
frame_tree_node_id_(node->frame_tree_node_id()) {}
FrameNavigateParamsCapturer::~FrameNavigateParamsCapturer() = default;
void FrameNavigateParamsCapturer::DidFinishNavigation(
NavigationHandle* navigation_handle) {
if (!navigation_handle->HasCommitted() ||
(frame_tree_node_id_.has_value() &&
navigation_handle->GetFrameTreeNodeId() !=
frame_tree_node_id_.value()) ||
navigations_remaining_ == 0) {
return;
}
--navigations_remaining_;
transitions_.push_back(navigation_handle->GetPageTransition());
urls_.push_back(navigation_handle->GetURL());
navigation_types_.push_back(
NavigationRequest::From(navigation_handle)->navigation_type());
is_same_documents_.push_back(navigation_handle->IsSameDocument());
did_replace_entries_.push_back(navigation_handle->DidReplaceEntry());
is_renderer_initiateds_.push_back(navigation_handle->IsRendererInitiated());
has_user_gestures_.push_back(navigation_handle->HasUserGesture());
is_overriding_user_agents_.push_back(
NavigationRequest::From(navigation_handle)->is_overriding_user_agent());
is_error_pages_.push_back(navigation_handle->IsErrorPage());
if (!navigations_remaining_ &&
(!web_contents()->IsLoading() || !wait_for_load_))
loop_.Quit();
}
void FrameNavigateParamsCapturer::Wait() {
loop_.Run();
}
void FrameNavigateParamsCapturer::DidStopLoading() {
if (!navigations_remaining_)
loop_.Quit();
}
RenderFrameHostCreatedObserver::RenderFrameHostCreatedObserver(
WebContents* web_contents)
: WebContentsObserver(web_contents) {}
RenderFrameHostCreatedObserver::RenderFrameHostCreatedObserver(
WebContents* web_contents,
int expected_frame_count)
: WebContentsObserver(web_contents),
expected_frame_count_(expected_frame_count) {}
RenderFrameHostCreatedObserver::RenderFrameHostCreatedObserver(
WebContents* web_contents,
OnRenderFrameHostCreatedCallback on_rfh_created)
: WebContentsObserver(web_contents),
on_rfh_created_(std::move(on_rfh_created)) {}
RenderFrameHostCreatedObserver::~RenderFrameHostCreatedObserver() = default;
RenderFrameHost* RenderFrameHostCreatedObserver::Wait() {
if (frames_created_ < expected_frame_count_)
run_loop_.Run();
return last_rfh_;
}
void RenderFrameHostCreatedObserver::RenderFrameCreated(
RenderFrameHost* render_frame_host) {
frames_created_++;
last_rfh_ = render_frame_host;
if (on_rfh_created_)
on_rfh_created_.Run(render_frame_host);
if (frames_created_ == expected_frame_count_)
run_loop_.Quit();
}
BackForwardCache::DisabledReason RenderFrameHostDisabledForTestingReason() {
static const BackForwardCache::DisabledReason reason =
BackForwardCache::DisabledReason(
BackForwardCache::DisabledSource::kTesting, 0, "disabled for testing",
/*context=*/"", "disabled");
return reason;
}
void DisableBFCacheForRFHForTesting(
content::RenderFrameHost* render_frame_host) {
content::BackForwardCache::DisableForRenderFrameHost(
render_frame_host, RenderFrameHostDisabledForTestingReason());
}
void DisableBFCacheForRFHForTesting(content::GlobalRenderFrameHostId id) {
content::BackForwardCache::DisableForRenderFrameHost(
id, RenderFrameHostDisabledForTestingReason());
}
void UserAgentInjector::DidStartNavigation(
NavigationHandle* navigation_handle) {
web_contents()->SetUserAgentOverride(user_agent_override_, false);
navigation_handle->SetIsOverridingUserAgent(is_overriding_user_agent_);
}
RenderFrameHostImplWrapper::RenderFrameHostImplWrapper(RenderFrameHost* rfh)
: RenderFrameHostWrapper(rfh) {}
RenderFrameHostImpl* RenderFrameHostImplWrapper::get() const {
return static_cast<RenderFrameHostImpl*>(RenderFrameHostWrapper::get());
}
RenderFrameHostImpl& RenderFrameHostImplWrapper::operator*() const {
DCHECK(get());
return *get();
}
RenderFrameHostImpl* RenderFrameHostImplWrapper::operator->() const {
DCHECK(get());
return get();
}
InactiveRenderFrameHostDeletionObserver::
InactiveRenderFrameHostDeletionObserver(WebContents* content)
: WebContentsObserver(content) {}
InactiveRenderFrameHostDeletionObserver::
~InactiveRenderFrameHostDeletionObserver() = default;
void InactiveRenderFrameHostDeletionObserver::Wait() {
// Some RenderFrameHost may remain in the BackForwardCache and or as
// prerendered pages. Trigger deletion for them asynchronously.
static_cast<WebContentsImpl*>(web_contents())
->GetController()
.GetBackForwardCache()
.Flush();
static_cast<WebContentsImpl*>(web_contents())
->GetPrerenderHostRegistry()
->CancelAllHostsForTesting();
for (RenderFrameHost* rfh : CollectAllRenderFrameHosts(web_contents())) {
// Keep track of all currently inactive RenderFrameHosts so that we can wait
// for all of them to be deleted.
if (!rfh->IsActive() && rfh->IsRenderFrameLive())
inactive_rfhs_.insert(rfh);