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 // | |
39 // Here is a procedure to update the schema for this store: | |
40 // * Decide how to detect that the store is on a particular version, which | |
41 // typically means that a certain field exists or is missing. This happens in | |
42 // Upgrade section of |CreateSchema| | |
43 // * Work out appropriate change and apply it to all existing upgrade paths. In | |
44 // the interest of performing a single update of the store, it upgrades from a | |
45 // detected version to the current one. This means that when making a change, | |
46 // more than a single query may have to be updated (in case of fields being | |
47 // removed or needed to be initialized to a specific, non-default value). | |
48 // Such approach is preferred to doing N updates for every changed version on | |
49 // a startup after browser update. | |
50 // * New upgrade method should specify which version it is upgrading from, e.g. | |
51 // |UpgradeFrom54|. | |
52 // * Upgrade should use |UpgradeWithQuery| and simply specify SQL command to | |
53 // move data from old table (prefixed by temp_) to the new one. | |
54 class OfflinePageMetadataStoreSQL : public OfflinePageMetadataStore { | |
55 public: | |
56 OfflinePageMetadataStoreSQL( | |
57 scoped_refptr<base::SequencedTaskRunner> background_task_runner, | |
58 const base::FilePath& database_dir); | |
59 ~OfflinePageMetadataStoreSQL() override; | |
60 | |
61 // Implementation methods. | |
62 void Initialize(const InitializeCallback& callback) override; | |
63 void GetOfflinePages(const LoadCallback& callback) override; | |
64 void AddOfflinePage(const OfflinePageItem& offline_page, | |
65 const AddCallback& callback) override; | |
66 void UpdateOfflinePages(const std::vector<OfflinePageItem>& pages, | |
67 const UpdateCallback& callback) override; | |
68 void RemoveOfflinePages(const std::vector<int64_t>& offline_ids, | |
69 const UpdateCallback& callback) override; | |
70 void Reset(const ResetCallback& callback) override; | |
71 StoreState state() const override; | |
72 | |
73 // Helper function used to force incorrect state for testing purposes. | |
74 void SetStateForTesting(StoreState state, bool reset_db); | |
75 | |
76 private: | |
77 // Used to conclude opening/resetting DB connection. | |
78 void OnOpenConnectionDone(const InitializeCallback& callback, bool success); | |
79 void OnResetDone(const ResetCallback& callback, bool success); | |
80 | |
81 // Checks whether a valid DB connection is present and store state is LOADED. | |
82 bool CheckDb(); | |
83 | |
84 // Background thread where all SQL access should be run. | |
85 scoped_refptr<base::SequencedTaskRunner> background_task_runner_; | |
86 | |
87 // Path to the database on disk. | |
88 base::FilePath db_file_path_; | |
89 | |
90 // Database connection. | |
91 std::unique_ptr<sql::Connection> db_; | |
92 | |
93 // State of the store. | |
94 StoreState state_; | |
95 | |
96 base::WeakPtrFactory<OfflinePageMetadataStoreSQL> weak_ptr_factory_; | |
97 | |
98 DISALLOW_COPY_AND_ASSIGN(OfflinePageMetadataStoreSQL); | |
99 }; | |
100 | |
101 } // namespace offline_pages | |
102 | |
103 #endif // COMPONENTS_OFFLINE_PAGES_OFFLINE_PAGE_METADATA_STORE_SQL_H_ | |
OLD | NEW |