File: browser_url_loader_throttle_unittest.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (1198 lines) | stat: -rw-r--r-- 45,985 bytes parent folder | download | duplicates (6)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/safe_browsing/content/browser/browser_url_loader_throttle.h"

#include <string_view>
#include <vector>

#include "base/functional/callback.h"
#include "base/memory/raw_ptr.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/mock_callback.h"
#include "base/test/scoped_feature_list.h"
#include "base/time/time.h"
#include "components/safe_browsing/content/browser/async_check_tracker.h"
#include "components/safe_browsing/content/browser/base_ui_manager.h"
#include "components/safe_browsing/content/browser/url_checker_holder.h"
#include "components/safe_browsing/core/browser/realtime/fake_url_lookup_service.h"
#include "components/safe_browsing/core/browser/safe_browsing_url_checker_impl.h"
#include "components/safe_browsing/core/browser/url_checker_delegate.h"
#include "components/safe_browsing/core/common/features.h"
#include "components/safe_browsing/core/common/hashprefix_realtime/hash_realtime_utils.h"
#include "content/public/browser/web_contents.h"
#include "content/public/test/browser_task_environment.h"
#include "content/public/test/test_browser_context.h"
#include "content/public/test/test_web_contents_factory.h"
#include "net/base/load_flags.h"
#include "net/base/net_errors.h"
#include "net/traffic_annotation/network_traffic_annotation_test_helper.h"
#include "services/network/public/cpp/resource_request.h"
#include "services/network/public/mojom/url_response_head.mojom.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace safe_browsing {

namespace {

using security_interstitials::UnsafeResource;

class MockUrlCheckerDelegate : public UrlCheckerDelegate {
 public:
  MockUrlCheckerDelegate() = default;

  MOCK_METHOD1(MaybeDestroyNoStatePrefetchContents,
               void(base::OnceCallback<content::WebContents*()>));
  MOCK_METHOD4(StartDisplayingBlockingPageHelper,
               void(const security_interstitials::UnsafeResource&,
                    const std::string&,
                    const net::HttpRequestHeaders&,
                    bool));
  MOCK_METHOD1(StartObservingInteractionsForDelayedBlockingPageHelper,
               void(const security_interstitials::UnsafeResource&));
  MOCK_METHOD1(NotifySuspiciousSiteDetected,
               void(const base::RepeatingCallback<content::WebContents*()>&));
  MOCK_METHOD0(GetUIManager, BaseUIManager*());
  MOCK_METHOD0(GetThreatTypes, const SBThreatTypeSet&());
  MOCK_METHOD1(IsUrlAllowlisted, bool(const GURL&));
  MOCK_METHOD1(SetPolicyAllowlistDomains,
               void(const std::vector<std::string>&));
  MOCK_METHOD2(SendUrlRealTimeAndHashRealTimeDiscrepancyReport,
               void(std::unique_ptr<ClientSafeBrowsingReportRequest>,
                    const base::RepeatingCallback<content::WebContents*()>&));
  MOCK_METHOD1(AreBackgroundHashRealTimeSampleLookupsAllowed,
               bool(const base::RepeatingCallback<content::WebContents*()>&));

  SafeBrowsingDatabaseManager* GetDatabaseManager() override { return nullptr; }

  bool ShouldSkipRequestCheck(
      const GURL& original_url,
      int frame_tree_node_id,
      int render_process_id,
      base::optional_ref<const base::UnguessableToken> render_frame_token,
      bool originated_from_service_worker) override {
    return should_skip_request_check_;
  }
  void EnableSkipRequestCheck() { should_skip_request_check_ = true; }

 protected:
  ~MockUrlCheckerDelegate() override = default;

 private:
  bool should_skip_request_check_ = false;
};

class MockThrottleDelegate : public blink::URLLoaderThrottle::Delegate {
 public:
  ~MockThrottleDelegate() override = default;

  void CancelWithError(int error_code,
                       std::string_view custom_reason) override {
    error_code_ = error_code;
    custom_reason_ = custom_reason;
  }
  void Resume() override { is_resumed_ = true; }

  int GetErrorCode() { return error_code_; }
  std::string_view GetCustomReason() { return custom_reason_; }
  bool IsResumed() { return is_resumed_; }

 private:
  int error_code_ = 0;
  std::string_view custom_reason_ = "";
  bool is_resumed_ = false;
};

class FakeRealTimeUrlLookupService
    : public testing::FakeRealTimeUrlLookupService {
 public:
  FakeRealTimeUrlLookupService() = default;

  // RealTimeUrlLookupServiceBase:
  void StartLookup(
      const GURL& url,
      RTLookupResponseCallback response_callback,
      scoped_refptr<base::SequencedTaskRunner> callback_task_runner,
      SessionID tab_id,
      std::optional<internal::ReferringAppInfo> referring_app_info) override {}
};

class MockSafeBrowsingUrlChecker : public SafeBrowsingUrlCheckerImpl {
 public:
  struct CallbackInfo {
    bool should_proceed = true;
    bool should_show_interstitial = false;
    bool should_delay_callback = false;
    NativeCheckUrlCallback callback;
    PerformedCheck performed_check = PerformedCheck::kHashDatabaseCheck;
  };

  MockSafeBrowsingUrlChecker(
      const net::HttpRequestHeaders& headers,
      int load_flags,
      bool has_user_gesture,
      scoped_refptr<UrlCheckerDelegate> url_checker_delegate,
      const base::RepeatingCallback<content::WebContents*()>&
          web_contents_getter,
      UnsafeResource::RenderProcessId render_process_id,
      const UnsafeResource::RenderFrameToken& render_frame_token,
      content::FrameTreeNodeId frame_tree_node_id,
      std::optional<int64_t> navigation_id,
      bool url_real_time_lookup_enabled,
      bool can_check_db,
      bool can_check_high_confidence_allowlist,
      std::string url_lookup_service_metric_suffix,
      scoped_refptr<base::SequencedTaskRunner> ui_task_runner,
      base::WeakPtr<RealTimeUrlLookupServiceBase> url_lookup_service_on_ui,
      base::WeakPtr<HashRealTimeService> hash_realtime_service_on_ui,
      hash_realtime_utils::HashRealTimeSelection hash_realtime_selection,
      bool is_async_check)
      : SafeBrowsingUrlCheckerImpl(
            headers,
            load_flags,
            has_user_gesture,
            url_checker_delegate,
            web_contents_getter,
            /*weak_web_state=*/nullptr,
            render_process_id,
            render_frame_token,
            frame_tree_node_id.value(),
            navigation_id,
            url_real_time_lookup_enabled,
            can_check_db,
            can_check_high_confidence_allowlist,
            url_lookup_service_metric_suffix,
            ui_task_runner,
            url_lookup_service_on_ui,
            hash_realtime_service_on_ui,
            hash_realtime_selection,
            is_async_check,
            /*check_allowlist_before_hash_database=*/false,
            SessionID::InvalidValue(),
            /*referring_app_info=*/std::nullopt) {}

