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" | |
8 #include "base/stringprintf.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/write_node.h" | |
14 #include "sync/internal_api/public/write_transaction.h" | |
15 #include "sync/syncable/directory.h" | |
16 | |
17 namespace browser_sync { | |
18 | |
19 namespace { | |
20 | |
21 // Return the DeviceInfo UNIQUE_CLIENT_TAG value for 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() | |
29 : ChangeProcessor(NULL), | |
30 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) { | |
31 } | |
32 | |
33 SyncedDeviceTracker::~SyncedDeviceTracker() { } | |
34 | |
35 void SyncedDeviceTracker::StartImpl(Profile* profile) { } | |
36 | |
37 void SyncedDeviceTracker::StopImpl() { } | |
38 | |
39 void SyncedDeviceTracker::ApplyChangesFromSyncModel( | |
40 const syncer::BaseTransaction* trans, | |
41 const syncer::ImmutableChangeRecordList& changes) { | |
42 // If desired, we could maintain a cache of device info. This method will be | |
43 // called with a transaction every time the device info is modified, so this | |
44 // would be the right place to update the cache. | |
45 } | |
46 | |
47 void SyncedDeviceTracker::CommitChangesFromSyncModel() { | |
48 // TODO(sync): notify our listeners. | |
49 } | |
50 | |
51 bool SyncedDeviceTracker::ReadLocalDeviceInfo(DeviceInfo* out) { | |
52 if (!running()) { | |
53 return false; | |
54 } | |
55 syncer::ReadTransaction trans(FROM_HERE, share_handle()); | |
56 return ReadLocalDeviceInfo(trans, out); | |
57 } | |
58 | |
59 bool SyncedDeviceTracker::ReadLocalDeviceInfo( | |
60 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
| |
61 syncer::ReadNode node(&trans); | |
62 const std::string lookup_string = | |
63 DeviceInfoLookupString(share_handle()->directory->cache_guid()); | |
64 if (node.InitByClientTagLookup(syncer::DEVICE_INFO, lookup_string) != | |
65 syncer::BaseNode::INIT_OK) { | |
66 return false; | |
67 } | |
68 | |
69 const sync_pb::DeviceInfoSpecifics& specifics = node.GetDeviceInfoSpecifics(); | |
70 DeviceInfo info(specifics.client_name(), | |
71 specifics.chrome_version(), | |
72 specifics.device_type()); | |
73 *out = info; | |
74 return true; | |
75 } | |
76 | |
77 void SyncedDeviceTracker::InitLocalDeviceInfo(const base::Closure& callback) { | |
78 DeviceInfo::CreateLocalDeviceInfo( | |
79 base::Bind(&SyncedDeviceTracker::InitLocalDeviceInfoContinuation, | |
80 weak_factory_.GetWeakPtr(), callback)); | |
81 } | |
82 | |
83 void SyncedDeviceTracker::InitLocalDeviceInfoContinuation( | |
84 const base::Closure& callback, const DeviceInfo& local_info) { | |
85 if (!running()) { | |
rlarocque
2012/09/15 01:36:37
I think this is the wrong way to handle it, but I'
| |
86 // We've been asked to shut down while we were waiting. We won't bother | |
87 // trying to write the device info, let's just return now. | |
88 callback.Run(); | |
Nicolas Zea
2012/09/17 17:54:00
return after.
| |
89 } | |
90 | |
91 DCHECK(!share_handle()->directory->cache_guid().empty()); | |
92 WriteLocalDeviceInfo(local_info); | |
93 callback.Run(); | |
94 } | |
95 | |
96 void SyncedDeviceTracker::WriteLocalDeviceInfo(const DeviceInfo& info) { | |
97 sync_pb::DeviceInfoSpecifics specifics; | |
98 specifics.set_cache_guid(share_handle()->directory->cache_guid()); | |
99 specifics.set_client_name(info.client_name()); | |
100 specifics.set_device_type(info.device_type()); | |
101 specifics.set_chrome_version(info.chrome_version()); | |
102 | |
103 syncer::WriteTransaction trans(FROM_HERE, share_handle()); | |
104 syncer::WriteNode node(&trans); | |
105 | |
106 const std::string& lookup_string = | |
107 DeviceInfoLookupString(share_handle()->directory->cache_guid()); | |
108 | |
109 if (node.InitByClientTagLookup(syncer::DEVICE_INFO, lookup_string) == | |
110 syncer::BaseNode::INIT_OK) { | |
111 node.SetDeviceInfoSpecifics(specifics); | |
112 } else { | |
113 syncer::ReadNode type_root(&trans); | |
114 syncer::BaseNode::InitByLookupResult type_root_lookup_result = | |
115 type_root.InitByTagLookup(ModelTypeToRootTag(syncer::DEVICE_INFO)); | |
116 DCHECK_EQ(syncer::BaseNode::INIT_OK, type_root_lookup_result); | |
117 | |
118 syncer::WriteNode new_node(&trans); | |
119 syncer::WriteNode::InitUniqueByCreationResult create_result = | |
120 new_node.InitUniqueByCreation(syncer::DEVICE_INFO, | |
121 type_root, | |
122 lookup_string); | |
123 DCHECK_EQ(syncer::WriteNode::INIT_SUCCESS, create_result); | |
124 new_node.SetDeviceInfoSpecifics(specifics); | |
125 } | |
126 } | |
127 | |
128 } // namespace browser_sync | |
OLD | NEW |