Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/values.h" | |
| 5 #include "chrome/browser/sync/glue/device_info.h" | 6 #include "chrome/browser/sync/glue/device_info.h" |
| 6 | |
| 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" | 7 #include "content/public/browser/browser_thread.h" |
| 13 #include "sync/util/get_session_name.h" | 8 #include "sync/util/get_session_name.h" |
| 14 #include "ui/base/device_form_factor.h" | |
| 15 | 9 |
| 16 namespace browser_sync { | 10 namespace browser_sync { |
| 17 | 11 |
| 18 namespace { | |
| 19 | |
| 20 #if defined(OS_ANDROID) | |
| 21 bool IsTabletUI() { | |
| 22 return ui::GetDeviceFormFactor() == ui::DEVICE_FORM_FACTOR_TABLET; | |
| 23 } | |
| 24 #endif | |
| 25 | |
| 26 // Converts VersionInfo::Channel to string for user-agent string. | |
| 27 std::string ChannelToString(chrome::VersionInfo::Channel channel) { | |
| 28 switch (channel) { | |
| 29 case chrome::VersionInfo::CHANNEL_UNKNOWN: | |
| 30 return "unknown"; | |
| 31 case chrome::VersionInfo::CHANNEL_CANARY: | |
| 32 return "canary"; | |
| 33 case chrome::VersionInfo::CHANNEL_DEV: | |
| 34 return "dev"; | |
| 35 case chrome::VersionInfo::CHANNEL_BETA: | |
| 36 return "beta"; | |
| 37 case chrome::VersionInfo::CHANNEL_STABLE: | |
| 38 return "stable"; | |
| 39 default: | |
| 40 NOTREACHED(); | |
| 41 return "unknown"; | |
| 42 } | |
| 43 } | |
| 44 | |
| 45 } // namespace | |
| 46 | |
| 47 DeviceInfo::DeviceInfo(const std::string& guid, | 12 DeviceInfo::DeviceInfo(const std::string& guid, |
| 48 const std::string& client_name, | 13 const std::string& client_name, |
| 49 const std::string& chrome_version, | 14 const std::string& chrome_version, |
| 50 const std::string& sync_user_agent, | 15 const std::string& sync_user_agent, |
| 51 const sync_pb::SyncEnums::DeviceType device_type, | 16 const sync_pb::SyncEnums::DeviceType device_type, |
| 52 const std::string& signin_scoped_device_id) | 17 const std::string& signin_scoped_device_id) |
| 53 : guid_(guid), | 18 : guid_(guid), |
| 54 client_name_(client_name), | 19 client_name_(client_name), |
| 55 chrome_version_(chrome_version), | 20 chrome_version_(chrome_version), |
| 56 sync_user_agent_(sync_user_agent), | 21 sync_user_agent_(sync_user_agent), |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 126 | 91 |
| 127 bool DeviceInfo::Equals(const DeviceInfo& other) const { | 92 bool DeviceInfo::Equals(const DeviceInfo& other) const { |
| 128 return this->guid() == other.guid() && | 93 return this->guid() == other.guid() && |
| 129 this->client_name() == other.client_name() && | 94 this->client_name() == other.client_name() && |
| 130 this->chrome_version() == other.chrome_version() && | 95 this->chrome_version() == other.chrome_version() && |
| 131 this->sync_user_agent() == other.sync_user_agent() && | 96 this->sync_user_agent() == other.sync_user_agent() && |
| 132 this->device_type() == other.device_type() && | 97 this->device_type() == other.device_type() && |
| 133 this->signin_scoped_device_id() == other.signin_scoped_device_id(); | 98 this->signin_scoped_device_id() == other.signin_scoped_device_id(); |
| 134 } | 99 } |
| 135 | 100 |
| 136 // static. | |
| 137 sync_pb::SyncEnums::DeviceType DeviceInfo::GetLocalDeviceType() { | |
| 138 #if defined(OS_CHROMEOS) | |
| 139 return sync_pb::SyncEnums_DeviceType_TYPE_CROS; | |
| 140 #elif defined(OS_LINUX) | |
| 141 return sync_pb::SyncEnums_DeviceType_TYPE_LINUX; | |
| 142 #elif defined(OS_MACOSX) | |
| 143 return sync_pb::SyncEnums_DeviceType_TYPE_MAC; | |
| 144 #elif defined(OS_WIN) | |
| 145 return sync_pb::SyncEnums_DeviceType_TYPE_WIN; | |
| 146 #elif defined(OS_ANDROID) | |
| 147 return IsTabletUI() ? | |
| 148 sync_pb::SyncEnums_DeviceType_TYPE_TABLET : | |
| 149 sync_pb::SyncEnums_DeviceType_TYPE_PHONE; | |
| 150 #else | |
| 151 return sync_pb::SyncEnums_DeviceType_TYPE_OTHER; | |
| 152 #endif | |
| 153 } | |
| 154 | |
| 155 // static | |
| 156 std::string DeviceInfo::MakeUserAgentForSyncApi( | |
| 157 const chrome::VersionInfo& version_info) { | |
| 158 std::string user_agent; | |
| 159 user_agent = "Chrome "; | |
| 160 #if defined(OS_WIN) | |
| 161 user_agent += "WIN "; | |
| 162 #elif defined(OS_CHROMEOS) | |
| 163 user_agent += "CROS "; | |
| 164 #elif defined(OS_ANDROID) | |
| 165 if (IsTabletUI()) | |
| 166 user_agent += "ANDROID-TABLET "; | |
| 167 else | |
| 168 user_agent += "ANDROID-PHONE "; | |
| 169 #elif defined(OS_LINUX) | |
| 170 user_agent += "LINUX "; | |
| 171 #elif defined(OS_FREEBSD) | |
| 172 user_agent += "FREEBSD "; | |
| 173 #elif defined(OS_OPENBSD) | |
| 174 user_agent += "OPENBSD "; | |
| 175 #elif defined(OS_MACOSX) | |
| 176 user_agent += "MAC "; | |
| 177 #endif | |
| 178 if (!version_info.is_valid()) { | |
| 179 DLOG(ERROR) << "Unable to create chrome::VersionInfo object"; | |
| 180 return user_agent; | |
| 181 } | |
| 182 | |
| 183 user_agent += version_info.Version(); | |
| 184 user_agent += " (" + version_info.LastChange() + ")"; | |
| 185 if (!version_info.IsOfficialBuild()) { | |
| 186 user_agent += "-devel"; | |
| 187 } else { | |
| 188 user_agent += " channel(" + | |
| 189 ChannelToString(version_info.GetChannel()) + ")"; | |
| 190 } | |
| 191 | |
| 192 return user_agent; | |
| 193 } | |
| 194 | |
| 195 base::DictionaryValue* DeviceInfo::ToValue() { | 101 base::DictionaryValue* DeviceInfo::ToValue() { |
| 196 base::DictionaryValue* value = new base::DictionaryValue(); | 102 base::DictionaryValue* value = new base::DictionaryValue(); |
| 197 value->SetString("name", client_name_); | 103 value->SetString("name", client_name_); |
| 198 value->SetString("id", public_id_); | 104 value->SetString("id", public_id_); |
| 199 value->SetString("os", GetOSString()); | 105 value->SetString("os", GetOSString()); |
| 200 value->SetString("type", GetDeviceTypeString()); | 106 value->SetString("type", GetDeviceTypeString()); |
| 201 value->SetString("chromeVersion", chrome_version_); | 107 value->SetString("chromeVersion", chrome_version_); |
| 202 return value; | 108 return value; |
| 203 } | 109 } |
| 204 | 110 |
| 205 void DeviceInfo::set_public_id(std::string id) { | 111 void DeviceInfo::set_public_id(std::string id) { |
| 206 public_id_ = id; | 112 public_id_ = id; |
| 207 } | 113 } |
| 208 | 114 |
| 209 // static. | 115 // static. |
| 210 void DeviceInfo::CreateLocalDeviceInfo( | |
| 211 const std::string& guid, | |
| 212 const std::string& signin_scoped_device_id, | |
| 213 base::Callback<void(const DeviceInfo& local_info)> callback) { | |
| 214 GetClientName(base::Bind(&DeviceInfo::CreateLocalDeviceInfoContinuation, | |
| 215 guid, | |
| 216 signin_scoped_device_id, | |
| 217 callback)); | |
| 218 } | |
| 219 | |
| 220 // static. | |
| 221 void DeviceInfo::GetClientName( | 116 void DeviceInfo::GetClientName( |
| 222 base::Callback<void(const std::string& client_name)> callback) { | 117 const base::Callback<void(const std::string& client_name)> callback) { |
|
maniscalco
2014/09/23 16:28:27
nit: any reason for the parameter name in the call
stanisc
2014/09/23 21:30:32
Done. Introduced the typedef you suggested. The na
| |
| 223 syncer::GetSessionName( | 118 syncer::GetSessionName(content::BrowserThread::GetBlockingPool(), callback); |
| 224 content::BrowserThread::GetBlockingPool(), | |
| 225 base::Bind(&DeviceInfo::GetClientNameContinuation, | |
| 226 callback)); | |
| 227 } | |
| 228 | |
| 229 void DeviceInfo::GetClientNameContinuation( | |
| 230 base::Callback<void(const std::string& local_info)> callback, | |
| 231 const std::string& session_name) { | |
| 232 callback.Run(session_name); | |
| 233 } | |
| 234 | |
| 235 // static. | |
| 236 void DeviceInfo::CreateLocalDeviceInfoContinuation( | |
| 237 const std::string& guid, | |
| 238 const std::string& signin_scoped_device_id, | |
| 239 base::Callback<void(const DeviceInfo& local_info)> callback, | |
| 240 const std::string& session_name) { | |
| 241 chrome::VersionInfo version_info; | |
| 242 | |
| 243 DeviceInfo local_info(guid, | |
| 244 session_name, | |
| 245 version_info.CreateVersionString(), | |
| 246 MakeUserAgentForSyncApi(version_info), | |
| 247 GetLocalDeviceType(), | |
| 248 signin_scoped_device_id); | |
| 249 | |
| 250 callback.Run(local_info); | |
| 251 } | 119 } |
| 252 | 120 |
| 253 } // namespace browser_sync | 121 } // namespace browser_sync |
| OLD | NEW |