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
|
// Copyright 2023 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/nearby_internals_ui_presence_handler.h"
#include "base/containers/contains.h"
#include "chrome/browser/ash/nearby/nearby_process_manager_factory.h"
#include "chrome/browser/ash/nearby/presence/nearby_presence_service_factory.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/push_notification/push_notification_service_factory.h"
#include "chromeos/ash/components/nearby/presence/credentials/prefs.h"
#include "chromeos/ash/components/nearby/presence/nearby_presence_service.h"
#include "chromeos/ash/services/nearby/public/mojom/nearby_share_settings.mojom.h"
#include "components/cross_device/logging/logging.h"
#include "components/prefs/pref_service.h"
#include "components/push_notification/push_notification_service.h"
namespace {
// Keys in the JSON representation of a device.
const char kDeviceNameKey[] = "name";
const char kTypeKey[] = "type";
const char kEndpointKey[] = "endpoint_id";
const char kActionsKey[] = "actions";
// ActionType strings representations.
const char kCallTransferAction[] = "Call Transfer";
const char kActiveUnlockAction[] = "Active Unlock";
const char kNearbyShareAction[] = "Nearby Share";
const char kInstantTetheringAction[] = "Instant Tethering";
const char kPhoneHubAction[] = "Phone Hub";
const char kPresenceManagerAction[] = "Presence Manager";
const char kFinderAction[] = "Finder";
const char kFastPairSassAction[] = "Fast Pair Sass";
const char kTapToTransferAction[] = "Tap To Transfer";
const char kLastAction[] = "Invalid Action";
// `PushNotificationMessage` key value pairs
const char kNotificationTypeIdKey[] = "type_id";
const char kNotificationClientIdKey[] = "client_id";
const char kNotificationClientIdValue[] = "nearby";
std::string PresenceActionToString(nearby::presence::PresenceAction action) {
switch (nearby::presence::ActionBit(action.GetActionIdentifier())) {
case nearby::presence::ActionBit::kCallTransferAction:
return kCallTransferAction;
case nearby::presence::ActionBit::kActiveUnlockAction:
return kActiveUnlockAction;
case nearby::presence::ActionBit::kNearbyShareAction:
return kNearbyShareAction;
case nearby::presence::ActionBit::kInstantTetheringAction:
return kInstantTetheringAction;
case nearby::presence::ActionBit::kPhoneHubAction:
return kPhoneHubAction;
case nearby::presence::ActionBit::kPresenceManagerAction:
return kPresenceManagerAction;
case nearby::presence::ActionBit::kFinderAction:
return kFinderAction;
case nearby::presence::ActionBit::kFastPairSassAction:
return kFastPairSassAction;
case nearby::presence::ActionBit::kTapToTransferAction:
return kTapToTransferAction;
case nearby::presence::ActionBit::kLastAction:
return kLastAction;
}
}
// Converts |presence_device| to a raw dictionary value used as a JSON argument
// to JavaScript functions.
base::Value::Dict PresenceDeviceToDictionary(
nearby::presence::PresenceDevice presence_device) {
base::Value::Dict dictionary;
dictionary.Set(kDeviceNameKey,
presence_device.GetDeviceIdentityMetadata().device_name());
// TODO(b/277820435): add other device type options.
if (presence_device.GetDeviceIdentityMetadata().device_type() ==
nearby::internal::DeviceType::DEVICE_TYPE_PHONE) {
dictionary.Set(kTypeKey, "DEVICE_TYPE_PHONE");
}
dictionary.Set(kEndpointKey, presence_device.GetEndpointId());
std::string actions_list;
for (auto action : presence_device.GetActions()) {
actions_list += PresenceActionToString(action);
actions_list += ", ";
}
// Remove the trailing comma and whitespace.
actions_list.pop_back();
actions_list.pop_back();
dictionary.Set(kActionsKey, actions_list);
return dictionary;
}
} // namespace
NearbyInternalsPresenceHandler::NearbyInternalsPresenceHandler(
content::BrowserContext* context)
: context_(context) {}
NearbyInternalsPresenceHandler::~NearbyInternalsPresenceHandler() = default;
void NearbyInternalsPresenceHandler::RegisterMessages() {
web_ui()->RegisterMessageCallback(
"InitializePresenceHandler",
base::BindRepeating(&NearbyInternalsPresenceHandler::Initialize,
base::Unretained(this)));
web_ui()->RegisterMessageCallback(
"StartPresenceScan",
base::BindRepeating(
&NearbyInternalsPresenceHandler::HandleStartPresenceScan,
base::Unretained(this)));
web_ui()->RegisterMessageCallback(
"StopPresenceScan",
base::BindRepeating(
&NearbyInternalsPresenceHandler::HandleStopPresenceScan,
base::Unretained(this)));
web_ui()->RegisterMessageCallback(
"SyncPresenceCredentials",
base::BindRepeating(
&NearbyInternalsPresenceHandler::HandleSyncPresenceCredentials,
base::Unretained(this)));
web_ui()->RegisterMessageCallback(
"FirstTimePresenceFlow",
base::BindRepeating(
&NearbyInternalsPresenceHandler::HandleFirstTimePresenceFlow,
base::Unretained(this)));
web_ui()->RegisterMessageCallback(
"ConnectToPresenceDevice",
base::BindRepeating(
&NearbyInternalsPresenceHandler::HandleConnectToPresenceDevice,
base::Unretained(this)));
web_ui()->RegisterMessageCallback(
"SendUpdateCredentialsMessage",
base::BindRepeating(
&NearbyInternalsPresenceHandler::HandleSendUpdateCredentialsMessage,
base::Unretained(this)));
}
void NearbyInternalsPresenceHandler::OnJavascriptAllowed() {}
void NearbyInternalsPresenceHandler::OnJavascriptDisallowed() {}
void NearbyInternalsPresenceHandler::Initialize(const base::Value::List& args) {
AllowJavascript();
}
void NearbyInternalsPresenceHandler::HandleStartPresenceScan(
const base::Value::List& args) {
ash::nearby::presence::NearbyPresenceService* service =
ash::nearby::presence::NearbyPresenceServiceFactory::GetForBrowserContext(
context_);
if (service) {
CD_LOG(VERBOSE, Feature::NEARBY_INFRA)
<< __func__ << ": NearbyPresenceService was retrieved successfully";
ash::nearby::presence::NearbyPresenceService::ScanFilter filter(
nearby::internal::IdentityType::IDENTITY_TYPE_PRIVATE_GROUP,
/*actions=*/{});
service->StartScan(
filter, /*scan_delegate=*/this,
base::BindOnce(&NearbyInternalsPresenceHandler::OnScanStarted,
weak_ptr_factory_.GetWeakPtr()));
}
}
void NearbyInternalsPresenceHandler::HandleStopPresenceScan(
const base::Value::List& args) {
scan_session_.reset();
}
void NearbyInternalsPresenceHandler::HandleSyncPresenceCredentials(
const base::Value::List& args) {
ash::nearby::presence::NearbyPresenceService* service =
ash::nearby::presence::NearbyPresenceServiceFactory::GetForBrowserContext(
context_);
if (service) {
CD_LOG(VERBOSE, Feature::NEARBY_INFRA)
<< __func__ << ": NearbyPresenceService was retrieved successfully";
service->UpdateCredentials();
}
}
void NearbyInternalsPresenceHandler::HandleFirstTimePresenceFlow(
const base::Value::List& args) {
ash::nearby::presence::NearbyPresenceService* service =
ash::nearby::presence::NearbyPresenceServiceFactory::GetForBrowserContext(
context_);
if (service) {
CD_LOG(VERBOSE, Feature::NEARBY_INFRA)
<< __func__ << ": NearbyPresenceService was retrieved successfully";
auto* pref_service = Profile::FromBrowserContext(context_)->GetPrefs();
// Reset the state that indicates that first time registration was
// completed for testing. This will trigger the first time flow in
// `NearbyPresenceService::Initialize()`, in the case that this was already
// set on the device for manual testing.
pref_service->SetBoolean(ash::nearby::presence::prefs::
kNearbyPresenceFirstTimeRegistrationComplete,
false);
service->Initialize(
base::BindOnce(&NearbyInternalsPresenceHandler::
OnNearbyPresenceCredentialManagerInitialized,
weak_ptr_factory_.GetWeakPtr()));
}
}
void NearbyInternalsPresenceHandler::OnScanStarted(
std::unique_ptr<ash::nearby::presence::NearbyPresenceService::ScanSession>
scan_session,
ash::nearby::presence::enums::StatusCode status) {
if (status == ash::nearby::presence::enums::StatusCode::kAbslOk) {
scan_session_ = std::move(scan_session);
CD_LOG(VERBOSE, Feature::NEARBY_INFRA)
<< __func__ << ": ScanSession remote successfully returned and bound.";
} else {
// TODO(b/276307539): Pass error status back to WebUI.
return;
}
}
void NearbyInternalsPresenceHandler::
OnNearbyPresenceCredentialManagerInitialized() {
CD_LOG(VERBOSE, Feature::NEARBY_INFRA) << __func__;
}
void NearbyInternalsPresenceHandler::OnPresenceDeviceFound(
nearby::presence::PresenceDevice presence_device) {
const std::string& endpoint_id = presence_device.GetEndpointId();
endpoint_id_to_presence_device_map_.emplace(
endpoint_id, std::make_unique<nearby::presence::PresenceDevice>(
std::move(presence_device)));
FireWebUIListener(
"presence-device-found",
PresenceDeviceToDictionary(
*endpoint_id_to_presence_device_map_.at(endpoint_id).get()));
}
void NearbyInternalsPresenceHandler::OnPresenceDeviceChanged(
nearby::presence::PresenceDevice presence_device) {
FireWebUIListener("presence-device-changed",
PresenceDeviceToDictionary(presence_device));
}
void NearbyInternalsPresenceHandler::OnPresenceDeviceLost(
nearby::presence::PresenceDevice presence_device) {
endpoint_id_to_presence_device_map_.erase(presence_device.GetEndpointId());
FireWebUIListener("presence-device-lost",
PresenceDeviceToDictionary(presence_device));
}
void NearbyInternalsPresenceHandler::OnScanSessionInvalidated() {
scan_session_.reset();
HandleStartPresenceScan(/*args=*/{});
}
void NearbyInternalsPresenceHandler::HandleConnectToPresenceDevice(
const base::Value::List& args) {
const std::string& endpoint_id = args[0].GetString();
CD_LOG(VERBOSE, Feature::NEARBY_INFRA)
<< __func__
<< ": Connection attempt for device with endpoint id: " << endpoint_id;
if (!base::Contains(endpoint_id_to_presence_device_map_, endpoint_id)) {
CD_LOG(WARNING, Feature::NEARBY_INFRA)
<< __func__ << ": Received endpoint_id for device no longer in map.";
return;
}
auto* presence_connections_manager = GetNearbyPresenceConnectionsManager();
if (presence_connections_manager) {
presence_connections_manager->ConnectV3(
*endpoint_id_to_presence_device_map_.at(endpoint_id).get(),
nearby_share::mojom::DataUsage::kOffline,
base::BindOnce(&NearbyInternalsPresenceHandler::OnConnection,
weak_ptr_factory_.GetWeakPtr(), endpoint_id));
}
}
void NearbyInternalsPresenceHandler::OnConnection(
const std::string& endpoint_id,
NearbyConnection* connection) {
// TODO(b/339674618): Add `Connected` / `Failed to Connect` label on device
// cell in the WebUI.
if (connection) {
CD_LOG(VERBOSE, Feature::NEARBY_INFRA)
<< "Successfully connected to endpoint_id=" << endpoint_id;
} else {
CD_LOG(WARNING, Feature::NEARBY_INFRA)
<< "Failed to connect to to endpoint_id=" << endpoint_id;
}
}
void NearbyInternalsPresenceHandler::HandleSendUpdateCredentialsMessage(
const base::Value::List& args) {
push_notification::PushNotificationService* service =
push_notification::PushNotificationServiceFactory::GetForBrowserContext(
context_);
CHECK(service);
push_notification::PushNotificationClientManager::PushNotificationMessage
message;
message.data.insert_or_assign(kNotificationTypeIdKey,
push_notification::kNearbyPresenceClientId);
message.data.insert_or_assign(kNotificationClientIdKey,
kNotificationClientIdValue);
service->GetPushNotificationClientManager()
->NotifyPushNotificationClientOfMessage(std::move(message));
}
ash::nearby::presence::NearbyPresenceConnectionsManager*
NearbyInternalsPresenceHandler::GetNearbyPresenceConnectionsManager() {
if (!nearby_presence_connections_manager_) {
ash::nearby::presence::NearbyPresenceService* service = ash::nearby::
presence::NearbyPresenceServiceFactory::GetForBrowserContext(context_);
if (service) {
CD_LOG(VERBOSE, Feature::NEARBY_INFRA)
<< __func__ << ": NearbyPresenceService was retrieved successfully";
nearby_presence_connections_manager_ =
service->CreateNearbyPresenceConnectionsManager();
}
}
return nearby_presence_connections_manager_.get();
}
|