download_db_impl.h 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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_DOWNLOAD_DATABASE_DOWNLOAD_DB_IMPL_H_
  5. #define COMPONENTS_DOWNLOAD_DATABASE_DOWNLOAD_DB_IMPL_H_
  6. #include <string>
  7. #include <vector>
  8. #include "base/files/file_path.h"
  9. #include "base/memory/weak_ptr.h"
  10. #include "components/download/database/download_db.h"
  11. #include "components/leveldb_proto/public/proto_database.h"
  12. namespace download_pb {
  13. class DownloadDBEntry;
  14. }
  15. namespace leveldb_proto {
  16. class ProtoDatabaseProvider;
  17. } // namespace leveldb_proto
  18. namespace download {
  19. // A protodb Implementation of DownloadDB.
  20. class DownloadDBImpl : public DownloadDB {
  21. public:
  22. DownloadDBImpl(DownloadNamespace download_namespace,
  23. const base::FilePath& database_dir,
  24. leveldb_proto::ProtoDatabaseProvider* db_provider);
  25. DownloadDBImpl(
  26. DownloadNamespace download_namespace,
  27. std::unique_ptr<
  28. leveldb_proto::ProtoDatabase<download_pb::DownloadDBEntry>> db);
  29. DownloadDBImpl(const DownloadDBImpl&) = delete;
  30. DownloadDBImpl& operator=(const DownloadDBImpl&) = delete;
  31. ~DownloadDBImpl() override;
  32. // DownloadDB implementation.
  33. void Initialize(DownloadDBCallback callback) override;
  34. void AddOrReplace(const DownloadDBEntry& entry) override;
  35. void AddOrReplaceEntries(const std::vector<DownloadDBEntry>& entries,
  36. DownloadDBCallback callback) override;
  37. void LoadEntries(LoadEntriesCallback callback) override;
  38. void Remove(const std::string& guid) override;
  39. private:
  40. friend class DownloadDBTest;
  41. bool IsInitialized();
  42. void DestroyAndReinitialize(DownloadDBCallback callback);
  43. // Returns the key of the db entry.
  44. std::string GetEntryKey(const std::string& guid) const;
  45. // Called when database is initialized.
  46. void OnDatabaseInitialized(DownloadDBCallback callback,
  47. leveldb_proto::Enums::InitStatus status);
  48. // Called when database is destroyed.
  49. void OnDatabaseDestroyed(DownloadDBCallback callback, bool success);
  50. // Called when entry is removed.
  51. void OnRemoveDone(bool success);
  52. // Called when all database entries are loaded.
  53. void OnAllEntriesLoaded(
  54. LoadEntriesCallback callback,
  55. bool success,
  56. std::unique_ptr<std::vector<download_pb::DownloadDBEntry>> entries);
  57. // Proto db for storing all the entries.
  58. std::unique_ptr<leveldb_proto::ProtoDatabase<download_pb::DownloadDBEntry>>
  59. db_;
  60. // Whether the object is initialized.
  61. bool is_initialized_ = false;
  62. // Namespace of this db.
  63. DownloadNamespace download_namespace_;
  64. // Number of initialize attempts.
  65. int num_initialize_attempts_ = 0;
  66. base::WeakPtrFactory<DownloadDBImpl> weak_factory_{this};
  67. };
  68. } // namespace download
  69. #endif // COMPONENTS_DOWNLOAD_DATABASE_DOWNLOAD_DB_IMPL_H_