Fix build break
[chromium-blink-merge.git] / chrome / browser / sync / glue / device_info.cc
blob8efc9944e492fa5c52327f681be8072aafa70175
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 {
16 namespace {
18 #if defined(OS_ANDROID)
19 bool IsTabletUI() {
20 return CommandLine::ForCurrentProcess()->HasSwitch(switches::kTabletUI);
22 #endif
24 // Converts VersionInfo::Channel to string for user-agent string.
25 std::string ChannelToString(chrome::VersionInfo::Channel channel) {
26 switch (channel) {
27 case chrome::VersionInfo::CHANNEL_UNKNOWN:
28 return "unknown";
29 case chrome::VersionInfo::CHANNEL_CANARY:
30 return "canary";
31 case chrome::VersionInfo::CHANNEL_DEV:
32 return "dev";
33 case chrome::VersionInfo::CHANNEL_BETA:
34 return "beta";
35 case chrome::VersionInfo::CHANNEL_STABLE:
36 return "stable";
37 default:
38 NOTREACHED();
39 return "unknown";
43 } // namespace
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 {
58 return client_name_;
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 {
70 return device_type_;
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();
80 // static.
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;
88 #elif defined(OS_WIN)
89 return sync_pb::SyncEnums_DeviceType_TYPE_WIN;
90 #elif defined(OS_ANDROID)
91 return IsTabletUI() ?
92 sync_pb::SyncEnums_DeviceType_TYPE_TABLET :
93 sync_pb::SyncEnums_DeviceType_TYPE_PHONE;
94 #else
95 return sync_pb::SyncEnums_DeviceType_TYPE_OTHER;
96 #endif
99 // static
100 std::string DeviceInfo::MakeUserAgentForSyncApi(
101 const chrome::VersionInfo& version_info) {
102 std::string user_agent;
103 user_agent = "Chrome ";
104 #if defined(OS_WIN)
105 user_agent += "WIN ";
106 #elif defined(OS_CHROMEOS)
107 user_agent += "CROS ";
108 #elif defined(OS_ANDROID)
109 if (IsTabletUI())
110 user_agent += "ANDROID-TABLET ";
111 else
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 ";
121 #endif
122 if (!version_info.is_valid()) {
123 DLOG(ERROR) << "Unable to create chrome::VersionInfo object";
124 return user_agent;
127 user_agent += version_info.Version();
128 user_agent += " (" + version_info.LastChange() + ")";
129 if (!version_info.IsOfficialBuild()) {
130 user_agent += "-devel";
131 } else {
132 user_agent += " channel(" +
133 ChannelToString(version_info.GetChannel()) + ")";
136 return user_agent;
139 // static.
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));
147 // static.
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(
154 session_name,
155 version_info.CreateVersionString(),
156 MakeUserAgentForSyncApi(version_info),
157 GetLocalDeviceType());
159 callback.Run(local_info);
162 } // namespace browser_sync