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/stringprintf.h" |
| 8 #include "base/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 ALLOW_THIS_IN_INITIALIZER_LIST(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() { |
| 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) { |
| 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.client_name(), |
| 71 specifics.chrome_version(), |
| 72 specifics.sync_user_agent(), |
| 73 specifics.device_type())); |
| 74 } |
| 75 |
| 76 void SyncedDeviceTracker::InitLocalDeviceInfo(const base::Closure& callback) { |
| 77 DeviceInfo::CreateLocalDeviceInfo( |
| 78 base::Bind(&SyncedDeviceTracker::InitLocalDeviceInfoContinuation, |
| 79 weak_factory_.GetWeakPtr(), callback)); |
| 80 } |
| 81 |
| 82 void SyncedDeviceTracker::InitLocalDeviceInfoContinuation( |
| 83 const base::Closure& callback, const DeviceInfo& local_info) { |
| 84 WriteLocalDeviceInfo(local_info); |
| 85 callback.Run(); |
| 86 } |
| 87 |
| 88 void SyncedDeviceTracker::WriteLocalDeviceInfo(const DeviceInfo& info) { |
| 89 sync_pb::DeviceInfoSpecifics specifics; |
| 90 specifics.set_cache_guid(cache_guid_); |
| 91 specifics.set_client_name(info.client_name()); |
| 92 specifics.set_chrome_version(info.chrome_version()); |
| 93 specifics.set_sync_user_agent(info.sync_user_agent()); |
| 94 specifics.set_device_type(info.device_type()); |
| 95 |
| 96 syncer::WriteTransaction trans(FROM_HERE, user_share_); |
| 97 syncer::WriteNode node(&trans); |
| 98 |
| 99 if (node.InitByClientTagLookup(syncer::DEVICE_INFO, local_device_info_tag_) == |
| 100 syncer::BaseNode::INIT_OK) { |
| 101 node.SetDeviceInfoSpecifics(specifics); |
| 102 node.SetTitle(ASCIIToWide(specifics.client_name())); |
| 103 } else { |
| 104 syncer::ReadNode type_root(&trans); |
| 105 syncer::BaseNode::InitByLookupResult type_root_lookup_result = |
| 106 type_root.InitByTagLookup(ModelTypeToRootTag(syncer::DEVICE_INFO)); |
| 107 DCHECK_EQ(syncer::BaseNode::INIT_OK, type_root_lookup_result); |
| 108 |
| 109 syncer::WriteNode new_node(&trans); |
| 110 syncer::WriteNode::InitUniqueByCreationResult create_result = |
| 111 new_node.InitUniqueByCreation(syncer::DEVICE_INFO, |
| 112 type_root, |
| 113 local_device_info_tag_); |
| 114 DCHECK_EQ(syncer::WriteNode::INIT_SUCCESS, create_result); |
| 115 new_node.SetDeviceInfoSpecifics(specifics); |
| 116 new_node.SetTitle(ASCIIToWide(specifics.client_name())); |
| 117 } |
| 118 } |
| 119 |
| 120 } // namespace browser_sync |
OLD | NEW |