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.
5 #include "chrome/browser/metrics/metrics_log_chromeos.h"
7 #include "base/prefs/pref_service.h"
8 #include "base/strings/string_number_conversions.h"
9 #include "base/strings/string_util.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "chrome/browser/chromeos/login/user_manager.h"
12 #include "chrome/common/pref_names.h"
13 #include "components/metrics/proto/chrome_user_metrics_extension.pb.h"
14 #include "device/bluetooth/bluetooth_adapter.h"
15 #include "device/bluetooth/bluetooth_adapter_factory.h"
16 #include "device/bluetooth/bluetooth_device.h"
17 #include "ui/events/event_utils.h"
18 #include "ui/gfx/screen.h"
21 #include "ui/events/x/touch_factory_x11.h"
22 #endif // defined(USE_X11)
24 using metrics::ChromeUserMetricsExtension
;
25 using metrics::PerfDataProto
;
26 using metrics::SystemProfileProto
;
27 typedef SystemProfileProto::Hardware::Bluetooth::PairedDevice PairedDevice
;
31 PairedDevice::Type
AsBluetoothDeviceType(
32 device::BluetoothDevice::DeviceType device_type
) {
33 switch (device_type
) {
34 case device::BluetoothDevice::DEVICE_UNKNOWN
:
35 return PairedDevice::DEVICE_UNKNOWN
;
36 case device::BluetoothDevice::DEVICE_COMPUTER
:
37 return PairedDevice::DEVICE_COMPUTER
;
38 case device::BluetoothDevice::DEVICE_PHONE
:
39 return PairedDevice::DEVICE_PHONE
;
40 case device::BluetoothDevice::DEVICE_MODEM
:
41 return PairedDevice::DEVICE_MODEM
;
42 case device::BluetoothDevice::DEVICE_AUDIO
:
43 return PairedDevice::DEVICE_AUDIO
;
44 case device::BluetoothDevice::DEVICE_CAR_AUDIO
:
45 return PairedDevice::DEVICE_CAR_AUDIO
;
46 case device::BluetoothDevice::DEVICE_VIDEO
:
47 return PairedDevice::DEVICE_VIDEO
;
48 case device::BluetoothDevice::DEVICE_PERIPHERAL
:
49 return PairedDevice::DEVICE_PERIPHERAL
;
50 case device::BluetoothDevice::DEVICE_JOYSTICK
:
51 return PairedDevice::DEVICE_JOYSTICK
;
52 case device::BluetoothDevice::DEVICE_GAMEPAD
:
53 return PairedDevice::DEVICE_GAMEPAD
;
54 case device::BluetoothDevice::DEVICE_KEYBOARD
:
55 return PairedDevice::DEVICE_KEYBOARD
;
56 case device::BluetoothDevice::DEVICE_MOUSE
:
57 return PairedDevice::DEVICE_MOUSE
;
58 case device::BluetoothDevice::DEVICE_TABLET
:
59 return PairedDevice::DEVICE_TABLET
;
60 case device::BluetoothDevice::DEVICE_KEYBOARD_MOUSE_COMBO
:
61 return PairedDevice::DEVICE_KEYBOARD_MOUSE_COMBO
;
65 return PairedDevice::DEVICE_UNKNOWN
;
68 void WriteExternalTouchscreensProto(SystemProfileProto::Hardware
* hardware
) {
70 std::set
<std::pair
<int, int> > touchscreen_ids
=
71 ui::TouchFactory::GetInstance()->GetTouchscreenIds();
72 for (std::set
<std::pair
<int, int> >::iterator it
= touchscreen_ids
.begin();
73 it
!= touchscreen_ids
.end();
75 SystemProfileProto::Hardware::TouchScreen
* touchscreen
=
76 hardware
->add_external_touchscreen();
77 touchscreen
->set_vendor_id(it
->first
);
78 touchscreen
->set_product_id(it
->second
);
80 #endif // defined(USE_X11)
85 MetricsLogChromeOS::~MetricsLogChromeOS() {
88 MetricsLogChromeOS::MetricsLogChromeOS(ChromeUserMetricsExtension
* uma_proto
)
89 : uma_proto_(uma_proto
) {
90 UpdateMultiProfileUserCount();
93 void MetricsLogChromeOS::LogChromeOSMetrics() {
94 std::vector
<PerfDataProto
> perf_data
;
95 if (perf_provider_
.GetPerfData(&perf_data
)) {
96 for (std::vector
<PerfDataProto
>::iterator iter
= perf_data
.begin();
97 iter
!= perf_data
.end();
99 uma_proto_
->add_perf_data()->Swap(&(*iter
));
103 WriteBluetoothProto();
104 UpdateMultiProfileUserCount();
106 SystemProfileProto::Hardware
* hardware
=
107 uma_proto_
->mutable_system_profile()->mutable_hardware();
108 gfx::Display::TouchSupport has_touch
= ui::GetInternalDisplayTouchSupport();
109 if (has_touch
== gfx::Display::TOUCH_SUPPORT_AVAILABLE
)
110 hardware
->set_internal_display_supports_touch(true);
111 else if (has_touch
== gfx::Display::TOUCH_SUPPORT_UNAVAILABLE
)
112 hardware
->set_internal_display_supports_touch(false);
113 WriteExternalTouchscreensProto(hardware
);
116 void MetricsLogChromeOS::WriteRealtimeStabilityAttributes(PrefService
* pref
) {
117 SystemProfileProto::Stability
* stability
=
118 uma_proto_
->mutable_system_profile()->mutable_stability();
120 int count
= pref
->GetInteger(prefs::kStabilityOtherUserCrashCount
);
122 stability
->set_other_user_crash_count(count
);
123 pref
->SetInteger(prefs::kStabilityOtherUserCrashCount
, 0);
126 count
= pref
->GetInteger(prefs::kStabilityKernelCrashCount
);
128 stability
->set_kernel_crash_count(count
);
129 pref
->SetInteger(prefs::kStabilityKernelCrashCount
, 0);
132 count
= pref
->GetInteger(prefs::kStabilitySystemUncleanShutdownCount
);
134 stability
->set_unclean_system_shutdown_count(count
);
135 pref
->SetInteger(prefs::kStabilitySystemUncleanShutdownCount
, 0);
139 void MetricsLogChromeOS::WriteBluetoothProto() {
140 SystemProfileProto::Hardware
* hardware
=
141 uma_proto_
->mutable_system_profile()->mutable_hardware();
143 // BluetoothAdapterFactory::GetAdapter is synchronous on Chrome OS; if that
144 // changes this will fail at the DCHECK().
145 device::BluetoothAdapterFactory::GetAdapter(
146 base::Bind(&MetricsLogChromeOS::SetBluetoothAdapter
,
147 base::Unretained(this)));
148 DCHECK(adapter_
.get());
150 SystemProfileProto::Hardware::Bluetooth
* bluetooth
=
151 hardware
->mutable_bluetooth();
153 bluetooth
->set_is_present(adapter_
->IsPresent());
154 bluetooth
->set_is_enabled(adapter_
->IsPowered());
156 device::BluetoothAdapter::DeviceList devices
= adapter_
->GetDevices();
157 for (device::BluetoothAdapter::DeviceList::iterator iter
=
158 devices
.begin(); iter
!= devices
.end(); ++iter
) {
159 device::BluetoothDevice
* device
= *iter
;
160 // Don't collect information about LE devices yet.
161 if (!device
->IsPaired())
164 PairedDevice
* paired_device
= bluetooth
->add_paired_device();
165 paired_device
->set_bluetooth_class(device
->GetBluetoothClass());
166 paired_device
->set_type(AsBluetoothDeviceType(device
->GetDeviceType()));
168 // |address| is xx:xx:xx:xx:xx:xx, extract the first three components and
169 // pack into a uint32.
170 std::string address
= device
->GetAddress();
171 if (address
.size() > 9 &&
172 address
[2] == ':' && address
[5] == ':' && address
[8] == ':') {
173 std::string vendor_prefix_str
;
174 uint64 vendor_prefix
;
176 base::RemoveChars(address
.substr(0, 9), ":", &vendor_prefix_str
);
177 DCHECK_EQ(6U, vendor_prefix_str
.size());
178 base::HexStringToUInt64(vendor_prefix_str
, &vendor_prefix
);
180 paired_device
->set_vendor_prefix(vendor_prefix
);
183 switch (device
->GetVendorIDSource()) {
184 case device::BluetoothDevice::VENDOR_ID_BLUETOOTH
:
185 paired_device
->set_vendor_id_source(PairedDevice::VENDOR_ID_BLUETOOTH
);
187 case device::BluetoothDevice::VENDOR_ID_USB
:
188 paired_device
->set_vendor_id_source(PairedDevice::VENDOR_ID_USB
);
191 paired_device
->set_vendor_id_source(PairedDevice::VENDOR_ID_UNKNOWN
);
194 paired_device
->set_vendor_id(device
->GetVendorID());
195 paired_device
->set_product_id(device
->GetProductID());
196 paired_device
->set_device_id(device
->GetDeviceID());
200 void MetricsLogChromeOS::UpdateMultiProfileUserCount() {
201 metrics::SystemProfileProto
* system_profile
=
202 uma_proto_
->mutable_system_profile();
204 if (chromeos::UserManager::IsInitialized() &&
205 chromeos::UserManager::Get()->IsMultipleProfilesAllowed()) {
206 size_t user_count
= chromeos::UserManager::Get()->GetLoggedInUsers().size();
208 // We invalidate the user count if it changed while the log was open.
209 if (system_profile
->has_multi_profile_user_count() &&
210 user_count
!= system_profile
->multi_profile_user_count()) {
214 system_profile
->set_multi_profile_user_count(user_count
);
218 void MetricsLogChromeOS::SetBluetoothAdapter(
219 scoped_refptr
<device::BluetoothAdapter
> adapter
) {