OLD | NEW |
| (Empty) |
1 // Copyright 2013 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_DOM_DISTILLER_CORE_DOM_DISTILLER_DATABASE_H_ | |
6 #define COMPONENTS_DOM_DISTILLER_CORE_DOM_DISTILLER_DATABASE_H_ | |
7 | |
8 #include <string> | |
9 #include <vector> | |
10 | |
11 #include "base/callback.h" | |
12 #include "base/files/file_path.h" | |
13 #include "base/memory/ref_counted.h" | |
14 #include "base/memory/scoped_ptr.h" | |
15 #include "base/memory/scoped_vector.h" | |
16 #include "base/memory/weak_ptr.h" | |
17 #include "base/threading/thread_checker.h" | |
18 #include "base/threading/thread_collision_warner.h" | |
19 #include "components/dom_distiller/core/article_entry.h" | |
20 | |
21 namespace base { | |
22 class SequencedTaskRunner; | |
23 class MessageLoop; | |
24 } | |
25 | |
26 namespace leveldb { | |
27 class DB; | |
28 } | |
29 | |
30 namespace dom_distiller { | |
31 | |
32 typedef std::vector<ArticleEntry> EntryVector; | |
33 | |
34 // Interface for classes providing persistent storage of DomDistiller entries. | |
35 class DomDistillerDatabaseInterface { | |
36 public: | |
37 typedef std::vector<std::string> ArticleEntryIds; | |
38 typedef base::Callback<void(bool success)> InitCallback; | |
39 typedef base::Callback<void(bool success)> UpdateCallback; | |
40 typedef base::Callback<void(bool success, scoped_ptr<EntryVector>)> | |
41 LoadCallback; | |
42 | |
43 virtual ~DomDistillerDatabaseInterface() {} | |
44 | |
45 // Asynchronously initializes the object. |callback| will be invoked on the UI | |
46 // thread when complete. | |
47 virtual void Init(const base::FilePath& database_dir, | |
48 InitCallback callback) = 0; | |
49 | |
50 // Asynchronously saves |entries_to_save| and deletes entries from | |
51 // |entries_to_remove| from the database. |callback| will be invoked on the UI | |
52 // thread when complete. | |
53 virtual void UpdateEntries(scoped_ptr<EntryVector> entries_to_save, | |
54 scoped_ptr<EntryVector> entries_to_remove, | |
55 UpdateCallback callback) = 0; | |
56 | |
57 // Asynchronously loads all entries from the database and invokes |callback| | |
58 // when complete. | |
59 virtual void LoadEntries(LoadCallback callback) = 0; | |
60 }; | |
61 | |
62 // When the DomDistillerDatabase instance is deleted, in-progress asynchronous | |
63 // operations will be completed and the corresponding callbacks will be called. | |
64 class DomDistillerDatabase | |
65 : public DomDistillerDatabaseInterface { | |
66 public: | |
67 // The underlying database. Calls to this type may be blocking. | |
68 class Database { | |
69 public: | |
70 virtual bool Init(const base::FilePath& database_dir) = 0; | |
71 virtual bool Save(const EntryVector& entries_to_save, | |
72 const EntryVector& entries_to_remove) = 0; | |
73 virtual bool Load(EntryVector* entries) = 0; | |
74 virtual ~Database() {} | |
75 }; | |
76 | |
77 // Once constructed, function calls and destruction should all occur on the | |
78 // same thread (not necessarily the same as the constructor). | |
79 class LevelDB : public Database { | |
80 public: | |
81 LevelDB(); | |
82 virtual ~LevelDB(); | |
83 virtual bool Init(const base::FilePath& database_dir) OVERRIDE; | |
84 virtual bool Save(const EntryVector& entries_to_save, | |
85 const EntryVector& entries_to_remove) OVERRIDE; | |
86 virtual bool Load(EntryVector* entries) OVERRIDE; | |
87 | |
88 private: | |
89 DFAKE_MUTEX(thread_checker_); | |
90 scoped_ptr<leveldb::DB> db_; | |
91 }; | |
92 | |
93 explicit DomDistillerDatabase( | |
94 scoped_refptr<base::SequencedTaskRunner> task_runner); | |
95 | |
96 virtual ~DomDistillerDatabase(); | |
97 | |
98 // DomDistillerDatabaseInterface implementation. | |
99 virtual void Init(const base::FilePath& database_dir, | |
100 InitCallback callback) OVERRIDE; | |
101 virtual void UpdateEntries(scoped_ptr<EntryVector> entries_to_save, | |
102 scoped_ptr<EntryVector> entries_to_remove, | |
103 UpdateCallback callback) OVERRIDE; | |
104 virtual void LoadEntries(LoadCallback callback) OVERRIDE; | |
105 | |
106 // Allow callers to provide their own Database implementation. | |
107 void InitWithDatabase(scoped_ptr<Database> database, | |
108 const base::FilePath& database_dir, | |
109 InitCallback callback); | |
110 | |
111 private: | |
112 base::ThreadChecker thread_checker_; | |
113 | |
114 // Used to run blocking tasks in-order. | |
115 scoped_refptr<base::SequencedTaskRunner> task_runner_; | |
116 | |
117 scoped_ptr<Database> db_; | |
118 | |
119 DISALLOW_COPY_AND_ASSIGN(DomDistillerDatabase); | |
120 }; | |
121 | |
122 } // namespace dom_distiller | |
123 | |
124 #endif // COMPONENTS_DOM_DISTILLER_CORE_DOM_DISTILLER_DATABASE_H_ | |
OLD | NEW |