full_restore_file_handler.h 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. // Copyright 2020 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef COMPONENTS_APP_RESTORE_FULL_RESTORE_FILE_HANDLER_H_
  5. #define COMPONENTS_APP_RESTORE_FULL_RESTORE_FILE_HANDLER_H_
  6. #include <memory>
  7. #include "base/component_export.h"
  8. #include "base/files/file_path.h"
  9. #include "base/memory/ref_counted_delete_on_sequence.h"
  10. #include "base/task/sequenced_task_runner_helpers.h"
  11. class SequencedTaskRunner;
  12. namespace app_restore {
  13. class RestoreData;
  14. }
  15. namespace full_restore {
  16. // FullRestoreFileHandler is the backend used by FullRestoreSaveHandler and
  17. // RestoreHandler. It reads and writes RestoreData from and to disk.
  18. // FullRestoreFileHandler is created on the main thread, and does no IO by
  19. // the constructor. The real work is done by WriteToFile and ReadFromFile, which
  20. // must be invoked on a background task runner |owning_task_runner|.
  21. class COMPONENT_EXPORT(APP_RESTORE) FullRestoreFileHandler
  22. : public base::RefCountedDeleteOnSequence<FullRestoreFileHandler> {
  23. public:
  24. // Creates a FullRestoreFileHandler. This method is invoked on the main
  25. // thread, and does no IO. |path| is the path of the full restore file.
  26. FullRestoreFileHandler(const base::FilePath& path);
  27. FullRestoreFileHandler(const FullRestoreFileHandler&) = delete;
  28. FullRestoreFileHandler& operator=(const FullRestoreFileHandler&) = delete;
  29. base::SequencedTaskRunner* owning_task_runner() {
  30. return base::RefCountedDeleteOnSequence<
  31. FullRestoreFileHandler>::owning_task_runner();
  32. }
  33. // Writes |restore_data| to the full restore file. This method must be invoked
  34. // on a background task runner |owning_task_runner|.
  35. void WriteToFile(std::unique_ptr<app_restore::RestoreData> restore_data);
  36. // Reads |restore_data| to the full restore file. This method must be invoked
  37. // on a background task runner |owning_task_runner|.
  38. std::unique_ptr<app_restore::RestoreData> ReadFromFile();
  39. private:
  40. friend class base::RefCountedDeleteOnSequence<FullRestoreFileHandler>;
  41. friend class base::DeleteHelper<FullRestoreFileHandler>;
  42. virtual ~FullRestoreFileHandler();
  43. // Performs blocking I/O. Called on a background task runner
  44. // |owning_task_runner|.
  45. void WriteDataBlocking(const std::string& full_restore_data);
  46. // Performs blocking I/O. Called on a background task runner.
  47. // |owning_task_runner|. Returns true on success and false on error. The
  48. // reading result is written to |full_restore_data|.
  49. bool ReadDataBlocking(std::string& full_restore_data);
  50. base::FilePath file_path_;
  51. };
  52. } // namespace full_restore
  53. #endif // COMPONENTS_APP_RESTORE_FULL_RESTORE_FILE_HANDLER_H_