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_BACKGROUND_REQUEST_QUEUE_STORE_SQL_H_ | |
6 #define COMPONENTS_OFFLINE_PAGES_BACKGROUND_REQUEST_QUEUE_STORE_SQL_H_ | |
7 | |
8 #include <stdint.h> | |
9 #include <memory> | |
10 #include <vector> | |
11 | |
12 #include "base/files/file_path.h" | |
13 #include "base/memory/weak_ptr.h" | |
14 #include "components/offline_pages/background/request_queue_store.h" | |
15 | |
16 namespace base { | |
17 class SequencedTaskRunner; | |
18 } | |
19 | |
20 namespace sql { | |
21 class Connection; | |
22 } | |
23 | |
24 namespace offline_pages { | |
25 | |
26 // SQLite implementation of RequestQueueStore. | |
27 class RequestQueueStoreSQL : public RequestQueueStore { | |
28 public: | |
29 RequestQueueStoreSQL( | |
30 scoped_refptr<base::SequencedTaskRunner> background_task_runner, | |
31 const base::FilePath& database_dir); | |
32 ~RequestQueueStoreSQL() override; | |
33 | |
34 // RequestQueueStore implementation. | |
35 void GetRequests(const GetRequestsCallback& callback) override; | |
36 // Note: current implementation of this method makes a SQL query per ID. This | |
37 // is OK as long as number of IDs stays low, which is a typical case. | |
38 // Implementation should be revisited in case that presumption changes. | |
39 void GetRequestsByIds(const std::vector<int64_t>& request_ids, | |
40 const UpdateCallback& callback) override; | |
41 void AddRequest(const SavePageRequest& offline_page, | |
42 const AddCallback& callback) override; | |
43 void UpdateRequests(const std::vector<SavePageRequest>& requests, | |
44 const UpdateCallback& callback) override; | |
45 void RemoveRequests(const std::vector<int64_t>& request_ids, | |
46 const UpdateCallback& callback) override; | |
47 void Reset(const ResetCallback& callback) override; | |
48 StoreState state() const override; | |
49 | |
50 private: | |
51 // Helper functions to return immediately if no database is found. | |
52 bool CheckDb(const base::Closure& callback); | |
53 | |
54 // Used to initialize DB connection. | |
55 void OpenConnection(); | |
56 void OnOpenConnectionDone(StoreState state); | |
57 | |
58 // Used to finalize connection reset. | |
59 void OnResetDone(const ResetCallback& callback, StoreState state); | |
60 | |
61 // Background thread where all SQL access should be run. | |
62 scoped_refptr<base::SequencedTaskRunner> background_task_runner_; | |
63 | |
64 // Path to the database on disk. | |
65 base::FilePath db_file_path_; | |
66 | |
67 // Database connection. | |
68 std::unique_ptr<sql::Connection> db_; | |
69 | |
70 // State of the store. | |
71 StoreState state_; | |
72 | |
73 base::WeakPtrFactory<RequestQueueStoreSQL> weak_ptr_factory_; | |
74 DISALLOW_COPY_AND_ASSIGN(RequestQueueStoreSQL); | |
75 }; | |
76 | |
77 } // namespace offline_pages | |
78 | |
79 #endif // COMPONENTS_OFFLINE_PAGES_BACKGROUND_REQUEST_QUEUE_STORE_SQL_H_ | |
OLD | NEW |