1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
|
// Copyright 2025 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/user_data_importer/content/content_bookmark_parser.h"
#include "base/files/file_util.h"
#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/task/bind_post_task.h"
#include "base/task/task_traits.h"
#include "base/task/thread_pool.h"
#include "components/strings/grit/components_strings.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/service_process_host.h"
namespace user_data_importer {
// Declared in bookmark_parser.h.
std::unique_ptr<BookmarkParser> MakeBookmarkParser() {
return std::make_unique<ContentBookmarkParser>();
}
ContentBookmarkParser::ContentBookmarkParser() {
// This class is created on the UI thread, but is used forever after from the
// background thread.
DETACH_FROM_SEQUENCE(sequence_checker_);
}
ContentBookmarkParser::~ContentBookmarkParser() = default;
void ContentBookmarkParser::SetServiceForTesting(
mojo::PendingRemote<user_data_importer::mojom::BookmarkHtmlParser> parser) {
CHECK(!html_parser_remote_);
html_parser_for_testing_ = std::move(parser);
}
void ContentBookmarkParser::Parse(
const base::FilePath& file_path,
BookmarkParser::BookmarkParsingCallback callback) {
CHECK(!content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
std::string raw_html;
// ReadFileToString can return false, but still populate something into
// `raw_html`. In that case, try to recover as much data as possible.
// (ParseImpl() will report an error if `raw_html` is empty, i.e. the
// read failed entirely.)
base::ReadFileToString(file_path, &raw_html);
ParseImpl(std::move(raw_html), std::move(callback));
}
void ContentBookmarkParser::Parse(base::File file,
BookmarkParsingCallback callback) {
CHECK(!content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
std::string raw_html;
// ReadStreamToString can return false, but still populate something into
// `raw_html`. In that case, try to recover as much data as possible.
// (ParseImpl() will report an error if `raw_html` is empty, i.e. the
// read failed entirely.)
base::ReadStreamToString(base::FileToFILE(std::move(file), "rb"), &raw_html);
ParseImpl(std::move(raw_html), std::move(callback));
}
void ContentBookmarkParser::ParseImpl(
std::string raw_html,
BookmarkParser::BookmarkParsingCallback callback) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
if (raw_html.empty()) {
std::move(callback).Run(base::unexpected(
BookmarkParser::BookmarkParsingError::kFailedToReadFile));
return;
}
if (!html_parser_remote_) {
if (html_parser_for_testing_) {
html_parser_remote_.Bind(std::move(html_parser_for_testing_));
} else {
html_parser_remote_ = content::ServiceProcessHost::Launch<
user_data_importer::mojom::BookmarkHtmlParser>(
content::ServiceProcessHost::Options()
.WithDisplayName(IDS_CONTENT_BOOKMARK_PARSER_SERVICE_DISPLAY_NAME)
.Pass());
}
html_parser_remote_.reset_on_disconnect();
}
html_parser_remote_->Parse(
std::move(raw_html),
base::BindOnce(&ContentBookmarkParser::OnParseFinished,
weak_ptr_factory_.GetWeakPtr(), std::move(callback)));
}
void ContentBookmarkParser::OnParseFinished(
user_data_importer::BookmarkParser::BookmarkParsingCallback callback,
user_data_importer::BookmarkParser::ParsedBookmarks parsed_bookmarks) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
std::move(callback).Run(std::move(parsed_bookmarks));
}
} // namespace user_data_importer
|