OLD | NEW |
| (Empty) |
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 | |
5 #ifndef COMPONENTS_OFFLINE_PAGES_OFFLINE_PAGE_METADATA_STORE_SQL_H_ | |
6 #define COMPONENTS_OFFLINE_PAGES_OFFLINE_PAGE_METADATA_STORE_SQL_H_ | |
7 | |
8 #include <stdint.h> | |
9 | |
10 #include <memory> | |
11 #include <vector> | |
12 | |
13 #include "base/files/file_path.h" | |
14 #include "base/memory/weak_ptr.h" | |
15 #include "components/offline_pages/offline_page_metadata_store.h" | |
16 | |
17 namespace base { | |
18 class SequencedTaskRunner; | |
19 } | |
20 | |
21 namespace sql { | |
22 class Connection; | |
23 } | |
24 | |
25 namespace offline_pages { | |
26 | |
27 // OfflinePageMetadataStoreSQL is an instance of OfflinePageMetadataStore | |
28 // which is implemented using a SQLite database. | |
29 // | |
30 // This store has a history of schema updates in pretty much every release. | |
31 // Original schema was delivered in M52. Since then, the following changes | |
32 // happened: | |
33 // * In M53 expiration_time was added, | |
34 // * In M54 title was added, | |
35 // * In M55 we dropped the following fields (never used): version, status, | |
36 // offline_url, user_initiated. | |
37 // * In M56 original_url was added. | |
38 // * In M57 expiration_time was dropped. Existing expired pages would be | |
39 // removed when metadata consistency check happens. | |
40 // | |
41 // Here is a procedure to update the schema for this store: | |
42 // * Decide how to detect that the store is on a particular version, which | |
43 // typically means that a certain field exists or is missing. This happens in | |
44 // Upgrade section of |CreateSchema| | |
45 // * Work out appropriate change and apply it to all existing upgrade paths. In | |
46 // the interest of performing a single update of the store, it upgrades from a | |
47 // detected version to the current one. This means that when making a change, | |
48 // more than a single query may have to be updated (in case of fields being | |
49 // removed or needed to be initialized to a specific, non-default value). | |
50 // Such approach is preferred to doing N updates for every changed version on | |
51 // a startup after browser update. | |
52 // * New upgrade method should specify which version it is upgrading from, e.g. | |
53 // |UpgradeFrom54|. | |
54 // * Upgrade should use |UpgradeWithQuery| and simply specify SQL command to | |
55 // move data from old table (prefixed by temp_) to the new one. | |
56 class OfflinePageMetadataStoreSQL : public OfflinePageMetadataStore { | |
57 public: | |
58 OfflinePageMetadataStoreSQL( | |
59 scoped_refptr<base::SequencedTaskRunner> background_task_runner, | |
60 const base::FilePath& database_dir); | |
61 ~OfflinePageMetadataStoreSQL() override; | |
62 | |
63 // Implementation methods. | |
64 void Initialize(const InitializeCallback& callback) override; | |
65 void GetOfflinePages(const LoadCallback& callback) override; | |
66 void AddOfflinePage(const OfflinePageItem& offline_page, | |
67 const AddCallback& callback) override; | |
68 void UpdateOfflinePages(const std::vector<OfflinePageItem>& pages, | |
69 const UpdateCallback& callback) override; | |
70 void RemoveOfflinePages(const std::vector<int64_t>& offline_ids, | |
71 const UpdateCallback& callback) override; | |
72 void Reset(const ResetCallback& callback) override; | |
73 StoreState state() const override; | |
74 | |
75 // Helper function used to force incorrect state for testing purposes. | |
76 void SetStateForTesting(StoreState state, bool reset_db); | |
77 | |
78 private: | |
79 // Used to conclude opening/resetting DB connection. | |
80 void OnOpenConnectionDone(const InitializeCallback& callback, bool success); | |
81 void OnResetDone(const ResetCallback& callback, bool success); | |
82 | |
83 // Checks whether a valid DB connection is present and store state is LOADED. | |
84 bool CheckDb() const; | |
85 | |
86 // Background thread where all SQL access should be run. | |
87 scoped_refptr<base::SequencedTaskRunner> background_task_runner_; | |
88 | |
89 // Path to the database on disk. | |
90 base::FilePath db_file_path_; | |
91 | |
92 // Database connection. | |
93 std::unique_ptr<sql::Connection> db_; | |
94 | |
95 // State of the store. | |
96 StoreState state_; | |
97 | |
98 base::WeakPtrFactory<OfflinePageMetadataStoreSQL> weak_ptr_factory_; | |
99 | |
100 DISALLOW_COPY_AND_ASSIGN(OfflinePageMetadataStoreSQL); | |
101 }; | |
102 | |
103 } // namespace offline_pages | |
104 | |
105 #endif // COMPONENTS_OFFLINE_PAGES_OFFLINE_PAGE_METADATA_STORE_SQL_H_ | |
OLD | NEW |