File: bluetooth_detailed_view_controller.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (329 lines) | stat: -rw-r--r-- 14,236 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ash/system/bluetooth/bluetooth_detailed_view_controller.h"

#include <optional>

#include "ash/constants/ash_switches.h"
#include "ash/public/cpp/bluetooth_config_service.h"
#include "ash/public/cpp/hats_bluetooth_revamp_trigger.h"
#include "ash/public/cpp/system_tray_client.h"
#include "ash/shell.h"
#include "ash/strings/grit/ash_strings.h"
#include "ash/system/bluetooth/hid_preserving_controller/hid_preserving_bluetooth_state_service.h"
#include "ash/system/model/system_tray_model.h"
#include "ash/system/unified/unified_system_tray_controller.h"
#include "base/check.h"
#include "base/command_line.h"
#include "base/memory/ptr_util.h"
#include "build/chromeos_buildflags.h"
#include "chromeos/ash/components/network/network_event_log.h"
#include "chromeos/ash/services/bluetooth_config/public/cpp/cros_bluetooth_config_util.h"
#include "chromeos/constants/chromeos_features.h"
#include "mojo/public/cpp/bindings/clone_traits.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/views/view.h"

namespace ash {

using bluetooth_config::IsBluetoothEnabledOrEnabling;
using bluetooth_config::mojom::AudioOutputCapability;
using bluetooth_config::mojom::BatteryProperties;
using bluetooth_config::mojom::BluetoothDeviceProperties;
using bluetooth_config::mojom::BluetoothDevicePropertiesPtr;
using bluetooth_config::mojom::BluetoothSystemState;
using bluetooth_config::mojom::DeviceBatteryInfo;
using bluetooth_config::mojom::DeviceBatteryInfoPtr;
using bluetooth_config::mojom::DeviceConnectionState;
using bluetooth_config::mojom::DeviceType;
using bluetooth_config::mojom::PairedBluetoothDeviceProperties;
using bluetooth_config::mojom::PairedBluetoothDevicePropertiesPtr;

BluetoothDetailedViewController::BluetoothDetailedViewController(
    UnifiedSystemTrayController* tray_controller)
    : detailed_view_delegate_(
          std::make_unique<DetailedViewDelegate>(tray_controller)),
      tray_controller_(tray_controller) {
  GetBluetoothConfigService(
      remote_cros_bluetooth_config_.BindNewPipeAndPassReceiver());
  remote_cros_bluetooth_config_->ObserveSystemProperties(
      cros_system_properties_observer_receiver_.BindNewPipeAndPassRemote());

  GetHidPreservingBluetoothStateControllerService(
      remote_hid_preserving_bluetooth_.BindNewPipeAndPassReceiver());
}

BluetoothDetailedViewController::~BluetoothDetailedViewController() = default;

std::unique_ptr<views::View> BluetoothDetailedViewController::CreateView() {
  DCHECK(!view_);
  std::unique_ptr<BluetoothDetailedView> bluetooth_detailed_view =
      BluetoothDetailedView::Factory::Create(detailed_view_delegate_.get(),
                                             /*delegate=*/this);
  view_ = bluetooth_detailed_view.get();
  device_list_controller_ = BluetoothDeviceListController::Factory::Create(
      bluetooth_detailed_view.get());
  BluetoothEnabledStateChanged();

  if (IsBluetoothEnabledOrEnabling(system_state_)) {
    device_list_controller_->UpdateDeviceList(connected_devices_,
                                              previously_connected_devices_);
  }

  // `bluetooth_detailed_view` is not a views::View, so we must GetAsView().
  return base::WrapUnique(bluetooth_detailed_view.release()->GetAsView());
}

std::u16string BluetoothDetailedViewController::GetAccessibleName() const {
  return l10n_util::GetStringUTF16(
      IDS_ASH_QUICK_SETTINGS_BUBBLE_BLUETOOTH_SETTINGS_ACCESSIBLE_DESCRIPTION);
}

void BluetoothDetailedViewController::OnPropertiesUpdated(
    bluetooth_config::mojom::BluetoothSystemPropertiesPtr properties) {
  // The tray controller should only be transitioning to the main view when this
  // feature is disabled since the detailed tray view and the Bluetooth Pod in
  // QS would be hidden. However, when the feature is enabled, the Bluetooth Pod
  // is visible and the user should be able to see the detailed tray view.
  if (!chromeos::features::IsBluetoothWifiQSPodRefreshEnabled() &&
      properties->system_state == BluetoothSystemState::kUnavailable) {
    tray_controller_->TransitionToMainView(
        /*restore_focus=*/true);  // Deletes |this|.
    return;
  }

  const bool has_bluetooth_enabled_state_changed =
      system_state_ != properties->system_state;
  system_state_ = properties->system_state;

  if (has_bluetooth_enabled_state_changed)
    BluetoothEnabledStateChanged();

  connected_devices_.clear();
  previously_connected_devices_.clear();

  if (base::CommandLine::ForCurrentProcess()->HasSwitch(
          switches::kQsAddFakeBluetoothDevices)) {
    AddFakeBluetoothDevices();
  }

  for (auto& paired_device : properties->paired_devices) {
    if (paired_device->device_properties->connection_state ==
        DeviceConnectionState::kConnected) {
      connected_devices_.push_back(std::move(paired_device));
    } else {
      previously_connected_devices_.push_back(std::move(paired_device));
    }
  }
  if (device_list_controller_ && IsBluetoothEnabledOrEnabling(system_state_)) {
    device_list_controller_->UpdateDeviceList(connected_devices_,
                                              previously_connected_devices_);
  }
}

void BluetoothDetailedViewController::OnToggleClicked(bool new_state) {
  remote_hid_preserving_bluetooth_->TryToSetBluetoothEnabledState(
      new_state, mojom::HidWarningDialogSource::kQuickSettings);

  if (auto* hats_bluetooth_revamp_trigger = HatsBluetoothRevampTrigger::Get()) {
    hats_bluetooth_revamp_trigger->TryToShowSurvey();
  }
}

void BluetoothDetailedViewController::OnPairNewDeviceRequested() {
  tray_controller_->CloseBubble();  // Deletes |this|.
  NET_LOG(EVENT) << "Attempting to show the bluetooth pairing dialog";
  Shell::Get()->system_tray_model()->client()->ShowBluetoothPairingDialog(
      /*device_address=*/std::nullopt);

  if (auto* hats_bluetooth_revamp_trigger = HatsBluetoothRevampTrigger::Get()) {
    hats_bluetooth_revamp_trigger->TryToShowSurvey();
  }
}

void BluetoothDetailedViewController::OnDeviceListItemSelected(
    const bluetooth_config::mojom::PairedBluetoothDevicePropertiesPtr& device) {
  // When CloseBubble() is called |device| will be deleted so we need to make a
  // copy of the device ID that was selected.
  const std::string device_id = device->device_properties->id;

  // Non-HID devices can be explicitly connected to, so we detect when this is
  // the case and attempt to connect to the device instead of navigating to the
  // Bluetooth Settings.
  if (device->device_properties->audio_capability ==
          AudioOutputCapability::kCapableOfAudioOutput &&
      device->device_properties->connection_state ==
          DeviceConnectionState::kNotConnected) {
    remote_cros_bluetooth_config_->Connect(device_id,
                                           /*callback=*/base::DoNothing());
    return;
  }
  tray_controller_->CloseBubble();  // Deletes |this|.
  Shell::Get()->system_tray_model()->client()->ShowBluetoothSettings(device_id);
}

void BluetoothDetailedViewController::BluetoothEnabledStateChanged() {
  if (view_)
    view_->UpdateBluetoothEnabledState(system_state_);
  if (device_list_controller_) {
    device_list_controller_->UpdateBluetoothEnabledState(
        IsBluetoothEnabledOrEnabling(system_state_));
  }
}

void BluetoothDetailedViewController::AddFakeBluetoothDevices() {
#if !BUILDFLAG(IS_CHROMEOS_DEVICE)
  // Only add fake devices in the linux-chromeos emulator. We don't want to
  // increase the binary size for real devices.
  {
    PairedBluetoothDevicePropertiesPtr paired_device_properties =
        PairedBluetoothDeviceProperties::New();
    paired_device_properties->device_properties =
        BluetoothDeviceProperties::New();
    paired_device_properties->device_properties->id = "Unknown";
    paired_device_properties->device_properties->public_name = u"Unknown";
    paired_device_properties->device_properties->connection_state =
        DeviceConnectionState::kConnected;
    paired_device_properties->device_properties->device_type =
        DeviceType::kUnknown;

    connected_devices_.push_back(mojo::Clone(paired_device_properties));
  }
  {
    PairedBluetoothDevicePropertiesPtr paired_device_properties =
        PairedBluetoothDeviceProperties::New();
    paired_device_properties->device_properties =
        BluetoothDeviceProperties::New();
    paired_device_properties->device_properties->id = "Computer";
    paired_device_properties->device_properties->public_name = u"Computer";
    paired_device_properties->device_properties->device_type =
        DeviceType::kComputer;
    paired_device_properties->device_properties->connection_state =
        DeviceConnectionState::kConnected;
    paired_device_properties->device_properties->battery_info =
        DeviceBatteryInfo::New();
    paired_device_properties->device_properties->battery_info
        ->default_properties = BatteryProperties::New();
    paired_device_properties->device_properties->battery_info
        ->default_properties->battery_percentage = 75;

    connected_devices_.push_back(mojo::Clone(paired_device_properties));
  }
  {
    PairedBluetoothDevicePropertiesPtr paired_device_properties =
        PairedBluetoothDeviceProperties::New();
    paired_device_properties->device_properties =
        BluetoothDeviceProperties::New();
    paired_device_properties->device_properties->id = "Phone";
    paired_device_properties->device_properties->public_name = u"Phone";
    paired_device_properties->device_properties->device_type =
        DeviceType::kPhone;
    paired_device_properties->device_properties->connection_state =
        DeviceConnectionState::kConnected;
    paired_device_properties->device_properties->battery_info =
        DeviceBatteryInfo::New();
    paired_device_properties->device_properties->battery_info->left_bud_info =
        BatteryProperties::New();
    paired_device_properties->device_properties->battery_info->left_bud_info
        ->battery_percentage = 5;
    paired_device_properties->device_properties->battery_info->case_info =
        BatteryProperties::New();
    paired_device_properties->device_properties->battery_info->case_info
        ->battery_percentage = 64;
    paired_device_properties->device_properties->battery_info->right_bud_info =
        BatteryProperties::New();
    paired_device_properties->device_properties->battery_info->right_bud_info
        ->battery_percentage = 9;

    connected_devices_.push_back(mojo::Clone(paired_device_properties));
  }
  {
    PairedBluetoothDevicePropertiesPtr paired_device_properties =
        PairedBluetoothDeviceProperties::New();
    paired_device_properties->device_properties =
        BluetoothDeviceProperties::New();
    paired_device_properties->device_properties->id = "Game Controller";
    paired_device_properties->device_properties->public_name =
        u"Game Controller";
    paired_device_properties->device_properties->device_type =
        DeviceType::kGameController;

    connected_devices_.push_back(mojo::Clone(paired_device_properties));
  }
  {
    PairedBluetoothDevicePropertiesPtr paired_device_properties =
        PairedBluetoothDeviceProperties::New();
    paired_device_properties->device_properties =
        BluetoothDeviceProperties::New();
    paired_device_properties->device_properties->id = "Keyboard";
    paired_device_properties->device_properties->public_name = u"Keyboard";
    paired_device_properties->device_properties->device_type =
        DeviceType::kKeyboard;

    connected_devices_.push_back(mojo::Clone(paired_device_properties));
  }
  {
    PairedBluetoothDevicePropertiesPtr paired_device_properties =
        PairedBluetoothDeviceProperties::New();
    paired_device_properties->device_properties =
        BluetoothDeviceProperties::New();
    paired_device_properties->device_properties->id = "Mouse";
    paired_device_properties->device_properties->public_name = u"Mouse";
    paired_device_properties->device_properties->device_type =
        DeviceType::kMouse;

    connected_devices_.push_back(mojo::Clone(paired_device_properties));
  }
  {
    PairedBluetoothDevicePropertiesPtr paired_device_properties =
        PairedBluetoothDeviceProperties::New();
    paired_device_properties->device_properties =
        BluetoothDeviceProperties::New();
    paired_device_properties->device_properties->id = "Tablet";
    paired_device_properties->device_properties->public_name = u"Tablet";
    paired_device_properties->device_properties->device_type =
        DeviceType::kTablet;

    connected_devices_.push_back(mojo::Clone(paired_device_properties));
  }
  {
    PairedBluetoothDevicePropertiesPtr paired_device_properties =
        PairedBluetoothDeviceProperties::New();
    paired_device_properties->device_properties =
        BluetoothDeviceProperties::New();
    paired_device_properties->device_properties->id = "Headset";
    paired_device_properties->device_properties->public_name = u"Headset";
    paired_device_properties->device_properties->device_type =
        DeviceType::kHeadset;
    paired_device_properties->device_properties->connection_state =
        DeviceConnectionState::kConnecting;

    previously_connected_devices_.push_back(
        mojo::Clone(paired_device_properties));
  }
  {
    PairedBluetoothDevicePropertiesPtr paired_device_properties =
        PairedBluetoothDeviceProperties::New();
    paired_device_properties->device_properties =
        BluetoothDeviceProperties::New();
    paired_device_properties->device_properties->id = "Video Camera";
    paired_device_properties->device_properties->public_name =
        u"Video Camera with a very very very very very very very long name";
    paired_device_properties->device_properties->device_type =
        DeviceType::kVideoCamera;
    paired_device_properties->device_properties->connection_state =
        DeviceConnectionState::kNotConnected;

    previously_connected_devices_.push_back(
        mojo::Clone(paired_device_properties));
  }
#endif  // !BUILDFLAG(IS_CHROMEOS_DEVICE)
}

void BluetoothDetailedViewController::ShutDown() {
  device_list_controller_.reset();
}

}  // namespace ash