- add sources.
[platform/framework/web/crosswalk.git] / src / components / dom_distiller / core / dom_distiller_database.h
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 "components/dom_distiller/core/article_entry.h"
18
19 namespace base {
20 class SequencedTaskRunner;
21 class MessageLoop;
22 }
23
24 namespace leveldb {
25 class DB;
26 }
27
28 namespace dom_distiller {
29
30 typedef std::vector<ArticleEntry> EntryVector;
31
32 // Interface for classes providing persistent storage of DomDistiller entries.
33 class DomDistillerDatabaseInterface {
34  public:
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>)>
39       LoadCallback;
40
41   virtual ~DomDistillerDatabaseInterface() {}
42
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
49   // thread when complete.
50   virtual void Init(const base::FilePath& database_dir,
51                     InitCallback callback) = 0;
52
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;
57
58   // Asynchronously loads all entries from the database and invokes |callback|
59   // when complete.
60   virtual void LoadEntries(LoadCallback callback) = 0;
61 };
62
63 class DomDistillerDatabase
64     : public DomDistillerDatabaseInterface {
65  public:
66   // The underlying database. Calls to this type may be blocking.
67   class Database {
68    public:
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() {}
73   };
74
75   class LevelDB : public Database {
76    public:
77     LevelDB();
78     virtual ~LevelDB();
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;
82
83    private:
84
85     scoped_ptr<leveldb::DB> db_;
86   };
87
88   DomDistillerDatabase(scoped_refptr<base::SequencedTaskRunner> task_runner);
89
90   virtual ~DomDistillerDatabase();
91
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;
99
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);
104
105  private:
106   // Whether currently being run by |task_runner_|.
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
128   // Used to run blocking tasks in-order.
129   scoped_refptr<base::SequencedTaskRunner> task_runner_;
130
131   scoped_ptr<Database> db_;
132
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);
138 };
139
140 }  // namespace dom_distiller
141
142 #endif  // COMPONENTS_DOM_DISTILLER_CORE_DOM_DISTILLER_DATABASE_H_