|
| 1 | +/* Copyright (c) 2023 The Brave Authors. All rights reserved. |
| 2 | + * This Source Code Form is subject to the terms of the Mozilla Public |
| 3 | + * License, v. 2.0. If a copy of the MPL was not distributed with this file, |
| 4 | + * You can obtain one at https://mozilla.org/MPL/2.0/. */ |
| 5 | + |
| 6 | +#include "brave/browser/ui/whats_new/whats_new_util.h" |
| 7 | + |
| 8 | +#include <string> |
| 9 | + |
| 10 | +#include "base/metrics/field_trial_params.h" |
| 11 | +#include "base/ranges/algorithm.h" |
| 12 | +#include "base/strings/string_number_conversions.h" |
| 13 | +#include "base/strings/stringprintf.h" |
| 14 | +#include "base/version.h" |
| 15 | +#include "brave/browser/ui/whats_new/pref_names.h" |
| 16 | +#include "brave/components/l10n/common/locale_util.h" |
| 17 | +#include "chrome/browser/ui/browser.h" |
| 18 | +#include "chrome/browser/ui/browser_tabstrip.h" |
| 19 | +#include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 20 | +#include "components/prefs/pref_registry_simple.h" |
| 21 | +#include "components/prefs/pref_service.h" |
| 22 | +#include "components/version_info/version_info.h" |
| 23 | +#include "url/gurl.h" |
| 24 | + |
| 25 | +namespace { |
| 26 | + |
| 27 | +constexpr char kWhatsNewTrial[] = "WhatsNewStudy"; |
| 28 | +constexpr char kWhatsNewTargetMajorVersion[] = "target_major_version"; |
| 29 | + |
| 30 | +absl::optional<double> GetTargetMajorVersion() { |
| 31 | + const std::string target_major_version_string = base::GetFieldTrialParamValue( |
| 32 | + kWhatsNewTrial, kWhatsNewTargetMajorVersion); |
| 33 | + // Field trial doesn't have this value. |
| 34 | + if (target_major_version_string.empty()) { |
| 35 | + return absl::nullopt; |
| 36 | + } |
| 37 | + |
| 38 | + double target_major_version; |
| 39 | + if (!base::StringToDouble(target_major_version_string, |
| 40 | + &target_major_version)) { |
| 41 | + return absl::nullopt; |
| 42 | + } |
| 43 | + |
| 44 | + return target_major_version; |
| 45 | +} |
| 46 | + |
| 47 | +// Returns 1.xx or 2.xx as double. |
| 48 | +absl::optional<double> GetCurrentBrowserVersion() { |
| 49 | + const auto& version = version_info::GetVersion(); |
| 50 | + DCHECK(version.IsValid()); |
| 51 | + DCHECK_EQ(version.components().size(), 4ul); |
| 52 | + |
| 53 | + // |version| has four components like 111.1.51.34. |
| 54 | + // First one is upstream's major version. |
| 55 | + // Brave's major version is second and third component - 1.51 |
| 56 | + // Ignored fourth number as its build number. |
| 57 | + double current_version; |
| 58 | + if (!base::StringToDouble(base::StringPrintf("%d.%d", version.components()[1], |
| 59 | + version.components()[2]), |
| 60 | + ¤t_version)) { |
| 61 | + return absl::nullopt; |
| 62 | + } |
| 63 | + |
| 64 | + return current_version; |
| 65 | +} |
| 66 | + |
| 67 | +} // namespace |
| 68 | + |
| 69 | +namespace whats_new { |
| 70 | + |
| 71 | +bool ShouldShowBraveWhatsNewForState(PrefService* local_state) { |
| 72 | + // Supported languages/translations for the What's New page are: |
| 73 | + // Simplified Chinese, French, German, Japanese, Korean, Portuguese, and |
| 74 | + // Spanish. |
| 75 | + constexpr std::array<const char*, 8> kSupportedLanguages = { |
| 76 | + "en", "zh", "fr", "de", "ja", "ko", "pt", "es"}; |
| 77 | + if (base::ranges::find(kSupportedLanguages, |
| 78 | + brave_l10n::GetDefaultISOLanguageCodeString()) == |
| 79 | + std::end(kSupportedLanguages)) { |
| 80 | + VLOG(2) << __func__ << " Not supported language - " |
| 81 | + << brave_l10n::GetDefaultISOLanguageCodeString(); |
| 82 | + return false; |
| 83 | + } |
| 84 | + |
| 85 | + auto target_major_version = GetTargetMajorVersion(); |
| 86 | + // false if whatsnew is not supported in this country. |
| 87 | + if (!target_major_version) { |
| 88 | + VLOG(2) << __func__ << " Field trial doesn't have target_version"; |
| 89 | + return false; |
| 90 | + } |
| 91 | + |
| 92 | + const auto current_version = GetCurrentBrowserVersion(); |
| 93 | + if (!current_version) { |
| 94 | + return false; |
| 95 | + } |
| 96 | + |
| 97 | + if (*current_version != *target_major_version) { |
| 98 | + VLOG(2) << __func__ << " Current version is different with target version"; |
| 99 | + return false; |
| 100 | + } |
| 101 | + |
| 102 | + // Already shown whatsnew. |
| 103 | + const double last_version = |
| 104 | + local_state->GetDouble(prefs::kWhatsNewLastVersion); |
| 105 | + if (last_version == *target_major_version) { |
| 106 | + VLOG(2) << __func__ << " Already shown for " << *target_major_version; |
| 107 | + return false; |
| 108 | + } |
| 109 | + |
| 110 | + // Set the last version here to indicate that What's New should not attempt |
| 111 | + // to display again for this milestone. This prevents the page from |
| 112 | + // potentially displaying multiple times in a given milestone, e.g. for |
| 113 | + // multiple profile relaunches (see https://crbug.com/1274313). |
| 114 | + local_state->SetDouble(prefs::kWhatsNewLastVersion, *target_major_version); |
| 115 | + return true; |
| 116 | +} |
| 117 | + |
| 118 | +void RegisterLocalStatePrefs(PrefRegistrySimple* registry) { |
| 119 | + registry->RegisterDoublePref(prefs::kWhatsNewLastVersion, 0); |
| 120 | +} |
| 121 | + |
| 122 | +void StartBraveWhatsNew(Browser* browser) { |
| 123 | + constexpr char kBraveWhatsNewURL[] = "https://brave.com/whats-new/"; |
| 124 | + chrome::AddTabAt(browser, GURL(kBraveWhatsNewURL), 0, true); |
| 125 | + browser->tab_strip_model()->ActivateTabAt( |
| 126 | + browser->tab_strip_model()->IndexOfFirstNonPinnedTab()); |
| 127 | +} |
| 128 | + |
| 129 | +} // namespace whats_new |
0 commit comments