Skip to content

Commit

Permalink
Merge pull request #5804 from brave/sync-v2-infobar
Browse files Browse the repository at this point in the history
[sync-v2 base] Sync: Show an infobar at startup for sync v1 users
  • Loading branch information
darkdh authored Jun 11, 2020
2 parents b5f41a3 + 1467ef7 commit 9ce2485
Show file tree
Hide file tree
Showing 10 changed files with 229 additions and 2 deletions.
7 changes: 7 additions & 0 deletions app/brave_generated_resources.grd
Original file line number Diff line number Diff line change
Expand Up @@ -583,6 +583,13 @@ By installing this extension, you are agreeing to the Google Widevine Terms of U
<message name="IDS_BRAVE_SYNC_RESET_CONFIRMATION" desc="Confirmation message prompt to make sure the user is ok with leaving sync">
If you reset Sync, you will have to re-enter a sync code from another device in order to sync with it. Are you sure you want to proceed?
</message>
<!-- Sync Infobar -->
<message name="IDS_BRAVE_SYNC_V2_MIGRATE_INFOBAR_MESSAGE" desc="Message text shown to users who used a previous version of Brave Sync and need to perform setup again for the new version.">
Brave Sync has been upgraded and requires setup
</message>
<message name="IDS_BRAVE_SYNC_V2_MIGRATE_INFOBAR_COMMAND" desc="Command text shown to users who used a previous version of Brave Sync and need to perform setup again for the new version.">
Begin Setup
</message>
<!-- Help Tips Page -->
<message name="IDS_SETTINGS_HELP_TIPS" desc="Title for an item in the 'Help Tips' section of Brave Settings">
Help Tips
Expand Down
18 changes: 18 additions & 0 deletions browser/brave_browser_main_parts.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
#if !defined(OS_ANDROID)
#include "brave/browser/infobars/brave_confirm_p3a_infobar_delegate.h"
#include "brave/browser/infobars/crypto_wallets_infobar_delegate.h"
#include "brave/components/brave_sync/buildflags/buildflags.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_finder.h"
#include "chrome/browser/infobars/infobar_service.h"
#include "content/public/browser/web_contents.h"
Expand All @@ -39,6 +41,13 @@
#include "chrome/browser/browser_process.h"
#endif

#if BUILDFLAG(ENABLE_BRAVE_SYNC) && !defined(OS_ANDROID)
#include "brave/browser/infobars/sync_v2_migrate_infobar_delegate.h"
#include "components/sync/driver/sync_service.h"
#include "components/sync/driver/sync_user_settings.h"
#include "chrome/browser/sync/profile_sync_service_factory.h"
#endif

void BraveBrowserMainParts::PostBrowserStart() {
ChromeBrowserMainParts::PostBrowserStart();

Expand Down Expand Up @@ -83,6 +92,15 @@ void BraveBrowserMainParts::PostBrowserStart() {
if (infobar_service) {
BraveConfirmP3AInfoBarDelegate::Create(
infobar_service, g_browser_process->local_state());
#if BUILDFLAG(ENABLE_BRAVE_SYNC)
auto* sync_service = ProfileSyncServiceFactory::IsSyncAllowed(profile())
? ProfileSyncServiceFactory::GetForProfile(profile())
: nullptr;
const bool is_v2_user = sync_service &&
sync_service->GetUserSettings()->IsFirstSetupComplete();
SyncV2MigrateInfoBarDelegate::Create(infobar_service, is_v2_user,
profile(), browser);
#endif // BUILDFLAG(ENABLE_BRAVE_SYNC)
}
}
}
Expand Down
13 changes: 13 additions & 0 deletions browser/infobars/BUILD.gn
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import("//build/config/features.gni")
import("//brave/components/brave_wayback_machine/buildflags/buildflags.gni")
import("//brave/components/brave_wallet/browser/buildflags/buildflags.gni")
import("//brave/components/brave_sync/buildflags/buildflags.gni")

