File: bluetooth_internals_handler.cc

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (326 lines) | stat: -rw-r--r-- 11,533 bytes parent folder | download | duplicates (5)
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
// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/ui/webui/bluetooth_internals/bluetooth_internals_handler.h"

#include <fcntl.h>

#include <optional>
#include <string>

#include "base/feature_list.h"
#include "base/files/file_util.h"
#include "base/functional/bind.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "components/user_manager/user_manager.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/web_contents.h"
#include "device/bluetooth/adapter.h"
#include "device/bluetooth/bluetooth_adapter_factory.h"
#include "mojo/public/cpp/bindings/pending_remote.h"
#include "mojo/public/cpp/bindings/self_owned_receiver.h"
#include "url/gurl.h"

#if BUILDFLAG(IS_CHROMEOS)
#include "ash/public/cpp/bluetooth_config_service.h"
#include "chrome/browser/ash/bluetooth/debug_logs_manager.h"
#include "chromeos/ash/components/dbus/debug_daemon/debug_daemon_client.h"
#include "device/bluetooth/chromeos_platform_features.h"
#endif  // BUILDFLAG(IS_CHROMEOS)

#if BUILDFLAG(IS_ANDROID)
#include "components/permissions/android/android_permission_util.h"
#endif  // BUILDFLAG(IS_ANDROID)

namespace {
using content::RenderFrameHost;
using content::WebContents;

#if BUILDFLAG(IS_CHROMEOS)
using ash::bluetooth_config::mojom::BluetoothSystemState;

constexpr char kBtsnoopFileName[] = "capture.btsnoop";
// TODO(b/347880605): find a better way to query the actual downloads path
constexpr char kDownloadsPath[] = "/home/chronos/user/Downloads/";
constexpr char kDownloadsPathNew[] = "/home/chronos/user/MyFiles/Downloads/";

class BluetoothBtsnoop : public mojom::BluetoothBtsnoop {
 public:
  explicit BluetoothBtsnoop(
      mojo::PendingReceiver<mojom::BluetoothBtsnoop> receiver,
      base::OnceCallback<void(StopCallback callback)> on_stop_callback)
      : receiver_(this, std::move(receiver)),
        on_stop_callback_(std::move(on_stop_callback)) {}

  BluetoothBtsnoop(const BluetoothBtsnoop&) = delete;
  BluetoothBtsnoop& operator=(const BluetoothBtsnoop&) = delete;
  ~BluetoothBtsnoop() override = default;

  void Stop(StopCallback callback) override;

 private:
  mojo::Receiver<mojom::BluetoothBtsnoop> receiver_;
  base::OnceCallback<void(StopCallback callback)> on_stop_callback_;
};
#endif  // BUILDFLAG(IS_CHROMEOS)
}  // namespace

BluetoothInternalsHandler::BluetoothInternalsHandler(
    content::RenderFrameHost* render_frame_host,
    mojo::PendingReceiver<mojom::BluetoothInternalsHandler> receiver)
    : render_frame_host_(*render_frame_host),
      receiver_(this, std::move(receiver)) {
#if BUILDFLAG(IS_CHROMEOS)
  ash::GetBluetoothConfigService(
      remote_cros_bluetooth_config_.BindNewPipeAndPassReceiver());
  remote_cros_bluetooth_config_->ObserveSystemProperties(
      cros_system_properties_observer_receiver_.BindNewPipeAndPassRemote());
#endif  // BUILDFLAG(IS_CHROMEOS)
}

BluetoothInternalsHandler::~BluetoothInternalsHandler() {
#if BUILDFLAG(IS_CHROMEOS)
  if (!turning_bluetooth_off_ && !turning_bluetooth_on_) {
    return;
  }

  remote_cros_bluetooth_config_->SetBluetoothEnabledState(
      bluetooth_initial_state_);
#endif  // BUILDFLAG(IS_CHROMEOS)
}

void BluetoothInternalsHandler::GetAdapter(GetAdapterCallback callback) {
  if (device::BluetoothAdapterFactory::IsBluetoothSupported()) {
    device::BluetoothAdapterFactory::Get()->GetAdapter(
        base::BindOnce(&BluetoothInternalsHandler::OnGetAdapter,
                       weak_ptr_factory_.GetWeakPtr(), std::move(callback)));
  } else {
    std::move(callback).Run(mojo::NullRemote() /* adapter */);
  }
}

