sanjeetnd | 54f6f9f4 | 2023-08-23 17:33:34 | [diff] [blame] | 1 | // Copyright 2023 The Chromium Authors |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Jeffrey Young | 593d591 | 2023-10-17 15:18:27 | [diff] [blame] | 5 | #include "components/manta/manta_service.h" |
sanjeetnd | 54f6f9f4 | 2023-08-23 17:33:34 | [diff] [blame] | 6 | |
| 7 | #include <memory> |
| 8 | |
Chuong Ho | 30c8ef3 | 2024-05-10 17:35:12 | [diff] [blame] | 9 | #include "base/containers/contains.h" |
| 10 | #include "base/containers/fixed_flat_set.h" |
Jeffrey Young | f6ef890 | 2023-10-11 19:39:31 | [diff] [blame] | 11 | #include "base/memory/scoped_refptr.h" |
Xinglong Luan | 04d05f1 | 2024-04-08 00:23:56 | [diff] [blame] | 12 | #include "build/chromeos_buildflags.h" |
Chuong Ho | 9a1e5fb | 2024-03-11 11:30:35 | [diff] [blame] | 13 | #include "components/account_id/account_id.h" |
Jacob Francis | e38e296 | 2024-06-11 02:49:34 | [diff] [blame^] | 14 | #include "components/manta/anchovy/anchovy_provider.h" |
Lauren Commeignes | bf6f8f9 | 2024-06-07 06:26:45 | [diff] [blame] | 15 | #include "components/manta/sparky/system_info_delegate.h" |
Chuong Ho | 9a1e5fb | 2024-03-11 11:30:35 | [diff] [blame] | 16 | #include "components/signin/public/identity_manager/account_capabilities.h" |
sanjeetnd | 54f6f9f4 | 2023-08-23 17:33:34 | [diff] [blame] | 17 | #include "components/signin/public/identity_manager/identity_manager.h" |
Chuong Ho | 9a1e5fb | 2024-03-11 11:30:35 | [diff] [blame] | 18 | #include "components/signin/public/identity_manager/tribool.h" |
Jeffrey Young | f6ef890 | 2023-10-11 19:39:31 | [diff] [blame] | 19 | #include "services/network/public/cpp/shared_url_loader_factory.h" |
sanjeetnd | 54f6f9f4 | 2023-08-23 17:33:34 | [diff] [blame] | 20 | |
Xinglong Luan | 93d670f | 2024-04-12 02:30:49 | [diff] [blame] | 21 | #if BUILDFLAG(IS_CHROMEOS_ASH) |
| 22 | #include "chromeos/constants/chromeos_features.h" // nogncheck |
| 23 | #include "components/manta/mahi_provider.h" |
| 24 | #include "components/manta/orca_provider.h" |
| 25 | #include "components/manta/snapper_provider.h" |
Lauren Commeignes | a5748c19 | 2024-05-07 06:51:44 | [diff] [blame] | 26 | #include "components/manta/sparky/sparky_provider.h" |
Xinglong Luan | 93d670f | 2024-04-12 02:30:49 | [diff] [blame] | 27 | #endif // BUILDFLAG(IS_CHROMEOS_ASH) |
| 28 | |
sanjeetnd | 54f6f9f4 | 2023-08-23 17:33:34 | [diff] [blame] | 29 | namespace manta { |
| 30 | |
Chuong Ho | 9a1e5fb | 2024-03-11 11:30:35 | [diff] [blame] | 31 | namespace { |
| 32 | |
| 33 | FeatureSupportStatus ConvertToMantaFeatureSupportStatus(signin::Tribool value) { |
| 34 | switch (value) { |
| 35 | case signin::Tribool::kUnknown: |
| 36 | return FeatureSupportStatus::kUnknown; |
| 37 | case signin::Tribool::kTrue: |
| 38 | return FeatureSupportStatus::kSupported; |
| 39 | case signin::Tribool::kFalse: |
| 40 | return FeatureSupportStatus::kUnsupported; |
| 41 | } |
| 42 | } |
| 43 | |
Chuong Ho | 30c8ef3 | 2024-05-10 17:35:12 | [diff] [blame] | 44 | #if BUILDFLAG(IS_CHROMEOS_ASH) |
| 45 | |
| 46 | constexpr auto kAllowedLanguagesForAddingLocaleToRequest = |
| 47 | base::MakeFixedFlatSet<std::string_view>({"de", "en", "en-GB", "fr", "ja"}); |
| 48 | |
| 49 | bool ShouldIncludeLocaleInRequest(std::string_view locale) { |
| 50 | return chromeos::features::IsOrcaUseL10nStringsEnabled() || |
| 51 | (chromeos::features::IsOrcaInternationalizeEnabled() && |
| 52 | base::Contains(kAllowedLanguagesForAddingLocaleToRequest, locale)); |
| 53 | } |
| 54 | |
| 55 | #endif // BUILDFLAG(IS_CHROMEOS_ASH) |
| 56 | |
Chuong Ho | 9a1e5fb | 2024-03-11 11:30:35 | [diff] [blame] | 57 | } // namespace |
| 58 | |
Jeffrey Young | f6ef890 | 2023-10-11 19:39:31 | [diff] [blame] | 59 | MantaService::MantaService( |
| 60 | scoped_refptr<network::SharedURLLoaderFactory> shared_url_loader_factory, |
Xinglong Luan | 013939e | 2024-04-02 05:06:10 | [diff] [blame] | 61 | signin::IdentityManager* identity_manager, |
Xinglong Luan | 04d05f1 | 2024-04-08 00:23:56 | [diff] [blame] | 62 | bool is_demo_mode, |
Jacob Francis | 65df707 | 2024-05-22 19:08:29 | [diff] [blame] | 63 | bool is_otr_profile, |
Xinglong Luan | 93d670f | 2024-04-12 02:30:49 | [diff] [blame] | 64 | const std::string& chrome_version, |
| 65 | const std::string& locale) |
Jeffrey Young | f6ef890 | 2023-10-11 19:39:31 | [diff] [blame] | 66 | : shared_url_loader_factory_(shared_url_loader_factory), |
Xinglong Luan | 013939e | 2024-04-02 05:06:10 | [diff] [blame] | 67 | identity_manager_(identity_manager), |
Xinglong Luan | 04d05f1 | 2024-04-08 00:23:56 | [diff] [blame] | 68 | is_demo_mode_(is_demo_mode), |
Jacob Francis | 65df707 | 2024-05-22 19:08:29 | [diff] [blame] | 69 | is_otr_profile_(is_otr_profile), |
Xinglong Luan | 93d670f | 2024-04-12 02:30:49 | [diff] [blame] | 70 | chrome_version_(chrome_version), |
| 71 | locale_(locale) {} |
Jeffrey Young | f6ef890 | 2023-10-11 19:39:31 | [diff] [blame] | 72 | |
| 73 | MantaService::~MantaService() = default; |
sanjeetnd | 54f6f9f4 | 2023-08-23 17:33:34 | [diff] [blame] | 74 | |
Chuong Ho | 9a1e5fb | 2024-03-11 11:30:35 | [diff] [blame] | 75 | FeatureSupportStatus MantaService::SupportsOrca() { |
Xinglong Luan | 013939e | 2024-04-02 05:06:10 | [diff] [blame] | 76 | if (is_demo_mode_) { |
| 77 | return FeatureSupportStatus::kSupported; |
| 78 | } |
Erica Lee | 3723167 | 2024-05-23 18:15:42 | [diff] [blame] | 79 | return CanAccessMantaFeaturesWithoutMinorRestrictions(); |
| 80 | } |
Xinglong Luan | 013939e | 2024-04-02 05:06:10 | [diff] [blame] | 81 | |
Erica Lee | 3723167 | 2024-05-23 18:15:42 | [diff] [blame] | 82 | FeatureSupportStatus |
| 83 | MantaService::CanAccessMantaFeaturesWithoutMinorRestrictions() { |
Chuong Ho | 9a1e5fb | 2024-03-11 11:30:35 | [diff] [blame] | 84 | if (identity_manager_ == nullptr) { |
| 85 | return FeatureSupportStatus::kUnknown; |
| 86 | } |
| 87 | |
| 88 | const auto account_id = |
| 89 | identity_manager_->GetPrimaryAccountId(signin::ConsentLevel::kSignin); |
| 90 | |
| 91 | if (account_id.empty()) { |
| 92 | return FeatureSupportStatus::kUnknown; |
| 93 | } |
| 94 | |
| 95 | const AccountInfo extended_account_info = |
| 96 | identity_manager_->FindExtendedAccountInfoByAccountId(account_id); |
| 97 | |
Erica Lee | 3723167 | 2024-05-23 18:15:42 | [diff] [blame] | 98 | // Fetches and uses the shared account capability for manta service. |
Chuong Ho | 9a1e5fb | 2024-03-11 11:30:35 | [diff] [blame] | 99 | return ConvertToMantaFeatureSupportStatus( |
| 100 | extended_account_info.capabilities.can_use_manta_service()); |
| 101 | } |
| 102 | |
Jacob Francis | 65df707 | 2024-05-22 19:08:29 | [diff] [blame] | 103 | std::unique_ptr<AnchovyProvider> MantaService::CreateAnchovyProvider() { |
| 104 | if (!identity_manager_) { |
| 105 | // Anchovy Provider supports API Key Requests. |
| 106 | return std::make_unique<AnchovyProvider>(shared_url_loader_factory_, |
| 107 | nullptr, is_otr_profile_, |
| 108 | chrome_version_, locale_); |
| 109 | } |
| 110 | return std::make_unique<AnchovyProvider>(shared_url_loader_factory_, |
| 111 | identity_manager_, is_otr_profile_, |
| 112 | chrome_version_, locale_); |
| 113 | } |
| 114 | |
Andrew Moylan | 22a6d8f1 | 2024-03-20 12:16:05 | [diff] [blame] | 115 | #if BUILDFLAG(IS_CHROMEOS_ASH) |
| 116 | |
Xinglong Luan | acb2f9b | 2023-09-05 23:47:13 | [diff] [blame] | 117 | std::unique_ptr<OrcaProvider> MantaService::CreateOrcaProvider() { |
Jeffrey Young | f6ef890 | 2023-10-11 19:39:31 | [diff] [blame] | 118 | if (!identity_manager_) { |
| 119 | return nullptr; |
| 120 | } |
Xinglong Luan | 93d670f | 2024-04-12 02:30:49 | [diff] [blame] | 121 | return std::make_unique<OrcaProvider>( |
| 122 | shared_url_loader_factory_, identity_manager_, is_demo_mode_, |
| 123 | chrome_version_, |
| 124 | /*locale=*/ |
Chuong Ho | 30c8ef3 | 2024-05-10 17:35:12 | [diff] [blame] | 125 | ShouldIncludeLocaleInRequest(locale_) ? locale_ : std::string()); |
Xinglong Luan | acb2f9b | 2023-09-05 23:47:13 | [diff] [blame] | 126 | } |
| 127 | |
sanjeetnd | 54f6f9f4 | 2023-08-23 17:33:34 | [diff] [blame] | 128 | std::unique_ptr<SnapperProvider> MantaService::CreateSnapperProvider() { |
Jeffrey Young | f6ef890 | 2023-10-11 19:39:31 | [diff] [blame] | 129 | if (!identity_manager_) { |
| 130 | return nullptr; |
| 131 | } |
| 132 | return std::make_unique<SnapperProvider>(shared_url_loader_factory_, |
Xinglong Luan | 04d05f1 | 2024-04-08 00:23:56 | [diff] [blame] | 133 | identity_manager_, is_demo_mode_, |
| 134 | chrome_version_); |
Jeffrey Young | f6ef890 | 2023-10-11 19:39:31 | [diff] [blame] | 135 | } |
sanjeetnd | 54f6f9f4 | 2023-08-23 17:33:34 | [diff] [blame] | 136 | |
Xinglong Luan | dfad413 | 2024-02-25 23:02:56 | [diff] [blame] | 137 | std::unique_ptr<MahiProvider> MantaService::CreateMahiProvider() { |
| 138 | if (!identity_manager_) { |
| 139 | return nullptr; |
| 140 | } |
| 141 | return std::make_unique<MahiProvider>(shared_url_loader_factory_, |
Xinglong Luan | 04d05f1 | 2024-04-08 00:23:56 | [diff] [blame] | 142 | identity_manager_, is_demo_mode_, |
| 143 | chrome_version_); |
Xinglong Luan | dfad413 | 2024-02-25 23:02:56 | [diff] [blame] | 144 | } |
| 145 | |
Lauren Commeignes | a5748c19 | 2024-05-07 06:51:44 | [diff] [blame] | 146 | std::unique_ptr<SparkyProvider> MantaService::CreateSparkyProvider( |
Lauren Commeignes | bf6f8f9 | 2024-06-07 06:26:45 | [diff] [blame] | 147 | std::unique_ptr<SparkyDelegate> sparky_delegate, |
| 148 | std::unique_ptr<SystemInfoDelegate> system_info_delegate) { |
| 149 | if (!identity_manager_ || !sparky_delegate || !system_info_delegate) { |
Lauren Commeignes | a5748c19 | 2024-05-07 06:51:44 | [diff] [blame] | 150 | return nullptr; |
| 151 | } |
| 152 | return std::make_unique<SparkyProvider>( |
| 153 | shared_url_loader_factory_, identity_manager_, is_demo_mode_, |
Lauren Commeignes | bf6f8f9 | 2024-06-07 06:26:45 | [diff] [blame] | 154 | chrome_version_, std::move(sparky_delegate), |
| 155 | std::move(system_info_delegate)); |
Lauren Commeignes | a5748c19 | 2024-05-07 06:51:44 | [diff] [blame] | 156 | } |
| 157 | |
Andrew Moylan | 22a6d8f1 | 2024-03-20 12:16:05 | [diff] [blame] | 158 | #endif // BUILDFLAG(IS_CHROMEOS_ASH) |
| 159 | |
Jeffrey Young | f6ef890 | 2023-10-11 19:39:31 | [diff] [blame] | 160 | void MantaService::Shutdown() { |
| 161 | identity_manager_ = nullptr; |
sanjeetnd | 54f6f9f4 | 2023-08-23 17:33:34 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | } // namespace manta |