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_MODEL_H_ 6 #define COMPONENTS_DOM_DISTILLER_CORE_DOM_DISTILLER_MODEL_H_ 7 8 #include <string> 9 #include <vector> 10 11 #include "base/containers/hash_tables.h" 12 #include "base/id_map.h" 13 #include "components/dom_distiller/core/article_entry.h" 14 #include "sync/api/sync_change.h" 15 #include "sync/api/sync_change_processor.h" // syncer::SyncChangeList 16 #include "sync/api/sync_data.h" 17 #include "url/gurl.h" 18 19 namespace dom_distiller { 20 21 // This stores the in-memory model of the DOM distiller list. Entries can be 22 // looked up by URL or by entry_id. 23 // The model assumes that an URL corresponds to at most a single entry. If this 24 // assumption is broken, lookup by URL may return unexpected results. 25 class DomDistillerModel { 26 public: 27 DomDistillerModel(); 28 explicit DomDistillerModel(const std::vector<ArticleEntry>& initial_data); 29 30 ~DomDistillerModel(); 31 32 // Lookup an ArticleEntry by ID or URL. Returns whether a corresponding entry 33 // was found. On success, if |entry| is not null, it will contain the entry. 34 bool GetEntryById(const std::string& entry_id, ArticleEntry* entry) const; 35 bool GetEntryByUrl(const GURL& url, ArticleEntry* entry) const; 36 37 std::vector<ArticleEntry> GetEntries() const; 38 size_t GetNumEntries() const; 39 40 syncer::SyncDataList GetAllSyncData() const; 41 42 // Convert a SyncDataList to a SyncChangeList of add or update changes based 43 // on the state of the model. Also calculate the entries missing from the 44 // SyncDataList. 45 void CalculateChangesForMerge(const syncer::SyncDataList& data, 46 syncer::SyncChangeList* changes_to_apply, 47 syncer::SyncChangeList* changes_missing); 48 49 // Applies the change list to the model, appending the actual changes made to 50 // the model to |changes_applied|. If conflict resolution does not apply the 51 // requested change, then adds the "diff" between what was requested and what 52 // was actually applied to |changes_missing|. 53 // Note: Currently conflicts are resolved by just applying the requested 54 // change. This means nothing will be added to |changes_missing|. 55 void ApplyChangesToModel(const syncer::SyncChangeList& change_list, 56 syncer::SyncChangeList* changes_applied, 57 syncer::SyncChangeList* changes_missing); 58 59 private: 60 typedef int32 KeyType; 61 typedef base::hash_map<KeyType, ArticleEntry> EntryMap; 62 typedef base::hash_map<std::string, KeyType> StringToKeyMap; 63 64 void AddEntry(const ArticleEntry& entry); 65 void RemoveEntry(const ArticleEntry& entry); 66 67 // Lookup an entry's key by ID or URL. Returns whether a corresponding key was 68 // found. On success, if |key| is not null, it will contain the entry. 69 bool GetKeyById(const std::string& entry_id, KeyType* key) const; 70 bool GetKeyByUrl(const GURL& url, KeyType* key) const; 71 72 // If |entry| is not null, assigns the entry for |key| to it. |key| must map 73 // to an entry in |entries_|. 74 void GetEntryByKey(KeyType key, ArticleEntry* entry) const; 75 76 void ApplyChangeToModel(const syncer::SyncChange& change, 77 syncer::SyncChangeList* changes_applied, 78 syncer::SyncChangeList* changes_missing); 79 80 KeyType next_key_; 81 EntryMap entries_; 82 StringToKeyMap url_to_key_map_; 83 StringToKeyMap entry_id_to_key_map_; 84 85 DISALLOW_COPY_AND_ASSIGN(DomDistillerModel); 86 }; 87 88 } // namespace dom_distiller 89 90 #endif // COMPONENTS_DOM_DISTILLER_CORE_DOM_DISTILLER_MODEL_H_ 91