  // Returns the CallbackInfo that was previously added in |AddCallbackInfo|.
  // It will crash if |AddCallbackInfo| was not called.
  // If |should_delay_callback| was set to true, the callback will be cached.
  // The callback can be invoked manually via |RestartDelayedCallback|.
  // Otherwise, the callback will be invoked immediately.
  void CheckUrl(const GURL& url,
                const std::string& method,
                NativeCheckUrlCallback callback) override {
    ASSERT_GT(callback_infos_.size(), check_url_called_cnt_)
        << "Unexpected call to |CheckUrl|. Make sure to call |AddCallbackInfo| "
           "in advance if this call is expected.";
    CallbackInfo& callback_info = callback_infos_[check_url_called_cnt_++];
    if (callback_info.should_delay_callback) {
      callback_info.callback = std::move(callback);
    } else {
      std::move(callback).Run(
          /*proceed=*/callback_info.should_proceed,
          /*show_interstitial=*/
          callback_info.should_show_interstitial,
          /*has_post_commit_interstitial_skipped=*/false,
          callback_info.performed_check);
    }
  }

  void RestartDelayedCallback(size_t index) {
    ASSERT_GT(callback_infos_.size(), index);
    ASSERT_TRUE(callback_infos_[index].should_delay_callback);
    std::move(callback_infos_[index].callback)
        .Run(/*proceed=*/callback_infos_[index].should_proceed,
             /*show_interstitial=*/
             callback_infos_[index].should_show_interstitial,
             /*has_post_commit_interstitial_skipped=*/false,
             callback_infos_[index].performed_check);
  }

  // Informs how the callback in |CheckUrl| should be handled. The info applies
  // to the callback sent in |CheckUrl| in sequence. That is, the first info
  // added will be applied to the first call of |CheckUrl|.
  void AddCallbackInfo(
      bool should_proceed,
      bool should_show_interstitial,
      bool should_delay_callback,
      PerformedCheck performed_check = PerformedCheck::kHashDatabaseCheck) {
    CallbackInfo callback_info;
    callback_info.should_proceed = should_proceed;
    callback_info.should_show_interstitial = should_show_interstitial;
    callback_info.should_delay_callback = should_delay_callback;
    callback_info.performed_check = performed_check;
    callback_infos_.push_back(std::move(callback_info));
  }

  base::WeakPtr<MockSafeBrowsingUrlChecker> GetWeakPtr() {
    return weak_factory_.GetWeakPtr();
  }

 private:
  size_t check_url_called_cnt_ = 0;
  std::vector<CallbackInfo> callback_infos_;
  base::WeakPtrFactory<MockSafeBrowsingUrlChecker> weak_factory_{this};
};

struct WillStartRequestOptionalArgs {
  network::mojom::RequestDestination destination =
      network::mojom::RequestDestination::kDocument;
  int load_flags = 0;
};

struct SetUpTestOptionalArgs {
  bool url_real_time_lookup_enabled = false;
  bool should_sync_checker_check_allowlist = false;
};

}  // namespace

class SBBrowserUrlLoaderThrottleTestBase : public ::testing::Test {
 protected:
  SBBrowserUrlLoaderThrottleTestBase()
      : web_contents_(
            web_contents_factory_.CreateWebContents(&browser_context_)) {}

  scoped_refptr<UrlCheckerDelegate> GetUrlCheckerDelegate() {
    return url_checker_delegate_;
  }

  void SetUpTest(
      bool async_check_enabled,
      SetUpTestOptionalArgs optional_args = SetUpTestOptionalArgs()) {
    auto url_checker_delegate_getter = base::BindRepeating(
        [](SBBrowserUrlLoaderThrottleTestBase* test) {
          return test->GetUrlCheckerDelegate();
        },
        base::Unretained(this));
    EXPECT_CALL(mock_web_contents_getter_, Run())
        .WillRepeatedly(::testing::Return(web_contents_));
    ui_manager_ = base::MakeRefCounted<BaseUIManager>();
    async_check_tracker_ =
        async_check_enabled
            ? base::WrapUnique(new AsyncCheckTracker(
                  web_contents_, ui_manager_.get(),
                  optional_args.should_sync_checker_check_allowlist))
            : nullptr;
    std::optional<int64_t> navigation_id =
        async_check_enabled ? std::optional<int64_t>(1u) : std::nullopt;

    throttle_ = BrowserURLLoaderThrottle::Create(
        std::move(url_checker_delegate_getter), mock_web_contents_getter_.Get(),
        content::FrameTreeNodeId(), navigation_id,
        optional_args.url_real_time_lookup_enabled
            ? url_lookup_service_->GetWeakPtr()
            : nullptr,
        /*hash_realtime_service=*/nullptr,
        /*hash_realtime_selection=*/
        async_check_enabled
            ? hash_realtime_utils::HashRealTimeSelection::kHashRealTimeService
            : hash_realtime_utils::HashRealTimeSelection::kNone,
        async_check_tracker_ ? async_check_tracker_->GetWeakPtr() : nullptr,
        /*referring_app_info=*/std::nullopt);

    url_checker_delegate_ = base::MakeRefCounted<MockUrlCheckerDelegate>();
    throttle_delegate_ = std::make_unique<MockThrottleDelegate>();

    std::unique_ptr<MockSafeBrowsingUrlChecker> sync_url_checker =
        std::make_unique<MockSafeBrowsingUrlChecker>(
            net::HttpRequestHeaders(), /*load_flags=*/0,
            /*has_user_gesture=*/false, url_checker_delegate_,
            mock_web_contents_getter_.Get(), UnsafeResource::kNoRenderProcessId,
            /*render_frame_token=*/std::nullopt, content::FrameTreeNodeId(),
            navigation_id, optional_args.url_real_time_lookup_enabled,
            /*can_check_db=*/true,
            /*can_check_high_confidence_allowlist=*/true,
            /*url_lookup_service_metric_suffix=*/"",
            /*ui_task_runner=*/base::SequencedTaskRunner::GetCurrentDefault(),
            /*url_lookup_service_on_ui=*/nullptr,
            /*hash_realtime_service_on_ui=*/nullptr,
            /*hash_realtime_selection=*/
            hash_realtime_utils::HashRealTimeSelection::kNone,
            /*is_async_check=*/false);
    sync_url_checker_ = sync_url_checker->GetWeakPtr();
    throttle_->SetOnSyncSBCheckerCreatedCallbackForTesting(base::BindOnce(
        &SBBrowserUrlLoaderThrottleTestBase::SetSyncUrlCheckerForTesting,
        base::Unretained(this), std::move(sync_url_checker)));
    if (async_check_enabled) {
      std::unique_ptr<MockSafeBrowsingUrlChecker> async_url_checker =
          std::make_unique<MockSafeBrowsingUrlChecker>(
              net::HttpRequestHeaders(), /*load_flags=*/0,
              /*has_user_gesture=*/false, url_checker_delegate_,
              mock_web_contents_getter_.Get(),
              UnsafeResource::kNoRenderProcessId,
              /*render_frame_token=*/std::nullopt, content::FrameTreeNodeId(),
              /*navigation_id=*/0, optional_args.url_real_time_lookup_enabled,
              /*can_check_db=*/true,
              /*can_check_high_confidence_allowlist=*/true,
              /*url_lookup_service_metric_suffix=*/"",
              /*ui_task_runner=*/base::SequencedTaskRunner::GetCurrentDefault(),
              /*url_lookup_service_on_ui=*/nullptr,
              /*hash_realtime_service_on_ui=*/nullptr,
              /*hash_realtime_selection=*/
              hash_realtime_utils::HashRealTimeSelection::kNone,
              /*is_async_check=*/true);
      async_url_checker_ = async_url_checker->GetWeakPtr();

      throttle_->SetOnAsyncSBCheckerCreatedCallbackForTesting(base::BindOnce(
          &SBBrowserUrlLoaderThrottleTestBase::SetAsyncUrlCheckerForTesting,
          base::Unretained(this), std::move(async_url_checker)));
    }
    throttle_->set_delegate(throttle_delegate_.get());

    url_ = GURL("https://example.com/");
    response_head_ = network::mojom::URLResponseHead::New();
  }