source_set("infobars") {
sources = [
Expand All @@ -17,6 +18,7 @@ source_set("infobars") {

deps = [
"//base",
"//brave/browser/ui",
"//brave/common",
"//brave/components/p3a",
"//brave/app:brave_generated_resources_grit",
Expand All @@ -40,4 +42,15 @@ source_set("infobars") {
"//brave/components/brave_wayback_machine",
]
}

if (enable_brave_sync) {
sources += [
"sync_v2_migrate_infobar_delegate.cc",
"sync_v2_migrate_infobar_delegate.h",
]

deps += [
"//brave/components/brave_sync",
]
}
}
115 changes: 115 additions & 0 deletions browser/infobars/sync_v2_migrate_infobar_delegate.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
// 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/browser/infobars/sync_v2_migrate_infobar_delegate.h"

#include <memory>
#include <utility>

#include "base/feature_list.h"
#include "brave/browser/ui/brave_pages.h"
#include "brave/common/pref_names.h"
#include "brave/common/url_constants.h"
#include "brave/components/brave_sync/brave_sync_prefs.h"
#include "brave/components/brave_sync/features.h"
#include "brave/grit/brave_generated_resources.h"
#include "chrome/browser/infobars/infobar_service.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/chrome_pages.h"
#include "chrome/common/webui_url_constants.h"
#include "chrome/grit/chromium_strings.h"
#include "components/infobars/core/infobar.h"
#include "components/prefs/pref_service.h"
#include "components/strings/grit/components_strings.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/views/vector_icons.h"

// static
void SyncV2MigrateInfoBarDelegate::Create(
InfoBarService* infobar_service, bool is_v2_user, Profile* profile,
Browser* browser) {
// Show infobar if user had enabled sync v1 (even if they hadn't
// re-enabled it via the flag).
// Don't show infobar if:
// - sync flag is disabled
// - problem with local_state
// - user has setup sync v2
// - dismissed notification (don't auto-dismiss)
const bool is_flag_enabled =
base::FeatureList::IsEnabled(brave_sync::features::kBraveSync);
if (!is_flag_enabled) {
return;
}
brave_sync::Prefs brave_sync_prefs(profile->GetPrefs());
const bool was_v1_user = brave_sync_prefs.IsSyncV1Enabled();
if (!was_v1_user) {
// Not v1 user
return;
}
const bool has_dismissed = brave_sync_prefs.IsSyncMigrateNoticeDismissed();
if (has_dismissed) {
return;
}
if (is_v2_user) {
// Make sure this doesn't automatically show again if sync is turned off.
// TODO(petemill): Might be better to change a pref in an event handler
// rather than here.
brave_sync_prefs.SetDismissSyncMigrateNotice(true);
return;
}
// Show infobar
infobar_service->AddInfoBar(infobar_service->CreateConfirmInfoBar(
std::unique_ptr<ConfirmInfoBarDelegate>(
new SyncV2MigrateInfoBarDelegate(browser, profile))));
}

// Start class impl
SyncV2MigrateInfoBarDelegate::SyncV2MigrateInfoBarDelegate(Browser* browser,
Profile* profile)
: ConfirmInfoBarDelegate(),
profile_(profile),
browser_(browser) {
}

SyncV2MigrateInfoBarDelegate::~SyncV2MigrateInfoBarDelegate() {
}

infobars::InfoBarDelegate::InfoBarIdentifier
SyncV2MigrateInfoBarDelegate::GetIdentifier() const {
return SYNC_V2_MIGRATE_INFOBAR_DELEGATE;
}

const gfx::VectorIcon& SyncV2MigrateInfoBarDelegate::GetVectorIcon() const {
return views::kInfoIcon;
}

bool SyncV2MigrateInfoBarDelegate::ShouldExpire(
const NavigationDetails& details) const {
return false;
}

void SyncV2MigrateInfoBarDelegate::InfoBarDismissed() {
brave_sync::Prefs brave_sync_prefs(profile_->GetPrefs());
brave_sync_prefs.SetDismissSyncMigrateNotice(true);
}

base::string16 SyncV2MigrateInfoBarDelegate::GetMessageText() const {
return l10n_util::GetStringUTF16(IDS_BRAVE_SYNC_V2_MIGRATE_INFOBAR_MESSAGE);
}

int SyncV2MigrateInfoBarDelegate::GetButtons() const {
return BUTTON_OK;
}

base::string16 SyncV2MigrateInfoBarDelegate::GetButtonLabel(
InfoBarButton button) const {
return l10n_util::GetStringUTF16(IDS_BRAVE_SYNC_V2_MIGRATE_INFOBAR_COMMAND);
}

bool SyncV2MigrateInfoBarDelegate::Accept() {
brave::ShowSync(browser_);
return true;
}
49 changes: 49 additions & 0 deletions browser/infobars/sync_v2_migrate_infobar_delegate.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// 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/.

#ifndef BRAVE_BROWSER_INFOBARS_SYNC_V2_MIGRATE_INFOBAR_DELEGATE_H_
#define BRAVE_BROWSER_INFOBARS_SYNC_V2_MIGRATE_INFOBAR_DELEGATE_H_

#include "base/compiler_specific.h"
#include "base/macros.h"
#include "base/strings/string16.h"
#include "components/infobars/core/confirm_infobar_delegate.h"
#include "url/gurl.h"

class Browser;
class InfoBarService;
class Profile;
class PrefService;

namespace brave_sync {
class Prefs;
} // namespace brave_sync

// An infobar that is run with a string, buttons, and a "Learn More" link.
class SyncV2MigrateInfoBarDelegate : public ConfirmInfoBarDelegate {
public:
static void Create(InfoBarService* infobar_service, bool is_v2_user,
Profile* profile, Browser* browser);

private:
explicit SyncV2MigrateInfoBarDelegate(Browser* browser, Profile* profile);
~SyncV2MigrateInfoBarDelegate() override;

infobars::InfoBarDelegate::InfoBarIdentifier GetIdentifier() const override;
const gfx::VectorIcon& GetVectorIcon() const override;
bool ShouldExpire(const NavigationDetails& details) const override;
void InfoBarDismissed() override;
base::string16 GetMessageText() const override;
int GetButtons() const override;
base::string16 GetButtonLabel(InfoBarButton button) const override;
bool Accept() override;

Profile* profile_;
Browser* browser_;

DISALLOW_COPY_AND_ASSIGN(SyncV2MigrateInfoBarDelegate);
};

#endif // BRAVE_BROWSER_INFOBARS_SYNC_V2_MIGRATE_INFOBAR_DELEGATE_H_
9 changes: 9 additions & 0 deletions browser/ui/brave_pages.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
#include "brave/browser/webcompat_reporter/webcompat_reporter_dialog.h"
#include "brave/common/webui_url_constants.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/chrome_pages.h"
#include "chrome/browser/ui/singleton_tabs.h"
#include "chrome/common/webui_url_constants.h"
#include "url/gurl.h"

namespace brave {
Expand All @@ -25,6 +27,13 @@ void ShowBraveAdblock(Browser* browser) {
GetSingletonTabNavigateParams(browser, GURL(kBraveUIAdblockURL)));
}

void ShowSync(Browser* browser) {
auto url = chrome::GetSettingsUrl(chrome::kSyncSetupSubPage);
ShowSingletonTabOverwritingNTP(
browser,
GetSingletonTabNavigateParams(browser, url));
}

void ShowWebcompatReporter(Browser* browser) {
content::WebContents* web_contents =
browser->tab_strip_model()->GetActiveWebContents();
Expand Down
1 change: 1 addition & 0 deletions browser/ui/brave_pages.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ void ShowWebcompatReporter(Browser* browser);
void ShowBraveRewards(Browser* browser);
void ShowBraveWallet(Browser* browser);
void ShowExtensionSettings(Browser* browser);
void ShowSync(Browser* browser);

} // namespace brave

Expand Down
3 changes: 2 additions & 1 deletion chromium_src/components/infobars/core/infobar_delegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
#define BRAVE_INFOBAR_DELEGATE_IDENTIFIERS \
BRAVE_CONFIRM_P3A_INFOBAR_DELEGATE = 500, \
CRYPTO_WALLETS_INFOBAR_DELEGATE = 501, \
WAYBACK_MACHINE_INFOBAR_DELEGATE = 502,
WAYBACK_MACHINE_INFOBAR_DELEGATE = 502, \
SYNC_V2_MIGRATE_INFOBAR_DELEGATE = 503,

#include "../../../../../components/infobars/core/infobar_delegate.h"

Expand Down
13 changes: 12 additions & 1 deletion components/brave_sync/brave_sync_prefs.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ const char kSyncV1Migrated[] = "brave_sync_v2.v1_migrated";
// Indicate all meta info set in V1 has been stripped in
// BraveBookmarkModelLoadedObserver
const char kSyncV1MetaInfoCleared[] = "brave_sync_v2.v1_meta_info_cleared";

// Has dismissed message about migration to sync v2
const char kSyncV2MigrateNoticeDismissed[] =
"brave_sync_v2.migrate_notice_dismissed";
// Deprecated
// ============================================================================
const char kSyncSeed[] = "brave_sync.seed";
Expand Down Expand Up @@ -65,6 +67,7 @@ void Prefs::RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry) {
registry->RegisterStringPref(kSyncV2Seed, std::string());
registry->RegisterBooleanPref(kSyncV1Migrated, false);
registry->RegisterBooleanPref(kSyncV1MetaInfoCleared, false);
registry->RegisterBooleanPref(kSyncV2MigrateNoticeDismissed, false);

// Deprecated
// ============================================================================
Expand Down Expand Up @@ -147,6 +150,14 @@ bool Prefs::IsSyncV1Enabled() const {
return pref_service_->GetBoolean(kSyncEnabled);
}

bool Prefs::IsSyncMigrateNoticeDismissed() const {
return pref_service_->GetBoolean(kSyncV2MigrateNoticeDismissed);
}

void Prefs::SetDismissSyncMigrateNotice(bool is_dismissed) {
pref_service_->SetBoolean(kSyncV2MigrateNoticeDismissed, is_dismissed);
}

void Prefs::Clear() {
pref_service_->ClearPref(kSyncV2Seed);
}
Expand Down
3 changes: 3 additions & 0 deletions components/brave_sync/brave_sync_prefs.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ class Prefs {
// It is only used to read whether sync v1 was enabled before upgrading
bool IsSyncV1Enabled() const;

bool IsSyncMigrateNoticeDismissed() const;
void SetDismissSyncMigrateNotice(bool is_dismissed);

void Clear();

private:
Expand Down

0 comments on commit 9ce2485

Please sign in to comment.