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/device_info.h"
7 #include "base/command_line.h"
8 #include "base/threading/sequenced_worker_pool.h"
9 #include "base/values.h"
10 #include "chrome/common/chrome_version_info.h"
11 #include "chrome/common/chrome_switches.h"
12 #include "content/public/browser/browser_thread.h"
13 #include "sync/util/get_session_name.h"
15 namespace browser_sync {
19 #if defined(OS_ANDROID)
21 return CommandLine::ForCurrentProcess()->HasSwitch(switches::kTabletUI);
25 // Converts VersionInfo::Channel to string for user-agent string.
26 std::string ChannelToString(chrome::VersionInfo::Channel channel) {
28 case chrome::VersionInfo::CHANNEL_UNKNOWN:
30 case chrome::VersionInfo::CHANNEL_CANARY:
32 case chrome::VersionInfo::CHANNEL_DEV:
34 case chrome::VersionInfo::CHANNEL_BETA:
36 case chrome::VersionInfo::CHANNEL_STABLE:
46 DeviceInfo::DeviceInfo(const std::string& guid,
47 const std::string& client_name,
48 const std::string& chrome_version,
49 const std::string& sync_user_agent,
50 const sync_pb::SyncEnums::DeviceType device_type)
52 client_name_(client_name),
53 chrome_version_(chrome_version),
54 sync_user_agent_(sync_user_agent),
55 device_type_(device_type) {
58 DeviceInfo::~DeviceInfo() { }
60 const std::string& DeviceInfo::guid() const {
64 const std::string& DeviceInfo::client_name() const {
68 const std::string& DeviceInfo::chrome_version() const {
69 return chrome_version_;
72 const std::string& DeviceInfo::sync_user_agent() const {
73 return sync_user_agent_;
76 const std::string& DeviceInfo::public_id() const {
80 sync_pb::SyncEnums::DeviceType DeviceInfo::device_type() const {
84 std::string DeviceInfo::GetOSString() const {
85 switch (device_type_) {
86 case sync_pb::SyncEnums_DeviceType_TYPE_WIN:
88 case sync_pb::SyncEnums_DeviceType_TYPE_MAC:
90 case sync_pb::SyncEnums_DeviceType_TYPE_LINUX:
92 case sync_pb::SyncEnums_DeviceType_TYPE_CROS:
94 case sync_pb::SyncEnums_DeviceType_TYPE_PHONE:
95 case sync_pb::SyncEnums_DeviceType_TYPE_TABLET:
96 // TODO(lipalani): crbug.com/170375. Add support for ios
97 // phones and tablets.
104 std::string DeviceInfo::GetDeviceTypeString() const {
105 switch (device_type_) {
106 case sync_pb::SyncEnums_DeviceType_TYPE_WIN:
107 case sync_pb::SyncEnums_DeviceType_TYPE_MAC:
108 case sync_pb::SyncEnums_DeviceType_TYPE_LINUX:
109 case sync_pb::SyncEnums_DeviceType_TYPE_CROS:
110 return "desktop_or_laptop";
111 case sync_pb::SyncEnums_DeviceType_TYPE_PHONE:
113 case sync_pb::SyncEnums_DeviceType_TYPE_TABLET:
120 bool DeviceInfo::Equals(const DeviceInfo& other) const {
121 return this->guid() == other.guid()
122 && this->client_name() == other.client_name()
123 && this->chrome_version() == other.chrome_version()
124 && this->sync_user_agent() == other.sync_user_agent()
125 && this->device_type() == other.device_type();
129 sync_pb::SyncEnums::DeviceType DeviceInfo::GetLocalDeviceType() {
130 #if defined(OS_CHROMEOS)
131 return sync_pb::SyncEnums_DeviceType_TYPE_CROS;
132 #elif defined(OS_LINUX)
133 return sync_pb::SyncEnums_DeviceType_TYPE_LINUX;
134 #elif defined(OS_MACOSX)
135 return sync_pb::SyncEnums_DeviceType_TYPE_MAC;
136 #elif defined(OS_WIN)
137 return sync_pb::SyncEnums_DeviceType_TYPE_WIN;
138 #elif defined(OS_ANDROID)
139 return IsTabletUI() ?
140 sync_pb::SyncEnums_DeviceType_TYPE_TABLET :
141 sync_pb::SyncEnums_DeviceType_TYPE_PHONE;
143 return sync_pb::SyncEnums_DeviceType_TYPE_OTHER;
148 std::string DeviceInfo::MakeUserAgentForSyncApi(
149 const chrome::VersionInfo& version_info) {
150 std::string user_agent;
151 user_agent = "Chrome ";
153 user_agent += "WIN ";
154 #elif defined(OS_CHROMEOS)
155 user_agent += "CROS ";
156 #elif defined(OS_ANDROID)
158 user_agent += "ANDROID-TABLET ";
160 user_agent += "ANDROID-PHONE ";
161 #elif defined(OS_LINUX)
162 user_agent += "LINUX ";
163 #elif defined(OS_FREEBSD)
164 user_agent += "FREEBSD ";
165 #elif defined(OS_OPENBSD)
166 user_agent += "OPENBSD ";
167 #elif defined(OS_MACOSX)
168 user_agent += "MAC ";
170 if (!version_info.is_valid()) {
171 DLOG(ERROR) << "Unable to create chrome::VersionInfo object";
175 user_agent += version_info.Version();
176 user_agent += " (" + version_info.LastChange() + ")";
177 if (!version_info.IsOfficialBuild()) {
178 user_agent += "-devel";
180 user_agent += " channel(" +
181 ChannelToString(version_info.GetChannel()) + ")";
187 base::DictionaryValue* DeviceInfo::ToValue() {
188 base::DictionaryValue* value = new base::DictionaryValue();
189 value->SetString("name", client_name_);
190 value->SetString("id", public_id_);
191 value->SetString("os", GetOSString());
192 value->SetString("type", GetDeviceTypeString());
193 value->SetString("chromeVersion", chrome_version_);
197 void DeviceInfo::set_public_id(std::string id) {
202 void DeviceInfo::CreateLocalDeviceInfo(
203 const std::string& guid,
204 base::Callback<void(const DeviceInfo& local_info)> callback) {
206 base::Bind(&DeviceInfo::CreateLocalDeviceInfoContinuation,
212 void DeviceInfo::GetClientName(
213 base::Callback<void(const std::string& client_name)> callback) {
214 syncer::GetSessionName(
215 content::BrowserThread::GetBlockingPool(),
216 base::Bind(&DeviceInfo::GetClientNameContinuation,
220 void DeviceInfo::GetClientNameContinuation(
221 base::Callback<void(const std::string& local_info)> callback,
222 const std::string& session_name) {
223 callback.Run(session_name);
227 void DeviceInfo::CreateLocalDeviceInfoContinuation(
228 const std::string& guid,
229 base::Callback<void(const DeviceInfo& local_info)> callback,
230 const std::string& session_name) {
231 chrome::VersionInfo version_info;
233 DeviceInfo local_info(
236 version_info.CreateVersionString(),
237 MakeUserAgentForSyncApi(version_info),
238 GetLocalDeviceType());
240 callback.Run(local_info);
243 } // namespace browser_sync