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
|
// Copyright 2020 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_contact_handler.h"
#include <string>
#include <utility>
#include "base/functional/bind.h"
#include "base/json/json_writer.h"
#include "base/time/time.h"
#include "chrome/browser/nearby_sharing/logging/proto_to_dictionary_conversion.h"
#include "chrome/browser/nearby_sharing/nearby_sharing_service.h"
#include "chrome/browser/nearby_sharing/nearby_sharing_service_factory.h"
#include "components/cross_device/logging/logging.h"
namespace {
std::string FormatListAsJSON(const base::Value::List& list) {
std::string json;
base::JSONWriter::WriteWithOptions(
list, base::JSONWriter::OPTIONS_PRETTY_PRINT, &json);
return json;
}
base::Value GetJavascriptTimestamp() {
return base::Value(
base::Time::Now().InMillisecondsFSinceUnixEpochIgnoringNull());
}
// Keys in the JSON representation of a contact message
const char kContactMessageTimeKey[] = "time";
const char kContactMessageContactsChangedKey[] = "contactsChanged";
const char kContactMessageAllowedIdsKey[] = "allowedIds";
const char kContactMessageContactRecordKey[] = "contactRecords";
const char kContactMessageNumUnreachableContactsKey[] =
"numUnreachableContacts";
// Converts Contact to a raw dictionary value used as a JSON argument to
// JavaScript functions.
// TODO(nohle): We should probably break up this dictionary into smaller
// dictionaries corresponding to each contact-manager observer functions. This
// will require changes at the javascript layer as well.
base::Value::Dict ContactMessageToDictionary(
std::optional<bool> did_contacts_change_since_last_upload,
const std::optional<std::set<std::string>>& allowed_contact_ids,
const std::optional<std::vector<nearby::sharing::proto::ContactRecord>>&
contacts,
std::optional<uint32_t> num_unreachable_contacts_filtered_out) {
base::Value::Dict dictionary;
dictionary.Set(kContactMessageTimeKey, GetJavascriptTimestamp());
if (did_contacts_change_since_last_upload.has_value()) {
dictionary.Set(kContactMessageContactsChangedKey,
*did_contacts_change_since_last_upload);
}
if (allowed_contact_ids) {
base::Value::List allowed_ids_list;
allowed_ids_list.reserve(allowed_contact_ids->size());
for (const auto& contact_id : *allowed_contact_ids) {
allowed_ids_list.Append(contact_id);
}
dictionary.Set(kContactMessageAllowedIdsKey,
FormatListAsJSON(allowed_ids_list));
}
if (contacts) {
base::Value::List contact_list;
contact_list.reserve(contacts->size());
for (const auto& contact : *contacts) {
contact_list.Append(ContactRecordToReadableDictionary(contact));
}
dictionary.Set(kContactMessageContactRecordKey,
FormatListAsJSON(contact_list));
}
if (num_unreachable_contacts_filtered_out.has_value()) {
dictionary.Set(kContactMessageNumUnreachableContactsKey,
int(*num_unreachable_contacts_filtered_out));
}
return dictionary;
}
} // namespace
NearbyInternalsContactHandler::NearbyInternalsContactHandler(
content::BrowserContext* context)
: context_(context) {}
NearbyInternalsContactHandler::~NearbyInternalsContactHandler() = default;
void NearbyInternalsContactHandler::RegisterMessages() {
web_ui()->RegisterMessageCallback(
"initializeContacts",
base::BindRepeating(&NearbyInternalsContactHandler::InitializeContents,
base::Unretained(this)));
web_ui()->RegisterMessageCallback(
"downloadContacts",
base::BindRepeating(
&NearbyInternalsContactHandler::HandleDownloadContacts,
base::Unretained(this)));
}
void NearbyInternalsContactHandler::OnJavascriptAllowed() {
NearbySharingService* service_ =
NearbySharingServiceFactory::GetForBrowserContext(context_);
if (service_) {
observation_.Observe(service_->GetContactManager());
} else {
CD_LOG(ERROR, Feature::NS) << "No NearbyShareService instance to call.";
}
}
void NearbyInternalsContactHandler::OnJavascriptDisallowed() {
observation_.Reset();
}
void NearbyInternalsContactHandler::InitializeContents(
const base::Value::List& args) {
AllowJavascript();
}
void NearbyInternalsContactHandler::HandleDownloadContacts(
const base::Value::List& args) {
NearbySharingService* service_ =
NearbySharingServiceFactory::GetForBrowserContext(context_);
if (service_) {
service_->GetContactManager()->DownloadContacts();
} else {
CD_LOG(ERROR, Feature::NS) << "No NearbyShareService instance to call.";
}
}
void NearbyInternalsContactHandler::OnContactsDownloaded(
const std::set<std::string>& allowed_contact_ids,
const std::vector<nearby::sharing::proto::ContactRecord>& contacts,
uint32_t num_unreachable_contacts_filtered_out) {
FireWebUIListener("contacts-updated",
ContactMessageToDictionary(
/*did_contacts_change_since_last_upload=*/std::nullopt,
allowed_contact_ids, contacts,
num_unreachable_contacts_filtered_out));
}
void NearbyInternalsContactHandler::OnContactsUploaded(
bool did_contacts_change_since_last_upload) {
FireWebUIListener(
"contacts-updated",
ContactMessageToDictionary(
did_contacts_change_since_last_upload,
/*allowed_contact_ids=*/std::nullopt,
/*contacts=*/std::nullopt,
/*num_unreachable_contacts_filtered_out=*/std::nullopt));
}
|