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 "base/threading/thread_checker.h"
18 #include "base/threading/thread_collision_warner.h"
19 #include "components/dom_distiller/core/article_entry.h"
22 class SequencedTaskRunner
;
30 namespace dom_distiller
{
32 typedef std::vector
<ArticleEntry
> EntryVector
;
34 // Interface for classes providing persistent storage of DomDistiller entries.
35 class DomDistillerDatabaseInterface
{
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
>)>
43 virtual ~DomDistillerDatabaseInterface() {}
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;
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;
57 // Asynchronously loads all entries from the database and invokes |callback|
59 virtual void LoadEntries(LoadCallback callback
) = 0;
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
{
67 // The underlying database. Calls to this type may be blocking.
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() {}
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
{
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
;
89 DFAKE_MUTEX(thread_checker_
);
90 scoped_ptr
<leveldb::DB
> db_
;
93 explicit DomDistillerDatabase(
94 scoped_refptr
<base::SequencedTaskRunner
> task_runner
);
96 virtual ~DomDistillerDatabase();
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
;
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
);
112 base::ThreadChecker thread_checker_
;
114 // Used to run blocking tasks in-order.
115 scoped_refptr
<base::SequencedTaskRunner
> task_runner_
;
117 scoped_ptr
<Database
> db_
;
119 DISALLOW_COPY_AND_ASSIGN(DomDistillerDatabase
);
122 } // namespace dom_distiller
124 #endif // COMPONENTS_DOM_DISTILLER_CORE_DOM_DISTILLER_DATABASE_H_