Skip to content

Commit 880ade2

Browse files
committed
Launch what's new page at major version update
fix brave/brave-browser#28947
1 parent 0c53ce4 commit 880ade2

File tree

6 files changed

+202
-0
lines changed

6 files changed

+202
-0
lines changed

browser/brave_local_state_prefs.cc

+2
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646

4747
#if !BUILDFLAG(IS_ANDROID)
4848
#include "brave/browser/p3a/p3a_core_metrics.h"
49+
#include "brave/browser/ui/whats_new/whats_new_util.h"
4950
#include "chrome/browser/first_run/first_run.h"
5051
#endif // !BUILDFLAG(IS_ANDROID)
5152

@@ -107,6 +108,7 @@ void RegisterLocalStatePrefs(PrefRegistrySimple* registry) {
107108
BraveWindowTracker::RegisterPrefs(registry);
108109
BraveUptimeTracker::RegisterPrefs(registry);
109110
dark_mode::RegisterBraveDarkModeLocalStatePrefs(registry);
111+
whats_new::RegisterLocalStatePrefs(registry);
110112
#endif
111113

112114
#if BUILDFLAG(ENABLE_CRASH_DIALOG)

browser/ui/BUILD.gn

+3
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,9 @@ source_set("ui") {
187187
"webui/welcome_page/brave_welcome_ui.h",
188188
"webui/welcome_page/welcome_dom_handler.cc",
189189
"webui/welcome_page/welcome_dom_handler.h",
190+
"whats_new/pref_names.h",
191+
"whats_new/whats_new_util.cc",
192+
"whats_new/whats_new_util.h",
190193
]
191194

192195
if (enable_pin_shortcut) {

browser/ui/whats_new/pref_names.h

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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+
#ifndef BRAVE_BROWSER_UI_WHATS_NEW_PREF_NAMES_H_
7+
#define BRAVE_BROWSER_UI_WHATS_NEW_PREF_NAMES_H_
8+
9+
namespace whats_new::prefs {
10+
11+
// Store lastly shown whats-new version - major version.
12+
// Ex, "1.50" means browser launched whats-new page for v1.50 update already.
13+
constexpr char kWhatsNewLastVersion[] = "brave.whats_new.last_version";
14+
15+
} // namespace whats_new::prefs
16+
17+
#endif // BRAVE_BROWSER_UI_WHATS_NEW_PREF_NAMES_H_
+129
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
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+
&current_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

browser/ui/whats_new/whats_new_util.h

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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+
#ifndef BRAVE_BROWSER_UI_WHATS_NEW_WHATS_NEW_UTIL_H_
7+
#define BRAVE_BROWSER_UI_WHATS_NEW_WHATS_NEW_UTIL_H_
8+
9+
class PrefRegistrySimple;
10+
class PrefService;
11+
class Browser;
12+
13+
namespace whats_new {
14+
15+
// Returns true when we want to show whats-new page in foreground tab.
16+
bool ShouldShowBraveWhatsNewForState(PrefService* local_state);
17+
void RegisterLocalStatePrefs(PrefRegistrySimple* registry);
18+
void StartBraveWhatsNew(Browser* browser);
19+
20+
} // namespace whats_new
21+
22+
#endif // BRAVE_BROWSER_UI_WHATS_NEW_WHATS_NEW_UTIL_H_
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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 "chrome/browser/ui/webui/whats_new/whats_new_util.h"
7+
8+
#include "brave/browser/ui/whats_new/whats_new_util.h"
9+
10+
#define ShouldShowForState ShouldShowForState_UnUsed
11+
#define StartWhatsNewFetch StartWhatsNewFetch_UnUsed
12+
13+
#include "src/chrome/browser/ui/webui/whats_new/whats_new_util.cc"
14+
15+
#undef StartWhatsNewFetch
16+
#undef ShouldShowForState
17+
18+
namespace whats_new {
19+
20+
bool ShouldShowForState(PrefService* local_state,
21+
bool promotional_tabs_enabled) {
22+
return ShouldShowBraveWhatsNewForState(local_state);
23+
}
24+
25+
void StartWhatsNewFetch(Browser* browser) {
26+
StartBraveWhatsNew(browser);
27+
}
28+
29+
} // namespace whats_new

0 commit comments

Comments
 (0)