1 // Copyright 2017 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/sync/model_impl/sync_metadata_store_change_list.h"
6 
7 #include "base/location.h"
8 
9 using base::Optional;
10 using syncer::ModelError;
11 
12 namespace syncer {
13 
SyncMetadataStoreChangeList(SyncMetadataStore * store,syncer::ModelType type)14 SyncMetadataStoreChangeList::SyncMetadataStoreChangeList(
15     SyncMetadataStore* store,
16     syncer::ModelType type)
17     : store_(store), type_(type) {
18   if (!store_) {
19     error_ = ModelError(FROM_HERE, "Invalid SyncMetadataStore");
20   }
21 }
22 
~SyncMetadataStoreChangeList()23 SyncMetadataStoreChangeList::~SyncMetadataStoreChangeList() {
24   DCHECK(!error_);
25 }
26 
UpdateModelTypeState(const sync_pb::ModelTypeState & model_type_state)27 void SyncMetadataStoreChangeList::UpdateModelTypeState(
28     const sync_pb::ModelTypeState& model_type_state) {
29   if (error_) {
30     return;
31   }
32 
33   if (!store_->UpdateModelTypeState(type_, model_type_state)) {
34     error_ = ModelError(FROM_HERE, "Failed to update ModelTypeState.");
35   }
36 }
37 
ClearModelTypeState()38 void SyncMetadataStoreChangeList::ClearModelTypeState() {
39   if (error_) {
40     return;
41   }
42 
43   if (!store_->ClearModelTypeState(type_)) {
44     error_ = ModelError(FROM_HERE, "Failed to clear ModelTypeState.");
45   }
46 }
47 
UpdateMetadata(const std::string & storage_key,const sync_pb::EntityMetadata & metadata)48 void SyncMetadataStoreChangeList::UpdateMetadata(
49     const std::string& storage_key,
50     const sync_pb::EntityMetadata& metadata) {
51   if (error_) {
52     return;
53   }
54 
55   if (!store_->UpdateSyncMetadata(type_, storage_key, metadata)) {
56     error_ = ModelError(FROM_HERE, "Failed to update entity metadata.");
57   }
58 }
59 
ClearMetadata(const std::string & storage_key)60 void SyncMetadataStoreChangeList::ClearMetadata(
61     const std::string& storage_key) {
62   if (error_) {
63     return;
64   }
65 
66   if (!store_->ClearSyncMetadata(type_, storage_key)) {
67     error_ = ModelError(FROM_HERE, "Failed to clear entity metadata.");
68   }
69 }
70 
TakeError()71 Optional<ModelError> SyncMetadataStoreChangeList::TakeError() {
72   Optional<ModelError> temp = error_;
73   error_.reset();
74   return temp;
75 }
76 
77 const SyncMetadataStore*
GetMetadataStoreForTesting() const78 SyncMetadataStoreChangeList::GetMetadataStoreForTesting() const {
79   return store_;
80 }
81 
82 }  // namespace syncer
83