  void SetSyncUrlCheckerForTesting(
      std::unique_ptr<MockSafeBrowsingUrlChecker> url_checker) {
    throttle_->GetSyncSBCheckerForTesting()->SetUrlCheckerForTesting(
        std::move(url_checker));
  }
  void SetAsyncUrlCheckerForTesting(
      std::unique_ptr<MockSafeBrowsingUrlChecker> url_checker) {
    throttle_->GetAsyncSBCheckerForTesting()->SetUrlCheckerForTesting(
        std::move(url_checker));
  }

  void AddCallbackInfo(
      bool should_proceed,
      bool should_show_interstitial,
      bool should_delay_callback,
      SafeBrowsingUrlCheckerImpl::PerformedCheck performed_check =
          SafeBrowsingUrlCheckerImpl::PerformedCheck::kHashDatabaseCheck) {
    sync_url_checker_->AddCallbackInfo(should_proceed, should_show_interstitial,
                                       should_delay_callback, performed_check);
    if (async_url_checker_) {
      async_url_checker_->AddCallbackInfo(
          should_proceed, should_show_interstitial, should_delay_callback,
          performed_check);
    }
  }

  // This function returns the value of |defer| after the function is called.
  bool CallWillStartRequest(WillStartRequestOptionalArgs optional_args =
                                WillStartRequestOptionalArgs()) {
    bool defer = false;
    network::ResourceRequest request;
    request.url = url_;
    request.destination = optional_args.destination;
    request.load_flags = optional_args.load_flags;
    throttle_->WillStartRequest(&request, &defer);
    task_environment_.RunUntilIdle();
    return defer;
  }

  // This function returns the value of |defer| after the function is called.
  bool CallWillRedirectRequest() {
    bool defer = false;
    net::RedirectInfo redirect_info;
    std::vector<std::string> to_be_removed_headers;
    net::HttpRequestHeaders modified_headers;
    net::HttpRequestHeaders modified_cors_exempt_headers;
    throttle_->WillRedirectRequest(&redirect_info, *response_head_, &defer,
                                   &to_be_removed_headers, &modified_headers,
                                   &modified_cors_exempt_headers);
    task_environment_.RunUntilIdle();
    return defer;
  }

  // This function returns the value of |defer| after the function is called.
  bool CallWillProcessResponse() {
    bool defer = false;
    throttle_->WillProcessResponse(url_, response_head_.get(), &defer);
    task_environment_.RunUntilIdle();
    return defer;
  }

  // This function returns the value of |defer| after the function is called.
  bool CallWillProcessResponseFromCache() {
    bool defer = false;
    response_head_->was_fetched_via_cache = true;
    response_head_->network_accessed = false;
    throttle_->WillProcessResponse(url_, response_head_.get(), &defer);
    task_environment_.RunUntilIdle();
    return defer;
  }

  base::test::ScopedFeatureList feature_list_;
  content::BrowserTaskEnvironment task_environment_{
      base::test::TaskEnvironment::TimeSource::MOCK_TIME};
  GURL url_;
  network::mojom::URLResponseHeadPtr response_head_;
  std::unique_ptr<BrowserURLLoaderThrottle> throttle_;
  // Owned by |throttle_|. May be deleted before the test completes. Prefer
  // setting it up at the start of the test.
  base::WeakPtr<MockSafeBrowsingUrlChecker> sync_url_checker_;
  base::WeakPtr<MockSafeBrowsingUrlChecker> async_url_checker_;
  std::unique_ptr<FakeRealTimeUrlLookupService> url_lookup_service_ =
      std::make_unique<FakeRealTimeUrlLookupService>();
  scoped_refptr<MockUrlCheckerDelegate> url_checker_delegate_;
  std::unique_ptr<MockThrottleDelegate> throttle_delegate_;
  std::unique_ptr<AsyncCheckTracker> async_check_tracker_;
  scoped_refptr<BaseUIManager> ui_manager_;
  content::TestBrowserContext browser_context_;
  content::TestWebContentsFactory web_contents_factory_;
  raw_ptr<content::WebContents> web_contents_;
  base::MockCallback<base::RepeatingCallback<content::WebContents*()>>
      mock_web_contents_getter_;
  base::HistogramTester histogram_tester_;
};

