File: webstore_private_api.cc

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (1392 lines) | stat: -rw-r--r-- 53,734 bytes parent folder | download | duplicates (2)
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
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
// 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 "chrome/browser/extensions/api/webstore_private/webstore_private_api.h"

#include <stddef.h>

#include <memory>
#include <string_view>
#include <utility>
#include <vector>

#include "base/auto_reset.h"
#include "base/base64.h"
#include "base/functional/bind.h"
#include "base/json/values_util.h"
#include "base/lazy_instance.h"
#include "base/memory/scoped_refptr.h"
#include "base/metrics/histogram_functions.h"
#include "base/metrics/histogram_macros.h"
#include "base/scoped_multi_source_observation.h"
#include "base/strings/utf_string_conversions.h"
#include "base/task/single_thread_task_runner.h"
#include "base/values.h"
#include "base/version.h"
#include "base/version_info/version_info.h"
#include "build/chromeos_buildflags.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/extensions/api/webstore_private/extension_install_status.h"
#include "chrome/browser/extensions/extension_allowlist.h"
#include "chrome/browser/extensions/extension_management.h"
#include "chrome/browser/extensions/install_approval.h"
#include "chrome/browser/extensions/install_tracker.h"
#include "chrome/browser/extensions/scoped_active_install.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/profiles/profile_observer.h"
#include "chrome/browser/safe_browsing/safe_browsing_metrics_collector_factory.h"
#include "chrome/browser/safe_browsing/safe_browsing_navigation_observer_manager_factory.h"
#include "chrome/browser/signin/identity_manager_factory.h"
#include "chrome/browser/supervised_user/supervised_user_browser_utils.h"
#include "chrome/common/extensions/extension_constants.h"
#include "chrome/common/pref_names.h"
#include "chrome/grit/generated_resources.h"
#include "components/crx_file/id_util.h"
#include "components/prefs/pref_service.h"
#include "components/prefs/scoped_user_pref_update.h"
#include "components/safe_browsing/buildflags.h"
#include "components/safe_browsing/content/browser/safe_browsing_navigation_observer_manager.h"
#include "components/safe_browsing/core/browser/safe_browsing_metrics_collector.h"
#include "components/signin/public/identity_manager/identity_manager.h"
#include "components/supervised_user/core/browser/supervised_user_preferences.h"
#include "components/supervised_user/core/common/features.h"
#include "content/public/browser/gpu_feature_checker.h"
#include "content/public/browser/storage_partition.h"
#include "content/public/browser/web_contents.h"
#include "extensions/browser/extension_dialog_auto_confirm.h"
#include "extensions/browser/extension_function_constants.h"
#include "extensions/browser/extension_registry.h"
#include "extensions/browser/extension_util.h"
#include "extensions/browser/extensions_browser_client.h"
#include "extensions/common/extension.h"
#include "extensions/common/manifest.h"
#include "extensions/common/manifest_constants.h"
#include "extensions/common/manifest_handlers/permissions_parser.h"
#include "extensions/common/permissions/permission_set.h"
#include "net/base/load_flags.h"
#include "ui/base/l10n/l10n_util.h"
#include "url/gurl.h"

#if BUILDFLAG(ENABLE_EXTENSIONS)
#include "chrome/browser/extensions/manifest_v2_experiment_manager.h"
#include "chrome/browser/extensions/mv2_experiment_stage.h"
#include "chrome/browser/ui/extensions/extensions_dialogs.h"
#include "extensions/browser/api/management/management_api.h"
#endif

#if BUILDFLAG(SAFE_BROWSING_AVAILABLE)
#include "chrome/browser/safe_browsing/safe_browsing_service.h"
#endif

using safe_browsing::SafeBrowsingNavigationObserverManager;

namespace extensions {

namespace BeginInstallWithManifest3 =
    api::webstore_private::BeginInstallWithManifest3;
namespace CompleteInstall = api::webstore_private::CompleteInstall;
namespace GetBrowserLogin = api::webstore_private::GetBrowserLogin;
namespace GetExtensionStatus = api::webstore_private::GetExtensionStatus;
namespace GetIsLauncherEnabled = api::webstore_private::GetIsLauncherEnabled;
namespace GetStoreLogin = api::webstore_private::GetStoreLogin;
namespace GetWebGLStatus = api::webstore_private::GetWebGLStatus;
namespace IsPendingCustodianApproval =
    api::webstore_private::IsPendingCustodianApproval;
namespace IsInIncognitoMode = api::webstore_private::IsInIncognitoMode;
namespace SetStoreLogin = api::webstore_private::SetStoreLogin;
namespace GetFullChromeVersion = api::webstore_private::GetFullChromeVersion;

namespace {

// Holds the Approvals between the time we prompt and start the installs.
class PendingApprovals : public ProfileObserver {
 public:
  PendingApprovals() = default;

  PendingApprovals(const PendingApprovals&) = delete;
  PendingApprovals& operator=(const PendingApprovals&) = delete;

  ~PendingApprovals() override = default;

  void PushApproval(std::unique_ptr<InstallApproval> approval);
  std::unique_ptr<InstallApproval> PopApproval(Profile* profile,
                                               const std::string& id);
  void Clear();

  int GetCount() const { return approvals_.size(); }

 private:
  // ProfileObserver
  // Remove pending approvals if the Profile is being destroyed.
  void OnProfileWillBeDestroyed(Profile* profile) override {
    std::erase_if(approvals_, [profile](const auto& approval) {
      return approval->profile == profile;
    });
    observation_.RemoveObservation(profile);
  }

  void MaybeAddObservation(Profile* profile) {
    if (!observation_.IsObservingSource(profile)) {
      observation_.AddObservation(profile);
    }
  }

  // Remove observation if there are no pending approvals
  // for the Profile.
  void MaybeRemoveObservation(Profile* profile) {
    for (const auto& entry : approvals_) {
      if (entry->profile == profile) {
        return;
      }
    }
    observation_.RemoveObservation(profile);
  }

  using ApprovalList = std::vector<std::unique_ptr<InstallApproval>>;

  ApprovalList approvals_;
  base::ScopedMultiSourceObservation<Profile, ProfileObserver> observation_{
      this};
};

void PendingApprovals::PushApproval(std::unique_ptr<InstallApproval> approval) {
  MaybeAddObservation(approval->profile);
  approvals_.push_back(std::move(approval));
}

std::unique_ptr<InstallApproval> PendingApprovals::PopApproval(
    Profile* profile,
    const std::string& id) {
  for (auto iter = approvals_.begin(); iter != approvals_.end(); ++iter) {
    if (iter->get()->extension_id == id &&
        profile->IsSameOrParent(iter->get()->profile)) {
      std::unique_ptr<InstallApproval> approval = std::move(*iter);
      approvals_.erase(iter);
      MaybeRemoveObservation(approval->profile);
      return approval;
    }
  }
  return nullptr;
}

void PendingApprovals::Clear() {
  approvals_.clear();
}

api::webstore_private::Result WebstoreInstallHelperResultToApiResult(
    WebstoreInstallHelper::Delegate::InstallHelperResultCode result) {
  switch (result) {
    case WebstoreInstallHelper::Delegate::UNKNOWN_ERROR:
      return api::webstore_private::Result::kUnknownError;
    case WebstoreInstallHelper::Delegate::ICON_ERROR:
      return api::webstore_private::Result::kIconError;
    case WebstoreInstallHelper::Delegate::kManifestError:
      return api::webstore_private::Result::kManifestError;
  }
  NOTREACHED();
}

static base::LazyInstance<PendingApprovals>::DestructorAtExit
    g_pending_approvals = LAZY_INSTANCE_INITIALIZER;

// A preference set by the web store to indicate login information for
// purchased apps.
const char kWebstoreLogin[] = "extensions.webstore_login";

// Error messages that can be returned by the API.
const char kAlreadyInstalledError[] = "This item is already installed";
const char kWebstoreInvalidIconUrlError[] = "Invalid icon url";
const char kWebstoreInvalidIdError[] = "Invalid id";
const char kWebstoreInvalidManifestError[] = "Invalid manifest";
const char kNoPreviousBeginInstallWithManifestError[] =
    "* does not match a previous call to beginInstallWithManifest3";
const char kWebstoreUserCancelledError[] = "User cancelled install";
const char kWebstoreBlockByPolicy[] =
    "Extension installation is blocked by policy";
const char kIncognitoError[] =
    "Apps cannot be installed in guest/incognito mode";

const char kParentBlockedExtensionInstallError[] =
    "Parent has blocked extension/app installation";

#if BUILDFLAG(SAFE_BROWSING_AVAILABLE)
// The number of user gestures to trace back for the referrer chain.
const int kExtensionReferrerUserGestureLimit = 2;
#endif

WebstorePrivateApi::Delegate* test_delegate = nullptr;

// We allow the web store to set a string containing login information when a
// purchase is made, so that when a user logs into sync with a different
// account we can recognize the situation. The Get function returns the login if
// there was previously stored data, or an empty string otherwise. The Set will
// overwrite any previous login.
std::string GetWebstoreLogin(Profile* profile) {
  if (profile->GetPrefs()->HasPrefPath(kWebstoreLogin)) {
    return profile->GetPrefs()->GetString(kWebstoreLogin);
  }
  return std::string();
}

void SetWebstoreLogin(Profile* profile, const std::string& login) {
  profile->GetPrefs()->SetString(kWebstoreLogin, login);
}

api::webstore_private::ExtensionInstallStatus
ConvertExtensionInstallStatusForAPI(ExtensionInstallStatus status) {
  switch (status) {
    case kCanRequest:
      return api::webstore_private::ExtensionInstallStatus::kCanRequest;
    case kRequestPending:
      return api::webstore_private::ExtensionInstallStatus::kRequestPending;
    case kBlockedByPolicy:
      return api::webstore_private::ExtensionInstallStatus::kBlockedByPolicy;
    case kInstallable:
      return api::webstore_private::ExtensionInstallStatus::kInstallable;
    case kEnabled:
      return api::webstore_private::ExtensionInstallStatus::kEnabled;
    case kDisabled:
      return api::webstore_private::ExtensionInstallStatus::kDisabled;
    case kTerminated:
      return api::webstore_private::ExtensionInstallStatus::kTerminated;
    case kBlocklisted:
      return api::webstore_private::ExtensionInstallStatus::kBlacklisted;
    case kCustodianApprovalRequired:
      return api::webstore_private::ExtensionInstallStatus::
          kCustodianApprovalRequired;
    case kCustodianApprovalRequiredForInstallation:
      return api::webstore_private::ExtensionInstallStatus::
          kCustodianApprovalRequiredForInstallation;
    case kForceInstalled:
      return api::webstore_private::ExtensionInstallStatus::kForceInstalled;
    case kDeprecatedManifestVersion:
      return api::webstore_private::ExtensionInstallStatus::
          kDeprecatedManifestVersion;
    case kCorrupted:
      return api::webstore_private::ExtensionInstallStatus::kCorrupted;
  }
  return api::webstore_private::ExtensionInstallStatus::kNone;
}

// Requests extension by adding the id into the pending list in Profile Prefs if
// available. Returns |kRequestPending| if the request has been added
// successfully. Otherwise, returns the initial extension install status.
ExtensionInstallStatus AddExtensionToPendingList(
    const ExtensionId& id,
    Profile* profile,
    const std::string& justification) {
  // There is no need to check whether the extension's required permissions or
  // manifest type are blocked  by the enterprise policy because extensions
  // blocked by those are still requestable.
  ExtensionInstallStatus status =
      GetWebstoreExtensionInstallStatus(id, profile);
  // We put the |id| into the pending request list if it can be requested.
  // Ideally we should not get here if the status is not |kCanRequest|. However
  // policy might be updated between the client calling |requestExtension| or
  // |beginInstallWithManifest3| and us checking the status here. Handle
  // approvals and rejections for this case by adding the |id| into the pending
  // list. ExtensionRequestObserver will observe this update and show the
  // notificaion immediately.
  // Please note that only the |id| that can be requested will be uploaded to
  // the server and ExtensionRequestObserver will also show notifications once
  // it's approved or rejected.
  // |id| will be removed from the pending list once the notification is
  // confirmed or closed by the user.
  if (status != kCanRequest && status != kInstallable &&
      status != kBlockedByPolicy && status != kForceInstalled) {
    return status;
  }

  ScopedDictPrefUpdate pending_requests_update(
      profile->GetPrefs(), prefs::kCloudExtensionRequestIds);
  DCHECK(!pending_requests_update->Find(id));
  base::Value::Dict request_data;
  request_data.Set(extension_misc::kExtensionRequestTimestamp,
                   ::base::TimeToValue(base::Time::Now()));
  if (!justification.empty()) {
    request_data.Set(extension_misc::kExtensionWorkflowJustification,
                     justification);
  }
  pending_requests_update->Set(id, std::move(request_data));
  // Query the new extension install status again. It should be changed from
  // |kCanRequest| to |kRequestPending| if the id has been added into pending
  // list successfully. Otherwise, it shouldn't be changed.
  ExtensionInstallStatus new_status =
      GetWebstoreExtensionInstallStatus(id, profile);
#if DCHECK_IS_ON()
  if (status == kCanRequest) {
    DCHECK_EQ(kRequestPending, new_status);
  } else {
    DCHECK_EQ(status, new_status);
  }
#endif  // DCHECK_IS_ON()
  return new_status;
}

// Returns the extension's icon if it exists, otherwise the default icon of the
// extension type.
gfx::ImageSkia GetIconImage(const SkBitmap& icon, bool is_app) {
  if (!icon.empty()) {
    return gfx::ImageSkia::CreateFrom1xBitmap(icon);
  }

  return is_app ? util::GetDefaultAppIcon() : util::GetDefaultExtensionIcon();
}

// These values are persisted to logs. Entries should not be renumbered and
// numeric values should never be reused.
enum class WebStoreInstallAllowlistParameter {
  kUndefined = 0,
  kAllowlisted = 1,
  kNotAllowlisted = 2,
  kMaxValue = kNotAllowlisted,
};

// Track the value of the allowlist parameter received from Chrome Web Store.
void ReportWebStoreInstallEsbAllowlistParameter(
    const std::optional<bool>& allowlist_parameter) {
  WebStoreInstallAllowlistParameter value;

  if (!allowlist_parameter) {
    value = WebStoreInstallAllowlistParameter::kUndefined;
  } else if (*allowlist_parameter) {
    value = WebStoreInstallAllowlistParameter::kAllowlisted;
  } else {
    value = WebStoreInstallAllowlistParameter::kNotAllowlisted;
  }

  base::UmaHistogramEnumeration(
      "Extensions.WebStoreInstall.EsbAllowlistParameter", value);
}

// Track if a user accepts to install a not allowlisted extensions.
void ReportWebStoreInstallNotAllowlistedInstalled(bool installed,
                                                  bool friction_dialog_shown) {
  if (friction_dialog_shown) {
    base::UmaHistogramBoolean(
        "Extensions.WebStoreInstall.NotAllowlistedInstalledWithFriction",
        installed);
  } else {
    base::UmaHistogramBoolean(
        "Extensions.WebStoreInstall.NotAllowlistedInstalledWithoutFriction",
        installed);
  }
}

// Returns whether the app launcher has been enabled.
bool IsAppLauncherEnabled() {
#if BUILDFLAG(IS_CHROMEOS)
  return true;
#else
  return false;
#endif
}

}  // namespace

// static
base::AutoReset<WebstorePrivateApi::Delegate*>
WebstorePrivateApi::SetDelegateForTesting(Delegate* delegate) {
  CHECK_EQ(nullptr, test_delegate);
  return base::AutoReset<Delegate*>(&test_delegate, delegate);
}

// static
std::unique_ptr<InstallApproval> WebstorePrivateApi::PopApprovalForTesting(
    Profile* profile,
    const std::string& extension_id) {
  return g_pending_approvals.Get().PopApproval(profile, extension_id);
}

void WebstorePrivateApi::ClearPendingApprovalsForTesting() {
  g_pending_approvals.Get().Clear();
}

int WebstorePrivateApi::GetPendingApprovalsCountForTesting() {
  return g_pending_approvals.Get().GetCount();
}

WebstorePrivateBeginInstallWithManifest3Function::
    WebstorePrivateBeginInstallWithManifest3Function() = default;

WebstorePrivateBeginInstallWithManifest3Function::
    ~WebstorePrivateBeginInstallWithManifest3Function() = default;

std::u16string WebstorePrivateBeginInstallWithManifest3Function::
    GetBlockedByPolicyErrorMessageForTesting() const {
  return blocked_by_policy_error_message_;
}

ExtensionFunction::ResponseAction
WebstorePrivateBeginInstallWithManifest3Function::Run() {
  params_ = Params::Create(args());
  EXTENSION_FUNCTION_VALIDATE(params_);

  profile_ = Profile::FromBrowserContext(browser_context());

  if (!crx_file::id_util::IdIsValid(details().id)) {
    return RespondNow(BuildResponse(api::webstore_private::Result::kInvalidId,
                                    kWebstoreInvalidIdError));
  }

  GURL icon_url;
  if (details().icon_url) {
    icon_url = source_url().Resolve(*details().icon_url);
    if (!icon_url.is_valid()) {
      return RespondNow(
          BuildResponse(api::webstore_private::Result::kInvalidIconUrl,
                        kWebstoreInvalidIconUrlError));
    }
  }

  InstallTracker* tracker = InstallTracker::Get(browser_context());
  DCHECK(tracker);
  bool is_installed =
      ExtensionRegistry::Get(browser_context())
          ->GetExtensionById(details().id, ExtensionRegistry::EVERYTHING) !=
      nullptr;
  if (is_installed || tracker->GetActiveInstall(details().id)) {
    return RespondNow(
        BuildResponse(api::webstore_private::Result::kAlreadyInstalled,
                      kAlreadyInstalledError));
  }
  ActiveInstallData install_data(details().id);
  scoped_active_install_ =
      std::make_unique<ScopedActiveInstall>(tracker, install_data);

  network::mojom::URLLoaderFactory* loader_factory = nullptr;
  if (!icon_url.is_empty()) {
    loader_factory = browser_context()
                         ->GetDefaultStoragePartition()
                         ->GetURLLoaderFactoryForBrowserProcess()
                         .get();
  }

  auto helper = base::MakeRefCounted<WebstoreInstallHelper>(
      this, details().id, details().manifest, icon_url);

  // The helper will call us back via OnWebstoreParseSuccess or
  // OnWebstoreParseFailure.
  helper->Start(loader_factory);

  // Matched with a Release in OnWebstoreParseSuccess/OnWebstoreParseFailure.
  AddRef();

  // The response is sent asynchronously in OnWebstoreParseSuccess/
  // OnWebstoreParseFailure.
  return RespondLater();
}

void WebstorePrivateBeginInstallWithManifest3Function::OnWebstoreParseSuccess(
    const std::string& id,
    const SkBitmap& icon,
    base::Value::Dict parsed_manifest) {
  CHECK_EQ(details().id, id);
  parsed_manifest_ = std::move(parsed_manifest);
  icon_ = icon;

  std::string localized_name =
      details().localized_name ? *details().localized_name : std::string();

  std::string error;
  dummy_extension_ = ExtensionInstallPrompt::GetLocalizedExtensionForDisplay(
      *parsed_manifest_, Extension::FROM_WEBSTORE, id, localized_name,
      std::string(), &error);

  if (!dummy_extension_.get()) {
    OnWebstoreParseFailure(details().id,
                           WebstoreInstallHelper::Delegate::kManifestError,
                           kWebstoreInvalidManifestError);
    return;
  }

  content::WebContents* web_contents = GetSenderWebContents();
  if (!web_contents) {
    // The browser window has gone away.
    Respond(BuildResponse(api::webstore_private::Result::kUserCancelled,
                          kWebstoreUserCancelledError));
    // Matches the AddRef in Run().
    Release();
    return;
  }

  // Check the management policy before the installation process begins.
  ExtensionInstallStatus install_status = GetWebstoreExtensionInstallStatus(
      id, profile_, dummy_extension_->manifest()->type(),
      PermissionsParser::GetRequiredPermissions(dummy_extension_.get()),
      dummy_extension_->manifest_version());
  if (install_status == kBlockedByPolicy) {
    ShowBlockedByPolicyDialog(
        dummy_extension_.get(), icon_, web_contents,
        base::BindOnce(&WebstorePrivateBeginInstallWithManifest3Function::
                           OnBlockByPolicyPromptDone,
                       this));
    return;
  }

  if (install_status == kCanRequest || install_status == kRequestPending) {
    install_prompt_ = std::make_unique<ExtensionInstallPrompt>(web_contents);
    install_prompt_->ShowDialog(
        base::BindRepeating(&WebstorePrivateBeginInstallWithManifest3Function::
                                OnRequestPromptDone,
                            this),
        dummy_extension_.get(), &icon_,
        std::make_unique<ExtensionInstallPrompt::Prompt>(
            install_status == kCanRequest
                ? ExtensionInstallPrompt::EXTENSION_REQUEST_PROMPT
                : ExtensionInstallPrompt::EXTENSION_PENDING_REQUEST_PROMPT),
        ExtensionInstallPrompt::GetDefaultShowDialogCallback());
  } else {
    ReportWebStoreInstallEsbAllowlistParameter(details().esb_allowlist);

    if (ShouldShowFrictionDialog(profile_)) {
      ShowInstallFrictionDialog(web_contents);
    } else {
      ShowInstallDialog(web_contents);
    }
  }
  // Control flow finishes up in OnInstallPromptDone, OnRequestPromptDone or
  // OnBlockByPolicyPromptDone.
}

void WebstorePrivateBeginInstallWithManifest3Function::OnWebstoreParseFailure(
    const std::string& id,
    WebstoreInstallHelper::Delegate::InstallHelperResultCode result,
    const std::string& error_message) {
  CHECK_EQ(details().id, id);

  Respond(BuildResponse(WebstoreInstallHelperResultToApiResult(result),
                        error_message));

  // Matches the AddRef in Run().
  Release();
}


void WebstorePrivateBeginInstallWithManifest3Function::RequestExtensionApproval(
    content::WebContents* web_contents) {
#if BUILDFLAG(ENABLE_EXTENSIONS)
  SupervisedUserExtensionsDelegate* supervised_user_extensions_delegate =
      ManagementAPI::GetFactoryInstance()
          ->Get(profile_)
          ->GetSupervisedUserExtensionsDelegate();
  CHECK(supervised_user_extensions_delegate);
  auto extension_approval_callback =
      base::BindOnce(&WebstorePrivateBeginInstallWithManifest3Function::
                         OnExtensionApprovalDone,
                     this);
  supervised_user_extensions_delegate->RequestToAddExtensionOrShowError(
      *dummy_extension_, web_contents,
      gfx::ImageSkia::CreateFrom1xBitmap(icon_),
      SupervisedUserExtensionParentApprovalEntryPoint::kOnWebstoreInstallation,
      std::move(extension_approval_callback));
#else
  // TODO(crbug.com/410616937): Support supervised user install controls on
  // desktop Android.
  NOTIMPLEMENTED() << "Supervised user checks not yet supported on Android.";
  OnExtensionApprovalDone(
      SupervisedUserExtensionsDelegate::ExtensionApprovalResult::kApproved);
#endif
}

void WebstorePrivateBeginInstallWithManifest3Function::OnExtensionApprovalDone(
    SupervisedUserExtensionsDelegate::ExtensionApprovalResult result) {
  switch (result) {
    case SupervisedUserExtensionsDelegate::ExtensionApprovalResult::kApproved:
      OnExtensionApprovalApproved();
      break;
    case SupervisedUserExtensionsDelegate::ExtensionApprovalResult::kCanceled:
      OnExtensionApprovalCanceled();
      break;
    case SupervisedUserExtensionsDelegate::ExtensionApprovalResult::kFailed:
      OnExtensionApprovalFailed();
      break;
    case SupervisedUserExtensionsDelegate::ExtensionApprovalResult::kBlocked:
      OnExtensionApprovalBlocked();
      break;
  }
  Release();  // Matches the AddRef in Run().
}

void WebstorePrivateBeginInstallWithManifest3Function::
    OnExtensionApprovalApproved() {
#if BUILDFLAG(ENABLE_EXTENSIONS)
  SupervisedUserExtensionsDelegate* supervised_user_extensions_delegate =
      ManagementAPI::GetFactoryInstance()
          ->Get(profile_)
          ->GetSupervisedUserExtensionsDelegate();
  CHECK(supervised_user_extensions_delegate);
  supervised_user_extensions_delegate->AddExtensionApproval(*dummy_extension_);
#else
  // TODO(crbug.com/410616937): Support supervised user install controls on
  // desktop Android.
  NOTIMPLEMENTED() << "Supervised user checks not yet supported on Android.";
#endif

  HandleInstallProceed();
}

void WebstorePrivateBeginInstallWithManifest3Function::
    OnExtensionApprovalCanceled() {
  if (test_delegate) {
    test_delegate->OnExtensionInstallFailure(
        dummy_extension_->id(),
        l10n_util::GetStringUTF8(
            IDS_EXTENSIONS_SUPERVISED_USER_PARENTAL_PERMISSION_FAILURE),
        WebstoreInstaller::FailureReason::FAILURE_REASON_CANCELLED);
  }

  HandleInstallAbort(true /* user_initiated */);
}

void WebstorePrivateBeginInstallWithManifest3Function::
    OnExtensionApprovalFailed() {
  if (test_delegate) {
    test_delegate->OnExtensionInstallFailure(
        dummy_extension_->id(),
        l10n_util::GetStringUTF8(
            IDS_EXTENSIONS_SUPERVISED_USER_PARENTAL_PERMISSION_FAILURE),
        WebstoreInstaller::FailureReason::FAILURE_REASON_OTHER);
  }

  Respond(BuildResponse(
      api::webstore_private::Result::kUnknownError,
      l10n_util::GetStringUTF8(
          IDS_EXTENSIONS_SUPERVISED_USER_PARENTAL_PERMISSION_FAILURE)));
}

void WebstorePrivateBeginInstallWithManifest3Function::
    OnExtensionApprovalBlocked() {
  Respond(BuildResponse(api::webstore_private::Result::kBlockedForChildAccount,
                        kParentBlockedExtensionInstallError));
}

bool WebstorePrivateBeginInstallWithManifest3Function::
    PromptForParentApproval() {
  DCHECK(supervised_user::AreExtensionsPermissionsEnabled(profile_));
  content::WebContents* web_contents = GetSenderWebContents();
  if (!web_contents) {
    // The browser window has gone away.
    Respond(BuildResponse(api::webstore_private::Result::kUserCancelled,
                          kWebstoreUserCancelledError));
    return false;
  }
  // Assume that the block dialog will not be shown by the
  // SupervisedUserExtensionsDelegate, because if permissions for extensions
  // were disabled, the block dialog would have been shown at the install prompt
  // step.
  RequestExtensionApproval(web_contents);

  return true;
}


void WebstorePrivateBeginInstallWithManifest3Function::OnFrictionPromptDone(
    bool result) {
  content::WebContents* web_contents = GetSenderWebContents();
  if (!result || !web_contents) {
    ReportWebStoreInstallNotAllowlistedInstalled(
        /*installed=*/false, /*friction_dialog_shown=*/true);

    Respond(BuildResponse(api::webstore_private::Result::kUserCancelled,
                          kWebstoreUserCancelledError));
    // Matches the AddRef in Run().
    Release();
    return;
  }

  ReportFrictionAcceptedEvent();
  ShowInstallDialog(web_contents);
}

void WebstorePrivateBeginInstallWithManifest3Function::
    ReportFrictionAcceptedEvent() {
  if (!profile_) {
    return;
  }
  auto* metrics_collector =
      safe_browsing::SafeBrowsingMetricsCollectorFactory::GetForProfile(
          profile_);
  // `metrics_collector` can be null in incognito.
  if (metrics_collector) {
    metrics_collector->AddSafeBrowsingEventToPref(
        safe_browsing::SafeBrowsingMetricsCollector::EventType::
            EXTENSION_ALLOWLIST_INSTALL_BYPASS);
  }
}

void WebstorePrivateBeginInstallWithManifest3Function::OnInstallPromptDone(
    ExtensionInstallPrompt::DoneCallbackPayload payload) {
  switch (payload.result) {
    case ExtensionInstallPrompt::Result::ACCEPTED:
    case ExtensionInstallPrompt::Result::ACCEPTED_WITH_WITHHELD_PERMISSIONS: {
      // TODO(b/202064235): The only user of this branch is ChromeOs v1 flow.
      // Handle parent permission for child accounts on ChromeOS.
      // Parent permission not required for theme installation.
      if (!dummy_extension_->is_theme() &&
          ExtensionsBrowserClient::Get()->IsValidContext(profile_) &&
          supervised_user::AreExtensionsPermissionsEnabled(profile_) &&
          !supervised_user::SupervisedUserCanSkipExtensionParentApprovals(
              profile_)) {
        if (PromptForParentApproval()) {
          // If we are showing parent permission dialog, return instead of
          // break, so that we don't release the ref below.
          return;
        } else {
          // An error occurred, break so that we release the ref below.
          break;
        }
      }
      bool withhold_permissions =
          payload.result ==
          ExtensionInstallPrompt::Result::ACCEPTED_WITH_WITHHELD_PERMISSIONS;
      HandleInstallProceed(withhold_permissions);
      break;
    }
    case ExtensionInstallPrompt::Result::USER_CANCELED:
    case ExtensionInstallPrompt::Result::ABORTED: {
      HandleInstallAbort(payload.result ==
                         ExtensionInstallPrompt::Result::USER_CANCELED);
      break;
    }
  }

  // Matches the AddRef in Run().
  Release();
}

void WebstorePrivateBeginInstallWithManifest3Function::OnRequestPromptDone(
    ExtensionInstallPrompt::DoneCallbackPayload payload) {
  switch (payload.result) {
    case ExtensionInstallPrompt::Result::ACCEPTED:
      AddExtensionToPendingList(details().id, profile_, payload.justification);
      break;
    case ExtensionInstallPrompt::Result::USER_CANCELED:
    case ExtensionInstallPrompt::Result::ABORTED:
      break;
    case ExtensionInstallPrompt::Result::ACCEPTED_WITH_WITHHELD_PERMISSIONS:
      NOTREACHED();
  }

  Respond(BuildResponse(api::webstore_private::Result::kUserCancelled,
                        kWebstoreUserCancelledError));
  // Matches the AddRef in Run().
  Release();
}
void WebstorePrivateBeginInstallWithManifest3Function::
    OnBlockByPolicyPromptDone() {
  Respond(BuildResponse(api::webstore_private::Result::kBlockedByPolicy,
                        kWebstoreBlockByPolicy));
  // Matches the AddRef in Run().
  Release();
}

void WebstorePrivateBeginInstallWithManifest3Function::HandleInstallProceed(
    bool withhold_permissions) {
  // This gets cleared in CrxInstaller::ConfirmInstall(). TODO(asargent) - in
  // the future we may also want to add time-based expiration, where an
  // allowlist entry is only valid for some number of minutes.
  DCHECK(parsed_manifest_);
  std::unique_ptr<InstallApproval> approval(
      InstallApproval::CreateWithNoInstallPrompt(
          profile_, details().id, std::move(*parsed_manifest_), false));
  approval->use_app_installed_bubble = !!details().app_install_bubble;
  // If we are enabling the launcher, we should not show the app list in order
  // to train the user to open it themselves at least once.
  approval->skip_post_install_ui = !!details().enable_launcher;
  approval->dummy_extension = dummy_extension_.get();
  approval->installing_icon = gfx::ImageSkia::CreateFrom1xBitmap(icon_);
  approval->bypassed_safebrowsing_friction = friction_dialog_shown_;
  approval->withhold_permissions = withhold_permissions;
  if (details().authuser) {
    approval->authuser = *details().authuser;
  }
  g_pending_approvals.Get().PushApproval(std::move(approval));

  DCHECK(scoped_active_install_.get());
  scoped_active_install_->CancelDeregister();

  // Record when the user accepted to install a not allowlisted extension.
  if (details().esb_allowlist && !*details().esb_allowlist) {
    ReportWebStoreInstallNotAllowlistedInstalled(
        /*installed=*/true, friction_dialog_shown_);
  }
  Respond(
      BuildResponse(api::webstore_private::Result::kSuccess, std::string()));
}

void WebstorePrivateBeginInstallWithManifest3Function::HandleInstallAbort(
    bool user_initiated) {
  if (details().esb_allowlist && !*details().esb_allowlist) {
    ReportWebStoreInstallNotAllowlistedInstalled(
        /*installed=*/false, friction_dialog_shown_);
  }

  Respond(BuildResponse(api::webstore_private::Result::kUserCancelled,
                        kWebstoreUserCancelledError));
}

ExtensionFunction::ResponseValue
WebstorePrivateBeginInstallWithManifest3Function::BuildResponse(
    api::webstore_private::Result result,
    const std::string& error) {
  if (result != api::webstore_private::Result::kSuccess) {
    // TODO(tjudkins): We should not be using ErrorWithArguments here as it
    // doesn't play well with promise based API calls (only emitting the error
    // and dropping the arguments). In almost every case the error directly
    // responds with the result enum value returned, so instead we should drop
    // the error and have the caller just base logic on the enum value alone.
    // In the cases where they do not correspond we should add a new enum value.
    // We will need to ensure that the Webstore is entirely basing its logic on
    // the result alone before removing the error.
    return ErrorWithArgumentsDoNotUse(
        BeginInstallWithManifest3::Results::Create(result), error);
  }

  // The old Webstore expects an empty string on success, so don't use
  // RESULT_SUCCESS here.
  // TODO(crbug.com/40514370): The new Webstore accepts either the empty string
  // or RESULT_SUCCESS on success now, so once the old Webstore is turned down
  // this can be changed over.
  return ArgumentList(BeginInstallWithManifest3::Results::Create(
      api::webstore_private::Result::kEmptyString));
}

bool WebstorePrivateBeginInstallWithManifest3Function::ShouldShowFrictionDialog(
    Profile* profile) {
  // Consider an extension to be allowlisted if either we have no indication in
  // the `esb_allowlist` param or if the param is explicitly set.
  bool consider_allowlisted =
      !details().esb_allowlist || *details().esb_allowlist;

  // Never show friction if the extension is considered allowlisted.
  if (consider_allowlisted) {
    return false;
  }

  // Only show friction if the allowlist warnings are enabled for the profile.
  return ExtensionAllowlist::Get(profile)->warnings_enabled();
}

void WebstorePrivateBeginInstallWithManifest3Function::
    ShowInstallFrictionDialog(content::WebContents* contents) {
  friction_dialog_shown_ = true;
#if BUILDFLAG(ENABLE_EXTENSIONS)
  ShowExtensionInstallFrictionDialog(
      contents,
      base::BindOnce(&WebstorePrivateBeginInstallWithManifest3Function::
                         OnFrictionPromptDone,
                     this));
#else
  NOTIMPLEMENTED() << "ShowInstallFrictionDialog not supported on Android";
  OnFrictionPromptDone(true);
#endif
}

void WebstorePrivateBeginInstallWithManifest3Function::ShowInstallDialog(
    content::WebContents* contents) {
  auto prompt = std::make_unique<ExtensionInstallPrompt::Prompt>(
      ExtensionInstallPrompt::INSTALL_PROMPT);

  if (!dummy_extension_->is_theme()) {
    const bool requires_parent_permission =
        supervised_user::AreExtensionsPermissionsEnabled(profile_) &&
        !supervised_user::SupervisedUserCanSkipExtensionParentApprovals(
            profile_);

    // We don't prompt for parent permission for themes, so no need
    // to configure the install prompt to indicate that this is a child
    // asking a parent for installation permission.
    prompt->set_requires_parent_permission(requires_parent_permission);
    // Record metrics for supervised users that are in "Skip parent approval"-mode
    // and use the Extension install dialog (that is used by non-supervised
    // users).
    if (supervised_user::AreExtensionsPermissionsEnabled(profile_)) {
      prompt->AddObserver(&supervised_user_extensions_metrics_recorder_);
    }
    if (requires_parent_permission) {
      // Bypass the install prompt dialog if V2 is enabled. The
      // ParentAccessDialog handles both the blocked and install use case.
#if BUILDFLAG(IS_CHROMEOS)
      RequestExtensionApproval(contents);
      return;
#elif BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_MAC) || BUILDFLAG(IS_WIN)
      // Shows a parental permission dialog directly bypassing the extension
      // install dialog view. The parental permission dialog contains a superset
      // of data from the extension install dialog: requested extension
      // permissions and also parent's password input.
      PromptForParentApproval();
      return;
#endif  // BUILDFLAG(IS_CHROMEOS)
    }
  }

  install_prompt_ = std::make_unique<ExtensionInstallPrompt>(contents);
  install_prompt_->ShowDialog(
      base::BindOnce(&WebstorePrivateBeginInstallWithManifest3Function::
                         OnInstallPromptDone,
                     this),
      dummy_extension_.get(), &icon_, std::move(prompt),
      ExtensionInstallPrompt::GetDefaultShowDialogCallback());
}

void WebstorePrivateBeginInstallWithManifest3Function::
    ShowBlockedByPolicyDialog(const Extension* extension,
                              const SkBitmap& icon,
                              content::WebContents* contents,
                              base::OnceClosure done_callback) {
  DCHECK(extension);
  DCHECK(contents);

  Profile* profile = Profile::FromBrowserContext(contents->GetBrowserContext());

  std::string message_from_admin =
      ExtensionManagementFactory::GetForBrowserContext(profile)
          ->BlockedInstallMessage(extension->id());
  if (!message_from_admin.empty()) {
    blocked_by_policy_error_message_ =
        l10n_util::GetStringFUTF16(IDS_EXTENSION_PROMPT_MESSAGE_FROM_ADMIN,
                                   base::UTF8ToUTF16(message_from_admin));
  }

  gfx::ImageSkia image = GetIconImage(icon, extension->is_app());

  if (ScopedTestDialogAutoConfirm::GetAutoConfirmValue() !=
      ScopedTestDialogAutoConfirm::NONE) {
    base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
        FROM_HERE, std::move(done_callback));
    return;
  }

#if BUILDFLAG(ENABLE_EXTENSIONS)
  ShowExtensionInstallBlockedDialog(extension->id(), extension->name(),
                                    blocked_by_policy_error_message_, image,
                                    contents, std::move(done_callback));
#else
  LOG(ERROR) << "Install blocked. Dialog not supported on Android. Extension: "
             << extension->name()
             << ", message: " << blocked_by_policy_error_message_;
#endif
}

