Commit 4bc661a2 authored by Juliet Levesque's avatar Juliet Levesque Committed by Commit Bot

[Nearby] Send HTTP messages to chrome://nearby-internals.

Create Notifier class to pass messages to observers, which will lead to
HTTP responses and requests to be displayed in the HTTP Messages
tab of the internal debug page.

Bug: 1093634
Change-Id: Iafcc02a1bcfb3e89085ab60ed35d16cecc312e79
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2299521
Commit-Queue: Juliet Levesque <julietlevesque@google.com>
Reviewed-by: default avatarJosh Nohle <nohle@chromium.org>
Cr-Commit-Position: refs/heads/master@{#791536}
parent f7262af1
......@@ -10,6 +10,8 @@ source_set("client") {
"nearby_share_client.h",
"nearby_share_client_impl.cc",
"nearby_share_client_impl.h",
"nearby_share_http_notifier.cc",
"nearby_share_http_notifier.h",
"nearby_share_request_error.cc",
"nearby_share_request_error.h",
"nearby_share_switches.cc",
......
......@@ -12,7 +12,9 @@
#include "base/no_destructor.h"
#include "base/strings/string_number_conversions.h"
#include "chrome/browser/nearby_sharing/client/nearby_share_api_call_flow_impl.h"
#include "chrome/browser/nearby_sharing/client/nearby_share_http_notifier.h"
#include "chrome/browser/nearby_sharing/client/nearby_share_switches.h"
#include "chrome/browser/nearby_sharing/logging/logging.h"
#include "chrome/browser/nearby_sharing/proto/certificate_rpc.pb.h"
#include "chrome/browser/nearby_sharing/proto/contact_rpc.pb.h"
#include "chrome/browser/nearby_sharing/proto/device_rpc.pb.h"
......@@ -218,10 +220,12 @@ GetListPublicCertificatesAnnotation() {
NearbyShareClientImpl::NearbyShareClientImpl(
std::unique_ptr<NearbyShareApiCallFlow> api_call_flow,
signin::IdentityManager* identity_manager,
scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory)
scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory,
NearbyShareHttpNotifier* notifier)
: api_call_flow_(std::move(api_call_flow)),
identity_manager_(identity_manager),
url_loader_factory_(std::move(url_loader_factory)),
notifier_(notifier),
has_call_started_(false) {}
NearbyShareClientImpl::~NearbyShareClientImpl() = default;
......@@ -230,6 +234,7 @@ void NearbyShareClientImpl::UpdateDevice(
const nearbyshare::proto::UpdateDeviceRequest& request,
UpdateDeviceCallback&& callback,
ErrorCallback&& error_callback) {
notifier_->NotifyOfRequest(request);
// TODO(cclem): Use correct device identifier
MakeApiCall(CreateV1RequestUrl(kUpdateDevicePath + request.device().name()),
RequestType::kPatch, request.SerializeAsString(),
......@@ -242,6 +247,7 @@ void NearbyShareClientImpl::CheckContactsReachability(
const nearbyshare::proto::CheckContactsReachabilityRequest& request,
CheckContactsReachabilityCallback&& callback,
ErrorCallback&& error_callback) {
notifier_->NotifyOfRequest(request);
MakeApiCall(CreateV1RequestUrl(kCheckContactsReachabilityPath),
RequestType::kPost, request.SerializeAsString(),
/*request_as_query_parameters=*/base::nullopt,
......@@ -253,6 +259,7 @@ void NearbyShareClientImpl::ListContactPeople(
const nearbyshare::proto::ListContactPeopleRequest& request,
ListContactPeopleCallback&& callback,
ErrorCallback&& error_callback) {
notifier_->NotifyOfRequest(request);
// TODO(cclem): Use correct identifier in URL
MakeApiCall(CreateV1RequestUrl(kListContactPeoplePathSeg1 + request.parent() +
kListContactPeoplePathSeg2),
......@@ -266,6 +273,7 @@ void NearbyShareClientImpl::ListPublicCertificates(
const nearbyshare::proto::ListPublicCertificatesRequest& request,
ListPublicCertificatesCallback&& callback,
ErrorCallback&& error_callback) {
notifier_->NotifyOfRequest(request);
// TODO(cclem): Use correct identifier in URL
MakeApiCall(
CreateV1RequestUrl(kListPublicCertificatesPathSeg1 + request.parent() +
......@@ -379,17 +387,21 @@ void NearbyShareClientImpl::OnFlowSuccess(
return;
}
std::move(result_callback).Run(response);
notifier_->NotifyOfResponse(response);
}
void NearbyShareClientImpl::OnApiCallFailed(NearbyShareRequestError error) {
std::move(error_callback_).Run(error);
NS_LOG(ERROR) << error;
}
NearbyShareClientFactoryImpl::NearbyShareClientFactoryImpl(
signin::IdentityManager* identity_manager,
scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory)
scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory,
NearbyShareHttpNotifier* notifier)
: identity_manager_(identity_manager),
url_loader_factory_(std::move(url_loader_factory)) {}
url_loader_factory_(std::move(url_loader_factory)),
notifier_(notifier) {}
NearbyShareClientFactoryImpl::~NearbyShareClientFactoryImpl() = default;
......@@ -397,5 +409,5 @@ std::unique_ptr<NearbyShareClient>
NearbyShareClientFactoryImpl::CreateInstance() {
return std::make_unique<NearbyShareClientImpl>(
std::make_unique<NearbyShareApiCallFlowImpl>(), identity_manager_,
url_loader_factory_);
url_loader_factory_, notifier_);
}
......@@ -30,6 +30,7 @@ class SharedURLLoaderFactory;
} // namespace network
class GoogleServiceAuthError;
class NearbyShareHttpNotifier;
// An implementation of NearbyShareClient that fetches access tokens for the
// primary account and makes HTTP calls using NearbyShareApiCallFlow.
......@@ -40,7 +41,8 @@ class NearbyShareClientImpl : public NearbyShareClient {
NearbyShareClientImpl(
std::unique_ptr<NearbyShareApiCallFlow> api_call_flow,
signin::IdentityManager* identity_manager,
scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory);
scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory,
NearbyShareHttpNotifier* notifier);
~NearbyShareClientImpl() override;
NearbyShareClientImpl(NearbyShareClientImpl&) = delete;
......@@ -126,6 +128,7 @@ class NearbyShareClientImpl : public NearbyShareClient {
access_token_fetcher_;
scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory_;
NearbyShareHttpNotifier* notifier_ = nullptr;
// True if an API call has been started. Remains true even after the API call
// completes.
......@@ -151,7 +154,8 @@ class NearbyShareClientFactoryImpl : public NearbyShareClientFactory {
// |url_loader_factory|: Used to make the HTTP requests.
NearbyShareClientFactoryImpl(
signin::IdentityManager* identity_manager,
scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory);
scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory,
NearbyShareHttpNotifier* notifier);
~NearbyShareClientFactoryImpl();
NearbyShareClientFactoryImpl(NearbyShareClientFactoryImpl&) = delete;
......@@ -164,6 +168,7 @@ class NearbyShareClientFactoryImpl : public NearbyShareClientFactory {
private:
signin::IdentityManager* identity_manager_;
const scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory_;
NearbyShareHttpNotifier* notifier_;
};
#endif // CHROME_BROWSER_NEARBY_SHARING_CLIENT_NEARBY_SHARE_CLIENT_IMPL_H_
......@@ -17,6 +17,7 @@
#include "chrome/browser/nearby_sharing/client/nearby_share_api_call_flow_impl.h"
#include "chrome/browser/nearby_sharing/client/nearby_share_client.h"
#include "chrome/browser/nearby_sharing/client/nearby_share_client_impl.h"
#include "chrome/browser/nearby_sharing/client/nearby_share_http_notifier.h"
#include "chrome/browser/nearby_sharing/client/nearby_share_switches.h"
#include "chrome/browser/nearby_sharing/proto/certificate_rpc.pb.h"
#include "chrome/browser/nearby_sharing/proto/contact_rpc.pb.h"
......@@ -170,7 +171,8 @@ void SaveResultConstRef(T* out, const T& result) {
} // namespace
class NearbyShareClientImplTest : public testing::Test {
class NearbyShareClientImplTest : public testing::Test,
public NearbyShareHttpNotifier::Observer {
protected:
NearbyShareClientImplTest() {
shared_factory_ =
......@@ -193,7 +195,56 @@ class NearbyShareClientImplTest : public testing::Test {
client_ = std::make_unique<NearbyShareClientImpl>(
std::move(api_call_flow), identity_test_environment_.identity_manager(),
shared_factory_);
shared_factory_, &notifier_);
notifier_.AddObserver(this);
}
void TearDown() override { notifier_.RemoveObserver(this); }
// NearbyShareHttpNotifier::Observer:
void OnUpdateDeviceRequest(
const nearbyshare::proto::UpdateDeviceRequest& request) override {
update_device_request_from_notifier_ = request;
}
void OnUpdateDeviceResponse(
const nearbyshare::proto::UpdateDeviceResponse& response) override {
update_device_response_from_notifier_ = response;
}
void OnListContactPeopleRequest(
const nearbyshare::proto::ListContactPeopleRequest& request) override {
list_contact_people_request_from_notifier_ = request;
}
void OnListContactPeopleResponse(
const nearbyshare::proto::ListContactPeopleResponse& response) override {
list_contact_people_response_from_notifier_ = response;
}
void OnListPublicCertificatesRequest(
const nearbyshare::proto::ListPublicCertificatesRequest& request)
override {
list_public_certificate_request_from_notifier_ = request;
}
void OnListPublicCertificatesResponse(
const nearbyshare::proto::ListPublicCertificatesResponse& response)
override {
list_public_certificate_response_from_notifier_ = response;
}
void OnCheckContactsReachabilityResponse(
const nearbyshare::proto::CheckContactsReachabilityResponse& response)
override {
check_contact_readability_response_from_notifier_ = response;
}
void OnCheckContactsReachabilityRequest(
const nearbyshare::proto::CheckContactsReachabilityRequest& request)
override {
check_contact_readability_request_from_notifier_ = request;
}
const std::string& http_method() { return api_call_flow_->http_method_; }
......@@ -221,14 +272,94 @@ class NearbyShareClientImplTest : public testing::Test {
std::move(api_call_flow_->error_callback_).Run(error);
}
void VerifyRequestNotification(
const nearbyshare::proto::UpdateDeviceRequest& expected_request) const {
ASSERT_TRUE(update_device_request_from_notifier_);
EXPECT_EQ(expected_request.SerializeAsString(),
update_device_request_from_notifier_->SerializeAsString());
}
void VerifyResponseNotification(
const nearbyshare::proto::UpdateDeviceResponse& expected_response) const {
ASSERT_TRUE(update_device_response_from_notifier_);
EXPECT_EQ(expected_response.SerializeAsString(),
update_device_response_from_notifier_->SerializeAsString());
}
void VerifyRequestNotification(
const nearbyshare::proto::ListContactPeopleRequest& expected_request)
const {
ASSERT_TRUE(list_contact_people_request_from_notifier_);
EXPECT_EQ(expected_request.SerializeAsString(),
list_contact_people_request_from_notifier_->SerializeAsString());
}
void VerifyResponseNotification(
const nearbyshare::proto::ListContactPeopleResponse& expected_response)
const {
ASSERT_TRUE(list_contact_people_response_from_notifier_);
EXPECT_EQ(expected_response.SerializeAsString(),
list_contact_people_response_from_notifier_->SerializeAsString());
}
void VerifyRequestNotification(
const nearbyshare::proto::ListPublicCertificatesRequest& expected_request)
const {
ASSERT_TRUE(list_public_certificate_request_from_notifier_);
EXPECT_EQ(
expected_request.SerializeAsString(),
list_public_certificate_request_from_notifier_->SerializeAsString());
}
void VerifyResponseNotification(
const nearbyshare::proto::ListPublicCertificatesResponse&
expected_response) const {
ASSERT_TRUE(list_public_certificate_response_from_notifier_);
EXPECT_EQ(
expected_response.SerializeAsString(),
list_public_certificate_response_from_notifier_->SerializeAsString());
}
void VerifyRequestNotification(
const nearbyshare::proto::CheckContactsReachabilityRequest&
expected_request) const {
ASSERT_TRUE(check_contact_readability_request_from_notifier_);
EXPECT_EQ(
expected_request.SerializeAsString(),
check_contact_readability_request_from_notifier_->SerializeAsString());
}
void VerifyResponseNotification(
const nearbyshare::proto::CheckContactsReachabilityResponse&
expected_response) const {
ASSERT_TRUE(check_contact_readability_response_from_notifier_);
EXPECT_EQ(
expected_response.SerializeAsString(),
check_contact_readability_response_from_notifier_->SerializeAsString());
}
protected:
base::Optional<nearbyshare::proto::UpdateDeviceRequest>
update_device_request_from_notifier_;
base::Optional<nearbyshare::proto::UpdateDeviceResponse>
update_device_response_from_notifier_;
base::Optional<nearbyshare::proto::ListContactPeopleRequest>
list_contact_people_request_from_notifier_;
base::Optional<nearbyshare::proto::ListContactPeopleResponse>
list_contact_people_response_from_notifier_;
base::Optional<nearbyshare::proto::ListPublicCertificatesRequest>
list_public_certificate_request_from_notifier_;
base::Optional<nearbyshare::proto::ListPublicCertificatesResponse>
list_public_certificate_response_from_notifier_;
base::Optional<nearbyshare::proto::CheckContactsReachabilityRequest>
check_contact_readability_request_from_notifier_;
base::Optional<nearbyshare::proto::CheckContactsReachabilityResponse>
check_contact_readability_response_from_notifier_;
base::test::TaskEnvironment task_environment_;
signin::IdentityTestEnvironment identity_test_environment_;
// Owned by |client_|.
FakeNearbyShareApiCallFlow* api_call_flow_;
scoped_refptr<network::SharedURLLoaderFactory> shared_factory_;
NearbyShareHttpNotifier notifier_;
std::unique_ptr<NearbyShareClient> client_;
};
......@@ -246,6 +377,8 @@ TEST_F(NearbyShareClientImplTest, UpdateDeviceSuccess) {
.WaitForAccessTokenRequestIfNecessaryAndRespondWithToken(
kAccessToken, base::Time::Max());
VerifyRequestNotification(request_proto);
EXPECT_EQ(kPatch, http_method());
EXPECT_EQ(request_url(),
GURL("https://www.nearbysharing-pa.testgoogleapis.com/v1/users/me/"
......@@ -274,6 +407,7 @@ TEST_F(NearbyShareClientImplTest, UpdateDeviceSuccess) {
device.mutable_contacts(2)->set_is_selected(true);
FinishApiCallFlow(&response_proto);
VerifyResponseNotification(response_proto);
// Check that the result received in callback is the same as the response.
ASSERT_EQ(3, result_proto.device().contacts_size());
......@@ -331,6 +465,8 @@ TEST_F(NearbyShareClientImplTest, CheckContactsReachabilitySuccess) {
.WaitForAccessTokenRequestIfNecessaryAndRespondWithToken(
kAccessToken, base::Time::Max());
VerifyRequestNotification(request_proto);
EXPECT_EQ(kPost, http_method());
EXPECT_EQ(request_url(),
"https://www.nearbysharing-pa.testgoogleapis.com/v1/"
......@@ -352,6 +488,7 @@ TEST_F(NearbyShareClientImplTest, CheckContactsReachabilitySuccess) {
response_proto.mutable_results(1)->set_is_recommended(true);
FinishApiCallFlow(&response_proto);
VerifyResponseNotification(response_proto);
// Check that the result received in callback is the same as the response.
ASSERT_EQ(2, result_proto.results_size());
......@@ -401,6 +538,8 @@ TEST_F(NearbyShareClientImplTest, ListContactPeopleSuccess) {
.WaitForAccessTokenRequestIfNecessaryAndRespondWithToken(
kAccessToken, base::Time::Max());
VerifyRequestNotification(request_proto);
EXPECT_EQ(kGet, http_method());
EXPECT_EQ(
request_url(),
......@@ -425,6 +564,7 @@ TEST_F(NearbyShareClientImplTest, ListContactPeopleSuccess) {
->set_obfuscated_gaia(kObfuscatedGaia1);
response_proto.set_next_page_token(kPageToken2);
FinishApiCallFlow(&response_proto);
VerifyResponseNotification(response_proto);
EXPECT_EQ(1, result_proto.contact_records_size());
EXPECT_EQ(kContactId1, result_proto.contact_records(0).id());
......@@ -459,6 +599,8 @@ TEST_F(NearbyShareClientImplTest, ListPublicCertificatesSuccess) {
.WaitForAccessTokenRequestIfNecessaryAndRespondWithToken(
kAccessToken, base::Time::Max());
VerifyRequestNotification(request_proto);
EXPECT_EQ(kGet, http_method());
EXPECT_EQ(
request_url(),
......@@ -501,6 +643,7 @@ TEST_F(NearbyShareClientImplTest, ListPublicCertificatesSuccess) {
response_proto.mutable_public_certificates(0)
->set_metadata_encryption_key_tag(kMetadataEncryptionKeyTag1);
FinishApiCallFlow(&response_proto);
VerifyResponseNotification(response_proto);
EXPECT_EQ(kPageToken2, result_proto.next_page_token());
EXPECT_EQ(1, result_proto.public_certificates_size());
......
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/nearby_sharing/client/nearby_share_http_notifier.h"
NearbyShareHttpNotifier::NearbyShareHttpNotifier() = default;
NearbyShareHttpNotifier::~NearbyShareHttpNotifier() = default;
void NearbyShareHttpNotifier::AddObserver(Observer* observer) {
observers_.AddObserver(observer);
}
void NearbyShareHttpNotifier::RemoveObserver(Observer* observer) {
observers_.RemoveObserver(observer);
}
void NearbyShareHttpNotifier::NotifyOfRequest(
const nearbyshare::proto::UpdateDeviceRequest& request) {
for (auto& observer : observers_)
observer.OnUpdateDeviceRequest(request);
}
void NearbyShareHttpNotifier::NotifyOfResponse(
const nearbyshare::proto::UpdateDeviceResponse& response) {
for (auto& observer : observers_)
observer.OnUpdateDeviceResponse(response);
}
void NearbyShareHttpNotifier::NotifyOfRequest(
const nearbyshare::proto::ListContactPeopleRequest& request) {
for (auto& observer : observers_)
observer.OnListContactPeopleRequest(request);
}
void NearbyShareHttpNotifier::NotifyOfResponse(
const nearbyshare::proto::ListContactPeopleResponse& response) {
for (auto& observer : observers_)
observer.OnListContactPeopleResponse(response);
}
void NearbyShareHttpNotifier::NotifyOfRequest(
const nearbyshare::proto::ListPublicCertificatesRequest& request) {
for (auto& observer : observers_)
observer.OnListPublicCertificatesRequest(request);
}
void NearbyShareHttpNotifier::NotifyOfResponse(
const nearbyshare::proto::ListPublicCertificatesResponse& response) {
for (auto& observer : observers_)
observer.OnListPublicCertificatesResponse(response);
}
void NearbyShareHttpNotifier::NotifyOfRequest(
const nearbyshare::proto::CheckContactsReachabilityRequest& request) {
for (auto& observer : observers_)
observer.OnCheckContactsReachabilityRequest(request);
}
void NearbyShareHttpNotifier::NotifyOfResponse(
const nearbyshare::proto::CheckContactsReachabilityResponse& response) {
for (auto& observer : observers_)
observer.OnCheckContactsReachabilityResponse(response);
}
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_BROWSER_NEARBY_SHARING_CLIENT_NEARBY_SHARE_HTTP_NOTIFIER_H_
#define CHROME_BROWSER_NEARBY_SHARING_CLIENT_NEARBY_SHARE_HTTP_NOTIFIER_H_
#include "base/observer_list.h"
#include "base/observer_list_types.h"
#include "chrome/browser/nearby_sharing/proto/certificate_rpc.pb.h"
#include "chrome/browser/nearby_sharing/proto/contact_rpc.pb.h"
#include "chrome/browser/nearby_sharing/proto/device_rpc.pb.h"
// Interface for passing HTTP Responses/Requests to observers, by passing
// instance of this class to each HTTP Client.
class NearbyShareHttpNotifier {
public:
class Observer : public base::CheckedObserver {
public:
// Called when HTTP RPC is made for request and responses.
virtual void OnUpdateDeviceRequest(
const nearbyshare::proto::UpdateDeviceRequest& request) = 0;
virtual void OnUpdateDeviceResponse(
const nearbyshare::proto::UpdateDeviceResponse& response) = 0;
virtual void OnListContactPeopleRequest(
const nearbyshare::proto::ListContactPeopleRequest& request) = 0;
virtual void OnListContactPeopleResponse(
const nearbyshare::proto::ListContactPeopleResponse& response) = 0;
virtual void OnListPublicCertificatesRequest(
const nearbyshare::proto::ListPublicCertificatesRequest& request) = 0;
virtual void OnListPublicCertificatesResponse(
const nearbyshare::proto::ListPublicCertificatesResponse& response) = 0;
virtual void OnCheckContactsReachabilityRequest(
const nearbyshare::proto::CheckContactsReachabilityRequest&
request) = 0;
virtual void OnCheckContactsReachabilityResponse(
const nearbyshare::proto::CheckContactsReachabilityResponse&
response) = 0;
};
NearbyShareHttpNotifier();
NearbyShareHttpNotifier(const NearbyShareHttpNotifier&) = delete;
NearbyShareHttpNotifier& operator=(const NearbyShareHttpNotifier&) = delete;
~NearbyShareHttpNotifier();
void AddObserver(Observer* observer);
void RemoveObserver(Observer* observer);
// Sends |request| to all observers.
void NotifyOfRequest(const nearbyshare::proto::UpdateDeviceRequest& request);
void NotifyOfRequest(
const nearbyshare::proto::CheckContactsReachabilityRequest& request);
void NotifyOfRequest(
const nearbyshare::proto::ListContactPeopleRequest& request);
void NotifyOfRequest(
const nearbyshare::proto::ListPublicCertificatesRequest& request);
// Sends |response| to all observers.
void NotifyOfResponse(
const nearbyshare::proto::UpdateDeviceResponse& response);
void NotifyOfResponse(
const nearbyshare::proto::ListContactPeopleResponse& response);
void NotifyOfResponse(
const nearbyshare::proto::ListPublicCertificatesResponse& response);
void NotifyOfResponse(
const nearbyshare::proto::CheckContactsReachabilityResponse& response);
private:
base::ObserverList<Observer> observers_;
};
#endif // CHROME_BROWSER_NEARBY_SHARING_CLIENT_NEARBY_SHARE_HTTP_NOTIFIER_H_
......@@ -16,6 +16,7 @@
#include "base/scoped_observer.h"
#include "base/sequence_checker.h"
#include "base/unguessable_token.h"
#include "chrome/browser/nearby_sharing/client/nearby_share_http_notifier.h"
#include "chrome/browser/nearby_sharing/common/nearby_share_enums.h"
#include "chrome/browser/nearby_sharing/incoming_share_target_info.h"
#include "chrome/browser/nearby_sharing/nearby_connections_manager.h"
......@@ -124,6 +125,7 @@ class NearbySharingServiceImpl
scoped_refptr<device::BluetoothAdapter> bluetooth_adapter_;
std::unique_ptr<FastInitiationManager> fast_initiation_manager_;
NearbyNotificationManager nearby_notification_manager_;
NearbyShareHttpNotifier nearby_share_http_notifier_;
// A list of foreground receivers.
base::ObserverList<TransferUpdateCallback> foreground_receive_callbacks_;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment