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
|
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chromeos/ash/components/file_manager/indexing/file_index_service.h"
#include <memory>
#include "base/task/thread_pool.h"
#include "chromeos/ash/components/file_manager/indexing/sql_storage.h"
namespace ash::file_manager {
// FileIndexService provides asynchronous version of operations defined in
// the FileIndex class. The current structure of the classes is as follows:
//
// [ FileIndexService ]---<>[ SequenceBound<FileIndex> ]
// |
// |
// [ IndexStorage ]<>----------------'
// [ (interface) ]
// ^
// |
// -------+--------.
// | |
// [ RamStorage ] [ SqlStorage ]
namespace {
base::FilePath MakeDbPath(base::FilePath& profile_path) {
return profile_path.AppendASCII("file_manager").AppendASCII("file_index.db");
}
constexpr char kSqlDatabaseUmaTag[] = "FileIndexService";
} // namespace
FileIndexService::FileIndexService(base::FilePath profile_path)
: file_index_(base::ThreadPool::CreateSequencedTaskRunner(
{base::MayBlock(), base::TaskPriority::USER_BLOCKING,
base::TaskShutdownBehavior::BLOCK_SHUTDOWN}),
std::make_unique<SqlStorage>(
MakeDbPath(profile_path),
sql::Database::Tag(kSqlDatabaseUmaTag))) {}
FileIndexService::~FileIndexService() = default;
void FileIndexService::Init(IndexingOperationCallback callback) {
if (inited_ != OpResults::kUndefined) {
std::move(callback).Run(inited_);
return;
}
file_index_.AsyncCall(&FileIndex::Init)
.Then(base::BindOnce(
[](IndexingOperationCallback callback, OpResults* inited,
OpResults result) {
*inited = result;
std::move(callback).Run(result);
},
std::move(callback), &inited_));
}
void FileIndexService::PutFileInfo(const FileInfo& file_info,
IndexingOperationCallback callback) {
if (inited_ != OpResults::kSuccess) {
std::move(callback).Run(OpResults::kUninitialized);
return;
}
file_index_.AsyncCall(&FileIndex::PutFileInfo)
.WithArgs(file_info)
.Then(std::move(callback));
}
void FileIndexService::SetTerms(const std::vector<Term>& terms,
const GURL& url,
IndexingOperationCallback callback) {
if (inited_ != OpResults::kSuccess) {
std::move(callback).Run(kUninitialized);
return;
}
file_index_.AsyncCall(&FileIndex::SetTerms)
.WithArgs(terms, url)
.Then(std::move(callback));
}
void FileIndexService::AddTerms(const std::vector<Term>& terms,
const GURL& url,
IndexingOperationCallback callback) {
if (inited_ != OpResults::kSuccess) {
std::move(callback).Run(kUninitialized);
return;
}
file_index_.AsyncCall(&FileIndex::AddTerms)
.WithArgs(terms, url)
.Then(std::move(callback));
}
void FileIndexService::RemoveFile(const GURL& url,
IndexingOperationCallback callback) {
if (inited_ != OpResults::kSuccess) {
std::move(callback).Run(kUninitialized);
return;
}
file_index_.AsyncCall(&FileIndex::RemoveFile)
.WithArgs(url)
.Then(std::move(callback));
}
void FileIndexService::MoveFile(const GURL& old_url,
const GURL& new_url,
IndexingOperationCallback callback) {
if (inited_ != OpResults::kSuccess) {
std::move(callback).Run(kUninitialized);
return;
}
file_index_.AsyncCall(&FileIndex::MoveFile)
.WithArgs(old_url, new_url)
.Then(std::move(callback));
}
void FileIndexService::RemoveTerms(const std::vector<Term>& terms,
const GURL& url,
IndexingOperationCallback callback) {
if (inited_ != OpResults::kSuccess) {
std::move(callback).Run(kUninitialized);
return;
}
file_index_.AsyncCall(&FileIndex::RemoveTerms)
.WithArgs(terms, url)
.Then(std::move(callback));
}
// Searches the index for file info matching the specified query.
void FileIndexService::Search(const Query& query,
SearchResultsCallback callback) {
if (inited_ != OpResults::kSuccess) {
std::move(callback).Run(SearchResults());
return;
}
file_index_.AsyncCall(&FileIndex::Search)
.WithArgs(query)
.Then(std::move(callback));
}
} // namespace ash::file_manager
|