WebstorePrivateCompleteInstallFunction::
    WebstorePrivateCompleteInstallFunction() = default;

WebstorePrivateCompleteInstallFunction::
    ~WebstorePrivateCompleteInstallFunction() = default;

ExtensionFunction::ResponseAction
WebstorePrivateCompleteInstallFunction::Run() {
  std::optional<CompleteInstall::Params> params =
      CompleteInstall::Params::Create(args());
  EXTENSION_FUNCTION_VALIDATE(params);
  Profile* const profile = Profile::FromBrowserContext(browser_context());
  if (profile->IsGuestSession() || profile->IsOffTheRecord()) {
    return RespondNow(Error(kIncognitoError));
  }

  if (!crx_file::id_util::IdIsValid(params->expected_id)) {
    return RespondNow(Error(kWebstoreInvalidIdError));
  }

  approval_ =
      g_pending_approvals.Get().PopApproval(profile, params->expected_id);
  if (!approval_) {
    return RespondNow(
        Error(kNoPreviousBeginInstallWithManifestError, params->expected_id));
  }

  content::WebContents* web_contents = GetSenderWebContents();
  if (!web_contents) {
    return RespondNow(
        Error(function_constants::kCouldNotFindSenderWebContents));
  }

  scoped_active_install_ = std::make_unique<ScopedActiveInstall>(
      InstallTracker::Get(browser_context()), params->expected_id);

  // Balanced in OnExtensionInstallSuccess() or OnExtensionInstallFailure().
  AddRef();

  // The extension will install through the normal extension install flow, but
  // the allowlist entry will bypass the normal permissions install dialog.
  scoped_refptr<WebstoreInstaller> installer = new WebstoreInstaller(
      profile,
      base::BindOnce(
          &WebstorePrivateCompleteInstallFunction::OnExtensionInstallSuccess,
          weak_ptr_factory_.GetWeakPtr()),
      base::BindOnce(
          &WebstorePrivateCompleteInstallFunction::OnExtensionInstallFailure,
          weak_ptr_factory_.GetWeakPtr()),
      web_contents, params->expected_id, std::move(approval_),
      WebstoreInstaller::INSTALL_SOURCE_OTHER);
  installer->Start();

  return RespondLater();
}

