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
|
// 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 "chrome/browser/ui/webui/nearby_internals/quick_pair/quick_pair_handler.h"
#include <memory>
#include <utility>
#include "ash/quick_pair/repository/fast_pair/fast_pair_image_decoder_impl.h"
#include "ash/quick_pair/ui/fast_pair/fast_pair_notification_controller.h"
#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/i18n/time_formatting.h"
#include "base/strings/string_number_conversions.h"
#include "base/values.h"
#include "ui/message_center/message_center.h"
namespace {
// Keys in the JSON representation of a log message
const char kLogMessageTextKey[] = "text";
const char kLogMessageTimeKey[] = "time";
const char kLogMessageFileKey[] = "file";
const char kLogMessageLineKey[] = "line";
const char kLogMessageSeverityKey[] = "severity";
// Test device metadata for debug purposes
const char16_t kTestDeviceName[] = u"Pixel Buds";
const char16_t kTestAppName[] = u"JBLTools";
const char16_t kTestEmail[] = u"testemail@gmail.com";
const char kImageUrl[] =
"https://lh3.googleusercontent.com/"
"kGH7uF95EhgI0XBRJOGh3l7KvPWsNAFwaxPfksIJloqk-"
"mh8cZYG9RITPS65UOtUNry9dnyYYMn5dQtFzVdagSE";
// Converts |log_message| to a raw dictionary value used as a JSON argument to
// JavaScript functions.
base::Value::Dict LogMessageToDictionary(
const ash::quick_pair::LogBuffer::LogMessage& log_message) {
base::Value::Dict dictionary;
dictionary.Set(kLogMessageTextKey, log_message.text);
dictionary.Set(kLogMessageTimeKey,
base::TimeFormatTimeOfDayWithMilliseconds(log_message.time));
dictionary.Set(kLogMessageFileKey, log_message.file);
dictionary.Set(kLogMessageLineKey, log_message.line);
dictionary.Set(kLogMessageSeverityKey, log_message.severity);
return dictionary;
}
} // namespace
QuickPairHandler::QuickPairHandler()
: fast_pair_notification_controller_(
std::make_unique<ash::quick_pair::FastPairNotificationController>(
message_center::MessageCenter::Get())),
image_decoder_(
std::make_unique<ash::quick_pair::FastPairImageDecoderImpl>()) {}
QuickPairHandler::~QuickPairHandler() = default;
void QuickPairHandler::RegisterMessages() {
web_ui()->RegisterMessageCallback(
"getQuickPairLogMessages",
base::BindRepeating(&QuickPairHandler::HandleGetLogMessages,
base::Unretained(this)));
web_ui()->RegisterMessageCallback(
"notifyFastPairError",
base::BindRepeating(&QuickPairHandler::NotifyFastPairError,
base::Unretained(this)));
web_ui()->RegisterMessageCallback(
"notifyFastPairDiscovery",
base::BindRepeating(&QuickPairHandler::NotifyFastPairDiscovery,
base::Unretained(this)));
web_ui()->RegisterMessageCallback(
"notifyFastPairPairing",
base::BindRepeating(&QuickPairHandler::NotifyFastPairPairing,
base::Unretained(this)));
web_ui()->RegisterMessageCallback(
"notifyFastPairApplicationAvailable",
base::BindRepeating(&QuickPairHandler::NotifyFastPairApplicationAvailable,
base::Unretained(this)));
web_ui()->RegisterMessageCallback(
"notifyFastPairApplicationInstalled",
base::BindRepeating(&QuickPairHandler::NotifyFastPairApplicationInstalled,
base::Unretained(this)));
web_ui()->RegisterMessageCallback(
"notifyFastPairAssociateAccount",
base::BindRepeating(&QuickPairHandler::NotifyFastPairAssociateAccountKey,
base::Unretained(this)));
}
void QuickPairHandler::OnJavascriptAllowed() {
observation_.Observe(ash::quick_pair::LogBuffer::GetInstance());
}
void QuickPairHandler::OnJavascriptDisallowed() {
observation_.Reset();
}
void QuickPairHandler::HandleGetLogMessages(const base::Value::List& args) {
AllowJavascript();
const base::Value& callback_id = args[0];
base::Value::List list;
for (const auto& log : *ash::quick_pair::LogBuffer::GetInstance()->logs()) {
list.Append(LogMessageToDictionary(log));
}
ResolveJavascriptCallback(callback_id, list);
}
void QuickPairHandler::OnLogBufferCleared() {
FireWebUIListener("quick-pair-log-buffer-cleared");
}
void QuickPairHandler::OnLogMessageAdded(
const ash::quick_pair::LogBuffer::LogMessage& log_message) {
FireWebUIListener("quick-pair-log-message-added",
LogMessageToDictionary(log_message));
}
void QuickPairHandler::NotifyFastPairError(const base::Value::List& args) {
image_decoder_->DecodeImageFromUrl(
GURL(kImageUrl),
/*resize_to_notification_size=*/true,
base::BindOnce(&QuickPairHandler::OnImageDecodedFastPairError,
weak_ptr_factory_.GetWeakPtr()));
}
void QuickPairHandler::OnImageDecodedFastPairError(gfx::Image image) {
fast_pair_notification_controller_->ShowErrorNotification(
kTestDeviceName, image, base::DoNothing(), base::DoNothing());
}
void QuickPairHandler::NotifyFastPairDiscovery(const base::Value::List& args) {
image_decoder_->DecodeImageFromUrl(
GURL(kImageUrl),
/*resize_to_notification_size=*/true,
base::BindOnce(&QuickPairHandler::OnImageDecodedFastPairDiscovery,
weak_ptr_factory_.GetWeakPtr()));
}
void QuickPairHandler::OnImageDecodedFastPairDiscovery(gfx::Image image) {
fast_pair_notification_controller_->ShowGuestDiscoveryNotification(
kTestDeviceName, image, base::DoNothing(), base::DoNothing(),
base::DoNothing());
}
void QuickPairHandler::NotifyFastPairPairing(const base::Value::List& args) {
image_decoder_->DecodeImageFromUrl(
GURL(kImageUrl),
/*resize_to_notification_size=*/true,
base::BindOnce(&QuickPairHandler::OnImageDecodedFastPairPairing,
weak_ptr_factory_.GetWeakPtr()));
}
void QuickPairHandler::OnImageDecodedFastPairPairing(gfx::Image image) {
fast_pair_notification_controller_->ShowPairingNotification(
kTestDeviceName, image, base::DoNothing());
}
void QuickPairHandler::NotifyFastPairApplicationAvailable(
const base::Value::List& args) {
image_decoder_->DecodeImageFromUrl(
GURL(kImageUrl),
/*resize_to_notification_size=*/true,
base::BindOnce(
&QuickPairHandler::OnImageDecodedFastPairApplicationAvailable,
weak_ptr_factory_.GetWeakPtr()));
}
void QuickPairHandler::OnImageDecodedFastPairApplicationAvailable(
gfx::Image image) {
fast_pair_notification_controller_->ShowApplicationAvailableNotification(
kTestDeviceName, image, base::DoNothing(), base::DoNothing());
}
void QuickPairHandler::NotifyFastPairApplicationInstalled(
const base::Value::List& args) {
image_decoder_->DecodeImageFromUrl(
GURL(kImageUrl),
/*resize_to_notification_size=*/true,
base::BindOnce(
&QuickPairHandler::OnImageDecodedFastPairApplicationInstalled,
weak_ptr_factory_.GetWeakPtr()));
}
void QuickPairHandler::OnImageDecodedFastPairApplicationInstalled(
gfx::Image image) {
fast_pair_notification_controller_->ShowApplicationInstalledNotification(
kTestDeviceName, image, kTestAppName, base::DoNothing(),
base::DoNothing());
}
void QuickPairHandler::NotifyFastPairAssociateAccountKey(
const base::Value::List& args) {
image_decoder_->DecodeImageFromUrl(
GURL(kImageUrl),
/*resize_to_notification_size=*/true,
base::BindOnce(
&QuickPairHandler::OnImageDecodedFastPairAssociateAccountKey,
weak_ptr_factory_.GetWeakPtr()));
}
void QuickPairHandler::OnImageDecodedFastPairAssociateAccountKey(
gfx::Image image) {
fast_pair_notification_controller_->ShowAssociateAccount(
kTestDeviceName, kTestEmail, image, base::DoNothing(), base::DoNothing(),
base::DoNothing());
}
|