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.
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"
12 namespace browser_sync
{
16 // Converts VersionInfo::Channel to string for user-agent string.
17 std::string
ChannelToString(chrome::VersionInfo::Channel channel
) {
19 case chrome::VersionInfo::CHANNEL_UNKNOWN
:
21 case chrome::VersionInfo::CHANNEL_CANARY
:
23 case chrome::VersionInfo::CHANNEL_DEV
:
25 case chrome::VersionInfo::CHANNEL_BETA
:
27 case chrome::VersionInfo::CHANNEL_STABLE
:
35 #if defined(OS_ANDROID)
37 return ui::GetDeviceFormFactor() == ui::DEVICE_FORM_FACTOR_TABLET
;
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
;
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
;
54 return sync_pb::SyncEnums_DeviceType_TYPE_OTHER
;
60 LocalDeviceInfoProviderImpl::LocalDeviceInfoProviderImpl()
61 : weak_factory_(this) {
64 LocalDeviceInfoProviderImpl::~LocalDeviceInfoProviderImpl() {
68 std::string
LocalDeviceInfoProviderImpl::MakeUserAgentForSyncApi(
69 const chrome::VersionInfo
& version_info
) {
70 std::string user_agent
;
71 user_agent
= "Chrome ";
74 #elif defined(OS_CHROMEOS)
75 user_agent
+= "CROS ";
76 #elif defined(OS_ANDROID)
78 user_agent
+= "ANDROID-TABLET ";
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)
90 user_agent
+= version_info
.Version();
91 user_agent
+= " (" + version_info
.LastChange() + ")";
92 if (!version_info
.IsOfficialBuild()) {
93 user_agent
+= "-devel";
96 " channel(" + ChannelToString(version_info
.GetChannel()) + ")";
102 const sync_driver::DeviceInfo
*
103 LocalDeviceInfoProviderImpl::GetLocalDeviceInfo() const {
104 return local_device_info_
.get();
107 std::string
LocalDeviceInfoProviderImpl::GetLocalSyncCacheGUID() const {
111 scoped_ptr
<sync_driver::LocalDeviceInfoProvider::Subscription
>
112 LocalDeviceInfoProviderImpl::RegisterOnInitializedCallback(
113 const base::Closure
& callback
) {
114 DCHECK(!local_device_info_
.get());
115 return callback_list_
.Add(callback
);
118 void LocalDeviceInfoProviderImpl::Initialize(
119 const std::string
& cache_guid
, const std::string
& signin_scoped_device_id
) {
120 DCHECK(!cache_guid
.empty());
121 cache_guid_
= cache_guid
;
123 syncer::GetSessionName(
124 content::BrowserThread::GetBlockingPool(),
125 base::Bind(&LocalDeviceInfoProviderImpl::InitializeContinuation
,
126 weak_factory_
.GetWeakPtr(),
128 signin_scoped_device_id
));
131 void LocalDeviceInfoProviderImpl::InitializeContinuation(
132 const std::string
& guid
,
133 const std::string
& signin_scoped_device_id
,
134 const std::string
& session_name
) {
135 chrome::VersionInfo version_info
;
137 local_device_info_
.reset(
138 new sync_driver::DeviceInfo(guid
,
140 version_info
.CreateVersionString(),
141 MakeUserAgentForSyncApi(version_info
),
142 GetLocalDeviceType(),
143 signin_scoped_device_id
));
146 callback_list_
.Notify();
149 } // namespace browser_sync