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..755ba10c756ec6ea50a0535241f229b1875106d7 |
| --- /dev/null |
| +++ b/chrome/browser/sync/glue/synced_device_tracker.cc |
| @@ -0,0 +1,153 @@ |
| +// 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 "base/threading/sequenced_worker_pool.h" |
| +#include "chrome/common/chrome_version_info.h" |
| +#include "content/public/browser/browser_thread.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" |
| +#include "sync/util/get_session_name.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::GetLocalDeviceInfo(DeviceInfo* out) { |
|
Nicolas Zea
2012/09/14 19:15:08
I wonder if you need to check running() here and r
rlarocque
2012/09/15 01:36:37
Good point. I'll check running() as a precaution.
|
| + syncer::ReadTransaction trans(FROM_HERE, share_handle()); |
| + return GetLocalDeviceInfo(trans, out); |
| +} |
| + |
| +bool SyncedDeviceTracker::GetLocalDeviceInfo( |
| + const syncer::BaseTransaction& trans, DeviceInfo* out) { |
| + 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(); |
| + out->client_name = specifics.client_name(); |
| + out->device_type = specifics.device_type(); |
| + out->chrome_version = specifics.chrome_version(); |
| + |
| + return true; |
| +} |
| + |
| +void SyncedDeviceTracker::InitLocalDeviceInfo(const base::Closure& callback) { |
| + const scoped_refptr<base::TaskRunner> blocking_task_runner( |
| + content::BrowserThread::GetBlockingPool()); |
| + syncer::GetSessionName( |
| + blocking_task_runner.get(), |
| + base::Bind(&SyncedDeviceTracker::InitLocalDeviceInfoContinuation, |
| + weak_factory_.GetWeakPtr(), callback)); |
| +} |
| + |
| +void SyncedDeviceTracker::InitLocalDeviceInfoContinuation( |
| + const base::Closure& callback, const std::string& session_name) { |
| + DCHECK(running()); |
|
Nicolas Zea
2012/09/14 19:15:08
does this need to be a dcheck? It seems valid for
rlarocque
2012/09/15 01:36:37
I'm struggling to understand the scenario where th
Nicolas Zea
2012/09/17 17:54:00
That's an option. Weak ptr factory has an Invalida
rlarocque
2012/09/19 21:37:58
Yep. I'm not sure whether or not it's considered
|
| + DCHECK(!share_handle()->directory->cache_guid().empty()); |
| + |
| + chrome::VersionInfo version_info; |
| + |
| + DeviceInfo info; |
| + info.client_name = session_name; |
| + info.device_type = GetLocalDeviceType(); |
| + info.chrome_version = version_info.CreateVersionString(); |
| + |
| + WriteLocalDeviceInfo(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); |
| + } |
| +} |
| + |
| +sync_pb::SyncEnums::DeviceType SyncedDeviceTracker::GetLocalDeviceType() { |
| +#if defined(OS_CHROMEOS) |
| + return sync_pb::SyncEnums_DeviceType_TYPE_CROS; |
| +#elif defined(OS_LINUX) |
| + return sync_pb::SyncEnums_DeviceType_TYPE_LINUX; |
| +#elif defined(OS_MACOSX) |
| + return sync_pb::SyncEnums_DeviceType_TYPE_MAC; |
| +#elif defined(OS_WIN) |
| + return sync_pb::SyncEnums_DeviceType_TYPE_WIN; |
| +#elif defined(OS_ANDROID) |
| + return IsTabletUI() ? |
| + sync_pb::SessionHeader_DeviceType_TYPE_TABLET : |
| + sync_pb::SessionHeader_DeviceType_TYPE_PHONE; |
| +#else |
| + return sync_pb::SessionHeader_DeviceType_TYPE_OTHER; |
| +#endif |
| +} |
| + |
| +} // namespace browser_sync |