Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Import source profiles to separate profiles #16640

Merged
merged 5 commits into from
Jan 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions browser/ui/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,8 @@ source_set("ui") {
"webui/settings/brave_adblock_handler.h",
"webui/settings/brave_appearance_handler.cc",
"webui/settings/brave_appearance_handler.h",
"webui/settings/brave_import_bulk_data_handler.cc",
"webui/settings/brave_import_bulk_data_handler.h",
"webui/settings/brave_import_data_handler.cc",
"webui/settings/brave_import_data_handler.h",
"webui/settings/brave_importer_observer.cc",
Expand All @@ -166,8 +168,6 @@ source_set("ui") {
"webui/settings/brave_wallet_handler.h",
"webui/settings/default_brave_shields_handler.cc",
"webui/settings/default_brave_shields_handler.h",
"webui/settings/import_feature.cc",
"webui/settings/import_feature.h",
"webui/speedreader/speedreader_panel_data_handler_impl.cc",
"webui/speedreader/speedreader_panel_data_handler_impl.h",
"webui/speedreader/speedreader_panel_handler_impl.cc",
Expand Down
4 changes: 2 additions & 2 deletions browser/ui/webui/brave_welcome_ui.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#include "base/feature_list.h"
#include "base/metrics/histogram_macros.h"
#include "brave/browser/ui/webui/brave_webui_source.h"
#include "brave/browser/ui/webui/settings/brave_import_data_handler.h"
#include "brave/browser/ui/webui/settings/brave_import_bulk_data_handler.h"
#include "brave/browser/ui/webui/settings/brave_search_engines_handler.h"
#include "brave/components/brave_welcome/common/features.h"
#include "brave/components/brave_welcome/resources/grit/brave_welcome_generated_map.h"
Expand Down Expand Up @@ -251,7 +251,7 @@ BraveWelcomeUI::BraveWelcomeUI(content::WebUI* web_ui, const std::string& name)
web_ui->AddMessageHandler(
std::make_unique<WelcomeDOMHandler>(Profile::FromWebUI(web_ui)));
web_ui->AddMessageHandler(
std::make_unique<settings::BraveImportDataHandler>());
std::make_unique<settings::BraveImportBulkDataHandler>());
web_ui->AddMessageHandler(
std::make_unique<settings::DefaultBrowserHandler>()); // set default
// browser
Expand Down
175 changes: 175 additions & 0 deletions browser/ui/webui/settings/brave_import_bulk_data_handler.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
/* Copyright (c) 2023 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 https://mozilla.org/MPL/2.0/. */

#include "brave/browser/ui/webui/settings/brave_import_bulk_data_handler.h"

#include <memory>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>

#include "base/bind.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/json/json_reader.h"
#include "base/rand_util.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/profiles/profile_attributes_entry.h"
#include "chrome/browser/profiles/profile_attributes_storage.h"
#include "chrome/browser/profiles/profile_avatar_icon_util.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "chrome/common/pref_names.h"
#include "components/prefs/pref_service.h"

namespace settings {

namespace {
base::FilePath GetProfilePathByName(const std::u16string& name) {
std::vector<ProfileAttributesEntry*> entries =
g_browser_process->profile_manager()
->GetProfileAttributesStorage()
.GetAllProfilesAttributesSortedByName();
for (auto* it : entries) {
if (it->GetName() == name) {
return it->GetPath();
}
}
return base::FilePath();
}
} // namespace

BraveImportBulkDataHandler::BraveImportBulkDataHandler() = default;
BraveImportBulkDataHandler::~BraveImportBulkDataHandler() = default;

void BraveImportBulkDataHandler::RegisterMessages() {
BraveImportDataHandler::RegisterMessages();
web_ui()->RegisterMessageCallback(
"importDataBulk",
base::BindRepeating(&BraveImportBulkDataHandler::HandleImportDataBulk,
base::Unretained(this)));
}

void BraveImportBulkDataHandler::PrepareProfile(
const std::u16string& name,
ProfileReadyCallback profile_ready_callback) {
ProfileManager* profile_manager = g_browser_process->profile_manager();
auto profile_path = GetProfilePathByName(name);
// If a profile with the given path is currently managed by this object and
// fully initialized, return a pointer to the corresponding Profile object.
Profile* loaded_profile = profile_manager->GetProfileByPath(profile_path);
if (loaded_profile) {
std::move(profile_ready_callback).Run(loaded_profile);
return;
}
// Asynchronously loads an existing profile given its |profile_base_name|
// (which is the directory name within the user data directory), optionally in
// |incognito| mode. The |callback| will be called with the Profile when it
// has been loaded, or with a nullptr otherwise.
profile_manager->LoadProfileByPath(
profile_path, false,
base::BindOnce(
[](ProfileReadyCallback profile_callback, const std::u16string& name,
Profile* existing_profile) {
// Existing profile loaded, reuse it.
if (existing_profile) {
std::move(profile_callback).Run(existing_profile);
return;
}
// Asynchronously creates a new profile in the next available
// multiprofile directory. Directories are named "profile_1",
// "profile_2", etc., in sequence of creation.
auto avatar_index =
base::RandInt(profiles::GetModernAvatarIconStartIndex(),
profiles::GetDefaultAvatarIconCount());
ProfileManager::CreateMultiProfileAsync(
name, avatar_index, false,
base::BindOnce(
[](ProfileReadyCallback initialized_callback,
Profile* created_profile) {
CHECK(created_profile);
// Migrate welcome page flag to new profiles.
created_profile->GetPrefs()->SetBoolean(
prefs::kHasSeenWelcomePage, true);
std::move(initialized_callback).Run(created_profile);
},
std::move(profile_callback)));
},
std::move(profile_ready_callback), name));
}

void BraveImportBulkDataHandler::HandleImportDataBulk(
const base::Value::List& args) {
CHECK_GE(args.size(), 2u);
const auto& list = args[0].GetList();
// Bulk profiles import assumes new profiles will be created on our side if
// they do not exist.
for (const auto& it : list) {
int browser_index = it.GetInt();
importing_profiles_.insert(browser_index);
base::Value::List single_profile_args;
single_profile_args.Append(base::Value(browser_index));
single_profile_args.Append(args[1].Clone());
BraveImportDataHandler::HandleImportData(single_profile_args);
}
}

absl::optional<int> BraveImportBulkDataHandler::GetProfileIndex(
const importer::SourceProfile& source_profile) {
for (auto index : importing_profiles_) {
const auto& profile = GetSourceProfileAt(index);
if (profile.source_path == source_profile.source_path) {
return index;
}
}
return absl::nullopt;
}

void BraveImportBulkDataHandler::StartImport(
const importer::SourceProfile& source_profile,
uint16_t imported_items) {
if (!imported_items)
return;
// If profile is not from the bulk import request fallback to single profile
// import.
if (!GetProfileIndex(source_profile).has_value()) {
simonhong marked this conversation as resolved.
Show resolved Hide resolved
BraveImportDataHandler::StartImport(source_profile, imported_items);
return;
}
auto profile_name = source_profile.profile.empty()
? source_profile.importer_name
: source_profile.profile;
auto import_callback = base::BindOnce(
&BraveImportBulkDataHandler::StartImportImpl, weak_factory_.GetWeakPtr(),
source_profile, imported_items);
#if BUILDFLAG(IS_MAC)
CheckDiskAccess(imported_items, source_profile.source_path,
source_profile.importer_type,
base::BindOnce(&BraveImportBulkDataHandler::PrepareProfile,
weak_factory_.GetWeakPtr(), profile_name,
std::move(import_callback)));
#else
PrepareProfile(profile_name, std::move(import_callback));
#endif
}

void BraveImportBulkDataHandler::NotifyImportProgress(
const importer::SourceProfile& source_profile,
const base::Value& info) {
FireWebUIListener("brave-import-data-status-changed", info);
}

void BraveImportBulkDataHandler::OnImportEnded(
const importer::SourceProfile& source_profile) {
auto index = GetProfileIndex(source_profile);
if (index.has_value()) {
importing_profiles_.erase(index.value());
return;
}
simonhong marked this conversation as resolved.
Show resolved Hide resolved
BraveImportDataHandler::OnImportEnded(source_profile);
}

} // namespace settings
65 changes: 65 additions & 0 deletions browser/ui/webui/settings/brave_import_bulk_data_handler.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/* Copyright (c) 2023 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 https://mozilla.org/MPL/2.0/. */

#ifndef BRAVE_BROWSER_UI_WEBUI_SETTINGS_BRAVE_IMPORT_BULK_DATA_HANDLER_H_
#define BRAVE_BROWSER_UI_WEBUI_SETTINGS_BRAVE_IMPORT_BULK_DATA_HANDLER_H_

#include <memory>

#include "base/callback.h"
#include "base/containers/flat_set.h"
#include "base/memory/weak_ptr.h"
#include "brave/browser/ui/webui/settings/brave_import_data_handler.h"
#include "brave/browser/ui/webui/settings/brave_importer_observer.h"
#include "build/build_config.h"
#include "third_party/abseil-cpp/absl/types/optional.h"

namespace settings {

// This class handles bulk requests to import multiple profiles to
// new target Brave profiles.
class BraveImportBulkDataHandler : public BraveImportDataHandler {
public:
BraveImportBulkDataHandler();
~BraveImportBulkDataHandler() override;

using ProfileReadyCallback = base::OnceCallback<void(Profile* profile)>;

BraveImportBulkDataHandler(const BraveImportBulkDataHandler&) = delete;
BraveImportBulkDataHandler& operator=(const BraveImportBulkDataHandler&) =
delete;

protected:
void HandleImportDataBulk(const base::Value::List& args);

absl::optional<int> GetProfileIndex(
const importer::SourceProfile& source_profile);

void PrepareProfile(const std::u16string& name,
ProfileReadyCallback callback);

void ProfileReadyForImport(const importer::SourceProfile& source_profile,
uint16_t imported_items,
Profile* profile);
// BraveImportDataHandler
void NotifyImportProgress(const importer::SourceProfile& source_profile,
const base::Value& info) override;
void OnImportEnded(const importer::SourceProfile& source_profile) override;

// SettingsPageUIHandler
void RegisterMessages() override;

// ImportDataHandler overrides:
void StartImport(const importer::SourceProfile& source_profile,
uint16_t imported_items) override;

private:
base::flat_set<int> importing_profiles_;
base::WeakPtrFactory<BraveImportBulkDataHandler> weak_factory_{this};
};

} // namespace settings

#endif // BRAVE_BROWSER_UI_WEBUI_SETTINGS_BRAVE_IMPORT_BULK_DATA_HANDLER_H_
Loading