Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1166)

Side by Side Diff: components/dom_distiller/core/dom_distiller_database.h

Issue 56193004: Re-work the thread restrictions on the DOM distiller database (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef COMPONENTS_DOM_DISTILLER_CORE_DOM_DISTILLER_DATABASE_H_ 5 #ifndef COMPONENTS_DOM_DISTILLER_CORE_DOM_DISTILLER_DATABASE_H_
6 #define COMPONENTS_DOM_DISTILLER_CORE_DOM_DISTILLER_DATABASE_H_ 6 #define COMPONENTS_DOM_DISTILLER_CORE_DOM_DISTILLER_DATABASE_H_
7 7
8 #include <string> 8 #include <string>
9 #include <vector> 9 #include <vector>
10 10
11 #include "base/callback.h" 11 #include "base/callback.h"
12 #include "base/files/file_path.h" 12 #include "base/files/file_path.h"
13 #include "base/memory/ref_counted.h" 13 #include "base/memory/ref_counted.h"
14 #include "base/memory/scoped_ptr.h" 14 #include "base/memory/scoped_ptr.h"
15 #include "base/memory/scoped_vector.h" 15 #include "base/memory/scoped_vector.h"
16 #include "base/memory/weak_ptr.h" 16 #include "base/memory/weak_ptr.h"
17 #include "base/threading/thread_checker.h"
17 #include "components/dom_distiller/core/article_entry.h" 18 #include "components/dom_distiller/core/article_entry.h"
18 19
19 namespace base { 20 namespace base {
20 class SequencedTaskRunner; 21 class SequencedTaskRunner;
21 class MessageLoop; 22 class MessageLoop;
22 } 23 }
23 24
24 namespace leveldb { 25 namespace leveldb {
25 class DB; 26 class DB;
26 } 27 }
27 28
28 namespace dom_distiller { 29 namespace dom_distiller {
29 30
30 typedef std::vector<ArticleEntry> EntryVector; 31 typedef std::vector<ArticleEntry> EntryVector;
31 32
32 // Interface for classes providing persistent storage of DomDistiller entries. 33 // Interface for classes providing persistent storage of DomDistiller entries.
33 class DomDistillerDatabaseInterface { 34 class DomDistillerDatabaseInterface {
34 public: 35 public:
35 typedef std::vector<std::string> ArticleEntryIds; 36 typedef std::vector<std::string> ArticleEntryIds;
36 typedef base::Callback<void(bool success)> InitCallback; 37 typedef base::Callback<void(bool success)> InitCallback;
37 typedef base::Callback<void(bool success)> SaveCallback; 38 typedef base::Callback<void(bool success)> SaveCallback;
38 typedef base::Callback<void(bool success, scoped_ptr<EntryVector>)> 39 typedef base::Callback<void(bool success, scoped_ptr<EntryVector>)>
39 LoadCallback; 40 LoadCallback;
40 41
41 virtual ~DomDistillerDatabaseInterface() {} 42 virtual ~DomDistillerDatabaseInterface() {}
42 43
43 // Asynchronously destroys the object after all in-progress file operations
44 // have completed. The callbacks for in-progress operations will still be
45 // called.
46 virtual void Destroy() {}
47
48 // Asynchronously initializes the object. |callback| will be invoked on the UI 44 // Asynchronously initializes the object. |callback| will be invoked on the UI
49 // thread when complete. 45 // thread when complete.
50 virtual void Init(const base::FilePath& database_dir, 46 virtual void Init(const base::FilePath& database_dir,
51 InitCallback callback) = 0; 47 InitCallback callback) = 0;
52 48
53 // Asynchronously saves |entries_to_save| database. |callback| will be invoked 49 // Asynchronously saves |entries_to_save| database. |callback| will be invoked
54 // on the UI thread when complete. 50 // on the UI thread when complete.
55 virtual void SaveEntries(scoped_ptr<EntryVector> entries_to_save, 51 virtual void SaveEntries(scoped_ptr<EntryVector> entries_to_save,
56 SaveCallback callback) = 0; 52 SaveCallback callback) = 0;
57 53
58 // Asynchronously loads all entries from the database and invokes |callback| 54 // Asynchronously loads all entries from the database and invokes |callback|
59 // when complete. 55 // when complete.
60 virtual void LoadEntries(LoadCallback callback) = 0; 56 virtual void LoadEntries(LoadCallback callback) = 0;
61 }; 57 };
62 58
59 // When the DomDistillerDatabase instance is deleted, in-progress asynchronous
60 // operations will be completed and the corresponding callbacks will be called.
63 class DomDistillerDatabase 61 class DomDistillerDatabase
64 : public DomDistillerDatabaseInterface { 62 : public DomDistillerDatabaseInterface {
65 public: 63 public:
66 // The underlying database. Calls to this type may be blocking. 64 // The underlying database. Calls to this type may be blocking.
67 class Database { 65 class Database {
68 public: 66 public:
69 virtual bool Init(const base::FilePath& database_dir) = 0; 67 virtual bool Init(const base::FilePath& database_dir) = 0;
70 virtual bool Save(const EntryVector& entries) = 0; 68 virtual bool Save(const EntryVector& entries) = 0;
71 virtual bool Load(EntryVector* entries) = 0; 69 virtual bool Load(EntryVector* entries) = 0;
72 virtual ~Database() {} 70 virtual ~Database() {}
73 }; 71 };
74 72
73 // Once constructed, function calls and destruction should all occur on the
74 // same thread (not necessarily the same as the constructor).
75 class LevelDB : public Database { 75 class LevelDB : public Database {
76 public: 76 public:
77 LevelDB(); 77 LevelDB();
78 virtual ~LevelDB(); 78 virtual ~LevelDB();
79 virtual bool Init(const base::FilePath& database_dir) OVERRIDE; 79 virtual bool Init(const base::FilePath& database_dir) OVERRIDE;
80 virtual bool Save(const EntryVector& entries) OVERRIDE; 80 virtual bool Save(const EntryVector& entries) OVERRIDE;
81 virtual bool Load(EntryVector* entries) OVERRIDE; 81 virtual bool Load(EntryVector* entries) OVERRIDE;
82 82
83 private: 83 private:
84 84 base::ThreadChecker thread_checker_;
85 scoped_ptr<leveldb::DB> db_; 85 scoped_ptr<leveldb::DB> db_;
86 }; 86 };
87 87
88 DomDistillerDatabase(scoped_refptr<base::SequencedTaskRunner> task_runner); 88 DomDistillerDatabase(scoped_refptr<base::SequencedTaskRunner> task_runner);
89 89
90 virtual ~DomDistillerDatabase(); 90 virtual ~DomDistillerDatabase();
91 91
92 // DomDistillerDatabaseInterface implementation. 92 // DomDistillerDatabaseInterface implementation.
93 virtual void Destroy() OVERRIDE;
94 virtual void Init(const base::FilePath& database_dir, 93 virtual void Init(const base::FilePath& database_dir,
95 InitCallback callback) OVERRIDE; 94 InitCallback callback) OVERRIDE;
96 virtual void SaveEntries(scoped_ptr<EntryVector> entries_to_save, 95 virtual void SaveEntries(scoped_ptr<EntryVector> entries_to_save,
97 SaveCallback callback) OVERRIDE; 96 SaveCallback callback) OVERRIDE;
98 virtual void LoadEntries(LoadCallback callback) OVERRIDE; 97 virtual void LoadEntries(LoadCallback callback) OVERRIDE;
99 98
100 // Allow callers to provide their own Database implementation. 99 // Allow callers to provide their own Database implementation.
101 void InitWithDatabase(scoped_ptr<Database> database, 100 void InitWithDatabase(scoped_ptr<Database> database,
102 const base::FilePath& database_dir, 101 const base::FilePath& database_dir,
103 InitCallback callback); 102 InitCallback callback);
104 103
105 private: 104 private:
106 // Whether currently being run by |task_runner_|. 105 base::ThreadChecker thread_checker_;
107 bool IsRunByTaskRunner() const;
108
109 // Whether currently being run on |main_loop_|.
110 bool IsRunOnMainLoop() const;
111
112 // Deletes |this|.
113 void DestroyFromTaskRunner();
114
115 // Initializes the database in |database_dir| and updates |success|.
116 void InitFromTaskRunner(const base::FilePath& database_dir, bool* success);
117
118 // Saves data to disk and updates |success|.
119 void SaveEntriesFromTaskRunner(scoped_ptr<EntryVector> entries_to_save,
120 bool* success);
121
122 // Loads entries from disk and updates |success|.
123 void LoadEntriesFromTaskRunner(EntryVector* entries, bool* success);
124
125 // The MessageLoop that the database was created on.
126 base::MessageLoop* main_loop_;
127 106
128 // Used to run blocking tasks in-order. 107 // Used to run blocking tasks in-order.
129 scoped_refptr<base::SequencedTaskRunner> task_runner_; 108 scoped_refptr<base::SequencedTaskRunner> task_runner_;
130 109
131 scoped_ptr<Database> db_; 110 scoped_ptr<Database> db_;
132 111
133 // Note: This should remain the last member so it'll be destroyed and
134 // invalidate its weak pointers before any other members are destroyed.
135 base::WeakPtrFactory<DomDistillerDatabase> weak_ptr_factory_;
136
137 DISALLOW_COPY_AND_ASSIGN(DomDistillerDatabase); 112 DISALLOW_COPY_AND_ASSIGN(DomDistillerDatabase);
138 }; 113 };
139 114
140 } // namespace dom_distiller 115 } // namespace dom_distiller
141 116
142 #endif // COMPONENTS_DOM_DISTILLER_CORE_DOM_DISTILLER_DATABASE_H_ 117 #endif // COMPONENTS_DOM_DISTILLER_CORE_DOM_DISTILLER_DATABASE_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698