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_STORE_H_
6 #define COMPONENTS_DOM_DISTILLER_CORE_DOM_DISTILLER_STORE_H_
11 #include "base/containers/hash_tables.h"
12 #include "base/memory/weak_ptr.h"
13 #include "base/observer_list.h"
14 #include "components/dom_distiller/core/article_entry.h"
15 #include "components/dom_distiller/core/dom_distiller_database.h"
16 #include "components/dom_distiller/core/dom_distiller_model.h"
17 #include "components/dom_distiller/core/dom_distiller_observer.h"
18 #include "sync/api/sync_change.h"
19 #include "sync/api/sync_data.h"
20 #include "sync/api/sync_error.h"
21 #include "sync/api/sync_error_factory.h"
22 #include "sync/api/sync_merge_result.h"
23 #include "sync/api/syncable_service.h"
30 namespace dom_distiller
{
32 // Interface for accessing the stored/synced DomDistiller entries.
33 class DomDistillerStoreInterface
{
35 virtual ~DomDistillerStoreInterface() {}
37 // Gets the syncable service for this store or null if it is not synced.
38 virtual syncer::SyncableService
* GetSyncableService() = 0;
40 virtual bool AddEntry(const ArticleEntry
& entry
) = 0;
42 // Returns false if |entry| is not present or |entry| was not updated.
43 virtual bool UpdateEntry(const ArticleEntry
& entry
) = 0;
45 virtual bool RemoveEntry(const ArticleEntry
& entry
) = 0;
47 // Lookup an ArticleEntry by ID or URL. Returns whether a corresponding entry
48 // was found. On success, if |entry| is not null, it will contain the entry.
49 virtual bool GetEntryById(const std::string
& entry_id
,
50 ArticleEntry
* entry
) = 0;
51 virtual bool GetEntryByUrl(const GURL
& url
, ArticleEntry
* entry
) = 0;
53 // Gets a copy of all the current entries.
54 virtual std::vector
<ArticleEntry
> GetEntries() const = 0;
56 virtual void AddObserver(DomDistillerObserver
* observer
) = 0;
58 virtual void RemoveObserver(DomDistillerObserver
* observer
) = 0;
61 // Implements syncing/storing of DomDistiller entries. This keeps three
62 // models of the DOM distiller data in sync: the local database, sync, and the
63 // user (i.e. of DomDistillerStore). No changes are accepted while the local
64 // database is loading. Once the local database has loaded, changes from any of
65 // the three sources (technically just two, since changes don't come from the
66 // database) are handled similarly:
67 // 1. convert the change to a SyncChangeList.
68 // 2. apply that change to the in-memory model, calculating what changed
69 // (changes_applied) and what is missing--i.e. entries missing for a full merge,
70 // conflict resolution for normal changes-- (changes_missing).
71 // 3. send a message (possibly handled asynchronously) containing
72 // changes_missing to the source of the change.
73 // 4. send messages (possibly handled asynchronously) containing changes_applied
74 // to the other (i.e. non-source) two models.
75 // TODO(cjhopman): Support deleting entries.
76 class DomDistillerStore
: public syncer::SyncableService
,
77 public DomDistillerStoreInterface
{
79 // Creates storage using the given database for local storage. Initializes the
80 // database with |database_dir|.
81 DomDistillerStore(scoped_ptr
<DomDistillerDatabaseInterface
> database
,
82 const base::FilePath
& database_dir
);
84 // Creates storage using the given database for local storage. Initializes the
85 // database with |database_dir|. Also initializes the internal model to
87 DomDistillerStore(scoped_ptr
<DomDistillerDatabaseInterface
> database
,
88 const std::vector
<ArticleEntry
>& initial_data
,
89 const base::FilePath
& database_dir
);
91 virtual ~DomDistillerStore();
93 // DomDistillerStoreInterface implementation.
94 virtual syncer::SyncableService
* GetSyncableService() OVERRIDE
;
95 virtual bool AddEntry(const ArticleEntry
& entry
) OVERRIDE
;
96 virtual bool UpdateEntry(const ArticleEntry
& entry
) OVERRIDE
;
97 virtual bool RemoveEntry(const ArticleEntry
& entry
) OVERRIDE
;
98 virtual bool GetEntryById(const std::string
& entry_id
,
99 ArticleEntry
* entry
) OVERRIDE
;
100 virtual bool GetEntryByUrl(const GURL
& url
, ArticleEntry
* entry
) OVERRIDE
;
101 virtual std::vector
<ArticleEntry
> GetEntries() const OVERRIDE
;
102 virtual void AddObserver(DomDistillerObserver
* observer
) OVERRIDE
;
103 virtual void RemoveObserver(DomDistillerObserver
* observer
) OVERRIDE
;
105 // syncer::SyncableService implementation.
106 virtual syncer::SyncMergeResult
MergeDataAndStartSyncing(
107 syncer::ModelType type
,
108 const syncer::SyncDataList
& initial_sync_data
,
109 scoped_ptr
<syncer::SyncChangeProcessor
> sync_processor
,
110 scoped_ptr
<syncer::SyncErrorFactory
> error_handler
) OVERRIDE
;
111 virtual void StopSyncing(syncer::ModelType type
) OVERRIDE
;
112 virtual syncer::SyncDataList
GetAllSyncData(syncer::ModelType type
) const
114 virtual syncer::SyncError
ProcessSyncChanges(
115 const tracked_objects::Location
& from_here
,
116 const syncer::SyncChangeList
& change_list
) OVERRIDE
;
118 void OnDatabaseInit(bool success
);
119 void OnDatabaseLoad(bool success
, scoped_ptr
<EntryVector
> entries
);
120 void OnDatabaseSave(bool success
);
122 syncer::SyncMergeResult
MergeDataWithModel(
123 const syncer::SyncDataList
& data
,
124 syncer::SyncChangeList
* changes_applied
,
125 syncer::SyncChangeList
* changes_missing
);
127 // Convert a SyncDataList to a SyncChangeList of add or update changes based
128 // on the state of the in-memory model. Also calculate the entries missing
129 // from the SyncDataList.
130 void CalculateChangesForMerge(const syncer::SyncDataList
& data
,
131 syncer::SyncChangeList
* changes_to_apply
,
132 syncer::SyncChangeList
* changes_missing
);
134 bool ApplyChangesToSync(const tracked_objects::Location
& from_here
,
135 const syncer::SyncChangeList
& change_list
);
136 bool ApplyChangesToDatabase(const syncer::SyncChangeList
& change_list
);
138 // Applies the changes to |model_|. If the model returns an error, disables
139 // syncing and database changes and returns false.
140 void ApplyChangesToModel(const syncer::SyncChangeList
& change_list
,
141 syncer::SyncChangeList
* changes_applied
,
142 syncer::SyncChangeList
* changes_missing
);
144 void NotifyObservers(const syncer::SyncChangeList
& changes
);
146 scoped_ptr
<syncer::SyncChangeProcessor
> sync_processor_
;
147 scoped_ptr
<syncer::SyncErrorFactory
> error_factory_
;
148 scoped_ptr
<DomDistillerDatabaseInterface
> database_
;
149 bool database_loaded_
;
150 ObserverList
<DomDistillerObserver
> observers_
;
152 DomDistillerModel model_
;
154 base::WeakPtrFactory
<DomDistillerStore
> weak_ptr_factory_
;
156 DISALLOW_COPY_AND_ASSIGN(DomDistillerStore
);
159 } // namespace dom_distiller
161 #endif // COMPONENTS_DOM_DISTILLER_CORE_DOM_DISTILLER_STORE_H_