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/stringprintf.h" | |
8 #include "base/threading/sequenced_worker_pool.h" | |
9 #include "chrome/common/chrome_version_info.h" | |
10 #include "content/public/browser/browser_thread.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 #include "sync/syncable/directory.h" | |
17 #include "sync/util/get_session_name.h" | |
18 | |
19 namespace browser_sync { | |
20 | |
21 SyncedDeviceTracker::SyncedDeviceTracker() : ChangeProcessor(NULL) { | |
22 } | |
23 | |
24 SyncedDeviceTracker::~SyncedDeviceTracker() { } | |
25 | |
26 void SyncedDeviceTracker::StartImpl(Profile* profile) { } | |
27 | |
28 void SyncedDeviceTracker::StopImpl() { } | |
29 | |
30 void SyncedDeviceTracker::ApplyChangesFromSyncModel( | |
31 const syncer::BaseTransaction* trans, | |
32 const syncer::ImmutableChangeRecordList& changes) { | |
33 // If desired, we could maintain a cache of device info. This method will be | |
34 // called with a transaction every time the device info is modified, so this | |
35 // would be the right place to update the cache. | |
36 } | |
37 | |
38 void SyncedDeviceTracker::CommitChangesFromSyncModel() { | |
39 // TODO(sync): notify our listeners. | |
40 } | |
41 | |
42 namespace { | |
43 | |
44 std::string DeviceInfoLookupString(const std::string& cache_guid) { | |
Nicolas Zea
2012/09/13 00:45:55
nit: I find it a bit clearer when local helper fun
rlarocque
2012/09/14 01:03:07
Done.
| |
45 return base::StringPrintf("DeviceInfo_%s", cache_guid.c_str()); | |
46 } | |
47 | |
48 std::string GetPlatformString() { | |
49 #if defined(OS_CHROMEOS) | |
50 return "ChromeOS"; | |
51 #elif defined(OS_LINUX) | |
52 return "Linux"; | |
53 #elif defined(OS_MACOSX) | |
54 return "Mac"; | |
55 #elif defined(OS_WIN) | |
56 return "Windows"; | |
57 #endif | |
58 return "Unknown"; | |
59 } | |
60 | |
61 } // namespace | |
62 | |
63 bool SyncedDeviceTracker::GetLocalDeviceInfo(DeviceInfo* out) { | |
64 syncer::ReadTransaction trans(FROM_HERE, share_handle()); | |
65 return GetLocalDeviceInfo(trans, out); | |
66 } | |
67 | |
68 bool SyncedDeviceTracker::GetLocalDeviceInfo( | |
Nicolas Zea
2012/09/13 00:45:55
Does this need to be public? If not, GetLocalDevic
rlarocque
2012/09/14 01:03:07
It's not currently used anywhere, but I expect thi
| |
69 syncer::BaseTransaction& trans, DeviceInfo* out) { | |
70 syncer::ReadNode node(&trans); | |
71 const std::string lookup_string = | |
Nicolas Zea
2012/09/13 00:45:55
make const ref?
rlarocque
2012/09/14 01:03:07
Done.
| |
72 DeviceInfoLookupString(share_handle()->directory->cache_guid()); | |
73 syncer::BaseNode::InitByLookupResult lookup_result = | |
74 node.InitByClientTagLookup(syncer::DEVICE_INFO, lookup_string); | |
75 if (lookup_result != syncer::BaseNode::INIT_OK) | |
Nicolas Zea
2012/09/13 00:45:55
nit: may as well just do the InitByClient.. call i
rlarocque
2012/09/14 01:03:07
Done.
| |
76 return false; | |
77 | |
78 const sync_pb::DeviceInfoSpecifics& specifics = node.GetDeviceInfoSpecifics(); | |
Nicolas Zea
2012/09/13 00:45:55
I'm a bit hesitant about how this will discard unk
rlarocque
2012/09/14 01:03:07
The client has no business processing fields that
| |
79 out->session_name = specifics.name(); | |
80 out->platform = specifics.platform(); | |
81 out->chrome_version = specifics.chrome_version(); | |
82 | |
83 return true; | |
84 } | |
85 | |
86 void SyncedDeviceTracker::InitLocalDeviceInfo(const base::Closure& callback) { | |
87 const scoped_refptr<base::TaskRunner> blocking_task_runner( | |
88 content::BrowserThread::GetBlockingPool()); | |
89 syncer::GetSessionName( | |
rlarocque
2012/09/08 01:20:44
I'd prefer it if we could just block the sync thre
Nicolas Zea
2012/09/13 00:45:55
Since you're using weak pointers, this class can b
rlarocque
2012/09/14 01:03:07
In theory, yes. I think this code is thread safe.
| |
90 blocking_task_runner.get(), | |
91 base::Bind(&SyncedDeviceTracker::InitLocalDeviceInfoContinuation, | |
92 AsWeakPtr(), callback)); | |
93 } | |
94 | |
95 void SyncedDeviceTracker::InitLocalDeviceInfoContinuation( | |
96 const base::Closure& callback, const std::string& session_name) { | |
97 DCHECK(running()); | |
98 DCHECK(!share_handle()->directory->cache_guid().empty()); | |
99 | |
100 chrome::VersionInfo version_info; | |
101 | |
102 DeviceInfo info; | |
103 info.session_name = session_name; | |
104 info.platform = GetPlatformString(); | |
105 info.chrome_version = version_info.CreateVersionString(); | |
106 | |
107 WriteLocalDeviceInfo(info); | |
108 | |
109 callback.Run(); | |
110 } | |
111 | |
112 void SyncedDeviceTracker::WriteLocalDeviceInfo(const DeviceInfo& info) { | |
113 sync_pb::DeviceInfoSpecifics specifics; | |
114 specifics.set_cache_guid(share_handle()->directory->cache_guid()); | |
115 specifics.set_name(info.session_name); | |
116 specifics.set_platform(info.platform); | |
117 specifics.set_chrome_version(info.chrome_version); | |
118 | |
119 syncer::WriteTransaction trans(FROM_HERE, share_handle()); | |
120 syncer::WriteNode node(&trans); | |
121 | |
122 std::string lookup_string = | |
123 DeviceInfoLookupString(share_handle()->directory->cache_guid()); | |
124 | |
125 syncer::BaseNode::InitByLookupResult lookup_result = | |
126 node.InitByClientTagLookup(syncer::DEVICE_INFO, lookup_string); | |
127 | |
128 if (lookup_result == syncer::BaseNode::INIT_OK) { | |
Nicolas Zea
2012/09/13 00:45:55
nit: no need to have lookup_result in it's own var
rlarocque
2012/09/14 01:03:07
Done.
| |
129 node.SetDeviceInfoSpecifics(specifics); | |
130 } else { | |
131 syncer::ReadNode type_root(&trans); | |
132 syncer::BaseNode::InitByLookupResult type_root_lookup_result = | |
133 type_root.InitByTagLookup(ModelTypeToRootTag(syncer::DEVICE_INFO)); | |
134 DCHECK(type_root_lookup_result == syncer::BaseNode::INIT_OK); | |
Nicolas Zea
2012/09/13 00:45:55
dcheck_eq here and below.
rlarocque
2012/09/14 01:03:07
Done.
| |
135 | |
136 syncer::WriteNode new_node(&trans); | |
137 syncer::WriteNode::InitUniqueByCreationResult create_result = | |
138 new_node.InitUniqueByCreation(syncer::DEVICE_INFO, | |
139 type_root, | |
140 lookup_string); | |
141 DCHECK(create_result == syncer::WriteNode::INIT_SUCCESS); | |
142 new_node.SetDeviceInfoSpecifics(specifics); | |
143 } | |
144 } | |
145 | |
146 } // namespace browser_sync | |
OLD | NEW |