void BluetoothInternalsHandler::GetDebugLogsChangeHandler(
    GetDebugLogsChangeHandlerCallback callback) {
  mojo::PendingRemote<mojom::DebugLogsChangeHandler> handler_remote;
  bool initial_toggle_value = false;

#if BUILDFLAG(IS_CHROMEOS)
  using ash::bluetooth::DebugLogsManager;

  // If no logs manager exists for this user, debug logs are not supported.
  DebugLogsManager::DebugLogsState state =
      debug_logs_manager_ ? debug_logs_manager_->GetDebugLogsState()
                          : DebugLogsManager::DebugLogsState::kNotSupported;

  switch (state) {
    case DebugLogsManager::DebugLogsState::kNotSupported:
      // Leave |handler_remote| NullRemote and |initial_toggle_value| false.
      break;
    case DebugLogsManager::DebugLogsState::kSupportedAndEnabled:
      initial_toggle_value = true;
      [[fallthrough]];
    case DebugLogsManager::DebugLogsState::kSupportedButDisabled:
      handler_remote = debug_logs_manager_->GenerateRemote();
      break;
  }
#endif

  std::move(callback).Run(std::move(handler_remote), initial_toggle_value);
}

void BluetoothInternalsHandler::OnGetAdapter(
    GetAdapterCallback callback,
    scoped_refptr<device::BluetoothAdapter> adapter) {
  mojo::PendingRemote<bluetooth::mojom::Adapter> pending_adapter;
  mojo::MakeSelfOwnedReceiver(std::make_unique<bluetooth::Adapter>(adapter),
                              pending_adapter.InitWithNewPipeAndPassReceiver());
  std::move(callback).Run(std::move(pending_adapter));
}

void BluetoothInternalsHandler::CheckSystemPermissions(
    CheckSystemPermissionsCallback callback) {
  bool need_location_permission = false;
  bool need_nearby_devices_permission = false;
  bool need_location_services = false;
  bool can_request_system_permissions = false;

#if BUILDFLAG(IS_ANDROID)
  WebContents* web_contents =
      content::WebContents::FromRenderFrameHost(&render_frame_host_.get());
  need_location_permission =
      permissions::NeedsLocationPermissionForBluetooth(web_contents);
  need_nearby_devices_permission =
      permissions::NeedsNearbyDevicesPermissionForBluetooth(web_contents);
  need_location_services = permissions::NeedsLocationServicesForBluetooth();
  can_request_system_permissions =
      permissions::CanRequestSystemPermissionsForBluetooth(web_contents);
#endif  // BUILDFLAG(IS_ANDROID)

  std::move(callback).Run(
      need_location_permission, need_nearby_devices_permission,
      need_location_services, can_request_system_permissions);
}

void BluetoothInternalsHandler::RequestSystemPermissions(
    RequestSystemPermissionsCallback callback) {
#if BUILDFLAG(IS_ANDROID)
  WebContents* web_contents =
      content::WebContents::FromRenderFrameHost(&render_frame_host_.get());
  permissions::RequestSystemPermissionsForBluetooth(web_contents);
#endif  // BUILDFLAG(IS_ANDROID)
  std::move(callback).Run();
}

void BluetoothInternalsHandler::RequestLocationServices(
    RequestLocationServicesCallback callback) {
#if BUILDFLAG(IS_ANDROID)
  WebContents* web_contents =
      content::WebContents::FromRenderFrameHost(&render_frame_host_.get());
  permissions::RequestLocationServices(web_contents);
#endif  // BUILDFLAG(IS_ANDROID)
  std::move(callback).Run();
}

#if BUILDFLAG(IS_CHROMEOS)
void BluetoothInternalsHandler::RestartSystemBluetooth(
    RestartSystemBluetoothCallback callback) {
  if (bluetooth_system_state_ == BluetoothSystemState::kUnavailable) {
    std::move(callback).Run();
    return;
  }

  restart_system_bluetooth_callback_ = std::move(callback);
  bool enable = false;
  turning_bluetooth_off_ = true;

  if (bluetooth_system_state_ == BluetoothSystemState::kDisabled ||
      bluetooth_system_state_ == BluetoothSystemState::kDisabling) {
    enable = true;
    turning_bluetooth_on_ = true;
    turning_bluetooth_off_ = false;
  }

  bluetooth_initial_state_ = !enable;
  remote_cros_bluetooth_config_->SetBluetoothEnabledState(enable);
}

