OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 #include "chrome/browser/sync/glue/synced_device_tracker.h" |
| 6 |
| 7 #include "base/compiler_specific.h" |
| 8 #include "base/stringprintf.h" |
| 9 #include "chrome/browser/sync/glue/device_info.h" |
| 10 #include "sync/internal_api/public/base/model_type.h" |
| 11 #include "sync/internal_api/public/read_node.h" |
| 12 #include "sync/internal_api/public/read_transaction.h" |
| 13 #include "sync/internal_api/public/write_node.h" |
| 14 #include "sync/internal_api/public/write_transaction.h" |
| 15 |
| 16 namespace browser_sync { |
| 17 |
| 18 namespace { |
| 19 |
| 20 // Return the DeviceInfo UNIQUE_CLIENT_TAG value for given sync cache_guid. |
| 21 std::string DeviceInfoLookupString(const std::string& cache_guid) { |
| 22 return base::StringPrintf("DeviceInfo_%s", cache_guid.c_str()); |
| 23 } |
| 24 |
| 25 } // namespace |
| 26 |
| 27 SyncedDeviceTracker::SyncedDeviceTracker() |
| 28 : ChangeProcessor(NULL), |
| 29 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) { |
| 30 } |
| 31 |
| 32 SyncedDeviceTracker::~SyncedDeviceTracker() { } |
| 33 |
| 34 void SyncedDeviceTracker::StartImpl(Profile* profile) { } |
| 35 |
| 36 void SyncedDeviceTracker::StopImpl() { |
| 37 // We won't be able to service requests anymore. |
| 38 weak_factory_.InvalidateWeakPtrs(); |
| 39 } |
| 40 |
| 41 void SyncedDeviceTracker::ApplyChangesFromSyncModel( |
| 42 const syncer::BaseTransaction* trans, |
| 43 const syncer::ImmutableChangeRecordList& changes) { |
| 44 // If desired, we could maintain a cache of device info. This method will be |
| 45 // called with a transaction every time the device info is modified, so this |
| 46 // would be the right place to update the cache. |
| 47 } |
| 48 |
| 49 void SyncedDeviceTracker::CommitChangesFromSyncModel() { |
| 50 // TODO(sync): notify our listeners. |
| 51 } |
| 52 |
| 53 bool SyncedDeviceTracker::ReadLocalDeviceInfo(DeviceInfo* out) { |
| 54 if (!running()) { |
| 55 return false; |
| 56 } |
| 57 syncer::ReadTransaction trans(FROM_HERE, share_handle()); |
| 58 return ReadLocalDeviceInfo(trans, out); |
| 59 } |
| 60 |
| 61 bool SyncedDeviceTracker::ReadLocalDeviceInfo( |
| 62 const syncer::BaseTransaction& trans, DeviceInfo* out) { |
| 63 syncer::ReadNode node(&trans); |
| 64 const std::string lookup_string = |
| 65 DeviceInfoLookupString(share_handle()->cache_guid()); |
| 66 if (node.InitByClientTagLookup(syncer::DEVICE_INFO, lookup_string) != |
| 67 syncer::BaseNode::INIT_OK) { |
| 68 return false; |
| 69 } |
| 70 |
| 71 const sync_pb::DeviceInfoSpecifics& specifics = node.GetDeviceInfoSpecifics(); |
| 72 DeviceInfo info(specifics.client_name(), |
| 73 specifics.sync_user_agent(), |
| 74 specifics.device_type()); |
| 75 *out = info; |
| 76 return true; |
| 77 } |
| 78 |
| 79 void SyncedDeviceTracker::InitLocalDeviceInfo(const base::Closure& callback) { |
| 80 DeviceInfo::CreateLocalDeviceInfo( |
| 81 base::Bind(&SyncedDeviceTracker::InitLocalDeviceInfoContinuation, |
| 82 weak_factory_.GetWeakPtr(), callback)); |
| 83 } |
| 84 |
| 85 void SyncedDeviceTracker::InitLocalDeviceInfoContinuation( |
| 86 const base::Closure& callback, const DeviceInfo& local_info) { |
| 87 DCHECK(running()); |
| 88 |
| 89 DCHECK(!share_handle()->cache_guid().empty()); |
| 90 WriteLocalDeviceInfo(local_info); |
| 91 callback.Run(); |
| 92 } |
| 93 |
| 94 void SyncedDeviceTracker::WriteLocalDeviceInfo(const DeviceInfo& info) { |
| 95 sync_pb::DeviceInfoSpecifics specifics; |
| 96 specifics.set_cache_guid(share_handle()->cache_guid()); |
| 97 specifics.set_client_name(info.client_name()); |
| 98 specifics.set_device_type(info.device_type()); |
| 99 specifics.set_sync_user_agent(info.sync_user_agent()); |
| 100 |
| 101 syncer::WriteTransaction trans(FROM_HERE, share_handle()); |
| 102 syncer::WriteNode node(&trans); |
| 103 |
| 104 const std::string& lookup_string = |
| 105 DeviceInfoLookupString(share_handle()->cache_guid()); |
| 106 |
| 107 if (node.InitByClientTagLookup(syncer::DEVICE_INFO, lookup_string) == |
| 108 syncer::BaseNode::INIT_OK) { |
| 109 node.SetDeviceInfoSpecifics(specifics); |
| 110 } else { |
| 111 syncer::ReadNode type_root(&trans); |
| 112 syncer::BaseNode::InitByLookupResult type_root_lookup_result = |
| 113 type_root.InitByTagLookup(ModelTypeToRootTag(syncer::DEVICE_INFO)); |
| 114 DCHECK_EQ(syncer::BaseNode::INIT_OK, type_root_lookup_result); |
| 115 |
| 116 syncer::WriteNode new_node(&trans); |
| 117 syncer::WriteNode::InitUniqueByCreationResult create_result = |
| 118 new_node.InitUniqueByCreation(syncer::DEVICE_INFO, |
| 119 type_root, |
| 120 lookup_string); |
| 121 DCHECK_EQ(syncer::WriteNode::INIT_SUCCESS, create_result); |
| 122 new_node.SetDeviceInfoSpecifics(specifics); |
| 123 } |
| 124 } |
| 125 |
| 126 } // namespace browser_sync |
OLD | NEW |