void WebstorePrivateCompleteInstallFunction::OnExtensionInstallSuccess(
    const std::string& id) {
  OnInstallSuccess(id);
  VLOG(1) << "Install success, sending response";
  Respond(NoArguments());

  // Matches the AddRef in Run().
  Release();
}

void WebstorePrivateCompleteInstallFunction::OnExtensionInstallFailure(
    const std::string& id,
    const std::string& error,
    WebstoreInstaller::FailureReason reason) {
  if (test_delegate) {
    test_delegate->OnExtensionInstallFailure(id, error, reason);
  }

  VLOG(1) << "Install failed, sending response";
  Respond(Error(error));

  // Matches the AddRef in Run().
  Release();
}

void WebstorePrivateCompleteInstallFunction::OnInstallSuccess(
    const std::string& id) {
  if (test_delegate) {
    test_delegate->OnExtensionInstallSuccess(id);
  }
}

WebstorePrivateEnableAppLauncherFunction::
    WebstorePrivateEnableAppLauncherFunction() = default;

WebstorePrivateEnableAppLauncherFunction::
    ~WebstorePrivateEnableAppLauncherFunction() = default;

ExtensionFunction::ResponseAction
WebstorePrivateEnableAppLauncherFunction::Run() {
  // TODO(crbug.com/40567472): Check if this API is still in use and whether we
  // can remove it.
  return RespondNow(NoArguments());
}

WebstorePrivateGetBrowserLoginFunction::
    WebstorePrivateGetBrowserLoginFunction() = default;

WebstorePrivateGetBrowserLoginFunction::
    ~WebstorePrivateGetBrowserLoginFunction() = default;

ExtensionFunction::ResponseAction
WebstorePrivateGetBrowserLoginFunction::Run() {
  GetBrowserLogin::Results::Info info;
  info.login =
      IdentityManagerFactory::GetForProfile(
          Profile::FromBrowserContext(browser_context())->GetOriginalProfile())
          ->GetPrimaryAccountInfo(signin::ConsentLevel::kSync)
          .email;
  return RespondNow(ArgumentList(GetBrowserLogin::Results::Create(info)));
}

WebstorePrivateGetStoreLoginFunction::WebstorePrivateGetStoreLoginFunction() =
    default;

WebstorePrivateGetStoreLoginFunction::~WebstorePrivateGetStoreLoginFunction() =
    default;

ExtensionFunction::ResponseAction WebstorePrivateGetStoreLoginFunction::Run() {
  return RespondNow(ArgumentList(GetStoreLogin::Results::Create(
      GetWebstoreLogin(Profile::FromBrowserContext(browser_context())))));
}

WebstorePrivateSetStoreLoginFunction::WebstorePrivateSetStoreLoginFunction() =
    default;