class SBBrowserUrlLoaderThrottleTest
    : public SBBrowserUrlLoaderThrottleTestBase,
      public ::testing::WithParamInterface<bool> {
 protected:
  void SetUpTest() {
    bool async_check_enabled = GetParam();
    SBBrowserUrlLoaderThrottleTestBase::SetUpTest(async_check_enabled);
  }

  void RunTotalDelayHistogramsUrlCheckTypeTest(
      SafeBrowsingUrlCheckerImpl::PerformedCheck performed_check,
      bool url_real_time_lookup_enabled,
      std::string expected_histogram) {
    bool async_check_enabled = GetParam();
    SBBrowserUrlLoaderThrottleTestBase::SetUpTest(
        async_check_enabled,
        /*optional_args=*/{.url_real_time_lookup_enabled =
                               url_real_time_lookup_enabled});
    base::HistogramTester histograms;
    AddCallbackInfo(/*should_proceed=*/true,
                    /*should_show_interstitial=*/false,
                    /*should_delay_callback=*/true, performed_check);

    CallWillStartRequest();
    CallWillProcessResponse();
    task_environment_.FastForwardBy(base::Milliseconds(200));
    sync_url_checker_->RestartDelayedCallback(/*index=*/0);
    if (async_url_checker_) {
      async_url_checker_->RestartDelayedCallback(/*index=*/0);
    }
    task_environment_.RunUntilIdle();

    histograms.ExpectUniqueTimeSample(expected_histogram,
                                      base::Milliseconds(200), 1);
  }
};

INSTANTIATE_TEST_SUITE_P(AsyncCheckEnabled,
                         SBBrowserUrlLoaderThrottleTest,
                         ::testing::Bool());

TEST_P(SBBrowserUrlLoaderThrottleTest,
       VerifyDefer_DoesNotDeferOnSafeDocumentUrl) {
  SetUpTest();
  AddCallbackInfo(/*should_proceed=*/true,
                  /*should_show_interstitial=*/false,
                  /*should_delay_callback=*/false);

  bool defer = CallWillStartRequest();
  EXPECT_FALSE(defer);
  EXPECT_EQ(throttle_delegate_->GetErrorCode(), 0);

  defer = CallWillProcessResponse();
  EXPECT_FALSE(defer);
}

TEST_P(SBBrowserUrlLoaderThrottleTest, VerifyDefer_DeferOnUnsafeDocumentUrl) {
  SetUpTest();
  AddCallbackInfo(/*should_proceed=*/false,
                  /*should_show_interstitial=*/true,
                  /*should_delay_callback=*/false);

  bool defer = CallWillStartRequest();
  // Safe Browsing and URL loader are performed in parallel. Safe Browsing
  // doesn't defer the start of the request.
  EXPECT_FALSE(defer);
  EXPECT_EQ(throttle_delegate_->GetErrorCode(), net::ERR_BLOCKED_BY_CLIENT);
  EXPECT_EQ(throttle_delegate_->GetCustomReason(), "SafeBrowsing");

  defer = CallWillProcessResponse();
  EXPECT_TRUE(defer);
}

TEST_P(SBBrowserUrlLoaderThrottleTest,
       VerifyDefer_DoesNotDeferOnUnsafeIframeUrl) {
  SetUpTest();
  // Do not call |AddSyncCallbackInfo| or |AddAsyncCallbackInfo|, so that if
  // CheckUrl is called for either (incorrectly), the test will fail.

  bool defer = CallWillStartRequest(
      {.destination = network::mojom::RequestDestination::kIframe});
  EXPECT_FALSE(defer);
  EXPECT_EQ(throttle_delegate_->GetErrorCode(), 0);

  defer = CallWillProcessResponse();
  EXPECT_FALSE(defer);
}

TEST_P(SBBrowserUrlLoaderThrottleTest, VerifyDefer_DeferIfRedirectUrlIsUnsafe) {
  SetUpTest();
  AddCallbackInfo(/*should_proceed=*/true,
                  /*should_show_interstitial=*/false,
                  /*should_delay_callback=*/false);
  AddCallbackInfo(/*should_proceed=*/false,
                  /*should_show_interstitial=*/true,
                  /*should_delay_callback=*/false);

  CallWillStartRequest();
  CallWillRedirectRequest();
  EXPECT_EQ(throttle_delegate_->GetErrorCode(), net::ERR_BLOCKED_BY_CLIENT);

  bool defer = CallWillProcessResponse();
  EXPECT_TRUE(defer);
}

TEST_P(SBBrowserUrlLoaderThrottleTest, VerifyDefer_DoesNotDeferOnSkippedUrl) {
  SetUpTest();
  AddCallbackInfo(/*should_proceed=*/false,
                  /*should_show_interstitial=*/true,
                  /*should_delay_callback=*/false);
  url_checker_delegate_->EnableSkipRequestCheck();

  CallWillStartRequest(
      {.destination = network::mojom::RequestDestination::kEmpty});

  bool defer = CallWillProcessResponse();
  // The loader is not deferred because the check has skipped.
  EXPECT_FALSE(defer);
}

TEST_P(SBBrowserUrlLoaderThrottleTest,
       VerifyDefer_DoesNotDeferOnSkippedDocumentUrl) {
  SetUpTest();
  AddCallbackInfo(/*should_proceed=*/false,
                  /*should_show_interstitial=*/true,
                  /*should_delay_callback=*/false);
  url_checker_delegate_->EnableSkipRequestCheck();

  CallWillStartRequest();

  bool defer = CallWillProcessResponse();
  // The loader is not deferred because the check has skipped.
  EXPECT_FALSE(defer);

  // Reset throttle to test histogram below, which only logs on destruct.
  throttle_.reset();
  // No histogram is logged because it's a skipped check (or because async
  // checks are not eligible).
  histogram_tester_.ExpectTotalCount(
      "SafeBrowsing.BrowserThrottle.IsAsyncCheckFasterThanSyncCheck",
      /*expected_count=*/0);
}

TEST_P(SBBrowserUrlLoaderThrottleTest, VerifyDefer_DoesNotDeferOnKnownSafeUrl) {
  SetUpTest();
  AddCallbackInfo(/*should_proceed=*/true,
                  /*should_show_interstitial=*/false,
                  /*should_delay_callback=*/true);
  AddCallbackInfo(/*should_proceed=*/true,
                  /*should_show_interstitial=*/false,
                  /*should_delay_callback=*/true);

  bool defer = false;
  network::ResourceRequest request;
  request.url = GURL("chrome://new-tab-page");
  throttle_->WillStartRequest(&request, &defer);
  task_environment_.RunUntilIdle();

  CallWillRedirectRequest();

  defer = CallWillProcessResponse();
  // The loader is not deferred because the URL is known to be safe.
  EXPECT_FALSE(defer);
}

