Ankush Singh | ee2362f | 2023-08-29 09:42:03 | [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 | |
| 5 | #include "components/sync/service/local_data_description.h" |
| 6 | |
Victor Hugo Vianna Silva | 452667a | 2024-07-18 21:57:33 | [diff] [blame] | 7 | #include <algorithm> |
| 8 | #include <ranges> |
| 9 | #include <set> |
Victor Hugo Vianna Silva | 9383548 | 2024-07-24 18:08:39 | [diff] [blame] | 10 | #include <utility> |
Victor Hugo Vianna Silva | 452667a | 2024-07-18 21:57:33 | [diff] [blame] | 11 | #include <vector> |
| 12 | |
Victor Hugo Vianna Silva | 9383548 | 2024-07-24 18:08:39 | [diff] [blame] | 13 | #include "base/i18n/message_formatter.h" |
Victor Hugo Vianna Silva | 452667a | 2024-07-18 21:57:33 | [diff] [blame] | 14 | #include "base/strings/utf_string_conversions.h" |
Victor Hugo Vianna Silva | 9383548 | 2024-07-24 18:08:39 | [diff] [blame] | 15 | #include "components/strings/grit/components_strings.h" |
Victor Hugo Vianna Silva | 452667a | 2024-07-18 21:57:33 | [diff] [blame] | 16 | #include "components/url_formatter/elide_url.h" |
Victor Hugo Vianna Silva | 9383548 | 2024-07-24 18:08:39 | [diff] [blame] | 17 | #include "ui/base/l10n/l10n_util.h" |
Victor Hugo Vianna Silva | 452667a | 2024-07-18 21:57:33 | [diff] [blame] | 18 | #include "url/gurl.h" |
| 19 | |
Mahmoud Rashad | 8bc4a6e | 2024-07-18 17:35:45 | [diff] [blame] | 20 | #if BUILDFLAG(IS_ANDROID) |
| 21 | #include "base/android/jni_array.h" |
Victor Hugo Vianna Silva | 9383548 | 2024-07-24 18:08:39 | [diff] [blame] | 22 | #include "base/android/jni_string.h" |
| 23 | |
| 24 | // Must come after all includes for JniType conversions. |
Mahmoud Rashad | 8bc4a6e | 2024-07-18 17:35:45 | [diff] [blame] | 25 | #include "components/sync/android/jni_headers/LocalDataDescription_jni.h" |
| 26 | |
| 27 | using base::android::ToJavaArrayOfStrings; |
| 28 | #endif |
| 29 | |
Ankush Singh | ee2362f | 2023-08-29 09:42:03 | [diff] [blame] | 30 | namespace syncer { |
| 31 | |
| 32 | LocalDataDescription::LocalDataDescription() = default; |
| 33 | |
Victor Hugo Vianna Silva | 452667a | 2024-07-18 21:57:33 | [diff] [blame] | 34 | LocalDataDescription::LocalDataDescription(const std::vector<GURL>& all_urls) |
| 35 | : item_count(all_urls.size()) { |
| 36 | // Using a set to get only the distinct domains. This also ensures an |
| 37 | // alphabetical ordering of the domains. |
| 38 | std::set<std::string> domain_set; |
| 39 | std::ranges::transform( |
| 40 | all_urls, std::inserter(domain_set, domain_set.end()), |
| 41 | [](const GURL& url) { |
| 42 | return base::UTF16ToUTF8( |
| 43 | url_formatter:: |
| 44 | FormatUrlForDisplayOmitSchemePathAndTrivialSubdomains(url)); |
| 45 | }); |
| 46 | domain_count = domain_set.size(); |
| 47 | // Add up to 3 domains as examples to be used in a string shown to the user. |
| 48 | std::ranges::copy_n(domain_set.begin(), |
| 49 | std::min(size_t{3}, domain_set.size()), |
| 50 | std::back_inserter(domains)); |
| 51 | } |
| 52 | |
Ankush Singh | ee2362f | 2023-08-29 09:42:03 | [diff] [blame] | 53 | LocalDataDescription::LocalDataDescription(const LocalDataDescription&) = |
| 54 | default; |
| 55 | |
| 56 | LocalDataDescription& LocalDataDescription::operator=( |
| 57 | const LocalDataDescription&) = default; |
| 58 | |
| 59 | LocalDataDescription::LocalDataDescription(LocalDataDescription&&) = default; |
| 60 | |
| 61 | LocalDataDescription& LocalDataDescription::operator=(LocalDataDescription&&) = |
| 62 | default; |
| 63 | |
| 64 | LocalDataDescription::~LocalDataDescription() = default; |
| 65 | |
Victor Hugo Vianna Silva | 9383548 | 2024-07-24 18:08:39 | [diff] [blame] | 66 | std::u16string GetDomainsDisplayText(const LocalDataDescription& description) { |
| 67 | CHECK_GT(description.domains.size(), 0u); |
| 68 | if (description.domains.size() == 1) { |
| 69 | return base::i18n::MessageFormatter::FormatWithNamedArgs( |
| 70 | l10n_util::GetStringUTF16(IDS_BULK_UPLOAD_SUBTITLE), "count", |
| 71 | static_cast<int>(description.domain_count), "website_1", |
| 72 | description.domains[0], "more_count", |
| 73 | static_cast<int>(description.domain_count - 1)); |
| 74 | } |
| 75 | return base::i18n::MessageFormatter::FormatWithNamedArgs( |
| 76 | l10n_util::GetStringUTF16(IDS_BULK_UPLOAD_SUBTITLE), "count", |
| 77 | static_cast<int>(description.domain_count), "website_1", |
| 78 | description.domains[0], "website_2", description.domains[1], "more_count", |
| 79 | static_cast<int>(description.domain_count - 2)); |
| 80 | } |
| 81 | |
Ankush Singh | 05d8b39e | 2023-09-08 13:13:35 | [diff] [blame] | 82 | void PrintTo(const LocalDataDescription& desc, std::ostream* os) { |
Victor Hugo Vianna Silva | a408c27 | 2024-07-15 14:57:13 | [diff] [blame] | 83 | *os << "{ item_count:" << desc.item_count << ", domains:["; |
Ankush Singh | 05d8b39e | 2023-09-08 13:13:35 | [diff] [blame] | 84 | for (const auto& domain : desc.domains) { |
| 85 | *os << domain << ","; |
| 86 | } |
| 87 | *os << "], domain_count:" << desc.domain_count; |
| 88 | } |
| 89 | |
Mahmoud Rashad | 8bc4a6e | 2024-07-18 17:35:45 | [diff] [blame] | 90 | #if BUILDFLAG(IS_ANDROID) |
| 91 | base::android::ScopedJavaLocalRef<jobject> ConvertToJavaLocalDataDescription( |
| 92 | JNIEnv* env, |
| 93 | const LocalDataDescription& local_data_description) { |
| 94 | return Java_LocalDataDescription_Constructor( |
| 95 | env, local_data_description.item_count, |
| 96 | base::android::ToJavaArrayOfStrings(env, local_data_description.domains), |
| 97 | local_data_description.domain_count); |
| 98 | } |
Victor Hugo Vianna Silva | 9383548 | 2024-07-24 18:08:39 | [diff] [blame] | 99 | |
| 100 | std::u16string JNI_LocalDataDescription_GetDomainsDisplayText( |
| 101 | JNIEnv* env, |
| 102 | int item_count, |
| 103 | std::vector<std::string>& domains, |
| 104 | int domain_count) { |
Victor Hugo Vianna Silva | df43047 | 2024-07-24 18:17:38 | [diff] [blame^] | 105 | LocalDataDescription description; |
Victor Hugo Vianna Silva | 9383548 | 2024-07-24 18:08:39 | [diff] [blame] | 106 | description.item_count = item_count; |
| 107 | description.domains = std::move(domains); |
| 108 | description.domain_count = domain_count; |
Victor Hugo Vianna Silva | df43047 | 2024-07-24 18:17:38 | [diff] [blame^] | 109 | return GetDomainsDisplayText(std::move(description)); |
Victor Hugo Vianna Silva | 9383548 | 2024-07-24 18:08:39 | [diff] [blame] | 110 | } |
Mahmoud Rashad | 8bc4a6e | 2024-07-18 17:35:45 | [diff] [blame] | 111 | #endif |
| 112 | |
Ankush Singh | ee2362f | 2023-08-29 09:42:03 | [diff] [blame] | 113 | } // namespace syncer |