model_loader.h 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. // Copyright 2018 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_BOOKMARKS_BROWSER_MODEL_LOADER_H_
  5. #define COMPONENTS_BOOKMARKS_BROWSER_MODEL_LOADER_H_
  6. #include <memory>
  7. #include "base/callback_forward.h"
  8. #include "base/memory/ref_counted.h"
  9. #include "base/memory/scoped_refptr.h"
  10. #include "base/synchronization/waitable_event.h"
  11. namespace base {
  12. class FilePath;
  13. class SequencedTaskRunner;
  14. } // namespace base
  15. namespace bookmarks {
  16. class BookmarkLoadDetails;
  17. class HistoryBookmarkModel;
  18. // ModelLoader is created by BookmarkModel to track loading of BookmarkModel.
  19. // ModelLoader may be used on multiple threads. ModelLoader may outlive
  20. // BookmarkModel.
  21. class ModelLoader : public base::RefCountedThreadSafe<ModelLoader> {
  22. public:
  23. using LoadCallback =
  24. base::OnceCallback<void(std::unique_ptr<BookmarkLoadDetails>)>;
  25. // Creates the ModelLoader, and schedules loading on a backend task runner.
  26. // |callback| is run once loading completes (on the main thread).
  27. static scoped_refptr<ModelLoader> Create(
  28. const base::FilePath& file_path,
  29. std::unique_ptr<BookmarkLoadDetails> details,
  30. LoadCallback callback);
  31. ModelLoader(const ModelLoader&) = delete;
  32. ModelLoader& operator=(const ModelLoader&) = delete;
  33. // Blocks until loaded. This is intended for usage on a thread other than
  34. // the main thread.
  35. void BlockTillLoaded();
  36. // Returns null until the model has loaded. Use BlockTillLoaded() to ensure
  37. // this returns non-null.
  38. HistoryBookmarkModel* history_bookmark_model() {
  39. return history_bookmark_model_.get();
  40. }
  41. private:
  42. friend class base::RefCountedThreadSafe<ModelLoader>;
  43. ModelLoader();
  44. ~ModelLoader();
  45. // Performs the load on a background thread.
  46. std::unique_ptr<BookmarkLoadDetails> DoLoadOnBackgroundThread(
  47. const base::FilePath& file_path,
  48. std::unique_ptr<BookmarkLoadDetails> details);
  49. scoped_refptr<base::SequencedTaskRunner> backend_task_runner_;
  50. scoped_refptr<HistoryBookmarkModel> history_bookmark_model_;
  51. // Signaled once loading completes.
  52. base::WaitableEvent loaded_signal_;
  53. };
  54. } // namespace bookmarks
  55. #endif // COMPONENTS_BOOKMARKS_BROWSER_MODEL_LOADER_H_