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 "base/threading/sequenced_worker_pool.h" | |
10 #include "chrome/common/chrome_version_info.h" | |
11 #include "content/public/browser/browser_thread.h" | |
12 #include "sync/internal_api/public/base/model_type.h" | |
13 #include "sync/internal_api/public/read_node.h" | |
14 #include "sync/internal_api/public/read_transaction.h" | |
15 #include "sync/internal_api/public/write_node.h" | |
16 #include "sync/internal_api/public/write_transaction.h" | |
17 #include "sync/syncable/directory.h" | |
18 #include "sync/util/get_session_name.h" | |
19 | |
20 namespace browser_sync { | |
21 | |
22 namespace { | |
23 | |
24 // Return the DeviceInfo UNIQUE_CLIENT_TAG value for given sync cache_guid. | |
25 std::string DeviceInfoLookupString(const std::string& cache_guid) { | |
26 return base::StringPrintf("DeviceInfo_%s", cache_guid.c_str()); | |
27 } | |
28 | |
29 } // namespace | |
30 | |
31 SyncedDeviceTracker::SyncedDeviceTracker() | |
32 : ChangeProcessor(NULL), | |
33 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) { | |
34 } | |
35 | |
36 SyncedDeviceTracker::~SyncedDeviceTracker() { } | |
37 | |
38 void SyncedDeviceTracker::StartImpl(Profile* profile) { } | |
39 | |
40 void SyncedDeviceTracker::StopImpl() { } | |
41 | |
42 void SyncedDeviceTracker::ApplyChangesFromSyncModel( | |
43 const syncer::BaseTransaction* trans, | |
44 const syncer::ImmutableChangeRecordList& changes) { | |
45 // If desired, we could maintain a cache of device info. This method will be | |
46 // called with a transaction every time the device info is modified, so this | |
47 // would be the right place to update the cache. | |
48 } | |
49 | |
50 void SyncedDeviceTracker::CommitChangesFromSyncModel() { | |
51 // TODO(sync): notify our listeners. | |
52 } | |
53 | |
54 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.
| |
55 syncer::ReadTransaction trans(FROM_HERE, share_handle()); | |
56 return GetLocalDeviceInfo(trans, out); | |
57 } | |
58 | |
59 bool SyncedDeviceTracker::GetLocalDeviceInfo( | |
60 const syncer::BaseTransaction& trans, DeviceInfo* out) { | |
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 out->client_name = specifics.client_name(); | |
71 out->device_type = specifics.device_type(); | |
72 out->chrome_version = specifics.chrome_version(); | |
73 | |
74 return true; | |
75 } | |
76 | |
77 void SyncedDeviceTracker::InitLocalDeviceInfo(const base::Closure& callback) { | |
78 const scoped_refptr<base::TaskRunner> blocking_task_runner( | |
79 content::BrowserThread::GetBlockingPool()); | |
80 syncer::GetSessionName( | |
81 blocking_task_runner.get(), | |
82 base::Bind(&SyncedDeviceTracker::InitLocalDeviceInfoContinuation, | |
83 weak_factory_.GetWeakPtr(), callback)); | |
84 } | |
85 | |
86 void SyncedDeviceTracker::InitLocalDeviceInfoContinuation( | |
87 const base::Closure& callback, const std::string& session_name) { | |
88 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
| |
89 DCHECK(!share_handle()->directory->cache_guid().empty()); | |
90 | |
91 chrome::VersionInfo version_info; | |
92 | |
93 DeviceInfo info; | |
94 info.client_name = session_name; | |
95 info.device_type = GetLocalDeviceType(); | |
96 info.chrome_version = version_info.CreateVersionString(); | |
97 | |
98 WriteLocalDeviceInfo(info); | |
99 | |
100 callback.Run(); | |
101 } | |
102 | |
103 void SyncedDeviceTracker::WriteLocalDeviceInfo(const DeviceInfo& info) { | |
104 sync_pb::DeviceInfoSpecifics specifics; | |
105 specifics.set_cache_guid(share_handle()->directory->cache_guid()); | |
106 specifics.set_client_name(info.client_name); | |
107 specifics.set_device_type(info.device_type); | |
108 specifics.set_chrome_version(info.chrome_version); | |
109 | |
110 syncer::WriteTransaction trans(FROM_HERE, share_handle()); | |
111 syncer::WriteNode node(&trans); | |
112 | |
113 const std::string& lookup_string = | |
114 DeviceInfoLookupString(share_handle()->directory->cache_guid()); | |
115 | |
116 if (node.InitByClientTagLookup(syncer::DEVICE_INFO, lookup_string) == | |
117 syncer::BaseNode::INIT_OK) { | |
118 node.SetDeviceInfoSpecifics(specifics); | |
119 } else { | |
120 syncer::ReadNode type_root(&trans); | |
121 syncer::BaseNode::InitByLookupResult type_root_lookup_result = | |
122 type_root.InitByTagLookup(ModelTypeToRootTag(syncer::DEVICE_INFO)); | |
123 DCHECK_EQ(syncer::BaseNode::INIT_OK, type_root_lookup_result); | |
124 | |
125 syncer::WriteNode new_node(&trans); | |
126 syncer::WriteNode::InitUniqueByCreationResult create_result = | |
127 new_node.InitUniqueByCreation(syncer::DEVICE_INFO, | |
128 type_root, | |
129 lookup_string); | |
130 DCHECK_EQ(syncer::WriteNode::INIT_SUCCESS, create_result); | |
131 new_node.SetDeviceInfoSpecifics(specifics); | |
132 } | |
133 } | |
134 | |
135 sync_pb::SyncEnums::DeviceType SyncedDeviceTracker::GetLocalDeviceType() { | |
136 #if defined(OS_CHROMEOS) | |
137 return sync_pb::SyncEnums_DeviceType_TYPE_CROS; | |
138 #elif defined(OS_LINUX) | |
139 return sync_pb::SyncEnums_DeviceType_TYPE_LINUX; | |
140 #elif defined(OS_MACOSX) | |
141 return sync_pb::SyncEnums_DeviceType_TYPE_MAC; | |
142 #elif defined(OS_WIN) | |
143 return sync_pb::SyncEnums_DeviceType_TYPE_WIN; | |
144 #elif defined(OS_ANDROID) | |
145 return IsTabletUI() ? | |
146 sync_pb::SessionHeader_DeviceType_TYPE_TABLET : | |
147 sync_pb::SessionHeader_DeviceType_TYPE_PHONE; | |
148 #else | |
149 return sync_pb::SessionHeader_DeviceType_TYPE_OTHER; | |
150 #endif | |
151 } | |
152 | |
153 } // namespace browser_sync | |
OLD | NEW |