WebstorePrivateSetStoreLoginFunction::~WebstorePrivateSetStoreLoginFunction() =
    default;

ExtensionFunction::ResponseAction WebstorePrivateSetStoreLoginFunction::Run() {
  std::optional<SetStoreLogin::Params> params =
      SetStoreLogin::Params::Create(args());
  EXTENSION_FUNCTION_VALIDATE(params);
  SetWebstoreLogin(Profile::FromBrowserContext(browser_context()),
                   params->login);
  return RespondNow(NoArguments());
}

WebstorePrivateGetWebGLStatusFunction::WebstorePrivateGetWebGLStatusFunction()
    : feature_checker_(content::GpuFeatureChecker::Create(
          gpu::GPU_FEATURE_TYPE_ACCELERATED_WEBGL,
          base::BindOnce(&WebstorePrivateGetWebGLStatusFunction::OnFeatureCheck,
                         base::Unretained(this)))) {}

WebstorePrivateGetWebGLStatusFunction::
    ~WebstorePrivateGetWebGLStatusFunction() = default;

ExtensionFunction::ResponseAction WebstorePrivateGetWebGLStatusFunction::Run() {
  feature_checker_->CheckGpuFeatureAvailability();
  return RespondLater();
}

void WebstorePrivateGetWebGLStatusFunction::OnFeatureCheck(
    bool feature_allowed) {
  Respond(ArgumentList(
      GetWebGLStatus::Results::Create(api::webstore_private::ParseWebGlStatus(
          feature_allowed ? "webgl_allowed" : "webgl_blocked"))));
}