TEST_P(SBBrowserUrlLoaderThrottleTest, VerifyDefer_DeferOnSlowCheck) {
  SetUpTest();
  AddCallbackInfo(/*should_proceed=*/true,
                  /*should_show_interstitial=*/false,
                  /*should_delay_callback=*/true);

  CallWillStartRequest();

  bool defer = CallWillProcessResponse();
  // Deferred because the check has not completed.
  EXPECT_TRUE(defer);
  EXPECT_FALSE(throttle_delegate_->IsResumed());

  sync_url_checker_->RestartDelayedCallback(/*index=*/0);
  task_environment_.RunUntilIdle();
  EXPECT_TRUE(throttle_delegate_->IsResumed());
}

TEST_P(SBBrowserUrlLoaderThrottleTest, VerifyDefer_DeferOnSlowRedirectCheck) {
  SetUpTest();
  AddCallbackInfo(/*should_proceed=*/true,
                  /*should_show_interstitial=*/false,
                  /*should_delay_callback=*/false);
  AddCallbackInfo(/*should_proceed=*/true,
                  /*should_show_interstitial=*/false,
                  /*should_delay_callback=*/true);

  CallWillStartRequest();
  CallWillRedirectRequest();

  bool defer = CallWillProcessResponse();
  // Deferred because the check for redirect URL has not completed.
  EXPECT_TRUE(defer);
  EXPECT_FALSE(throttle_delegate_->IsResumed());

  sync_url_checker_->RestartDelayedCallback(/*index=*/1);
  task_environment_.RunUntilIdle();
  EXPECT_TRUE(throttle_delegate_->IsResumed());
}

TEST_P(SBBrowserUrlLoaderThrottleTest,
       VerifyDefer_DoesNotResumeOnSlowCheckNotProceed) {
  SetUpTest();
  AddCallbackInfo(/*should_proceed=*/false,
                  /*should_show_interstitial=*/true,
                  /*should_delay_callback=*/true);

  CallWillStartRequest();

  bool defer = CallWillProcessResponse();
  EXPECT_TRUE(defer);
  EXPECT_FALSE(throttle_delegate_->IsResumed());

  sync_url_checker_->RestartDelayedCallback(/*index=*/0);
  task_environment_.RunUntilIdle();
  // Resume should not be called because the slow check returns don't proceed.
  EXPECT_FALSE(throttle_delegate_->IsResumed());
  EXPECT_EQ(throttle_delegate_->GetErrorCode(), net::ERR_BLOCKED_BY_CLIENT);
}

TEST_P(SBBrowserUrlLoaderThrottleTest,
       VerifyDefer_DoesNotDeferRedirectOnSlowCheck) {
  SetUpTest();
  AddCallbackInfo(/*should_proceed=*/true,
                  /*should_show_interstitial=*/false,
                  /*should_delay_callback=*/true);
  AddCallbackInfo(/*should_proceed=*/true,
                  /*should_show_interstitial=*/false,
                  /*should_delay_callback=*/false);

  CallWillStartRequest();

  bool defer = CallWillRedirectRequest();
  // Although the first check has not completed, redirect should not be
  // deferred.
  EXPECT_FALSE(defer);
}

TEST_P(SBBrowserUrlLoaderThrottleTest,
       VerifyDefer_DeferRedirectWhenFirstUrlAlreadyBlocked) {
  SetUpTest();
  AddCallbackInfo(/*should_proceed=*/false,
                  /*should_show_interstitial=*/true,
                  /*should_delay_callback=*/false);

  CallWillStartRequest();
  EXPECT_EQ(throttle_delegate_->GetErrorCode(), net::ERR_BLOCKED_BY_CLIENT);

  bool defer = CallWillRedirectRequest();
  EXPECT_TRUE(defer);
}

TEST_P(SBBrowserUrlLoaderThrottleTest,
       VerifyErrorCodeWhenInterstitialNotShown) {
  SetUpTest();
  AddCallbackInfo(/*should_proceed=*/false,
                  /*should_show_interstitial=*/false,
                  /*should_delay_callback=*/false);

  CallWillStartRequest();
  EXPECT_EQ(throttle_delegate_->GetErrorCode(), net::ERR_ABORTED);
}

TEST_P(SBBrowserUrlLoaderThrottleTest,
       VerifyTotalDelayHistograms_FastCheckFromNetwork) {
  SetUpTest();
  base::HistogramTester histograms;
  AddCallbackInfo(/*should_proceed=*/true,
                  /*should_show_interstitial=*/false,
                  /*should_delay_callback=*/false);

  CallWillStartRequest();
  task_environment_.FastForwardBy(base::Milliseconds(200));
  CallWillProcessResponse();

  histograms.ExpectUniqueTimeSample(
      "SafeBrowsing.BrowserThrottle.TotalDelay2.HashPrefixDatabaseCheck",
      base::Milliseconds(0), 1);
  histograms.ExpectUniqueTimeSample(
      "SafeBrowsing.BrowserThrottle.TotalDelay2.FromNetwork",
      base::Milliseconds(0), 1);
  histograms.ExpectTotalCount(
      "SafeBrowsing.BrowserThrottle.TotalDelay2.FromCache", 0);
}

TEST_P(SBBrowserUrlLoaderThrottleTest,
       VerifyTotalDelayHistograms_FastCheckFromCache) {
  SetUpTest();
  base::HistogramTester histograms;
  AddCallbackInfo(/*should_proceed=*/true,
                  /*should_show_interstitial=*/false,
                  /*should_delay_callback=*/false);

  CallWillStartRequest();
  task_environment_.FastForwardBy(base::Milliseconds(200));
  CallWillProcessResponseFromCache();

  histograms.ExpectUniqueTimeSample(
      "SafeBrowsing.BrowserThrottle.TotalDelay2.HashPrefixDatabaseCheck",
      base::Milliseconds(0), 1);
  histograms.ExpectUniqueTimeSample(
      "SafeBrowsing.BrowserThrottle.TotalDelay2.FromCache",
      base::Milliseconds(0), 1);
  histograms.ExpectTotalCount(
      "SafeBrowsing.BrowserThrottle.TotalDelay2.FromNetwork", 0);
}

