offline_page_metadata_store.h 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. // Copyright 2016 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_OFFLINE_PAGES_CORE_OFFLINE_PAGE_METADATA_STORE_H_
  5. #define COMPONENTS_OFFLINE_PAGES_CORE_OFFLINE_PAGE_METADATA_STORE_H_
  6. #include <stdint.h>
  7. #include "base/bind.h"
  8. #include "base/files/file_path.h"
  9. #include "base/memory/weak_ptr.h"
  10. #include "base/task/task_runner_util.h"
  11. #include "base/threading/thread_task_runner_handle.h"
  12. #include "components/offline_pages/core/offline_page_item.h"
  13. #include "components/offline_pages/core/offline_store_types.h"
  14. #include "components/offline_pages/task/sql_store_base.h"
  15. namespace base {
  16. class SequencedTaskRunner;
  17. }
  18. namespace sql {
  19. class Database;
  20. }
  21. namespace offline_pages {
  22. typedef StoreUpdateResult<OfflinePageItem> OfflinePagesUpdateResult;
  23. // OfflinePageMetadataStore keeps metadata for the offline pages in an SQLite
  24. // database.
  25. //
  26. // When updating the schema, be sure to do the following:
  27. // * Increment the version number kCurrentVersion (let's call its new value N).
  28. // * Write a function "UpgradeFromVersion<N-1>ToVersion<N>". This function
  29. // should upgrade an existing database of the (previously) latest version and
  30. // should call meta_table->SetVersionNumber(N). Add a case for version N-1 to
  31. // the loop in CreateSchema.
  32. // * Update CreateLatestSchema() as necessary: this function creates a new empty
  33. // DB. If there were changes to existing tables, their original "CREATE"
  34. // queries should be copied into the new "UpgradeFromVersion..." function.
  35. // * Update `kCompatibleVersion` when a new schema becomes incompatible with
  36. // old code (for instance, if a column is removed). Change it to the earliest
  37. // version that is compatible with the new schema; that is very likely to be
  38. // the version that broke compatibility.
  39. // * Add a test for upgrading to the latest database version to
  40. // offline_page_metadata_store_unittest.cc. Good luck.
  41. //
  42. class OfflinePageMetadataStore : public SqlStoreBase {
  43. public:
  44. // This is the first version saved in the meta table, which was introduced in
  45. // the store in M65. It is set once a legacy upgrade is run successfully for
  46. // the last time in |UpgradeFromLegacyVersion|.
  47. static const int kFirstPostLegacyVersion = 1;
  48. static const int kCurrentVersion = 4;
  49. static const int kCompatibleVersion = kFirstPostLegacyVersion;
  50. // TODO(fgorski): Move to private and expose ForTest factory.
  51. // Applies in PrefetchStore as well.
  52. // Creates the store in memory. Should only be used for testing.
  53. explicit OfflinePageMetadataStore(
  54. scoped_refptr<base::SequencedTaskRunner> background_task_runner);
  55. // Creates the store with database pointing to provided directory.
  56. OfflinePageMetadataStore(
  57. scoped_refptr<base::SequencedTaskRunner> background_task_runner,
  58. const base::FilePath& database_dir);
  59. ~OfflinePageMetadataStore() override;
  60. // Helper function used to force incorrect state for testing purposes.
  61. StoreState GetStateForTesting() const;
  62. protected:
  63. // SqlStoreBase:
  64. base::OnceCallback<bool(sql::Database* db)> GetSchemaInitializationFunction()
  65. override;
  66. void OnOpenStart(base::TimeTicks last_open_time) override;
  67. void OnOpenDone(bool success) override;
  68. void OnTaskBegin(bool is_initialized) override;
  69. void OnTaskRunComplete() override;
  70. void OnTaskReturnComplete() override;
  71. void OnCloseStart(InitializationStatus status_before_close) override;
  72. void OnCloseComplete() override;
  73. };
  74. } // namespace offline_pages
  75. #endif // COMPONENTS_OFFLINE_PAGES_CORE_OFFLINE_PAGE_METADATA_STORE_H_