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 "chrome/common/chrome_version_info.h"
10 #include "chrome/common/chrome_switches.h"
11 #include "content/public/browser/browser_thread.h"
12 #include "sync/util/get_session_name.h"
14 namespace browser_sync
{
18 #if defined(OS_ANDROID)
20 return CommandLine::ForCurrentProcess()->HasSwitch(switches::kTabletUI
);
24 // Converts VersionInfo::Channel to string for user-agent string.
25 std::string
ChannelToString(chrome::VersionInfo::Channel channel
) {
27 case chrome::VersionInfo::CHANNEL_UNKNOWN
:
29 case chrome::VersionInfo::CHANNEL_CANARY
:
31 case chrome::VersionInfo::CHANNEL_DEV
:
33 case chrome::VersionInfo::CHANNEL_BETA
:
35 case chrome::VersionInfo::CHANNEL_STABLE
:
45 DeviceInfo::DeviceInfo(const std::string
& client_name
,
46 const std::string
& chrome_version
,
47 const std::string
& sync_user_agent
,
48 const sync_pb::SyncEnums::DeviceType device_type
)
49 : client_name_(client_name
),
50 chrome_version_(chrome_version
),
51 sync_user_agent_(sync_user_agent
),
52 device_type_(device_type
) {
55 DeviceInfo::~DeviceInfo() { }
57 const std::string
& DeviceInfo::client_name() const {
61 const std::string
& DeviceInfo::chrome_version() const {
62 return chrome_version_
;
65 const std::string
& DeviceInfo::sync_user_agent() const {
66 return sync_user_agent_
;
69 sync_pb::SyncEnums::DeviceType
DeviceInfo::device_type() const {
73 bool DeviceInfo::Equals(const DeviceInfo
& other
) const {
74 return this->client_name() == other
.client_name()
75 && this->chrome_version() == other
.chrome_version()
76 && this->sync_user_agent() == other
.sync_user_agent()
77 && this->device_type() == other
.device_type();
81 sync_pb::SyncEnums::DeviceType
DeviceInfo::GetLocalDeviceType() {
82 #if defined(OS_CHROMEOS)
83 return sync_pb::SyncEnums_DeviceType_TYPE_CROS
;
84 #elif defined(OS_LINUX)
85 return sync_pb::SyncEnums_DeviceType_TYPE_LINUX
;
86 #elif defined(OS_MACOSX)
87 return sync_pb::SyncEnums_DeviceType_TYPE_MAC
;
89 return sync_pb::SyncEnums_DeviceType_TYPE_WIN
;
90 #elif defined(OS_ANDROID)
92 sync_pb::SyncEnums_DeviceType_TYPE_TABLET
:
93 sync_pb::SyncEnums_DeviceType_TYPE_PHONE
;
95 return sync_pb::SyncEnums_DeviceType_TYPE_OTHER
;
100 std::string
DeviceInfo::MakeUserAgentForSyncApi(
101 const chrome::VersionInfo
& version_info
) {
102 std::string user_agent
;
103 user_agent
= "Chrome ";
105 user_agent
+= "WIN ";
106 #elif defined(OS_CHROMEOS)
107 user_agent
+= "CROS ";
108 #elif defined(OS_ANDROID)
110 user_agent
+= "ANDROID-TABLET ";
112 user_agent
+= "ANDROID-PHONE ";
113 #elif defined(OS_LINUX)
114 user_agent
+= "LINUX ";
115 #elif defined(OS_FREEBSD)
116 user_agent
+= "FREEBSD ";
117 #elif defined(OS_OPENBSD)
118 user_agent
+= "OPENBSD ";
119 #elif defined(OS_MACOSX)
120 user_agent
+= "MAC ";
122 if (!version_info
.is_valid()) {
123 DLOG(ERROR
) << "Unable to create chrome::VersionInfo object";
127 user_agent
+= version_info
.Version();
128 user_agent
+= " (" + version_info
.LastChange() + ")";
129 if (!version_info
.IsOfficialBuild()) {
130 user_agent
+= "-devel";
132 user_agent
+= " channel(" +
133 ChannelToString(version_info
.GetChannel()) + ")";
140 void DeviceInfo::CreateLocalDeviceInfo(
141 base::Callback
<void(const DeviceInfo
& local_info
)> callback
) {
142 syncer::GetSessionName(
143 content::BrowserThread::GetBlockingPool(),
144 base::Bind(&DeviceInfo::CreateLocalDeviceInfoContinuation
, callback
));
148 void DeviceInfo::CreateLocalDeviceInfoContinuation(
149 base::Callback
<void(const DeviceInfo
& local_info
)> callback
,
150 const std::string
& session_name
) {
151 chrome::VersionInfo version_info
;
153 DeviceInfo
local_info(
155 version_info
.CreateVersionString(),
156 MakeUserAgentForSyncApi(version_info
),
157 GetLocalDeviceType());
159 callback
.Run(local_info
);
162 } // namespace browser_sync