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
);
26 DeviceInfo::DeviceInfo()
27 : client_name_("Unset"),
28 sync_user_agent_("Unset"),
29 device_type_(sync_pb::SyncEnums::TYPE_OTHER
) {
32 DeviceInfo::DeviceInfo(const std::string
& client_name
,
33 const std::string
& chrome_version
,
34 const std::string
& sync_user_agent
,
35 const sync_pb::SyncEnums::DeviceType device_type
)
36 : client_name_(client_name
),
37 chrome_version_(chrome_version
),
38 sync_user_agent_(sync_user_agent
),
39 device_type_(device_type
) {
42 DeviceInfo::~DeviceInfo() { }
44 const std::string
& DeviceInfo::client_name() const {
48 const std::string
& DeviceInfo::chrome_version() const {
49 return chrome_version_
;
52 const std::string
& DeviceInfo::sync_user_agent() const {
53 return sync_user_agent_
;
56 sync_pb::SyncEnums::DeviceType
DeviceInfo::device_type() const {
60 bool DeviceInfo::Equals(const DeviceInfo
& other
) const {
61 return this->client_name() == other
.client_name()
62 && this->chrome_version() == other
.chrome_version()
63 && this->sync_user_agent() == other
.sync_user_agent()
64 && this->device_type() == other
.device_type();
68 sync_pb::SyncEnums::DeviceType
DeviceInfo::GetLocalDeviceType() {
69 #if defined(OS_CHROMEOS)
70 return sync_pb::SyncEnums_DeviceType_TYPE_CROS
;
71 #elif defined(OS_LINUX)
72 return sync_pb::SyncEnums_DeviceType_TYPE_LINUX
;
73 #elif defined(OS_MACOSX)
74 return sync_pb::SyncEnums_DeviceType_TYPE_MAC
;
76 return sync_pb::SyncEnums_DeviceType_TYPE_WIN
;
77 #elif defined(OS_ANDROID)
79 sync_pb::SyncEnums_DeviceType_TYPE_TABLET
:
80 sync_pb::SyncEnums_DeviceType_TYPE_PHONE
;
82 return sync_pb::SyncEnums_DeviceType_TYPE_OTHER
;
87 std::string
DeviceInfo::MakeUserAgentForSyncApi(
88 const chrome::VersionInfo
& version_info
) {
89 std::string user_agent
;
90 user_agent
= "Chrome ";
93 #elif defined(OS_CHROMEOS)
94 user_agent
+= "CROS ";
95 #elif defined(OS_ANDROID)
96 user_agent
+= "ANDROID ";
97 #elif defined(OS_LINUX)
98 user_agent
+= "LINUX ";
99 #elif defined(OS_FREEBSD)
100 user_agent
+= "FREEBSD ";
101 #elif defined(OS_OPENBSD)
102 user_agent
+= "OPENBSD ";
103 #elif defined(OS_MACOSX)
104 user_agent
+= "MAC ";
106 if (!version_info
.is_valid()) {
107 DLOG(ERROR
) << "Unable to create chrome::VersionInfo object";
111 user_agent
+= version_info
.Version();
112 user_agent
+= " (" + version_info
.LastChange() + ")";
113 if (!version_info
.IsOfficialBuild())
114 user_agent
+= "-devel";
119 void DeviceInfo::CreateLocalDeviceInfo(
120 base::Callback
<void(const DeviceInfo
& local_info
)> callback
) {
121 syncer::GetSessionName(
122 content::BrowserThread::GetBlockingPool(),
123 base::Bind(&DeviceInfo::CreateLocalDeviceInfoContinuation
, callback
));
127 void DeviceInfo::CreateLocalDeviceInfoContinuation(
128 base::Callback
<void(const DeviceInfo
& local_info
)> callback
,
129 const std::string
& session_name
) {
130 chrome::VersionInfo version_info
;
132 DeviceInfo
local_info(
134 version_info
.CreateVersionString(),
135 MakeUserAgentForSyncApi(version_info
),
136 GetLocalDeviceType());
138 callback
.Run(local_info
);
141 } // namespace browser_sync