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.
5 #ifndef COMPONENTS_DOM_DISTILLER_CORE_DOM_DISTILLER_DATABASE_H_
6 #define COMPONENTS_DOM_DISTILLER_CORE_DOM_DISTILLER_DATABASE_H_
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 "components/dom_distiller/core/article_entry.h"
20 class SequencedTaskRunner;
28 namespace dom_distiller {
30 typedef std::vector<ArticleEntry> EntryVector;
32 // Interface for classes providing persistent storage of DomDistiller entries.
33 class DomDistillerDatabaseInterface {
35 typedef std::vector<std::string> ArticleEntryIds;
36 typedef base::Callback<void(bool success)> InitCallback;
37 typedef base::Callback<void(bool success)> SaveCallback;
38 typedef base::Callback<void(bool success, scoped_ptr<EntryVector>)>
41 virtual ~DomDistillerDatabaseInterface() {}
43 // Asynchronously destroys the object after all in-progress file operations
44 // have completed. The callbacks for in-progress operations will still be
46 virtual void Destroy() {}
48 // Asynchronously initializes the object. |callback| will be invoked on the UI
49 // thread when complete.
50 virtual void Init(const base::FilePath& database_dir,
51 InitCallback callback) = 0;
53 // Asynchronously saves |entries_to_save| database. |callback| will be invoked
54 // on the UI thread when complete.
55 virtual void SaveEntries(scoped_ptr<EntryVector> entries_to_save,
56 SaveCallback callback) = 0;
58 // Asynchronously loads all entries from the database and invokes |callback|
60 virtual void LoadEntries(LoadCallback callback) = 0;
63 class DomDistillerDatabase
64 : public DomDistillerDatabaseInterface {
66 // The underlying database. Calls to this type may be blocking.
69 virtual bool Init(const base::FilePath& database_dir) = 0;
70 virtual bool Save(const EntryVector& entries) = 0;
71 virtual bool Load(EntryVector* entries) = 0;
72 virtual ~Database() {}
75 class LevelDB : public Database {
79 virtual bool Init(const base::FilePath& database_dir) OVERRIDE;
80 virtual bool Save(const EntryVector& entries) OVERRIDE;
81 virtual bool Load(EntryVector* entries) OVERRIDE;
85 scoped_ptr<leveldb::DB> db_;
88 DomDistillerDatabase(scoped_refptr<base::SequencedTaskRunner> task_runner);
90 virtual ~DomDistillerDatabase();
92 // DomDistillerDatabaseInterface implementation.
93 virtual void Destroy() OVERRIDE;
94 virtual void Init(const base::FilePath& database_dir,
95 InitCallback callback) OVERRIDE;
96 virtual void SaveEntries(scoped_ptr<EntryVector> entries_to_save,
97 SaveCallback callback) OVERRIDE;
98 virtual void LoadEntries(LoadCallback callback) OVERRIDE;
100 // Allow callers to provide their own Database implementation.
101 void InitWithDatabase(scoped_ptr<Database> database,
102 const base::FilePath& database_dir,
103 InitCallback callback);
106 // Whether currently being run by |task_runner_|.
107 bool IsRunByTaskRunner() const;
109 // Whether currently being run on |main_loop_|.
110 bool IsRunOnMainLoop() const;
113 void DestroyFromTaskRunner();
115 // Initializes the database in |database_dir| and updates |success|.
116 void InitFromTaskRunner(const base::FilePath& database_dir, bool* success);
118 // Saves data to disk and updates |success|.
119 void SaveEntriesFromTaskRunner(scoped_ptr<EntryVector> entries_to_save,
122 // Loads entries from disk and updates |success|.
123 void LoadEntriesFromTaskRunner(EntryVector* entries, bool* success);
125 // The MessageLoop that the database was created on.
126 base::MessageLoop* main_loop_;
128 // Used to run blocking tasks in-order.
129 scoped_refptr<base::SequencedTaskRunner> task_runner_;
131 scoped_ptr<Database> db_;
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_;
137 DISALLOW_COPY_AND_ASSIGN(DomDistillerDatabase);
140 } // namespace dom_distiller
142 #endif // COMPONENTS_DOM_DISTILLER_CORE_DOM_DISTILLER_DATABASE_H_