chromeos: Fix StartupBrowserCreatorImpl::Launch crash.
[chromium-blink-merge.git] / sync / internal_api / read_node.cc
blob9f3178170d0afba63a31fe218ca983ae9962bd8a
1 // Copyright (c) 2012 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 "sync/internal_api/public/read_node.h"
7 #include "base/logging.h"
8 #include "sync/internal_api/public/base_transaction.h"
9 #include "sync/syncable/base_transaction.h"
10 #include "sync/syncable/entry.h"
12 namespace syncer {
14 //////////////////////////////////////////////////////////////////////////
15 // ReadNode member definitions
16 ReadNode::ReadNode(const BaseTransaction* transaction)
17 : entry_(NULL), transaction_(transaction) {
18 DCHECK(transaction);
21 ReadNode::ReadNode() {
22 entry_ = NULL;
23 transaction_ = NULL;
26 ReadNode::~ReadNode() {
27 delete entry_;
30 void ReadNode::InitByRootLookup() {
31 DCHECK(!entry_) << "Init called twice";
32 syncable::BaseTransaction* trans = transaction_->GetWrappedTrans();
33 entry_ = new syncable::Entry(trans, syncable::GET_BY_ID, trans->root_id());
34 if (!entry_->good())
35 DCHECK(false) << "Could not lookup root node for reading.";
38 BaseNode::InitByLookupResult ReadNode::InitByIdLookup(int64 id) {
39 DCHECK(!entry_) << "Init called twice";
40 DCHECK_NE(id, kInvalidId);
41 syncable::BaseTransaction* trans = transaction_->GetWrappedTrans();
42 entry_ = new syncable::Entry(trans, syncable::GET_BY_HANDLE, id);
43 if (!entry_->good())
44 return INIT_FAILED_ENTRY_NOT_GOOD;
45 if (entry_->Get(syncable::IS_DEL))
46 return INIT_FAILED_ENTRY_IS_DEL;
47 syncer::ModelType model_type = GetModelType();
48 LOG_IF(WARNING, model_type == syncer::UNSPECIFIED ||
49 model_type == syncer::TOP_LEVEL_FOLDER)
50 << "SyncAPI InitByIdLookup referencing unusual object.";
51 return DecryptIfNecessary() ? INIT_OK : INIT_FAILED_DECRYPT_IF_NECESSARY;
54 BaseNode::InitByLookupResult ReadNode::InitByClientTagLookup(
55 syncer::ModelType model_type,
56 const std::string& tag) {
57 DCHECK(!entry_) << "Init called twice";
58 if (tag.empty())
59 return INIT_FAILED_PRECONDITION;
61 const std::string hash = GenerateSyncableHash(model_type, tag);
63 entry_ = new syncable::Entry(transaction_->GetWrappedTrans(),
64 syncable::GET_BY_CLIENT_TAG, hash);
65 if (!entry_->good())
66 return INIT_FAILED_ENTRY_NOT_GOOD;
67 if (entry_->Get(syncable::IS_DEL))
68 return INIT_FAILED_ENTRY_IS_DEL;
69 return DecryptIfNecessary() ? INIT_OK : INIT_FAILED_DECRYPT_IF_NECESSARY;
72 const syncable::Entry* ReadNode::GetEntry() const {
73 return entry_;
76 const BaseTransaction* ReadNode::GetTransaction() const {
77 return transaction_;
80 BaseNode::InitByLookupResult ReadNode::InitByTagLookup(
81 const std::string& tag) {
82 DCHECK(!entry_) << "Init called twice";
83 if (tag.empty())
84 return INIT_FAILED_PRECONDITION;
85 syncable::BaseTransaction* trans = transaction_->GetWrappedTrans();
86 entry_ = new syncable::Entry(trans, syncable::GET_BY_SERVER_TAG, tag);
87 if (!entry_->good())
88 return INIT_FAILED_ENTRY_NOT_GOOD;
89 if (entry_->Get(syncable::IS_DEL))
90 return INIT_FAILED_ENTRY_IS_DEL;
91 syncer::ModelType model_type = GetModelType();
92 LOG_IF(WARNING, model_type == syncer::UNSPECIFIED ||
93 model_type == syncer::TOP_LEVEL_FOLDER)
94 << "SyncAPI InitByTagLookup referencing unusually typed object.";
95 return DecryptIfNecessary() ? INIT_OK : INIT_FAILED_DECRYPT_IF_NECESSARY;
98 } // namespace syncer