Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / components / dom_distiller / core / article_entry.cc
blob9ed9d7e4f09a107e3980663c64ef4053c2b442bd
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 #include "components/dom_distiller/core/article_entry.h"
7 #include "base/logging.h"
8 #include "components/dom_distiller/core/article_attachments_data.h"
9 #include "sync/api/sync_change.h"
11 using sync_pb::EntitySpecifics;
12 using sync_pb::ArticlePage;
13 using sync_pb::ArticleSpecifics;
15 namespace dom_distiller {
17 bool IsEntryPageValid(const ArticleEntryPage& page) { return page.has_url(); }
19 bool IsEntryValid(const ArticleEntry& entry) {
20 if (!entry.has_entry_id())
21 return false;
22 for (int i = 0; i < entry.pages_size(); ++i) {
23 if (!IsEntryPageValid(entry.pages(i)))
24 return false;
26 return true;
29 bool AreEntriesEqual(const ArticleEntry& left, const ArticleEntry& right) {
30 DCHECK(IsEntryValid(left));
31 DCHECK(IsEntryValid(right));
32 return left.SerializeAsString() == right.SerializeAsString();
35 ArticleEntry EntryFromSpecifics(const EntitySpecifics& specifics) {
36 DCHECK(specifics.has_article());
37 const ArticleSpecifics& article_specifics = specifics.article();
38 ArticleEntry entry = article_specifics;
39 DCHECK(IsEntryValid(entry));
40 return entry;
43 EntitySpecifics SpecificsFromEntry(const ArticleEntry& entry) {
44 DCHECK(IsEntryValid(entry));
45 EntitySpecifics specifics;
46 *specifics.mutable_article() = entry;
47 return specifics;
50 ArticleEntry GetEntryFromChange(const syncer::SyncChange& change) {
51 DCHECK(change.IsValid());
52 DCHECK(change.sync_data().IsValid());
53 return EntryFromSpecifics(change.sync_data().GetSpecifics());
56 std::string GetEntryIdFromSyncData(const syncer::SyncData& data) {
57 const EntitySpecifics& entity = data.GetSpecifics();
58 DCHECK(entity.has_article());
59 const ArticleSpecifics& specifics = entity.article();
60 DCHECK(specifics.has_entry_id());
61 return specifics.entry_id();
64 syncer::SyncData CreateLocalData(const ArticleEntry& entry) {
65 EntitySpecifics specifics = SpecificsFromEntry(entry);
66 const std::string& entry_id = entry.entry_id();
67 return syncer::SyncData::CreateLocalDataWithAttachments(
68 entry_id, entry_id, specifics, GetAttachmentIds(entry.attachments()));
71 } // namespace dom_distiller