Fix build break
[chromium-blink-merge.git] / chrome / browser / sync / glue / synced_device_tracker.cc
blob933a611614ec871e76d6b81ac82c5b54af7fed72
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.
5 #include "chrome/browser/sync/glue/synced_device_tracker.h"
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"
17 namespace browser_sync {
19 namespace {
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());
26 } // namespace
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)) {
37 SyncedDeviceTracker::~SyncedDeviceTracker() {
40 void SyncedDeviceTracker::StartImpl(Profile* profile) { }
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.
51 void SyncedDeviceTracker::CommitChangesFromSyncModel() {
52 // TODO(sync): notify our listeners.
55 scoped_ptr<DeviceInfo> SyncedDeviceTracker::ReadLocalDeviceInfo() const {
56 syncer::ReadTransaction trans(FROM_HERE, user_share_);
57 return ReadLocalDeviceInfo(trans);
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>();
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()));
76 scoped_ptr<DeviceInfo> SyncedDeviceTracker::ReadDeviceInfo(
77 const std::string& client_id) const {
78 syncer::ReadTransaction trans(FROM_HERE, user_share_);
79 syncer::ReadNode node(&trans);
80 std::string lookup_string = DeviceInfoLookupString(client_id);
81 if (node.InitByClientTagLookup(syncer::DEVICE_INFO, lookup_string) !=
82 syncer::BaseNode::INIT_OK) {
83 return scoped_ptr<DeviceInfo>();
86 const sync_pb::DeviceInfoSpecifics& specifics = node.GetDeviceInfoSpecifics();
87 return scoped_ptr<DeviceInfo> (
88 new DeviceInfo(specifics.client_name(),
89 specifics.chrome_version(),
90 specifics.sync_user_agent(),
91 specifics.device_type()));
94 void SyncedDeviceTracker::InitLocalDeviceInfo(const base::Closure& callback) {
95 DeviceInfo::CreateLocalDeviceInfo(
96 base::Bind(&SyncedDeviceTracker::InitLocalDeviceInfoContinuation,
97 weak_factory_.GetWeakPtr(), callback));
100 void SyncedDeviceTracker::InitLocalDeviceInfoContinuation(
101 const base::Closure& callback, const DeviceInfo& local_info) {
102 WriteLocalDeviceInfo(local_info);
103 callback.Run();
106 void SyncedDeviceTracker::WriteLocalDeviceInfo(const DeviceInfo& info) {
107 sync_pb::DeviceInfoSpecifics specifics;
108 specifics.set_cache_guid(cache_guid_);
109 specifics.set_client_name(info.client_name());
110 specifics.set_chrome_version(info.chrome_version());
111 specifics.set_sync_user_agent(info.sync_user_agent());
112 specifics.set_device_type(info.device_type());
114 syncer::WriteTransaction trans(FROM_HERE, user_share_);
115 syncer::WriteNode node(&trans);
117 if (node.InitByClientTagLookup(syncer::DEVICE_INFO, local_device_info_tag_) ==
118 syncer::BaseNode::INIT_OK) {
119 node.SetDeviceInfoSpecifics(specifics);
120 node.SetTitle(UTF8ToWide(specifics.client_name()));
121 } else {
122 syncer::ReadNode type_root(&trans);
123 syncer::BaseNode::InitByLookupResult type_root_lookup_result =
124 type_root.InitByTagLookup(ModelTypeToRootTag(syncer::DEVICE_INFO));
125 DCHECK_EQ(syncer::BaseNode::INIT_OK, type_root_lookup_result);
127 syncer::WriteNode new_node(&trans);
128 syncer::WriteNode::InitUniqueByCreationResult create_result =
129 new_node.InitUniqueByCreation(syncer::DEVICE_INFO,
130 type_root,
131 local_device_info_tag_);
132 DCHECK_EQ(syncer::WriteNode::INIT_SUCCESS, create_result);
133 new_node.SetDeviceInfoSpecifics(specifics);
134 new_node.SetTitle(UTF8ToWide(specifics.client_name()));
138 } // namespace browser_sync