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 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
|
// Copyright 2014 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/bookmarks/browser/bookmark_storage.h"
#include <stddef.h>
#include <algorithm>
#include <unordered_map>
#include <utility>
#include "base/compiler_specific.h"
#include "base/files/file_util.h"
#include "base/functional/bind.h"
#include "base/json/json_file_value_serializer.h"
#include "base/json/json_writer.h"
#include "base/notreached.h"
#include "base/numerics/safe_conversions.h"
#include "base/task/sequenced_task_runner.h"
#include "base/task/task_traits.h"
#include "base/task/thread_pool.h"
#include "base/uuid.h"
#include "components/bookmarks/browser/bookmark_codec.h"
#include "components/bookmarks/browser/bookmark_model.h"
#include "components/bookmarks/browser/bookmark_node.h"
#include "components/bookmarks/common/bookmark_metrics.h"
namespace bookmarks {
namespace {
// Extension used for backup files (copy of main file created during startup).
const base::FilePath::CharType kBackupExtension[] = FILE_PATH_LITERAL("bak");
void BackupCallback(const base::FilePath& path) {
base::FilePath backup_path = path.ReplaceExtension(kBackupExtension);
base::CopyFile(path, backup_path);
}
base::Value::Dict EncodeModelToDict(
const BookmarkModel* model,
BookmarkStorage::PermanentNodeSelection permanent_node_selection) {
BookmarkCodec codec;
switch (permanent_node_selection) {
case BookmarkStorage::kSelectLocalOrSyncableNodes:
return codec.Encode(
model->bookmark_bar_node(), model->other_node(), model->mobile_node(),
model->client()->EncodeLocalOrSyncableBookmarkSyncMetadata());
case BookmarkStorage::kSelectAccountNodes:
// Either all permanent folders or none should exist.
if (model->account_bookmark_bar_node()) {
CHECK(model->account_other_node());
CHECK(model->account_mobile_node());
} else {
// Encode the model even for the null-permanent-folder case in case
// there is sync metadata to persist (e.g. the notion of a user having
// too many bookmarks server-side).
CHECK(!model->account_other_node());
CHECK(!model->account_mobile_node());
}
return codec.Encode(model->account_bookmark_bar_node(),
model->account_other_node(),
model->account_mobile_node(),
model->client()->EncodeAccountBookmarkSyncMetadata());
}
NOTREACHED();
}
bool ShouldSaveBackupFile(
BookmarkStorage::PermanentNodeSelection permanent_node_selection) {
switch (permanent_node_selection) {
case BookmarkStorage::kSelectLocalOrSyncableNodes:
return true;
case BookmarkStorage::kSelectAccountNodes:
return false;
}
NOTREACHED();
}
} // namespace
// static
constexpr base::TimeDelta BookmarkStorage::kSaveDelay;
BookmarkStorage::BookmarkStorage(
const BookmarkModel* model,
PermanentNodeSelection permanent_node_selection,
const base::FilePath& file_path)
: model_(model),
backend_task_runner_(base::ThreadPool::CreateSequencedTaskRunner(
{base::MayBlock(), base::TaskPriority::BEST_EFFORT,
base::TaskShutdownBehavior::BLOCK_SHUTDOWN})),
permanent_node_selection_(permanent_node_selection),
writer_(file_path, backend_task_runner_, kSaveDelay, "BookmarkStorage"),
last_scheduled_save_(base::TimeTicks::Now()) {
CHECK(!file_path.empty());
}
BookmarkStorage::~BookmarkStorage() {
SaveNowIfScheduled();
}
void BookmarkStorage::ScheduleSave() {
// If this is the first scheduled save, create a backup before overwriting the
// JSON file.
if (!backup_triggered_ && ShouldSaveBackupFile(permanent_node_selection_)) {
backup_triggered_ = true;
backend_task_runner_->PostTask(
FROM_HERE, base::BindOnce(&BackupCallback, writer_.path()));
}
writer_.ScheduleWriteWithBackgroundDataSerializer(this);
const base::TimeDelta schedule_delta =
base::TimeTicks::Now() - last_scheduled_save_;
metrics::RecordTimeSinceLastScheduledSave(schedule_delta);
last_scheduled_save_ = base::TimeTicks::Now();
}
base::ImportantFileWriter::BackgroundDataProducerCallback
BookmarkStorage::GetSerializedDataProducerForBackgroundSequence() {
base::Value::Dict value =
EncodeModelToDict(model_, permanent_node_selection_);
return base::BindOnce(
[](base::Value::Dict value) -> std::optional<std::string> {
// This runs on the background sequence.
std::string output;
if (!base::JSONWriter::WriteWithOptions(
value, base::JSONWriter::OPTIONS_PRETTY_PRINT, &output)) {
return std::nullopt;
}
return output;
},
std::move(value));
}
bool BookmarkStorage::HasScheduledSaveForTesting() const {
return writer_.HasPendingWrite();
}
void BookmarkStorage::SaveNowIfScheduledForTesting() {
SaveNowIfScheduled();
}
void BookmarkStorage::SaveNowIfScheduled() {
if (writer_.HasPendingWrite()) {
writer_.DoScheduledWrite();
}
}
} // namespace bookmarks
|