Fixed threading retaining with weak/strong specifies.

Added explicit ARC assertions to the top of the files.
This commit is contained in:
Brandon
2020-11-30 07:34:51 -05:00
parent 980e70a23c
commit d65e26242d
3 changed files with 85 additions and 28 deletions
@@ -13,6 +13,7 @@ NS_ASSUME_NONNULL_BEGIN
typedef NS_ENUM(NSUInteger, BraveBookmarksExporterState) {
BraveBookmarksExporterStateCompleted,
BraveBookmarksExporterStateStarted,
BraveBookmarksExporterStateCancelled,
BraveBookmarksExporterStateErrorCreatingFile,
BraveBookmarksExporterStateErrorWritingHeader,
BraveBookmarksExporterStateErrorWritingNodes
@@ -9,6 +9,7 @@
#include "base/bind_helpers.h"
#include "base/files/file_path.h"
#include "base/base_paths.h"
#include "base/mac/foundation_util.h"
#include "base/path_service.h"
#include "base/sequenced_task_runner.h"
#include "base/strings/sys_string_conversions.h"
@@ -23,6 +24,10 @@
#import "net/base/mac/url_conversions.h"
#include "url/gurl.h"
#if !defined(__has_feature) || !__has_feature(objc_arc)
#error "This file requires ARC support."
#endif
class BraveBookmarksExportObserver: public BookmarksExportObserver {
public:
BraveBookmarksExportObserver(std::function<void(BraveBookmarksExporterState)> on_export_finished);
@@ -122,27 +127,43 @@ void BraveBookmarksExportObserver::OnExportFinished(Result result) {
- (void)exportToFile:(NSString *)filePath
withListener:(void(^)(BraveBookmarksExporterState))listener {
auto start_export = [](NSString *filePath,
auto start_export = [](BraveBookmarksExporter* weak_exporter,
NSString *filePath,
std::function<void(BraveBookmarksExporterState)> listener){
//Export cancelled as the exporter has been deallocated
__strong BraveBookmarksExporter* exporter = weak_exporter;
if (!exporter) {
listener(BraveBookmarksExporterStateStarted);
listener(BraveBookmarksExporterStateCancelled);
return;
}
DCHECK(GetApplicationContext());
base::FilePath destination_file_path =
base::FilePath::FromUTF8Unsafe([filePath UTF8String]);
base::mac::NSStringToFilePath(filePath);
listener(BraveBookmarksExporterStateStarted);
ios::ChromeBrowserStateManager* browserStateManager =
GetApplicationContext()->GetChromeBrowserStateManager();
DCHECK(browserStateManager);
ChromeBrowserState* chromeBrowserState =
browserStateManager->GetLastUsedBrowserState();
DCHECK(chromeBrowserState);
bookmark_html_writer::WriteBookmarks(chromeBrowserState,
destination_file_path,
new BraveBookmarksExportObserver(listener)
);
};
__weak BraveBookmarksExporter* weakSelf = self;
base::PostTask(FROM_HERE,
{web::WebThread::UI},
base::BindOnce(start_export,
weakSelf,
filePath,
listener)
);
@@ -151,16 +172,27 @@ void BraveBookmarksExportObserver::OnExportFinished(Result result) {
- (void)exportToFile:(NSString *)filePath
bookmarks:(NSArray<BraveExportedBookmark *> *)bookmarks
withListener:(void(^)(BraveBookmarksExporterState))listener {
if ([bookmarks count] == 0) {
listener(BraveBookmarksExporterStateStarted);
listener(BraveBookmarksExporterStateCompleted);
return;
}
auto start_export = [](BraveBookmarksExporter* exporter,
auto start_export = [](BraveBookmarksExporter* weak_exporter,
NSString *filePath,
NSArray<BraveExportedBookmark *> *bookmarks,
std::function<void(BraveBookmarksExporterState)> listener){
base::FilePath destination_file_path =
base::FilePath::FromUTF8Unsafe([filePath UTF8String]);
//Export cancelled as the exporter has been deallocated
__strong BraveBookmarksExporter* exporter = weak_exporter;
if (!exporter) {
listener(BraveBookmarksExporterStateStarted);
listener(BraveBookmarksExporterStateCancelled);
return;
}
listener(BraveBookmarksExporterStateStarted);
base::FilePath destination_file_path =
base::mac::NSStringToFilePath(filePath);
std::unique_ptr<ExportedRootBookmarkEntry> root_node = ExportedBookmarkEntry::get_root_node();
for (auto& bookmark : [exporter convertToChromiumExportedBookmarks: bookmarks]) {
//We export as the |mobile_bookmarks_node| by default.
@@ -174,16 +206,17 @@ void BraveBookmarksExportObserver::OnExportFinished(Result result) {
);
};
__weak BraveBookmarksExporter* weakSelf = self;
base::PostTask(FROM_HERE,
{base::ThreadPool(), base::MayBlock(),
base::TaskPriority::USER_VISIBLE,
base::TaskShutdownBehavior::BLOCK_SHUTDOWN},
base::BindOnce(start_export,
base::Unretained(self),
weakSelf,
filePath,
bookmarks,
listener)
);
);
}
// Converts an array of Chromium imported bookmarks to iOS exported bookmarks.
@@ -8,6 +8,7 @@
#include "base/bind_helpers.h"
#include "base/files/file_path.h"
#include "base/base_paths.h"
#include "base/mac/foundation_util.h"
#include "base/path_service.h"
#include "base/sequenced_task_runner.h"
#include "base/strings/sys_string_conversions.h"
@@ -18,6 +19,10 @@
#import "net/base/mac/url_conversions.h"
#include "url/gurl.h"
#if !defined(__has_feature) || !__has_feature(objc_arc)
#error "This file requires ARC support."
#endif
@implementation BraveImportedBookmark
- (instancetype)initFromChromiumImportedBookmark:(const ImportedBookmarkEntry&)entry {
if ((self = [super init])) {
@@ -69,7 +74,7 @@
- (void)dealloc {
[self cancel];
self->import_thread_.reset();
import_thread_.reset();
}
- (void)cancel {
@@ -82,23 +87,30 @@
withListener:(void(^)(BraveBookmarksImporterState, NSArray<BraveImportedBookmark *> * _Nullable))listener {
base::FilePath source_file_path =
base::FilePath::FromUTF8Unsafe([filePath UTF8String]);
base::mac::NSStringToFilePath(filePath);
//In Chromium, this is IDS_BOOKMARK_GROUP (804)
base::string16 top_level_folder_name =
base::SysNSStringToUTF16(folderName);
auto start_import = [](BraveBookmarksImporter* importer,
auto start_import = [](BraveBookmarksImporter* weak_importer,
const base::FilePath& source_file_path,
const base::string16& top_level_folder_name,
bool automaticImport,
std::function<void(BraveBookmarksImporterState, NSArray<BraveImportedBookmark *> *)> listener){
listener(BraveBookmarksImporterStateStarted, nullptr);
//Import cancelled as the importer has been deallocated
__strong BraveBookmarksImporter* importer = weak_importer;
if (!importer) {
listener(BraveBookmarksImporterStateStarted, nullptr);
listener(BraveBookmarksImporterStateCancelled, nullptr);
return;
}
listener(BraveBookmarksImporterStateStarted, nullptr);
std::vector<ImportedBookmarkEntry> bookmarks;
bookmark_html_reader::ImportBookmarksFile(
base::BindRepeating([](BraveBookmarksImporter* importer) -> bool {
return importer ? [importer isImporterCancelled] : true;
return [importer isImporterCancelled];
}, base::Unretained(importer)),
base::BindRepeating([](BraveBookmarksImporter* importer, const GURL& url) -> bool {
return [importer canImportURL:url];
@@ -132,22 +144,23 @@
// Create worker thread in which importer runs.
// In Chromium, this is created with `base::Thread("import_thread")`
if (!self->import_thread_) {
self->import_thread_ = base::CreateSequencedTaskRunner(
if (!import_thread_) {
import_thread_ = base::CreateSequencedTaskRunner(
{base::ThreadPool(), base::MayBlock(),
base::TaskPriority::USER_VISIBLE,
base::TaskShutdownBehavior::BLOCK_SHUTDOWN});
}
// Run the importer on the sequenced task runner.
self->import_thread_->PostTask(FROM_HERE,
base::BindOnce(start_import,
base::Unretained(self),
source_file_path,
top_level_folder_name,
automaticImport,
listener)
);
__weak BraveBookmarksImporter* weakSelf = self;
import_thread_->PostTask(FROM_HERE,
base::BindOnce(start_import,
weakSelf,
source_file_path,
top_level_folder_name,
automaticImport,
listener)
);
}
- (void)importFromArray:(NSArray<BraveImportedBookmark *> *)bookmarks
@@ -158,24 +171,34 @@
base::string16 top_level_folder_name =
base::SysNSStringToUTF16(folderName);
auto start_import = [](BraveBookmarksImporter* importer,
auto start_import = [](BraveBookmarksImporter* weak_importer,
NSArray<BraveImportedBookmark *> *bookmarks,
const base::string16& top_level_folder_name,
std::function<void(BraveBookmarksImporterState)> listener){
//Import cancelled as the importer has been deallocated
__strong BraveBookmarksImporter* importer = weak_importer;
if (!importer) {
listener(BraveBookmarksImporterStateStarted);
listener(BraveBookmarksImporterStateCancelled);
return;
}
listener(BraveBookmarksImporterStateStarted);
BookmarksImporter::AddBookmarks(top_level_folder_name,
[importer convertToChromiumImportedBookmarks:bookmarks]);
listener(BraveBookmarksImporterStateCompleted);
};
// Import into the Profile/ChromeBrowserState on the main-thread.
__weak BraveBookmarksImporter* weakSelf = self;
base::PostTask(FROM_HERE,
{web::WebThread::UI},
base::BindOnce(start_import,
base::Unretained(self),
weakSelf,
bookmarks,
top_level_folder_name,
listener)
);
);
}
// MARK: - Private