void BluetoothInternalsHandler::OnPropertiesUpdated(
    ash::bluetooth_config::mojom::BluetoothSystemPropertiesPtr properties) {
  bluetooth_system_state_ = properties->system_state;
  if (bluetooth_system_state_ == BluetoothSystemState::kUnavailable) {
    return;
  }

  if (!turning_bluetooth_off_ && !turning_bluetooth_on_) {
    return;
  }

  if (bluetooth_system_state_ == BluetoothSystemState::kDisabling ||
      bluetooth_system_state_ == BluetoothSystemState::kEnabling) {
    return;
  }

  CHECK(restart_system_bluetooth_callback_);
  if (bluetooth_system_state_ == BluetoothSystemState::kDisabled &&
      turning_bluetooth_off_) {
    turning_bluetooth_off_ = false;
    turning_bluetooth_on_ = true;
    remote_cros_bluetooth_config_->SetBluetoothEnabledState(true);
  }

  if (bluetooth_system_state_ == BluetoothSystemState::kEnabled &&
      turning_bluetooth_on_) {
    turning_bluetooth_on_ = false;
    turning_bluetooth_off_ = false;
    std::move(restart_system_bluetooth_callback_).Run();
  }
}
#endif  // BUILDFLAG(IS_CHROMEOS)

void BluetoothInternalsHandler::StartBtsnoop(StartBtsnoopCallback callback) {
#if BUILDFLAG(IS_CHROMEOS)
  ash::DebugDaemonClient::Get()->BluetoothStartBtsnoop(
      base::BindOnce(&BluetoothInternalsHandler::OnStartBtsnoopResp,
                     weak_ptr_factory_.GetWeakPtr(), std::move(callback)));
#else
  std::move(callback).Run(mojo::NullRemote());
#endif
}

void BluetoothInternalsHandler::IsBtsnoopFeatureEnabled(
    IsBtsnoopFeatureEnabledCallback callback) {
#if BUILDFLAG(IS_CHROMEOS)
  bool enabled = base::FeatureList::IsEnabled(
      chromeos::bluetooth::features::kBluetoothBtsnoopInternals);
  std::move(callback).Run(enabled);
#else
  std::move(callback).Run(false);
#endif
}

#if BUILDFLAG(IS_CHROMEOS)
void BluetoothInternalsHandler::OnStartBtsnoopResp(
    StartBtsnoopCallback callback,
    bool success) {
  if (!success) {
    std::move(callback).Run(mojo::NullRemote());
    return;
  }

  mojo::PendingRemote<mojom::BluetoothBtsnoop> remote_btsnoop;
  btsnoop_ = std::make_unique<BluetoothBtsnoop>(
      remote_btsnoop.InitWithNewPipeAndPassReceiver(),
      base::BindOnce(&BluetoothInternalsHandler::StopBtsnoop,
                     base::Unretained(this)));
  std::move(callback).Run(std::move(remote_btsnoop));
}

void BluetoothInternalsHandler::StopBtsnoop(
    mojom::BluetoothBtsnoop::StopCallback callback) {
  std::optional<base::FilePath> dir_path = GetDownloadsPath();
  if (!dir_path.has_value()) {
    std::move(callback).Run(false);
    return;
  }

  // Open the file for writing, then pass the fd via DBus.
  base::FilePath file_path = dir_path.value().Append(kBtsnoopFileName);
  base::ScopedFD fd(HANDLE_EINTR(
      open(file_path.value().c_str(), O_WRONLY | O_CREAT | O_TRUNC, 0644)));
  if (!fd.is_valid()) {
    std::move(callback).Run(false);
    return;
  }

  ash::DebugDaemonClient::Get()->BluetoothStopBtsnoop(
      fd.get(),
      base::BindOnce(&BluetoothInternalsHandler::OnStopBtsnoopResp,
                     weak_ptr_factory_.GetWeakPtr(), std::move(callback)));
}

void BluetoothInternalsHandler::OnStopBtsnoopResp(
    mojom::BluetoothBtsnoop::StopCallback callback,
    bool success) {
  std::move(callback).Run(success);
  btsnoop_ = nullptr;
}

std::optional<base::FilePath> BluetoothInternalsHandler::GetDownloadsPath() {
  // Check for both downloads paths. There ought to be a better way to do this,
  // but we can iterate later.
  base::FilePath dir_path = base::FilePath(kDownloadsPathNew);
  if (!base::PathExists(dir_path)) {
    dir_path = base::FilePath(kDownloadsPath);
    if (!base::PathExists(dir_path)) {
      return std::nullopt;
    }
  }
  return std::optional<base::FilePath>(dir_path);
}

void BluetoothBtsnoop::Stop(StopCallback callback) {
  std::move(on_stop_callback_).Run(std::move(callback));
}
#endif  // BUILDFLAG(IS_CHROMEOS)