Skip to content

Commit

Permalink
Destroy observer on ImportEnded
Browse files Browse the repository at this point in the history
  • Loading branch information
spylogsster committed Nov 1, 2022
1 parent 094d0ec commit 41d5257
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 10 deletions.
17 changes: 16 additions & 1 deletion browser/ui/webui/settings/brave_import_data_handler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@
#include "brave/browser/ui/webui/settings/brave_import_data_handler.h"

#include <memory>
#include <string>
#include <unordered_map>

#include "brave/browser/ui/webui/settings/import_feature.h"
#include "chrome/browser/importer/external_process_importer_host.h"
#include "chrome/browser/importer/profile_writer.h"
#include "chrome/browser/profiles/profile.h"
#include "content/public/browser/browser_task_traits.h"

#if BUILDFLAG(IS_MAC)
#include "base/files/file_path.h"
Expand Down Expand Up @@ -99,8 +101,21 @@ void BraveImportDataHandler::StartImportImpl(
new ProfileWriter(profile));
}

void BraveImportDataHandler::NotifyImportProgress(const base::Value& info) {
void BraveImportDataHandler::NotifyImportProgress(
const importer::SourceProfile& source_profile,
const base::Value& info) {
FireWebUIListener("brave-import-data-status-changed", info);
const std::string* event = info.FindStringKey("event");
if (event && *event == "ImportEnded") {
content::GetUIThreadTaskRunner({})->PostTask(
FROM_HERE,
base::BindOnce(&BraveImportDataHandler::OnImportEnded,
weak_factory_.GetWeakPtr(), source_profile.source_path));
}
}

void BraveImportDataHandler::OnImportEnded(const base::FilePath& source_path) {
import_observers_.erase(source_path);
}

#if BUILDFLAG(IS_MAC)
Expand Down
4 changes: 3 additions & 1 deletion browser/ui/webui/settings/brave_import_data_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ class BraveImportDataHandler : public ImportDataHandler,

void StartImportImpl(const importer::SourceProfile& source_profile,
uint16_t imported_items);
void NotifyImportProgress(const base::Value& info);
void NotifyImportProgress(const importer::SourceProfile& source_profile,
const base::Value& info);
void OnImportEnded(const base::FilePath& source_path);
#if BUILDFLAG(IS_MAC)
void CheckDiskAccess(const importer::SourceProfile& source_profile,
uint16_t imported_items);
Expand Down
10 changes: 6 additions & 4 deletions browser/ui/webui/settings/brave_importer_observer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ void BraveImporterObserver::ImportStarted() {
data.Set("importer_type", source_profile_.importer_type);
data.Set("items_to_import", imported_items_);
data.Set("event", "ImportStarted");
callback_.Run(base::Value(std::move(data)));
callback_.Run(source_profile_, base::Value(std::move(data)));
}

void BraveImporterObserver::ImportItemStarted(importer::ImportItem item) {
Expand All @@ -48,7 +48,7 @@ void BraveImporterObserver::ImportItemStarted(importer::ImportItem item) {
data.Set("items_to_import", imported_items_);
data.Set("event", "ImportItemStarted");
data.Set("item", item);
callback_.Run(base::Value(std::move(data)));
callback_.Run(source_profile_, base::Value(std::move(data)));
}

void BraveImporterObserver::ImportItemEnded(importer::ImportItem item) {
Expand All @@ -58,7 +58,7 @@ void BraveImporterObserver::ImportItemEnded(importer::ImportItem item) {
data.Set("items_to_import", imported_items_);
data.Set("event", "ImportItemEnded");
data.Set("item", item);
callback_.Run(base::Value(std::move(data)));
callback_.Run(source_profile_, base::Value(std::move(data)));
}

void BraveImporterObserver::ImportEnded() {
Expand All @@ -67,12 +67,14 @@ void BraveImporterObserver::ImportEnded() {
data.Set("importer_type", source_profile_.importer_type);
data.Set("items_to_import", imported_items_);
data.Set("event", "ImportEnded");
callback_.Run(base::Value(std::move(data)));

DCHECK(importer_host_);
if (importer_host_)
importer_host_->set_observer(nullptr);

importer_host_ = nullptr;

callback_.Run(source_profile_, base::Value(std::move(data)));
}

ExternalProcessImporterHost*
Expand Down
4 changes: 2 additions & 2 deletions browser/ui/webui/settings/brave_importer_observer.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ class ExternalProcessImporterHost;

class BraveImporterObserver : public importer::ImporterProgressObserver {
public:
using ReportProgressCallback =
base::RepeatingCallback<void(const base::Value&)>;
using ReportProgressCallback = base::RepeatingCallback<
void(const importer::SourceProfile& source_profile, const base::Value&)>;

explicit BraveImporterObserver(ExternalProcessImporterHost* host,
const importer::SourceProfile& source_profile,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ class BraveImporterObserverUnitTest : public testing::Test {
void SetExpectedInfo(base::Value value) { expected_info_ = std::move(value); }
void SetExpectedCalls(int value) { expected_calls_ = value; }
int GetExpectedCalls() { return expected_calls_; }
void NotifyImportProgress(const base::Value& info) {
void NotifyImportProgress(const importer::SourceProfile& source_profile,
const base::Value& info) {
EXPECT_EQ(expected_info_, info);
expected_calls_++;
}
Expand Down
2 changes: 1 addition & 1 deletion browser/ui/webui/settings/import_feature.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
namespace settings {

const base::Feature kParallelImports{"ParallelImports",
base::FEATURE_DISABLED_BY_DEFAULT};
base::FEATURE_ENABLED_BY_DEFAULT};

// Temporary flag to keep old way until
// https://github.com/brave/brave-core/pull/15637 landed.
Expand Down

0 comments on commit 41d5257

Please sign in to comment.