TEST_P(SBBrowserUrlLoaderThrottleTest,
       VerifyTotalDelayHistograms_SlowCheckFromNetwork) {
  SetUpTest();
  base::HistogramTester histograms;
  AddCallbackInfo(/*should_proceed=*/true,
                  /*should_show_interstitial=*/false,
                  /*should_delay_callback=*/true);

  CallWillStartRequest();
  CallWillProcessResponse();
  task_environment_.FastForwardBy(base::Milliseconds(200));
  sync_url_checker_->RestartDelayedCallback(/*index=*/0);
  task_environment_.RunUntilIdle();

  histograms.ExpectUniqueTimeSample(
      "SafeBrowsing.BrowserThrottle.TotalDelay2.HashPrefixDatabaseCheck",
      base::Milliseconds(200), 1);
  histograms.ExpectUniqueTimeSample(
      "SafeBrowsing.BrowserThrottle.TotalDelay2.FromNetwork",
      base::Milliseconds(200), 1);
  histograms.ExpectTotalCount(
      "SafeBrowsing.BrowserThrottle.TotalDelay2.FromCache", 0);
}

TEST_P(SBBrowserUrlLoaderThrottleTest,
       VerifyTotalDelayHistograms_SlowCheckFromCache) {
  SetUpTest();
  base::HistogramTester histograms;
  AddCallbackInfo(/*should_proceed=*/true,
                  /*should_show_interstitial=*/false,
                  /*should_delay_callback=*/true);

  CallWillStartRequest();
  CallWillProcessResponseFromCache();
  task_environment_.FastForwardBy(base::Milliseconds(200));
  sync_url_checker_->RestartDelayedCallback(/*index=*/0);
  task_environment_.RunUntilIdle();

  histograms.ExpectUniqueTimeSample(
      "SafeBrowsing.BrowserThrottle.TotalDelay2.HashPrefixDatabaseCheck",
      base::Milliseconds(200), 1);
  histograms.ExpectUniqueTimeSample(
      "SafeBrowsing.BrowserThrottle.TotalDelay2.FromCache",
      base::Milliseconds(200), 1);
  histograms.ExpectTotalCount(
      "SafeBrowsing.BrowserThrottle.TotalDelay2.FromNetwork", 0);
}

TEST_P(SBBrowserUrlLoaderThrottleTest,
       VerifyTotalDelayHistograms_HashPrefixDatabaseCheck) {
  RunTotalDelayHistogramsUrlCheckTypeTest(
      SafeBrowsingUrlCheckerImpl::PerformedCheck::kHashDatabaseCheck,
      /*url_real_time_lookup_enabled=*/false,
      "SafeBrowsing.BrowserThrottle.TotalDelay2.HashPrefixDatabaseCheck");
}

TEST_P(SBBrowserUrlLoaderThrottleTest,
       VerifyTotalDelayHistograms_HashPrefixRealTimeCheck) {
  RunTotalDelayHistogramsUrlCheckTypeTest(
      SafeBrowsingUrlCheckerImpl::PerformedCheck::kHashRealTimeCheck,
      /*url_real_time_lookup_enabled=*/false,
      "SafeBrowsing.BrowserThrottle.TotalDelay2.HashPrefixRealTimeCheck");
}

TEST_P(SBBrowserUrlLoaderThrottleTest,
       VerifyTotalDelayHistograms_SkippedCheck) {
  RunTotalDelayHistogramsUrlCheckTypeTest(
      SafeBrowsingUrlCheckerImpl::PerformedCheck::kCheckSkipped,
      /*url_real_time_lookup_enabled=*/false,
      "SafeBrowsing.BrowserThrottle.TotalDelay2.SkippedCheck");
}

TEST_P(SBBrowserUrlLoaderThrottleTest,
       VerifyTotalDelayHistograms_UrlRealTimeCheck) {
  RunTotalDelayHistogramsUrlCheckTypeTest(
      SafeBrowsingUrlCheckerImpl::PerformedCheck::kUrlRealTimeCheck,
      /*url_real_time_lookup_enabled=*/true,
      "SafeBrowsing.BrowserThrottle.TotalDelay2.MockFullUrlLookup");
}

class SBBrowserUrlLoaderThrottleAsyncCheckTest
    : public SBBrowserUrlLoaderThrottleTestBase {
 protected:
  void SetUpTest(
      SetUpTestOptionalArgs optional_args = SetUpTestOptionalArgs()) {
    SBBrowserUrlLoaderThrottleTestBase::SetUpTest(/*async_check_enabled=*/true,
                                                  optional_args);
  }

  void AddSyncCallbackInfo(bool should_proceed, bool should_delay_callback) {
    sync_url_checker_->AddCallbackInfo(
        should_proceed,
        /*should_show_interstitial=*/!should_proceed,
        /*should_delay_callback=*/should_delay_callback);
  }

  void AddAsyncCallbackInfo(bool should_proceed, bool should_delay_callback) {
    async_url_checker_->AddCallbackInfo(
        should_proceed,
        /*should_show_interstitial=*/!should_proceed,
        /*should_delay_callback=*/should_delay_callback);
  }

  void VerifyHistograms(std::optional<bool> is_async_check_faster,
                        std::optional<bool> is_async_check_transferred) {
    if (is_async_check_faster.has_value()) {
      histogram_tester_.ExpectUniqueSample(
          "SafeBrowsing.BrowserThrottle.IsAsyncCheckFasterThanSyncCheck",
          /*sample=*/is_async_check_faster.value(),
          /*expected_bucket_count=*/1);
    } else {
      histogram_tester_.ExpectTotalCount(
          "SafeBrowsing.BrowserThrottle.IsAsyncCheckFasterThanSyncCheck",
          /*expected_count=*/0);
    }
    if (is_async_check_transferred.has_value()) {
      histogram_tester_.ExpectUniqueSample(
          "SafeBrowsing.BrowserThrottle.IsAsyncCheckerTransferred",
          /*sample=*/is_async_check_transferred.value(),
          /*expected_bucket_count=*/1);
    } else {
      histogram_tester_.ExpectTotalCount(
          "SafeBrowsing.BrowserThrottle.IsAsyncCheckerTransferred",
          /*expected_count=*/0);
    }
  }
};

TEST_F(SBBrowserUrlLoaderThrottleAsyncCheckTest, VerifyCheckerParams) {
  SetUpTest();
  EXPECT_EQ(throttle_->GetSyncSBCheckerForTesting(), nullptr);
  EXPECT_EQ(throttle_->GetAsyncSBCheckerForTesting(), nullptr);
  AddCallbackInfo(/*should_proceed=*/true,
                  /*should_show_interstitial=*/false,
                  /*should_delay_callback=*/false);
  CallWillStartRequest();
  EXPECT_FALSE(
      throttle_->GetSyncSBCheckerForTesting()->IsRealTimeCheckForTesting());
  EXPECT_FALSE(
      throttle_->GetSyncSBCheckerForTesting()->IsAsyncCheckForTesting());
  EXPECT_FALSE(throttle_->GetSyncSBCheckerForTesting()
                   ->IsCheckAllowlistBeforeHashDatabaseForTesting());
  EXPECT_TRUE(
      throttle_->GetAsyncSBCheckerForTesting()->IsRealTimeCheckForTesting());
  EXPECT_TRUE(
      throttle_->GetAsyncSBCheckerForTesting()->IsAsyncCheckForTesting());
  EXPECT_FALSE(throttle_->GetSyncSBCheckerForTesting()
                   ->IsCheckAllowlistBeforeHashDatabaseForTesting());
}

