Home | History | Annotate | Download | only in glue
      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.
      4 
      5 #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"
     13 #include "sync/util/get_session_name.h"
     14 
     15 namespace browser_sync {
     16 
     17 namespace {
     18 
     19 #if defined(OS_ANDROID)
     20 bool IsTabletUI() {
     21   return CommandLine::ForCurrentProcess()->HasSwitch(switches::kTabletUI);
     22 }
     23 #endif
     24 
     25 // Converts VersionInfo::Channel to string for user-agent string.
     26 std::string ChannelToString(chrome::VersionInfo::Channel channel) {
     27   switch (channel) {
     28     case chrome::VersionInfo::CHANNEL_UNKNOWN:
     29       return "unknown";
     30     case chrome::VersionInfo::CHANNEL_CANARY:
     31       return "canary";
     32     case chrome::VersionInfo::CHANNEL_DEV:
     33       return "dev";
     34     case chrome::VersionInfo::CHANNEL_BETA:
     35       return "beta";
     36     case chrome::VersionInfo::CHANNEL_STABLE:
     37       return "stable";
     38     default:
     39       NOTREACHED();
     40       return "unknown";
     41   };
     42 }
     43 
     44 std::string DeviceTypeToString(sync_pb::SyncEnums::DeviceType device_type) {
     45   switch (device_type) {
     46     case sync_pb::SyncEnums_DeviceType_TYPE_WIN:
     47       return "WIN";
     48     case sync_pb::SyncEnums_DeviceType_TYPE_MAC:
     49       return "MAC";
     50     case sync_pb::SyncEnums_DeviceType_TYPE_LINUX:
     51       return "LINUX";
     52     case sync_pb::SyncEnums_DeviceType_TYPE_CROS:
     53       return "CHROME OS";
     54     case sync_pb::SyncEnums_DeviceType_TYPE_OTHER:
     55       return "OTHER";
     56     case sync_pb::SyncEnums_DeviceType_TYPE_PHONE:
     57       return "PHONE";
     58     case sync_pb::SyncEnums_DeviceType_TYPE_TABLET:
     59       return "TABLET";
     60     default:
     61       NOTREACHED();
     62       return "UNKNOWN";
     63   }
     64 }
     65 
     66 }  // namespace
     67 
     68 DeviceInfo::DeviceInfo(const std::string& guid,
     69                        const std::string& client_name,
     70                        const std::string& chrome_version,
     71                        const std::string& sync_user_agent,
     72                        const sync_pb::SyncEnums::DeviceType device_type)
     73     : guid_(guid),
     74       client_name_(client_name),
     75       chrome_version_(chrome_version),
     76       sync_user_agent_(sync_user_agent),
     77       device_type_(device_type) {
     78 }
     79 
     80 DeviceInfo::~DeviceInfo() { }
     81 
     82 const std::string& DeviceInfo::guid() const {
     83   return guid_;
     84 }
     85 
     86 const std::string& DeviceInfo::client_name() const {
     87   return client_name_;
     88 }
     89 
     90 const std::string& DeviceInfo::chrome_version() const {
     91   return chrome_version_;
     92 }
     93 
     94 const std::string& DeviceInfo::sync_user_agent() const {
     95   return sync_user_agent_;
     96 }
     97 
     98 const std::string& DeviceInfo::public_id() const {
     99   return public_id_;
    100 }
    101 
    102 sync_pb::SyncEnums::DeviceType DeviceInfo::device_type() const {
    103   return device_type_;
    104 }
    105 
    106 bool DeviceInfo::Equals(const DeviceInfo& other) const {
    107   return this->guid() == other.guid()
    108       && this->client_name() == other.client_name()
    109       && this->chrome_version() == other.chrome_version()
    110       && this->sync_user_agent() == other.sync_user_agent()
    111       && this->device_type() == other.device_type();
    112 }
    113 
    114 // static.
    115 sync_pb::SyncEnums::DeviceType DeviceInfo::GetLocalDeviceType() {
    116 #if defined(OS_CHROMEOS)
    117   return sync_pb::SyncEnums_DeviceType_TYPE_CROS;
    118 #elif defined(OS_LINUX)
    119   return sync_pb::SyncEnums_DeviceType_TYPE_LINUX;
    120 #elif defined(OS_MACOSX)
    121   return sync_pb::SyncEnums_DeviceType_TYPE_MAC;
    122 #elif defined(OS_WIN)
    123   return sync_pb::SyncEnums_DeviceType_TYPE_WIN;
    124 #elif defined(OS_ANDROID)
    125   return IsTabletUI() ?
    126       sync_pb::SyncEnums_DeviceType_TYPE_TABLET :
    127       sync_pb::SyncEnums_DeviceType_TYPE_PHONE;
    128 #else
    129   return sync_pb::SyncEnums_DeviceType_TYPE_OTHER;
    130 #endif
    131 }
    132 
    133 // static
    134 std::string DeviceInfo::MakeUserAgentForSyncApi(
    135     const chrome::VersionInfo& version_info) {
    136   std::string user_agent;
    137   user_agent = "Chrome ";
    138 #if defined(OS_WIN)
    139   user_agent += "WIN ";
    140 #elif defined(OS_CHROMEOS)
    141   user_agent += "CROS ";
    142 #elif defined(OS_ANDROID)
    143   if (IsTabletUI())
    144     user_agent += "ANDROID-TABLET ";
    145   else
    146     user_agent += "ANDROID-PHONE ";
    147 #elif defined(OS_LINUX)
    148   user_agent += "LINUX ";
    149 #elif defined(OS_FREEBSD)
    150   user_agent += "FREEBSD ";
    151 #elif defined(OS_OPENBSD)
    152   user_agent += "OPENBSD ";
    153 #elif defined(OS_MACOSX)
    154   user_agent += "MAC ";
    155 #endif
    156   if (!version_info.is_valid()) {
    157     DLOG(ERROR) << "Unable to create chrome::VersionInfo object";
    158     return user_agent;
    159   }
    160 
    161   user_agent += version_info.Version();
    162   user_agent += " (" + version_info.LastChange() + ")";
    163   if (!version_info.IsOfficialBuild()) {
    164     user_agent += "-devel";
    165   } else {
    166     user_agent += " channel(" +
    167         ChannelToString(version_info.GetChannel()) + ")";
    168   }
    169 
    170   return user_agent;
    171 }
    172 
    173 base::DictionaryValue* DeviceInfo::ToValue() {
    174   base::DictionaryValue* value = new base::DictionaryValue();
    175   value->SetString("Id", public_id_);
    176   value->SetString("Client Name", client_name_);
    177   value->SetString("Chrome Version", chrome_version_);
    178   value->SetString("Sync User Agent", sync_user_agent_);
    179   value->SetString("Device Type", DeviceTypeToString(device_type_));
    180   return value;
    181 }
    182 
    183 void DeviceInfo::set_public_id(std::string id) {
    184   public_id_ = id;
    185 }
    186 
    187 // static.
    188 void DeviceInfo::CreateLocalDeviceInfo(
    189     const std::string& guid,
    190     base::Callback<void(const DeviceInfo& local_info)> callback) {
    191   GetClientName(
    192       base::Bind(&DeviceInfo::CreateLocalDeviceInfoContinuation,
    193                  guid,
    194                  callback));
    195 }
    196 
    197 // static.
    198 void DeviceInfo::GetClientName(
    199     base::Callback<void(const std::string& client_name)> callback) {
    200   syncer::GetSessionName(
    201       content::BrowserThread::GetBlockingPool(),
    202       base::Bind(&DeviceInfo::GetClientNameContinuation,
    203                  callback));
    204 }
    205 
    206 void DeviceInfo::GetClientNameContinuation(
    207     base::Callback<void(const std::string& local_info)> callback,
    208     const std::string& session_name) {
    209   callback.Run(session_name);
    210 }
    211 
    212 // static.
    213 void DeviceInfo::CreateLocalDeviceInfoContinuation(
    214     const std::string& guid,
    215     base::Callback<void(const DeviceInfo& local_info)> callback,
    216     const std::string& session_name) {
    217   chrome::VersionInfo version_info;
    218 
    219   DeviceInfo local_info(
    220       guid,
    221       session_name,
    222       version_info.CreateVersionString(),
    223       MakeUserAgentForSyncApi(version_info),
    224       GetLocalDeviceType());
    225 
    226   callback.Run(local_info);
    227 }
    228 
    229 }  // namespace browser_sync
    230