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