File: bluetooth_pairing_dialog.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; 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,806; 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 (173 lines) | stat: -rw-r--r-- 6,150 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
// Copyright 2013 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/ash/bluetooth/bluetooth_pairing_dialog.h"

#include <memory>
#include <string_view>

#include "ash/public/cpp/bluetooth_config_service.h"
#include "ash/webui/common/trusted_types_util.h"
#include "base/check.h"
#include "base/json/json_writer.h"
#include "base/memory/ptr_util.h"
#include "base/values.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/webui/ash/bluetooth/bluetooth_shared_load_time_data_provider.h"
#include "chrome/common/url_constants.h"
#include "chrome/common/webui_url_constants.h"
#include "chrome/grit/bluetooth_pairing_dialog_resources.h"
#include "chrome/grit/bluetooth_pairing_dialog_resources_map.h"
#include "chrome/grit/generated_resources.h"
#include "chromeos/ash/components/network/network_event_log.h"
#include "components/session_manager/core/session_manager.h"
#include "components/strings/grit/components_strings.h"
#include "content/public/browser/web_ui.h"
#include "content/public/browser/web_ui_data_source.h"
#include "device/bluetooth/bluetooth_device.h"
#include "device/bluetooth/chromeos/bluetooth_utils.h"
#include "device/bluetooth/public/cpp/bluetooth_address.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/webui/color_change_listener/color_change_handler.h"
#include "ui/webui/webui_util.h"
#include "ui/wm/core/shadow_types.h"

namespace ash {

namespace {

constexpr int kBluetoothPairingDialogHeight = 430;

void AddBluetoothStrings(content::WebUIDataSource* html_source) {
  struct {
    const char* name;
    int id;
  } localized_strings[] = {
      {"ok", IDS_OK},
      {"cancel", IDS_CANCEL},
      {"close", IDS_CLOSE},
  };
  for (const auto& entry : localized_strings) {
    html_source->AddLocalizedString(entry.name, entry.id);
  }
  bluetooth::AddLoadTimeData(html_source);
}

}  // namespace

// static
SystemWebDialogDelegate* BluetoothPairingDialog::ShowDialog(
    std::optional<std::string_view> device_address) {
  NET_LOG(EVENT) << "Attempting to display bluetooth pairing dialog";
  std::string dialog_id = chrome::kChromeUIBluetoothPairingURL;
  std::optional<std::string> canonical_device_address;

  if (device_address.has_value()) {
    canonical_device_address =
        device::CanonicalizeBluetoothAddress(device_address.value());

    if (canonical_device_address->empty()) {
      LOG(ERROR) << "BluetoothPairingDialog: Invalid address: "
                 << device_address.value();
      return nullptr;
    }
    dialog_id = canonical_device_address.value();
  }

  SystemWebDialogDelegate* existing_dialog =
      SystemWebDialogDelegate::FindInstance(dialog_id);

  if (existing_dialog) {
    LOG(ERROR) << "Refocusing on the existing dialog";
    existing_dialog->Focus();
    return existing_dialog;
  }

  // This object manages its own lifetime so we don't bother wrapping in a
  // std::unique_ptr to release quickly after.
  BluetoothPairingDialog* dialog =
      new BluetoothPairingDialog(dialog_id, canonical_device_address);
  dialog->ShowSystemDialog();
  return dialog;
}

BluetoothPairingDialog::BluetoothPairingDialog(
    const std::string& dialog_id,
    std::optional<std::string_view> canonical_device_address)
    : SystemWebDialogDelegate(GURL(chrome::kChromeUIBluetoothPairingURL),
                              /*title=*/std::u16string()),
      dialog_id_(dialog_id) {
  set_dialog_size(gfx::Size(SystemWebDialogDelegate::kDialogWidth,
                            kBluetoothPairingDialogHeight));

  base::Value::Dict device_data;
  if (canonical_device_address.has_value()) {
    device_data.Set("address", canonical_device_address.value());
  }
  device_data.Set("shouldOmitLinks",
                  session_manager::SessionManager::Get()->session_state() !=
                      session_manager::SessionState::ACTIVE);

  std::optional<std::string> args = base::WriteJson(device_data);
  CHECK(args.has_value());
  set_dialog_args(*args);

  set_web_view_element_id(kBluetoothPairingDialogElementId);
}

BluetoothPairingDialog::~BluetoothPairingDialog() = default;

std::string BluetoothPairingDialog::Id() {
  return dialog_id_;
}

void BluetoothPairingDialog::AdjustWidgetInitParams(
    views::Widget::InitParams* params) {
  params->type = views::Widget::InitParams::Type::TYPE_WINDOW_FRAMELESS;
  params->shadow_type = views::Widget::InitParams::ShadowType::kDrop;
  params->shadow_elevation = wm::kShadowElevationActiveWindow;
}

DEFINE_CLASS_ELEMENT_IDENTIFIER_VALUE(BluetoothPairingDialog,
                                      kBluetoothPairingDialogElementId);

// BluetoothPairingUI

BluetoothPairingDialogUI::BluetoothPairingDialogUI(content::WebUI* web_ui)
    : ui::MojoWebDialogUI(web_ui) {
  content::WebUIDataSource* source = content::WebUIDataSource::CreateAndAdd(
      Profile::FromWebUI(web_ui), chrome::kChromeUIBluetoothPairingHost);

  AddBluetoothStrings(source);
  source->AddLocalizedString("title", IDS_BLUETOOTH_PAIRING_PAIR_NEW_DEVICES);

  webui::SetupWebUIDataSource(
      source, kBluetoothPairingDialogResources,
      IDR_BLUETOOTH_PAIRING_DIALOG_BLUETOOTH_PAIRING_DIALOG_CONTAINER_HTML);
  // Enabling trusted types via trusted_types_util must be done after
  // webui::SetupWebUIDataSource to override the trusted type CSP with correct
  // policies for JS WebUIs.
  ash::EnableTrustedTypesCSP(source);

  device::RecordUiSurfaceDisplayed(
      device::BluetoothUiSurface::kStandalonePairingDialog);
}

BluetoothPairingDialogUI::~BluetoothPairingDialogUI() = default;

void BluetoothPairingDialogUI::BindInterface(
    mojo::PendingReceiver<bluetooth_config::mojom::CrosBluetoothConfig>
        receiver) {
  GetBluetoothConfigService(std::move(receiver));
}

void BluetoothPairingDialogUI::BindInterface(
    mojo::PendingReceiver<color_change_listener::mojom::PageHandler> receiver) {
  color_change_handler_ = std::make_unique<ui::ColorChangeHandler>(
      web_ui()->GetWebContents(), std::move(receiver));
}

WEB_UI_CONTROLLER_TYPE_IMPL(BluetoothPairingDialogUI)

}  // namespace ash