WebstorePrivateGetIsLauncherEnabledFunction::
    WebstorePrivateGetIsLauncherEnabledFunction() = default;

WebstorePrivateGetIsLauncherEnabledFunction::
    ~WebstorePrivateGetIsLauncherEnabledFunction() = default;

ExtensionFunction::ResponseAction
WebstorePrivateGetIsLauncherEnabledFunction::Run() {
  return RespondNow(ArgumentList(
      GetIsLauncherEnabled::Results::Create(IsAppLauncherEnabled())));
}

WebstorePrivateIsInIncognitoModeFunction::
    WebstorePrivateIsInIncognitoModeFunction() = default;

WebstorePrivateIsInIncognitoModeFunction::
    ~WebstorePrivateIsInIncognitoModeFunction() = default;

ExtensionFunction::ResponseAction
WebstorePrivateIsInIncognitoModeFunction::Run() {
  Profile* profile = Profile::FromBrowserContext(browser_context());
  return RespondNow(ArgumentList(IsInIncognitoMode::Results::Create(
      profile != profile->GetOriginalProfile())));
}

WebstorePrivateIsPendingCustodianApprovalFunction::
    WebstorePrivateIsPendingCustodianApprovalFunction() = default;

WebstorePrivateIsPendingCustodianApprovalFunction::
    ~WebstorePrivateIsPendingCustodianApprovalFunction() = default;

ExtensionFunction::ResponseAction
WebstorePrivateIsPendingCustodianApprovalFunction::Run() {
  std::optional<IsPendingCustodianApproval::Params> params =
      IsPendingCustodianApproval::Params::Create(args());
  EXTENSION_FUNCTION_VALIDATE(params);

  auto* profile = Profile::FromBrowserContext(browser_context());
  if (!supervised_user::AreExtensionsPermissionsEnabled(profile)) {
    return RespondNow(BuildResponse(false));
  }
  ExtensionRegistry* registry = ExtensionRegistry::Get(browser_context());
  const Extension* extension =
      registry->GetExtensionById(params->id, ExtensionRegistry::EVERYTHING);
  if (!extension) {
    return RespondNow(BuildResponse(false));
  }

  ExtensionPrefs* extensions_prefs = ExtensionPrefs::Get(browser_context());

  if (extensions_prefs->HasDisableReason(
          params->id, disable_reason::DISABLE_PERMISSIONS_INCREASE)) {
    return RespondNow(BuildResponse(true));
  }

  bool is_pending_approval = extensions_prefs->HasDisableReason(
      params->id, disable_reason::DISABLE_CUSTODIAN_APPROVAL_REQUIRED);

  return RespondNow(BuildResponse(is_pending_approval));
}

ExtensionFunction::ResponseValue
WebstorePrivateIsPendingCustodianApprovalFunction::BuildResponse(bool result) {
  return WithArguments(result);
}

WebstorePrivateGetReferrerChainFunction::
    WebstorePrivateGetReferrerChainFunction() = default;

WebstorePrivateGetReferrerChainFunction::
    ~WebstorePrivateGetReferrerChainFunction() = default;

