1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/sync/glue/device_info.h"
#include "base/command_line.h"
#include "base/threading/sequenced_worker_pool.h"
#include "chrome/common/chrome_version_info.h"
#include "chrome/common/chrome_switches.h"
#include "content/public/browser/browser_thread.h"
#include "sync/util/get_session_name.h"
namespace browser_sync {
namespace {
#if defined(OS_ANDROID)
bool IsTabletUI() {
return CommandLine::ForCurrentProcess()->HasSwitch(switches::kTabletUI);
}
#endif
// Converts VersionInfo::Channel to string for user-agent string.
std::string ChannelToString(chrome::VersionInfo::Channel channel) {
switch (channel) {
case chrome::VersionInfo::CHANNEL_UNKNOWN:
return "unknown";
case chrome::VersionInfo::CHANNEL_CANARY:
return "canary";
case chrome::VersionInfo::CHANNEL_DEV:
return "dev";
case chrome::VersionInfo::CHANNEL_BETA:
return "beta";
case chrome::VersionInfo::CHANNEL_STABLE:
return "stable";
default:
NOTREACHED();
return "unknown";
};
}
} // namespace
DeviceInfo::DeviceInfo(const std::string& client_name,
const std::string& chrome_version,
const std::string& sync_user_agent,
const sync_pb::SyncEnums::DeviceType device_type)
: client_name_(client_name),
chrome_version_(chrome_version),
sync_user_agent_(sync_user_agent),
device_type_(device_type) {
}
DeviceInfo::~DeviceInfo() { }
const std::string& DeviceInfo::client_name() const {
return client_name_;
}
const std::string& DeviceInfo::chrome_version() const {
return chrome_version_;
}
const std::string& DeviceInfo::sync_user_agent() const {
return sync_user_agent_;
}
sync_pb::SyncEnums::DeviceType DeviceInfo::device_type() const {
return device_type_;
}
bool DeviceInfo::Equals(const DeviceInfo& other) const {
return this->client_name() == other.client_name()
&& this->chrome_version() == other.chrome_version()
&& this->sync_user_agent() == other.sync_user_agent()
&& this->device_type() == other.device_type();
}
// static.
sync_pb::SyncEnums::DeviceType DeviceInfo::GetLocalDeviceType() {
#if defined(OS_CHROMEOS)
return sync_pb::SyncEnums_DeviceType_TYPE_CROS;
#elif defined(OS_LINUX)
return sync_pb::SyncEnums_DeviceType_TYPE_LINUX;
#elif defined(OS_MACOSX)
return sync_pb::SyncEnums_DeviceType_TYPE_MAC;
#elif defined(OS_WIN)
return sync_pb::SyncEnums_DeviceType_TYPE_WIN;
#elif defined(OS_ANDROID)
return IsTabletUI() ?
sync_pb::SyncEnums_DeviceType_TYPE_TABLET :
sync_pb::SyncEnums_DeviceType_TYPE_PHONE;
#else
return sync_pb::SyncEnums_DeviceType_TYPE_OTHER;
#endif
}
// static
std::string DeviceInfo::MakeUserAgentForSyncApi(
const chrome::VersionInfo& version_info) {
std::string user_agent;
user_agent = "Chrome ";
#if defined(OS_WIN)
user_agent += "WIN ";
#elif defined(OS_CHROMEOS)
user_agent += "CROS ";
#elif defined(OS_ANDROID)
if (IsTabletUI())
user_agent += "ANDROID-TABLET ";
else
user_agent += "ANDROID-PHONE ";
#elif defined(OS_LINUX)
user_agent += "LINUX ";
#elif defined(OS_FREEBSD)
user_agent += "FREEBSD ";
#elif defined(OS_OPENBSD)
user_agent += "OPENBSD ";
#elif defined(OS_MACOSX)
user_agent += "MAC ";
#endif
if (!version_info.is_valid()) {
DLOG(ERROR) << "Unable to create chrome::VersionInfo object";
return user_agent;
}
user_agent += version_info.Version();
user_agent += " (" + version_info.LastChange() + ")";
if (!version_info.IsOfficialBuild()) {
user_agent += "-devel";
} else {
user_agent += " channel(" +
ChannelToString(version_info.GetChannel()) + ")";
}
return user_agent;
}
// static.
void DeviceInfo::CreateLocalDeviceInfo(
base::Callback<void(const DeviceInfo& local_info)> callback) {
syncer::GetSessionName(
content::BrowserThread::GetBlockingPool(),
base::Bind(&DeviceInfo::CreateLocalDeviceInfoContinuation, callback));
}
// static.
void DeviceInfo::CreateLocalDeviceInfoContinuation(
base::Callback<void(const DeviceInfo& local_info)> callback,
const std::string& session_name) {
chrome::VersionInfo version_info;
DeviceInfo local_info(
session_name,
version_info.CreateVersionString(),
MakeUserAgentForSyncApi(version_info),
GetLocalDeviceType());
callback.Run(local_info);
}
} // namespace browser_sync
|