From e373bc574c40510a05880bcf95d7bb18e952def6 Mon Sep 17 00:00:00 2001 From: Simon Hong Date: Tue, 26 Jan 2021 14:37:13 +0900 Subject: [PATCH] Launch first run dialog on macOS Fix https://github.com/brave/brave-browser/issues/12679 --- app/brave_generated_resources.grd | 6 ++ .../first_run/first_run_internal_posix.cc | 39 +++++++++++++ .../browser/ui/cocoa/first_run_dialog.mm | 55 +++++++++++++++++++ .../ui/cocoa/first_run_dialog_controller.mm | 17 ++++++ 4 files changed, 117 insertions(+) create mode 100644 chromium_src/chrome/browser/first_run/first_run_internal_posix.cc create mode 100644 chromium_src/chrome/browser/ui/cocoa/first_run_dialog.mm create mode 100644 chromium_src/chrome/browser/ui/cocoa/first_run_dialog_controller.mm diff --git a/app/brave_generated_resources.grd b/app/brave_generated_resources.grd index 77250048b2ea..57689205f11c 100644 --- a/app/brave_generated_resources.grd +++ b/app/brave_generated_resources.grd @@ -1033,6 +1033,12 @@ By installing this extension, you are agreeing to the Google Widevine Terms of U Reset Brave Rewards data... + + + + Help improve $1Brave by sending crash reports and completely anonymised, private product analytics. + + diff --git a/chromium_src/chrome/browser/first_run/first_run_internal_posix.cc b/chromium_src/chrome/browser/first_run/first_run_internal_posix.cc new file mode 100644 index 000000000000..453cc52646b3 --- /dev/null +++ b/chromium_src/chrome/browser/first_run/first_run_internal_posix.cc @@ -0,0 +1,39 @@ +/* Copyright (c) 2020 The Brave Authors. All rights reserved. + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#include "chrome/browser/first_run/first_run_internal.h" + +#include "base/files/file_path.h" +#include "base/files/file_util.h" +#include "base/no_destructor.h" +#include "base/path_service.h" +#include "build/branding_buildflags.h" +#include "build/build_config.h" +#include "chrome/browser/browser_process.h" +#include "chrome/browser/first_run/first_run.h" +#include "chrome/browser/first_run/first_run_dialog.h" +#include "chrome/browser/metrics/metrics_reporting_state.h" +#include "chrome/common/chrome_constants.h" +#include "chrome/common/chrome_paths.h" +#include "chrome/common/chrome_switches.h" +#include "chrome/installer/util/google_update_settings.h" +#include "chrome/installer/util/initial_preferences.h" +#include "components/metrics/metrics_pref_names.h" +#include "components/metrics/metrics_reporting_default_state.h" +#include "components/prefs/pref_service.h" +#include "components/startup_metric_utils/browser/startup_metric_utils.h" + +// All above headers copied from original first_run_internal_posix.cc are +// included to prevent below GOOGLE_CHROEM_BUILD affect them. + +// First run dialog is only enabled for google chrome by default. +#if defined(OFFICIAL_BUILD) && defined(OS_MAC) +#undef BUILDFLAG_INTERNAL_GOOGLE_CHROME_BRANDING +#define BUILDFLAG_INTERNAL_GOOGLE_CHROME_BRANDING() (1) +#endif +#include "../../../../../chrome/browser/first_run/first_run_internal_posix.cc" +#if defined(OFFICIAL_BUILD) && defined(OS_MAC) +#undef BUILDFLAG_INTERNAL_GOOGLE_CHROME_BRANDING +#endif diff --git a/chromium_src/chrome/browser/ui/cocoa/first_run_dialog.mm b/chromium_src/chrome/browser/ui/cocoa/first_run_dialog.mm new file mode 100644 index 000000000000..c86fa73132b5 --- /dev/null +++ b/chromium_src/chrome/browser/ui/cocoa/first_run_dialog.mm @@ -0,0 +1,55 @@ +/* Copyright (c) 2020 The Brave Authors. All rights reserved. + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#include "brave/components/p3a/buildflags.h" +#include "chrome/browser/browser_process.h" +#include "components/prefs/pref_service.h" + +#if BUILDFLAG(BRAVE_P3A_ENABLED) +#include "brave/components/p3a/pref_names.h" +#endif + +#define ShowFirstRunDialog ShowFirstRunDialog_UnUsed +#include "../../../../../../chrome/browser/ui/cocoa/first_run_dialog.mm" +#undef ShowFirstRunDialog + +namespace { + +// Copied ShowFirstRunModal from upstream and p3a prefs part added. +void ShowFirstRunModalBrave(Profile* profile) { + base::scoped_nsobject dialog( + [[FirstRunDialogController alloc] init]); + + [dialog.get() showWindow:nil]; + + // If the dialog asked the user to opt-in for stats and crash reporting, + // record the decision and enable the crash reporter if appropriate. + bool consent_given = [dialog.get() isStatsReportingEnabled]; + ChangeMetricsReportingState(consent_given); + +#if BUILDFLAG(BRAVE_P3A_ENABLED) + PrefService* local_state = g_browser_process->local_state(); + local_state->SetBoolean(brave::kP3AEnabled, consent_given); +#endif + + // If selected, set as default browser. Skip in automated tests so that an OS + // dialog confirming the default browser choice isn't left on screen. + BOOL make_default_browser = + [dialog.get() isMakeDefaultBrowserEnabled] && + !base::CommandLine::ForCurrentProcess()->HasSwitch(switches::kTestType); + if (make_default_browser) { + bool success = shell_integration::SetAsDefaultBrowser(); + DCHECK(success); + } +} +} // namespace + +namespace first_run { + +void ShowFirstRunDialog(Profile* profile) { + ShowFirstRunModalBrave(profile); +} + +} // namespace first_run diff --git a/chromium_src/chrome/browser/ui/cocoa/first_run_dialog_controller.mm b/chromium_src/chrome/browser/ui/cocoa/first_run_dialog_controller.mm new file mode 100644 index 000000000000..bb6900f9b83e --- /dev/null +++ b/chromium_src/chrome/browser/ui/cocoa/first_run_dialog_controller.mm @@ -0,0 +1,17 @@ +/* Copyright (c) 2020 The Brave Authors. All rights reserved. + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#include "brave/grit/brave_generated_resources.h" +#include "chrome/grit/generated_resources.h" + +// Replaced string here instead of by running 'npm run chromium_rebase_l10n' +// because string replacement failed with string that include place holder like +// $1Brave. I assume this include some +// special charactors. +#undef IDS_FIRSTRUN_DLG_MAC_OPTIONS_SEND_USAGE_STATS_LABEL +#define IDS_FIRSTRUN_DLG_MAC_OPTIONS_SEND_USAGE_STATS_LABEL \ + IDS_FIRSTRUN_DLG_MAC_OPTIONS_SEND_USAGE_STATS_LABEL_BRAVE +#include "../../../../../../chrome/browser/ui/cocoa/first_run_dialog_controller.mm" +#undef IDS_FIRSTRUN_DLG_MAC_OPTIONS_SEND_USAGE_STATS_LABEL