ExtensionFunction::ResponseAction
WebstorePrivateGetReferrerChainFunction::Run() {
#if BUILDFLAG(SAFE_BROWSING_AVAILABLE)
  Profile* profile = Profile::FromBrowserContext(browser_context());
  if (!SafeBrowsingNavigationObserverManager::IsEnabledAndReady(
          profile->GetPrefs(), g_browser_process->safe_browsing_service()))
    return RespondNow(ArgumentList(
        api::webstore_private::GetReferrerChain::Results::Create("")));

  content::RenderFrameHost* outermost_render_frame_host =
      render_frame_host() ? render_frame_host()->GetOutermostMainFrame()
                          : nullptr;

  if (!outermost_render_frame_host) {
    return RespondNow(ErrorWithArgumentsDoNotUse(
        api::webstore_private::GetReferrerChain::Results::Create(""),
        kWebstoreUserCancelledError));
  }

  SafeBrowsingNavigationObserverManager* navigation_observer_manager =
      safe_browsing::SafeBrowsingNavigationObserverManagerFactory::
          GetForBrowserContext(profile);

  safe_browsing::ReferrerChain referrer_chain;
  SafeBrowsingNavigationObserverManager::AttributionResult result =
      navigation_observer_manager->IdentifyReferrerChainByRenderFrameHost(
          outermost_render_frame_host, kExtensionReferrerUserGestureLimit,
          &referrer_chain);

  // If the referrer chain is incomplete we'll append the most recent
  // navigations to referrer chain for diagnostic purposes. This only happens if
  // the user is not in incognito mode and has opted into extended reporting or
  // Scout reporting. Otherwise, |CountOfRecentNavigationsToAppend| returns 0.
  int recent_navigations_to_collect =
      SafeBrowsingNavigationObserverManager::CountOfRecentNavigationsToAppend(
          profile, profile->GetPrefs(), result);
  if (recent_navigations_to_collect > 0) {
    navigation_observer_manager->AppendRecentNavigations(
        recent_navigations_to_collect, &referrer_chain);
  }

  safe_browsing::ExtensionWebStoreInstallRequest request;
  request.mutable_referrer_chain()->Swap(&referrer_chain);
  request.mutable_referrer_chain_options()->set_recent_navigations_to_collect(
      recent_navigations_to_collect);

  // Base64 encode the request to avoid issues with base::Value rejecting
  // strings which are not valid UTF8.
  return RespondNow(
      ArgumentList(api::webstore_private::GetReferrerChain::Results::Create(
          base::Base64Encode(request.SerializeAsString()))));
#else
  return RespondNow(ArgumentList(
      api::webstore_private::GetReferrerChain::Results::Create("")));
#endif
}

WebstorePrivateGetExtensionStatusFunction::
    WebstorePrivateGetExtensionStatusFunction() = default;
WebstorePrivateGetExtensionStatusFunction::
    ~WebstorePrivateGetExtensionStatusFunction() = default;

ExtensionFunction::ResponseAction
WebstorePrivateGetExtensionStatusFunction::Run() {
  std::optional<GetExtensionStatus::Params> params =
      GetExtensionStatus::Params::Create(args());
  EXTENSION_FUNCTION_VALIDATE(params);

  const ExtensionId& extension_id = params->id;

  if (!crx_file::id_util::IdIsValid(extension_id)) {
    return RespondNow(Error(kWebstoreInvalidIdError));
  }

  if (!params->manifest) {
    return RespondNow(BuildResponseWithoutManifest(extension_id));
  }

  data_decoder::DataDecoder::ParseJsonIsolated(
      *(params->manifest),
      base::BindOnce(
          &WebstorePrivateGetExtensionStatusFunction::OnManifestParsed, this,
          extension_id));
  return RespondLater();
}

ExtensionFunction::ResponseValue
WebstorePrivateGetExtensionStatusFunction::BuildResponseWithoutManifest(
    const ExtensionId& extension_id) {
  ExtensionInstallStatus status = GetWebstoreExtensionInstallStatus(
      extension_id, Profile::FromBrowserContext(browser_context()));
  api::webstore_private::ExtensionInstallStatus api_status =
      ConvertExtensionInstallStatusForAPI(status);
  return ArgumentList(GetExtensionStatus::Results::Create(api_status));
}

void WebstorePrivateGetExtensionStatusFunction::OnManifestParsed(
    const ExtensionId& extension_id,
    data_decoder::DataDecoder::ValueOrError result) {
  if (!result.has_value() || !result->is_dict()) {
    Respond(Error(kWebstoreInvalidManifestError));
    return;
  }

  Profile* const profile = Profile::FromBrowserContext(browser_context());
  if (!ExtensionsBrowserClient::Get()->IsValidContext(profile)) {
    Respond(Error(kWebstoreUserCancelledError));
    return;
  }

  std::string error;
  auto dummy_extension = Extension::Create(
      base::FilePath(), mojom::ManifestLocation::kInternal, result->GetDict(),
      Extension::FROM_WEBSTORE, extension_id, &error);

  if (!dummy_extension) {
    Respond(Error(kWebstoreInvalidManifestError));
    return;
  }

  ExtensionInstallStatus status = GetWebstoreExtensionInstallStatus(
      extension_id, profile, dummy_extension->GetType(),
      PermissionsParser::GetRequiredPermissions(dummy_extension.get()),
      dummy_extension->manifest_version());
  api::webstore_private::ExtensionInstallStatus api_status =
      ConvertExtensionInstallStatusForAPI(status);
  Respond(ArgumentList(GetExtensionStatus::Results::Create(api_status)));
}

WebstorePrivateGetFullChromeVersionFunction::
    WebstorePrivateGetFullChromeVersionFunction() = default;
WebstorePrivateGetFullChromeVersionFunction::
    ~WebstorePrivateGetFullChromeVersionFunction() = default;

ExtensionFunction::ResponseAction
WebstorePrivateGetFullChromeVersionFunction::Run() {
  std::string_view version = version_info::GetVersionNumber();
  GetFullChromeVersion::Results::Info info;
  info.version_number = std::string(version);
  return RespondNow(ArgumentList(GetFullChromeVersion::Results::Create(info)));
}

WebstorePrivateGetMV2DeprecationStatusFunction::
    WebstorePrivateGetMV2DeprecationStatusFunction() = default;
WebstorePrivateGetMV2DeprecationStatusFunction::
    ~WebstorePrivateGetMV2DeprecationStatusFunction() = default;

ExtensionFunction::ResponseAction
WebstorePrivateGetMV2DeprecationStatusFunction::Run() {
#if BUILDFLAG(ENABLE_EXTENSIONS)
  ManifestV2ExperimentManager* experiment_manager =
      ManifestV2ExperimentManager::Get(browser_context());
  MV2ExperimentStage current_stage =
      experiment_manager->GetCurrentExperimentStage();
  api::webstore_private::MV2DeprecationStatus api_status =
      api::webstore_private::MV2DeprecationStatus::kInactive;
  switch (current_stage) {
    case MV2ExperimentStage::kNone:
      api_status = api::webstore_private::MV2DeprecationStatus::kInactive;
      break;
    case MV2ExperimentStage::kWarning:
      api_status = api::webstore_private::MV2DeprecationStatus::kWarning;
      break;
    case MV2ExperimentStage::kDisableWithReEnable:
      api_status = api::webstore_private::MV2DeprecationStatus::kSoftDisable;
      break;
    case MV2ExperimentStage::kUnsupported:
      api_status = api::webstore_private::MV2DeprecationStatus::kHardDisable;
      break;
  }

  return RespondNow(ArgumentList(
      api::webstore_private::GetMV2DeprecationStatus::Results::Create(
          api_status)));
#else
  // Android does not support Manifest V2 experiments.
  return RespondNow(ArgumentList(
      api::webstore_private::GetMV2DeprecationStatus::Results::Create(
          api::webstore_private::MV2DeprecationStatus::kInactive)));
#endif
}

}  // namespace extensions