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/strings/stringprintf.h" 8 #include "base/strings/utf_string_conversions.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/user_share.h" 14 #include "sync/internal_api/public/write_node.h" 15 #include "sync/internal_api/public/write_transaction.h" 16 17 namespace browser_sync { 18 19 namespace { 20 21 // Return the DeviceInfo UNIQUE_CLIENT_TAG value for the given sync cache_guid. 22 std::string DeviceInfoLookupString(const std::string& cache_guid) { 23 return base::StringPrintf("DeviceInfo_%s", cache_guid.c_str()); 24 } 25 26 } // namespace 27 28 SyncedDeviceTracker::SyncedDeviceTracker(syncer::UserShare* user_share, 29 const std::string& cache_guid) 30 : ChangeProcessor(NULL), 31 weak_factory_(this), 32 user_share_(user_share), 33 cache_guid_(cache_guid), 34 local_device_info_tag_(DeviceInfoLookupString(cache_guid)) { 35 } 36 37 SyncedDeviceTracker::~SyncedDeviceTracker() { 38 } 39 40 void SyncedDeviceTracker::StartImpl(Profile* profile) { } 41 42 void SyncedDeviceTracker::ApplyChangesFromSyncModel( 43 const syncer::BaseTransaction* trans, 44 int64 model_version, 45 const syncer::ImmutableChangeRecordList& changes) { 46 // If desired, we could maintain a cache of device info. This method will be 47 // called with a transaction every time the device info is modified, so this 48 // would be the right place to update the cache. 49 } 50 51 void SyncedDeviceTracker::CommitChangesFromSyncModel() { 52 // TODO(sync): notify our listeners. 53 } 54 55 scoped_ptr<DeviceInfo> SyncedDeviceTracker::ReadLocalDeviceInfo() const { 56 syncer::ReadTransaction trans(FROM_HERE, user_share_); 57 return ReadLocalDeviceInfo(trans); 58 } 59 60 scoped_ptr<DeviceInfo> SyncedDeviceTracker::ReadLocalDeviceInfo( 61 const syncer::BaseTransaction& trans) const { 62 syncer::ReadNode node(&trans); 63 if (node.InitByClientTagLookup(syncer::DEVICE_INFO, local_device_info_tag_) != 64 syncer::BaseNode::INIT_OK) { 65 return scoped_ptr<DeviceInfo>(); 66 } 67 68 const sync_pb::DeviceInfoSpecifics& specifics = node.GetDeviceInfoSpecifics(); 69 return scoped_ptr<DeviceInfo> ( 70 new DeviceInfo(specifics.cache_guid(), 71 specifics.client_name(), 72 specifics.chrome_version(), 73 specifics.sync_user_agent(), 74 specifics.device_type())); 75 } 76 77 scoped_ptr<DeviceInfo> SyncedDeviceTracker::ReadDeviceInfo( 78 const std::string& client_id) const { 79 syncer::ReadTransaction trans(FROM_HERE, user_share_); 80 syncer::ReadNode node(&trans); 81 std::string lookup_string = DeviceInfoLookupString(client_id); 82 if (node.InitByClientTagLookup(syncer::DEVICE_INFO, lookup_string) != 83 syncer::BaseNode::INIT_OK) { 84 return scoped_ptr<DeviceInfo>(); 85 } 86 87 const sync_pb::DeviceInfoSpecifics& specifics = node.GetDeviceInfoSpecifics(); 88 return scoped_ptr<DeviceInfo> ( 89 new DeviceInfo(specifics.cache_guid(), 90 specifics.client_name(), 91 specifics.chrome_version(), 92 specifics.sync_user_agent(), 93 specifics.device_type())); 94 } 95 96 void SyncedDeviceTracker::GetAllSyncedDeviceInfo( 97 ScopedVector<DeviceInfo>* device_info) const { 98 if (device_info == NULL) 99 return; 100 101 device_info->clear(); 102 103 syncer::ReadTransaction trans(FROM_HERE, user_share_); 104 syncer::ReadNode root_node(&trans); 105 106 if (root_node.InitByTagLookup( 107 syncer::ModelTypeToRootTag(syncer::DEVICE_INFO)) != 108 syncer::BaseNode::INIT_OK) { 109 return; 110 } 111 112 // Get all the children of the root node and use the child id to read 113 // device info for devices. 114 std::vector<int64> children; 115 root_node.GetChildIds(&children); 116 117 for (std::vector<int64>::const_iterator it = children.begin(); 118 it != children.end(); ++it) { 119 syncer::ReadNode node(&trans); 120 if (node.InitByIdLookup(*it) != syncer::BaseNode::INIT_OK) 121 return; 122 123 const sync_pb::DeviceInfoSpecifics& specifics = 124 node.GetDeviceInfoSpecifics(); 125 device_info->push_back( 126 new DeviceInfo(specifics.cache_guid(), 127 specifics.client_name(), 128 specifics.chrome_version(), 129 specifics.sync_user_agent(), 130 specifics.device_type())); 131 132 } 133 } 134 135 void SyncedDeviceTracker::InitLocalDeviceInfo(const base::Closure& callback) { 136 DeviceInfo::CreateLocalDeviceInfo( 137 cache_guid_, 138 base::Bind(&SyncedDeviceTracker::InitLocalDeviceInfoContinuation, 139 weak_factory_.GetWeakPtr(), callback)); 140 } 141 142 void SyncedDeviceTracker::InitLocalDeviceInfoContinuation( 143 const base::Closure& callback, const DeviceInfo& local_info) { 144 WriteLocalDeviceInfo(local_info); 145 callback.Run(); 146 } 147 148 void SyncedDeviceTracker::WriteLocalDeviceInfo(const DeviceInfo& info) { 149 sync_pb::DeviceInfoSpecifics specifics; 150 DCHECK_EQ(cache_guid_, info.guid()); 151 specifics.set_cache_guid(cache_guid_); 152 specifics.set_client_name(info.client_name()); 153 specifics.set_chrome_version(info.chrome_version()); 154 specifics.set_sync_user_agent(info.sync_user_agent()); 155 specifics.set_device_type(info.device_type()); 156 157 WriteDeviceInfo(specifics, local_device_info_tag_); 158 } 159 160 void SyncedDeviceTracker::WriteDeviceInfo( 161 const sync_pb::DeviceInfoSpecifics& specifics, 162 const std::string& tag) { 163 syncer::WriteTransaction trans(FROM_HERE, user_share_); 164 syncer::WriteNode node(&trans); 165 166 if (node.InitByClientTagLookup(syncer::DEVICE_INFO, tag) == 167 syncer::BaseNode::INIT_OK) { 168 node.SetDeviceInfoSpecifics(specifics); 169 node.SetTitle(UTF8ToWide(specifics.client_name())); 170 } else { 171 syncer::ReadNode type_root(&trans); 172 syncer::BaseNode::InitByLookupResult type_root_lookup_result = 173 type_root.InitByTagLookup(ModelTypeToRootTag(syncer::DEVICE_INFO)); 174 DCHECK_EQ(syncer::BaseNode::INIT_OK, type_root_lookup_result); 175 176 syncer::WriteNode new_node(&trans); 177 syncer::WriteNode::InitUniqueByCreationResult create_result = 178 new_node.InitUniqueByCreation(syncer::DEVICE_INFO, 179 type_root, 180 tag); 181 DCHECK_EQ(syncer::WriteNode::INIT_SUCCESS, create_result); 182 new_node.SetDeviceInfoSpecifics(specifics); 183 new_node.SetTitle(UTF8ToWide(specifics.client_name())); 184 } 185 } 186 187 } // namespace browser_sync 188