TEST_F(SBBrowserUrlLoaderThrottleAsyncCheckTest,
       VerifyCheckerParams_WithSyncCheckerCheckAllowlistEnabled) {
  SetUpTest(/*optional_args=*/{.should_sync_checker_check_allowlist = true});
  EXPECT_EQ(throttle_->GetSyncSBCheckerForTesting(), nullptr);
  EXPECT_EQ(throttle_->GetAsyncSBCheckerForTesting(), nullptr);
  AddCallbackInfo(/*should_proceed=*/true,
                  /*should_show_interstitial=*/false,
                  /*should_delay_callback=*/false);
  CallWillStartRequest();
  EXPECT_TRUE(throttle_->GetSyncSBCheckerForTesting()
                  ->IsCheckAllowlistBeforeHashDatabaseForTesting());
  EXPECT_FALSE(throttle_->GetAsyncSBCheckerForTesting()
                   ->IsCheckAllowlistBeforeHashDatabaseForTesting());
}

// Sync check completed -> WillProcessResponse called -> async check completed.
TEST_F(SBBrowserUrlLoaderThrottleAsyncCheckTest, VerifyDefer_AsyncNotDeferred) {
  SetUpTest();
  AddSyncCallbackInfo(/*should_proceed=*/true,
                      /*should_delay_callback=*/false);
  AddAsyncCallbackInfo(
      /*should_proceed=*/true,
      /*should_delay_callback=*/true);

  bool defer = CallWillStartRequest();
  EXPECT_FALSE(defer);

  defer = CallWillProcessResponse();
  EXPECT_FALSE(defer);
  // Async check is transferred to the tracker.
  EXPECT_EQ(async_check_tracker_->PendingCheckersSizeForTesting(), 1u);

  // Async check completed afterwards doesn't cause a crash.
  EXPECT_FALSE(async_url_checker_.WasInvalidated());
  async_url_checker_->RestartDelayedCallback(/*index=*/0);
  task_environment_.RunUntilIdle();

  // Reset throttle to test histogram below, some of which only logs on
  // destruct.
  throttle_.reset();
  VerifyHistograms(/*is_async_check_faster=*/false,
                   /*is_async_check_transferred=*/true);
}

// WillProcessResponse called -> Sync check completed.
TEST_F(SBBrowserUrlLoaderThrottleAsyncCheckTest, VerifyDefer_SyncResumed) {
  SetUpTest();
  AddCallbackInfo(/*should_proceed=*/true,
                  /*should_show_interstitial=*/false,
                  /*should_delay_callback=*/true);

  bool defer = CallWillStartRequest();
  EXPECT_FALSE(defer);

  defer = CallWillProcessResponse();
  EXPECT_TRUE(defer);
  // Async check is not yet transferred to the tracker because sync check has
  // not completed.
  EXPECT_EQ(async_check_tracker_->PendingCheckersSizeForTesting(), 0u);

  sync_url_checker_->RestartDelayedCallback(/*index=*/0);
  task_environment_.RunUntilIdle();
  EXPECT_EQ(async_check_tracker_->PendingCheckersSizeForTesting(), 1u);

  // Reset throttle to test histogram below, some of which only logs on
  // destruct.
  throttle_.reset();
  VerifyHistograms(/*is_async_check_faster=*/false,
                   /*is_async_check_transferred=*/true);
}

// Async check completed -> WillProcessResponse called -> Sync check completed.
TEST_F(SBBrowserUrlLoaderThrottleAsyncCheckTest,
       VerifyDefer_AsyncCompletedBeforeSync) {
  SetUpTest();
  AddSyncCallbackInfo(/*should_proceed=*/true,
                      /*should_delay_callback=*/true);
  AddAsyncCallbackInfo(
      /*should_proceed=*/true,
      /*should_delay_callback=*/false);

  bool defer = CallWillStartRequest();
  EXPECT_FALSE(defer);

  defer = CallWillProcessResponse();
  EXPECT_TRUE(defer);

  EXPECT_FALSE(sync_url_checker_.WasInvalidated());
  sync_url_checker_->RestartDelayedCallback(/*index=*/0);
  task_environment_.RunUntilIdle();

  EXPECT_TRUE(throttle_delegate_->IsResumed());
  // Async check already completed, so it is not transferred to the tracker.
  EXPECT_EQ(async_check_tracker_->PendingCheckersSizeForTesting(), 0u);

  // Reset throttle to test histogram below, some of which only logs on
  // destruct.
  throttle_.reset();
  VerifyHistograms(/*is_async_check_faster=*/true,
                   /*is_async_check_transferred=*/false);
}

// URL redirected -> Sync check completed -> WillProcessResponse called.
TEST_F(SBBrowserUrlLoaderThrottleAsyncCheckTest,
       VerifyDefer_AsyncRedirectCheckNotCompleted) {
  SetUpTest();
  AddSyncCallbackInfo(/*should_proceed=*/true,
                      /*should_delay_callback=*/false);
  AddSyncCallbackInfo(/*should_proceed=*/true,
                      /*should_delay_callback=*/false);
  AddAsyncCallbackInfo(
      /*should_proceed=*/true,
      /*should_delay_callback=*/false);
  AddAsyncCallbackInfo(
      /*should_proceed=*/true,
      /*should_delay_callback=*/true);

  CallWillStartRequest();
  CallWillRedirectRequest();

  bool defer = CallWillProcessResponse();
  EXPECT_FALSE(defer);
  EXPECT_EQ(async_check_tracker_->PendingCheckersSizeForTesting(), 1u);

  // Reset throttle to test histogram below, some of which only logs on
  // destruct.
  throttle_.reset();
  VerifyHistograms(/*is_async_check_faster=*/false,
                   /*is_async_check_transferred=*/true);
}

