Chromium Code Reviews| Index: chrome/browser/sync/glue/synced_device_tracker.cc |
| diff --git a/chrome/browser/sync/glue/synced_device_tracker.cc b/chrome/browser/sync/glue/synced_device_tracker.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..44efab6b034ebccb755740b664598b18dc31576e |
| --- /dev/null |
| +++ b/chrome/browser/sync/glue/synced_device_tracker.cc |
| @@ -0,0 +1,128 @@ |
| +// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "chrome/browser/sync/glue/synced_device_tracker.h" |
| + |
| +#include "base/compiler_specific.h" |
| +#include "base/stringprintf.h" |
| +#include "chrome/browser/sync/glue/device_info.h" |
| +#include "sync/internal_api/public/base/model_type.h" |
| +#include "sync/internal_api/public/read_node.h" |
| +#include "sync/internal_api/public/read_transaction.h" |
| +#include "sync/internal_api/public/write_node.h" |
| +#include "sync/internal_api/public/write_transaction.h" |
| +#include "sync/syncable/directory.h" |
| + |
| +namespace browser_sync { |
| + |
| +namespace { |
| + |
| +// Return the DeviceInfo UNIQUE_CLIENT_TAG value for given sync cache_guid. |
| +std::string DeviceInfoLookupString(const std::string& cache_guid) { |
| + return base::StringPrintf("DeviceInfo_%s", cache_guid.c_str()); |
| +} |
| + |
| +} // namespace |
| + |
| +SyncedDeviceTracker::SyncedDeviceTracker() |
| + : ChangeProcessor(NULL), |
| + ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) { |
| +} |
| + |
| +SyncedDeviceTracker::~SyncedDeviceTracker() { } |
| + |
| +void SyncedDeviceTracker::StartImpl(Profile* profile) { } |
| + |
| +void SyncedDeviceTracker::StopImpl() { } |
| + |
| +void SyncedDeviceTracker::ApplyChangesFromSyncModel( |
| + const syncer::BaseTransaction* trans, |
| + const syncer::ImmutableChangeRecordList& changes) { |
| + // If desired, we could maintain a cache of device info. This method will be |
| + // called with a transaction every time the device info is modified, so this |
| + // would be the right place to update the cache. |
| +} |
| + |
| +void SyncedDeviceTracker::CommitChangesFromSyncModel() { |
| + // TODO(sync): notify our listeners. |
| +} |
| + |
| +bool SyncedDeviceTracker::ReadLocalDeviceInfo(DeviceInfo* out) { |
| + if (!running()) { |
| + return false; |
| + } |
| + syncer::ReadTransaction trans(FROM_HERE, share_handle()); |
| + return ReadLocalDeviceInfo(trans, out); |
| +} |
| + |
| +bool SyncedDeviceTracker::ReadLocalDeviceInfo( |
| + const syncer::BaseTransaction& trans, DeviceInfo* out) { |
|
Nicolas Zea
2012/09/17 17:54:00
Since this is a public method, I wonder if we shou
|
| + syncer::ReadNode node(&trans); |
| + const std::string lookup_string = |
| + DeviceInfoLookupString(share_handle()->directory->cache_guid()); |
| + if (node.InitByClientTagLookup(syncer::DEVICE_INFO, lookup_string) != |
| + syncer::BaseNode::INIT_OK) { |
| + return false; |
| + } |
| + |
| + const sync_pb::DeviceInfoSpecifics& specifics = node.GetDeviceInfoSpecifics(); |
| + DeviceInfo info(specifics.client_name(), |
| + specifics.chrome_version(), |
| + specifics.device_type()); |
| + *out = info; |
| + return true; |
| +} |
| + |
| +void SyncedDeviceTracker::InitLocalDeviceInfo(const base::Closure& callback) { |
| + DeviceInfo::CreateLocalDeviceInfo( |
| + base::Bind(&SyncedDeviceTracker::InitLocalDeviceInfoContinuation, |
| + weak_factory_.GetWeakPtr(), callback)); |
| +} |
| + |
| +void SyncedDeviceTracker::InitLocalDeviceInfoContinuation( |
| + const base::Closure& callback, const DeviceInfo& local_info) { |
| + if (!running()) { |
|
rlarocque
2012/09/15 01:36:37
I think this is the wrong way to handle it, but I'
|
| + // We've been asked to shut down while we were waiting. We won't bother |
| + // trying to write the device info, let's just return now. |
| + callback.Run(); |
|
Nicolas Zea
2012/09/17 17:54:00
return after.
|
| + } |
| + |
| + DCHECK(!share_handle()->directory->cache_guid().empty()); |
| + WriteLocalDeviceInfo(local_info); |
| + callback.Run(); |
| +} |
| + |
| +void SyncedDeviceTracker::WriteLocalDeviceInfo(const DeviceInfo& info) { |
| + sync_pb::DeviceInfoSpecifics specifics; |
| + specifics.set_cache_guid(share_handle()->directory->cache_guid()); |
| + specifics.set_client_name(info.client_name()); |
| + specifics.set_device_type(info.device_type()); |
| + specifics.set_chrome_version(info.chrome_version()); |
| + |
| + syncer::WriteTransaction trans(FROM_HERE, share_handle()); |
| + syncer::WriteNode node(&trans); |
| + |
| + const std::string& lookup_string = |
| + DeviceInfoLookupString(share_handle()->directory->cache_guid()); |
| + |
| + if (node.InitByClientTagLookup(syncer::DEVICE_INFO, lookup_string) == |
| + syncer::BaseNode::INIT_OK) { |
| + node.SetDeviceInfoSpecifics(specifics); |
| + } else { |
| + syncer::ReadNode type_root(&trans); |
| + syncer::BaseNode::InitByLookupResult type_root_lookup_result = |
| + type_root.InitByTagLookup(ModelTypeToRootTag(syncer::DEVICE_INFO)); |
| + DCHECK_EQ(syncer::BaseNode::INIT_OK, type_root_lookup_result); |
| + |
| + syncer::WriteNode new_node(&trans); |
| + syncer::WriteNode::InitUniqueByCreationResult create_result = |
| + new_node.InitUniqueByCreation(syncer::DEVICE_INFO, |
| + type_root, |
| + lookup_string); |
| + DCHECK_EQ(syncer::WriteNode::INIT_SUCCESS, create_result); |
| + new_node.SetDeviceInfoSpecifics(specifics); |
| + } |
| +} |
| + |
| +} // namespace browser_sync |