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 #include "components/dom_distiller/core/article_entry.h" 6 7 #include "base/logging.h" 8 #include "sync/api/sync_change.h" 9 10 using sync_pb::EntitySpecifics; 11 using sync_pb::ArticlePage; 12 using sync_pb::ArticleSpecifics; 13 14 namespace dom_distiller { 15 16 bool IsEntryPageValid(const ArticleEntryPage& page) { return page.has_url(); } 17 18 bool IsEntryValid(const ArticleEntry& entry) { 19 if (!entry.has_entry_id()) 20 return false; 21 for (int i = 0; i < entry.pages_size(); ++i) { 22 if (!IsEntryPageValid(entry.pages(i))) 23 return false; 24 } 25 return true; 26 } 27 28 bool AreEntriesEqual(const ArticleEntry& left, const ArticleEntry& right) { 29 DCHECK(IsEntryValid(left)); 30 DCHECK(IsEntryValid(right)); 31 return left.SerializeAsString() == right.SerializeAsString(); 32 } 33 34 ArticleEntry EntryFromSpecifics(const EntitySpecifics& specifics) { 35 DCHECK(specifics.has_article()); 36 const ArticleSpecifics& article_specifics = specifics.article(); 37 ArticleEntry entry = article_specifics; 38 DCHECK(IsEntryValid(entry)); 39 return entry; 40 } 41 42 EntitySpecifics SpecificsFromEntry(const ArticleEntry& entry) { 43 DCHECK(IsEntryValid(entry)); 44 EntitySpecifics specifics; 45 *specifics.mutable_article() = entry; 46 return specifics; 47 } 48 49 ArticleEntry GetEntryFromChange(const syncer::SyncChange& change) { 50 DCHECK(change.IsValid()); 51 DCHECK(change.sync_data().IsValid()); 52 return EntryFromSpecifics(change.sync_data().GetSpecifics()); 53 } 54 55 std::string GetEntryIdFromSyncData(const syncer::SyncData& data) { 56 const EntitySpecifics& entity = data.GetSpecifics(); 57 DCHECK(entity.has_article()); 58 const ArticleSpecifics& specifics = entity.article(); 59 DCHECK(specifics.has_entry_id()); 60 return specifics.entry_id(); 61 } 62 63 syncer::SyncData CreateLocalData(const ArticleEntry& entry) { 64 EntitySpecifics specifics = SpecificsFromEntry(entry); 65 const std::string& entry_id = entry.entry_id(); 66 return syncer::SyncData::CreateLocalData(entry_id, entry_id, specifics); 67 } 68 69 } // namespace dom_distiller 70