Chromium Code Reviews| 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" | |
|
Nicolas Zea
2012/11/16 19:42:27
is this used?
rlarocque
2012/11/17 00:07:21
Nope. Fixed.
| |
| 8 #include "base/stringprintf.h" | |
| 9 #include "base/utf_string_conversions.h" | |
| 10 #include "chrome/browser/sync/glue/device_info.h" | |
| 11 #include "sync/internal_api/public/base/model_type.h" | |
| 12 #include "sync/internal_api/public/read_node.h" | |
| 13 #include "sync/internal_api/public/read_transaction.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 given sync cache_guid. | |
|
Nicolas Zea
2012/11/16 19:42:27
DeviceInfo's
"for the given"
rlarocque
2012/11/17 00:07:21
Done.
| |
| 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(const std::string& cache_guid) | |
| 29 : ChangeProcessor(NULL), | |
| 30 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)), | |
| 31 cache_guid_(cache_guid), | |
| 32 local_device_info_tag_(DeviceInfoLookupString(cache_guid)) { | |
| 33 } | |
| 34 | |
| 35 SyncedDeviceTracker::~SyncedDeviceTracker() { | |
| 36 // We won't be able to service requests anymore. | |
| 37 weak_factory_.InvalidateWeakPtrs(); | |
|
Nicolas Zea
2012/11/16 19:42:27
This isn't necessary (destroying the factory inval
rlarocque
2012/11/17 00:07:21
Done.
| |
| 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, share_handle()); | |
| 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) { | |
|
Nicolas Zea
2012/11/16 19:42:27
!= on previous line, then indent four spaces
(chro
rlarocque
2012/11/17 00:07:21
Done.
| |
| 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, share_handle()); | |
| 97 syncer::WriteNode node(&trans); | |
| 98 | |
| 99 if (node.InitByClientTagLookup(syncer::DEVICE_INFO, local_device_info_tag_) | |
| 100 == syncer::BaseNode::INIT_OK) { | |
|
Nicolas Zea
2012/11/16 19:42:27
newline after boolean operator here too
rlarocque
2012/11/17 00:07:21
Done.
| |
| 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 } | |
| 117 } | |
| 118 | |
| 119 } // namespace browser_sync | |
| OLD | NEW |