1 // Copyright 2014 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 "base/bind.h"
6 #include "chrome/browser/sync/glue/local_device_info_provider_impl.h"
7 #include "chrome/common/chrome_version_info.h"
8 #include "content/public/browser/browser_thread.h"
9 #include "sync/util/get_session_name.h"
10 #include "ui/base/device_form_factor.h"
11
12 namespace browser_sync {
13
14 namespace {
15
16 // Converts VersionInfo::Channel to string for user-agent string.
ChannelToString(chrome::VersionInfo::Channel channel)17 std::string ChannelToString(chrome::VersionInfo::Channel channel) {
18 switch (channel) {
19 case chrome::VersionInfo::CHANNEL_UNKNOWN:
20 return "unknown";
21 case chrome::VersionInfo::CHANNEL_CANARY:
22 return "canary";
23 case chrome::VersionInfo::CHANNEL_DEV:
24 return "dev";
25 case chrome::VersionInfo::CHANNEL_BETA:
26 return "beta";
27 case chrome::VersionInfo::CHANNEL_STABLE:
28 return "stable";
29 default:
30 NOTREACHED();
31 return "unknown";
32 }
33 }
34
35 #if defined(OS_ANDROID)
IsTabletUI()36 bool IsTabletUI() {
37 return ui::GetDeviceFormFactor() == ui::DEVICE_FORM_FACTOR_TABLET;
38 }
39 #endif
40
GetLocalDeviceType()41 sync_pb::SyncEnums::DeviceType GetLocalDeviceType() {
42 #if defined(OS_CHROMEOS)
43 return sync_pb::SyncEnums_DeviceType_TYPE_CROS;
44 #elif defined(OS_LINUX)
45 return sync_pb::SyncEnums_DeviceType_TYPE_LINUX;
46 #elif defined(OS_MACOSX)
47 return sync_pb::SyncEnums_DeviceType_TYPE_MAC;
48 #elif defined(OS_WIN)
49 return sync_pb::SyncEnums_DeviceType_TYPE_WIN;
50 #elif defined(OS_ANDROID)
51 return IsTabletUI() ? sync_pb::SyncEnums_DeviceType_TYPE_TABLET
52 : sync_pb::SyncEnums_DeviceType_TYPE_PHONE;
53 #else
54 return sync_pb::SyncEnums_DeviceType_TYPE_OTHER;
55 #endif
56 }
57
58 } // namespace
59
LocalDeviceInfoProviderImpl()60 LocalDeviceInfoProviderImpl::LocalDeviceInfoProviderImpl()
61 : weak_factory_(this) {
62 }
63
~LocalDeviceInfoProviderImpl()64 LocalDeviceInfoProviderImpl::~LocalDeviceInfoProviderImpl() {
65 }
66
67 // static.
MakeUserAgentForSyncApi(const chrome::VersionInfo & version_info)68 std::string LocalDeviceInfoProviderImpl::MakeUserAgentForSyncApi(
69 const chrome::VersionInfo& version_info) {
70 std::string user_agent;
71 user_agent = "Chrome ";
72 #if defined(OS_WIN)
73 user_agent += "WIN ";
74 #elif defined(OS_CHROMEOS)
75 user_agent += "CROS ";
76 #elif defined(OS_ANDROID)
77 if (IsTabletUI())
78 user_agent += "ANDROID-TABLET ";
79 else
80 user_agent += "ANDROID-PHONE ";
81 #elif defined(OS_LINUX)
82 user_agent += "LINUX ";
83 #elif defined(OS_FREEBSD)
84 user_agent += "FREEBSD ";
85 #elif defined(OS_OPENBSD)
86 user_agent += "OPENBSD ";
87 #elif defined(OS_MACOSX)
88 user_agent += "MAC ";
89 #endif
90 if (!version_info.is_valid()) {
91 DLOG(ERROR) << "Unable to create chrome::VersionInfo object";
92 return user_agent;
93 }
94
95 user_agent += version_info.Version();
96 user_agent += " (" + version_info.LastChange() + ")";
97 if (!version_info.IsOfficialBuild()) {
98 user_agent += "-devel";
99 } else {
100 user_agent +=
101 " channel(" + ChannelToString(version_info.GetChannel()) + ")";
102 }
103
104 return user_agent;
105 }
106
107 const sync_driver::DeviceInfo*
GetLocalDeviceInfo() const108 LocalDeviceInfoProviderImpl::GetLocalDeviceInfo() const {
109 return local_device_info_.get();
110 }
111
GetLocalSyncCacheGUID() const112 std::string LocalDeviceInfoProviderImpl::GetLocalSyncCacheGUID() const {
113 return cache_guid_;
114 }
115
116 scoped_ptr<sync_driver::LocalDeviceInfoProvider::Subscription>
RegisterOnInitializedCallback(const base::Closure & callback)117 LocalDeviceInfoProviderImpl::RegisterOnInitializedCallback(
118 const base::Closure& callback) {
119 DCHECK(!local_device_info_.get());
120 return callback_list_.Add(callback);
121 }
122
Initialize(const std::string & cache_guid,const std::string & signin_scoped_device_id)123 void LocalDeviceInfoProviderImpl::Initialize(
124 const std::string& cache_guid, const std::string& signin_scoped_device_id) {
125 DCHECK(!cache_guid.empty());
126 cache_guid_ = cache_guid;
127
128 syncer::GetSessionName(
129 content::BrowserThread::GetBlockingPool(),
130 base::Bind(&LocalDeviceInfoProviderImpl::InitializeContinuation,
131 weak_factory_.GetWeakPtr(),
132 cache_guid,
133 signin_scoped_device_id));
134 }
135
InitializeContinuation(const std::string & guid,const std::string & signin_scoped_device_id,const std::string & session_name)136 void LocalDeviceInfoProviderImpl::InitializeContinuation(
137 const std::string& guid,
138 const std::string& signin_scoped_device_id,
139 const std::string& session_name) {
140 chrome::VersionInfo version_info;
141
142 local_device_info_.reset(
143 new sync_driver::DeviceInfo(guid,
144 session_name,
145 version_info.CreateVersionString(),
146 MakeUserAgentForSyncApi(version_info),
147 GetLocalDeviceType(),
148 signin_scoped_device_id));
149
150 // Notify observers.
151 callback_list_.Notify();
152 }
153
154 } // namespace browser_sync
155