| 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 |
| 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/background/request_queue_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 // SQLite implementation of RequestQueueStore. |
| 28 class RequestQueueStoreSQL : public RequestQueueStore { |
| 29 public: |
| 30 RequestQueueStoreSQL( |
| 31 scoped_refptr<base::SequencedTaskRunner> background_task_runner, |
| 32 const base::FilePath& database_dir); |
| 33 ~RequestQueueStoreSQL() override; |
| 34 |
| 35 // RequestQueueStore implementation. |
| 36 void GetRequests(const GetRequestsCallback& callback) override; |
| 37 void AddOrUpdateRequest(const SavePageRequest& offline_page, |
| 38 const UpdateCallback& callback) override; |
| 39 void RemoveRequests(const std::vector<int64_t>& request_ids, |
| 40 const RemoveCallback& callback) override; |
| 41 void Reset(const ResetCallback& callback) override; |
| 42 |
| 43 private: |
| 44 // Synchronous implementations, these are run on the background thread |
| 45 // and actually do the work to access SQL. The implementations above |
| 46 // simply dispatch to the corresponding *Sync method on the background thread. |
| 47 // 'runner' is where to run the callback. |
| 48 static void GetRequestsSync( |
| 49 sql::Connection* db, |
| 50 scoped_refptr<base::SingleThreadTaskRunner> runner, |
| 51 const GetRequestsCallback& callback); |
| 52 static void AddOrUpdateRequestSync( |
| 53 sql::Connection* db, |
| 54 scoped_refptr<base::SingleThreadTaskRunner> runner, |
| 55 const SavePageRequest& offline_page, |
| 56 const UpdateCallback& callback); |
| 57 static void RemoveRequestsSync( |
| 58 sql::Connection* db, |
| 59 scoped_refptr<base::SingleThreadTaskRunner> runner, |
| 60 const std::vector<int64_t>& request_ids, |
| 61 const RemoveCallback& callback); |
| 62 static void ResetSync(sql::Connection* db, |
| 63 const base::FilePath& db_file_path, |
| 64 scoped_refptr<base::SingleThreadTaskRunner> runner, |
| 65 const ResetCallback& callback); |
| 66 |
| 67 // Used to initialize DB connection. |
| 68 static void OpenConnectionSync( |
| 69 sql::Connection* db, |
| 70 scoped_refptr<base::SingleThreadTaskRunner> runner, |
| 71 const base::FilePath& path, |
| 72 const base::Callback<void(bool)>& callback); |
| 73 void OpenConnection(); |
| 74 void OnOpenConnectionDone(bool success); |
| 75 |
| 76 // Used to finalize connection reset. |
| 77 void OnResetDone(const ResetCallback& callback, bool success); |
| 78 |
| 79 // Background thread where all SQL access should be run. |
| 80 scoped_refptr<base::SequencedTaskRunner> background_task_runner_; |
| 81 |
| 82 // Path to the database on disk. |
| 83 base::FilePath db_file_path_; |
| 84 |
| 85 // Database connection. |
| 86 std::unique_ptr<sql::Connection> db_; |
| 87 |
| 88 base::WeakPtrFactory<RequestQueueStoreSQL> weak_ptr_factory_; |
| 89 DISALLOW_COPY_AND_ASSIGN(RequestQueueStoreSQL); |
| 90 }; |
| 91 |
| 92 } // namespace offline_pages |
| 93 |
| 94 #endif // COMPONENTS_OFFLINE_PAGES_BACKGROUND_REQUEST_QUEUE_STORE_SQL_H_ |
| OLD | NEW |