// Async check completed -> Sync check completed -> WillProcessResponse called.
TEST_F(SBBrowserUrlLoaderThrottleAsyncCheckTest,
       VerifyDefer_AsyncCheckCompletedBeforeSyncCheck) {
  SetUpTest();
  AddSyncCallbackInfo(/*should_proceed=*/true,
                      /*should_delay_callback=*/true);
  AddAsyncCallbackInfo(
      /*should_proceed=*/true,
      /*should_delay_callback=*/true);

  CallWillStartRequest();
  async_url_checker_->RestartDelayedCallback(/*index=*/0);
  task_environment_.RunUntilIdle();
  sync_url_checker_->RestartDelayedCallback(/*index=*/0);
  task_environment_.RunUntilIdle();

  bool defer = CallWillProcessResponse();
  EXPECT_FALSE(defer);
  EXPECT_EQ(async_check_tracker_->PendingCheckersSizeForTesting(), 0u);

  // Reset throttle to test histogram below, some of which only logs on
  // destruct.
  throttle_.reset();
  VerifyHistograms(/*is_async_check_faster=*/true,
                   /*is_async_check_transferred=*/false);
}

// WillProcessResponse called -> Async check completed -> Sync check completed.
TEST_F(SBBrowserUrlLoaderThrottleAsyncCheckTest,
       VerifyDefer_AsyncCheckCompletedAfterProcessResponseBeforeSyncCheck) {
  SetUpTest();
  AddSyncCallbackInfo(/*should_proceed=*/true,
                      /*should_delay_callback=*/true);
  AddAsyncCallbackInfo(
      /*should_proceed=*/true,
      /*should_delay_callback=*/true);

  CallWillStartRequest();

  bool defer = CallWillProcessResponse();
  EXPECT_TRUE(defer);

  async_url_checker_->RestartDelayedCallback(/*index=*/0);
  task_environment_.RunUntilIdle();
  EXPECT_FALSE(throttle_delegate_->IsResumed());

  sync_url_checker_->RestartDelayedCallback(/*index=*/0);
  task_environment_.RunUntilIdle();
  EXPECT_TRUE(throttle_delegate_->IsResumed());

  // Reset throttle to test histogram below, some of which only logs on
  // destruct.
  throttle_.reset();
  VerifyHistograms(/*is_async_check_faster=*/true,
                   /*is_async_check_transferred=*/false);
}

TEST_F(SBBrowserUrlLoaderThrottleAsyncCheckTest,
       VerifyErrorCode_SyncCheckBlocked) {
  SetUpTest();
  AddSyncCallbackInfo(/*should_proceed=*/false,
                      /*should_delay_callback=*/false);
  AddAsyncCallbackInfo(
      /*should_proceed=*/true,
      /*should_delay_callback=*/true);

  CallWillStartRequest();
  EXPECT_EQ(throttle_delegate_->GetErrorCode(), net::ERR_BLOCKED_BY_CLIENT);

  bool defer = CallWillProcessResponse();
  EXPECT_TRUE(defer);
  // Although async check has not completed, it should be deleted because the
  // loader is already blocked by sync check.
  EXPECT_TRUE(async_url_checker_.WasInvalidated());
  EXPECT_EQ(async_check_tracker_->PendingCheckersSizeForTesting(), 0u);

  // Reset throttle to test histogram below, some of which only logs on
  // destruct.
  throttle_.reset();
  VerifyHistograms(/*is_async_check_faster=*/false,
                   /*is_async_check_transferred=*/std::nullopt);
}

TEST_F(SBBrowserUrlLoaderThrottleAsyncCheckTest,
       VerifyErrorCode_SyncCheckBlockedOnRedirect) {
  SetUpTest();
  AddSyncCallbackInfo(/*should_proceed=*/true,
                      /*should_delay_callback=*/false);
  AddSyncCallbackInfo(/*should_proceed=*/false,
                      /*should_delay_callback=*/false);
  AddAsyncCallbackInfo(
      /*should_proceed=*/true,
      /*should_delay_callback=*/false);
  AddAsyncCallbackInfo(
      /*should_proceed=*/true,
      /*should_delay_callback=*/true);

  CallWillStartRequest();
  CallWillRedirectRequest();
  EXPECT_EQ(throttle_delegate_->GetErrorCode(), net::ERR_BLOCKED_BY_CLIENT);

  bool defer = CallWillProcessResponse();
  EXPECT_TRUE(defer);
  // Although async check has not completed, it should be deleted because the
  // loader is already blocked by sync check.
  EXPECT_TRUE(async_url_checker_.WasInvalidated());
  EXPECT_EQ(async_check_tracker_->PendingCheckersSizeForTesting(), 0u);

  // Reset throttle to test histogram below, some of which only logs on
  // destruct.
  throttle_.reset();
  VerifyHistograms(/*is_async_check_faster=*/false,
                   /*is_async_check_transferred=*/std::nullopt);
}

TEST_F(SBBrowserUrlLoaderThrottleAsyncCheckTest,
       VerifyErrorCode_AsyncCheckBlocked) {
  SetUpTest();
  AddSyncCallbackInfo(/*should_proceed=*/true,
                      /*should_delay_callback=*/true);
  AddAsyncCallbackInfo(
      /*should_proceed=*/false,
      /*should_delay_callback=*/false);

  CallWillStartRequest();
  // Once async check completes, it should not wait for sync check to block the
  // loader.
  EXPECT_EQ(throttle_delegate_->GetErrorCode(), net::ERR_BLOCKED_BY_CLIENT);
  EXPECT_TRUE(sync_url_checker_.WasInvalidated());

  bool defer = CallWillProcessResponse();
  EXPECT_TRUE(defer);
  EXPECT_EQ(async_check_tracker_->PendingCheckersSizeForTesting(), 0u);

  // Reset throttle to test histogram below, some of which only logs on
  // destruct.
  throttle_.reset();
  VerifyHistograms(/*is_async_check_faster=*/true,
                   /*is_async_check_transferred=*/std::nullopt);
}

TEST_F(SBBrowserUrlLoaderThrottleAsyncCheckTest,
       VerifyAsyncChecksNotEligible_LoadPrefetch) {
  SetUpTest();
  AddSyncCallbackInfo(/*should_proceed=*/true,
                      /*should_delay_callback=*/true);
  // Do not call |AddAsyncCallbackInfo| so that if the async callback is
  // called (incorrectly), the test will fail.

  CallWillStartRequest({.load_flags = net::LOAD_PREFETCH});
  CallWillProcessResponse();

  sync_url_checker_->RestartDelayedCallback(/*index=*/0);
  task_environment_.RunUntilIdle();
  EXPECT_TRUE(throttle_delegate_->IsResumed());

  // Reset throttle to test histogram below, some of which only logs on
  // destruct.
  throttle_.reset();
  VerifyHistograms(/*is_async_check_faster=*/std::nullopt,
                   /*is_async_check_transferred=*/std::nullopt);
}

}  // namespace safe_browsing