Commit 9bb9dc3a authored by Nancy Wang's avatar Nancy Wang Committed by Chromium LUCI CQ

Implement the reading function to load the restore data.

Add the class RestoreHandler to call FullRestoreFileHandler to read the
full restore data file, and load the RestoreData.

RestoreHandler is used at the system startup phase to load the restore
data for restoration.

There will be a separate CL to add tests.

Bug:1146900

Change-Id: If43082936394997f5b8e90125b43baa6577e9f38
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2592275
Commit-Queue: Nancy Wang <nancylingwang@chromium.org>
Reviewed-by: default avatarDominick Ng <dominickn@chromium.org>
Cr-Commit-Position: refs/heads/master@{#837483}
parent 724dacb7
...@@ -11,6 +11,8 @@ component("full_restore") { ...@@ -11,6 +11,8 @@ component("full_restore") {
"app_restore_data.h", "app_restore_data.h",
"full_restore_file_handler.cc", "full_restore_file_handler.cc",
"full_restore_file_handler.h", "full_restore_file_handler.h",
"full_restore_read_handler.cc",
"full_restore_read_handler.h",
"full_restore_save_handler.cc", "full_restore_save_handler.cc",
"full_restore_save_handler.h", "full_restore_save_handler.h",
"full_restore_utils.cc", "full_restore_utils.cc",
......
...@@ -4,6 +4,8 @@ ...@@ -4,6 +4,8 @@
#include "components/full_restore/full_restore_file_handler.h" #include "components/full_restore/full_restore_file_handler.h"
#include <utility>
#include "base/files/file_util.h" #include "base/files/file_util.h"
#include "base/json/json_string_value_serializer.h" #include "base/json/json_string_value_serializer.h"
#include "base/logging.h" #include "base/logging.h"
...@@ -38,6 +40,28 @@ void FullRestoreFileHandler::WriteToFile( ...@@ -38,6 +40,28 @@ void FullRestoreFileHandler::WriteToFile(
WriteDataBlocking(json_string); WriteDataBlocking(json_string);
} }
std::unique_ptr<RestoreData> FullRestoreFileHandler::ReadFromFile() {
std::string full_restore_data;
if (!ReadDataBlocking(full_restore_data) || full_restore_data.empty())
return nullptr;
// This JSON file is written by Chrome, so it is safe to deserialise it
// in-process.
JSONStringValueDeserializer deserializer(full_restore_data);
int error_code;
std::string error_message;
auto full_restore_value =
deserializer.Deserialize(&error_code, &error_message);
if (!full_restore_value) {
DVLOG(0) << "Fail to deserialize json value from string with error code: "
<< error_code << " and error message: " << error_message;
return nullptr;
}
return std::make_unique<RestoreData>(std::move(full_restore_value));
}
FullRestoreFileHandler::~FullRestoreFileHandler() = default; FullRestoreFileHandler::~FullRestoreFileHandler() = default;
void FullRestoreFileHandler::WriteDataBlocking( void FullRestoreFileHandler::WriteDataBlocking(
...@@ -50,4 +74,10 @@ void FullRestoreFileHandler::WriteDataBlocking( ...@@ -50,4 +74,10 @@ void FullRestoreFileHandler::WriteDataBlocking(
DVLOG(0) << "Fail to write full restore data to " << file_path_; DVLOG(0) << "Fail to write full restore data to " << file_path_;
} }
bool FullRestoreFileHandler::ReadDataBlocking(std::string& full_restore_data) {
base::ScopedBlockingCall scoped_blocking_call(FROM_HERE,
base::BlockingType::MAY_BLOCK);
return base::ReadFileToString(file_path_, &full_restore_data);
}
} // namespace full_restore } // namespace full_restore
...@@ -25,8 +25,6 @@ class RestoreData; ...@@ -25,8 +25,6 @@ class RestoreData;
// FullRestoreFileHandler is created on the main thread, and does no IO by // FullRestoreFileHandler is created on the main thread, and does no IO by
// the constructor. The real work is done by WriteToFile and ReadFromFile, which // the constructor. The real work is done by WriteToFile and ReadFromFile, which
// must be invoked on a background task runner |owning_task_runner|. // must be invoked on a background task runner |owning_task_runner|.
//
// TODO(crbug.com/1146900): Add the ReadFromFile interface.
class COMPONENT_EXPORT(FULL_RESTORE) FullRestoreFileHandler class COMPONENT_EXPORT(FULL_RESTORE) FullRestoreFileHandler
: public base::RefCountedDeleteOnSequence<FullRestoreFileHandler> { : public base::RefCountedDeleteOnSequence<FullRestoreFileHandler> {
public: public:
...@@ -43,9 +41,13 @@ class COMPONENT_EXPORT(FULL_RESTORE) FullRestoreFileHandler ...@@ -43,9 +41,13 @@ class COMPONENT_EXPORT(FULL_RESTORE) FullRestoreFileHandler
} }
// Writes |restore_data| to the full restore file. This method must be invoked // Writes |restore_data| to the full restore file. This method must be invoked
// on a background task runer |owning_task_runner|. // on a background task runner |owning_task_runner|.
void WriteToFile(std::unique_ptr<RestoreData> restore_data); void WriteToFile(std::unique_ptr<RestoreData> restore_data);
// Reads |restore_data| to the full restore file. This method must be invoked
// on a background task runner |owning_task_runner|.
std::unique_ptr<RestoreData> ReadFromFile();
private: private:
friend class base::RefCountedDeleteOnSequence<FullRestoreFileHandler>; friend class base::RefCountedDeleteOnSequence<FullRestoreFileHandler>;
friend class base::DeleteHelper<FullRestoreFileHandler>; friend class base::DeleteHelper<FullRestoreFileHandler>;
...@@ -56,6 +58,11 @@ class COMPONENT_EXPORT(FULL_RESTORE) FullRestoreFileHandler ...@@ -56,6 +58,11 @@ class COMPONENT_EXPORT(FULL_RESTORE) FullRestoreFileHandler
// |owning_task_runner|. // |owning_task_runner|.
void WriteDataBlocking(const std::string& full_restore_data); void WriteDataBlocking(const std::string& full_restore_data);
// Performs blocking I/O. Called on a background task runner.
// |owning_task_runner|. Returns true on success and false on error. The
// reading result is written to |full_restore_data|.
bool ReadDataBlocking(std::string& full_restore_data);
base::FilePath file_path_; base::FilePath file_path_;
}; };
......
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/full_restore/full_restore_read_handler.h"
#include <utility>
#include "base/bind.h"
#include "base/files/file_path.h"
#include "base/no_destructor.h"
#include "base/task/post_task.h"
#include "base/threading/thread_task_runner_handle.h"
#include "components/full_restore/full_restore_file_handler.h"
#include "components/full_restore/restore_data.h"
namespace full_restore {
FullRestoreReadHandler* FullRestoreReadHandler::GetInstance() {
static base::NoDestructor<FullRestoreReadHandler> full_restore_read_handler;
return full_restore_read_handler.get();
}
FullRestoreReadHandler::FullRestoreReadHandler() = default;
FullRestoreReadHandler::~FullRestoreReadHandler() = default;
void FullRestoreReadHandler::ReadFromFile(const base::FilePath& file_path) {
auto file_handler = base::MakeRefCounted<FullRestoreFileHandler>(file_path);
file_handler->owning_task_runner()->PostTaskAndReplyWithResult(
FROM_HERE,
base::BindOnce(&FullRestoreFileHandler::ReadFromFile, file_handler.get()),
base::BindOnce(&FullRestoreReadHandler::OnGetRestoreData,
weak_factory_.GetWeakPtr(), file_path));
}
void FullRestoreReadHandler::OnGetRestoreData(
const base::FilePath& file_path,
std::unique_ptr<RestoreData> restore_data) {
// TODO(crbug.com/1146900): Implement the restore_data saving.
}
} // namespace full_restore
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef COMPONENTS_FULL_RESTORE_FULL_RESTORE_READ_HANDLER_H_
#define COMPONENTS_FULL_RESTORE_FULL_RESTORE_READ_HANDLER_H_
#include <memory>
#include "base/component_export.h"
#include "base/memory/weak_ptr.h"
namespace base {
class FilePath;
} // namespace base
namespace full_restore {
class FullRestoreFileHandler;
class RestoreData;
// FullRestoreSaveHandler is responsible for reading |RestoreData| from the full
// restore data file. RestoreHandler runs on the main thread and creates
// FullRestoreFileHandler (which runs on a background task runner) for the
// actual reading.
class COMPONENT_EXPORT(FULL_RESTORE) FullRestoreReadHandler {
public:
static FullRestoreReadHandler* GetInstance();
FullRestoreReadHandler();
virtual ~FullRestoreReadHandler();
FullRestoreReadHandler(const FullRestoreReadHandler&) = delete;
FullRestoreReadHandler& operator=(const FullRestoreReadHandler&) = delete;
void ReadFromFile(const base::FilePath& profile_dir);
private:
void OnGetRestoreData(const base::FilePath& file_path,
std::unique_ptr<RestoreData>);
base::WeakPtrFactory<FullRestoreReadHandler> weak_factory_{this};
};
} // namespace full_restore
#endif // COMPONENTS_FULL_RESTORE_FULL_RESTORE_READ_HANDLER_H_
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment