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
|
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CONTENT_BROWSER_WEBID_DIGITAL_CREDENTIALS_DIGITAL_IDENTITY_REQUEST_IMPL_H_
#define CONTENT_BROWSER_WEBID_DIGITAL_CREDENTIALS_DIGITAL_IDENTITY_REQUEST_IMPL_H_
#include <memory>
#include <string>
#include <vector>
#include "base/memory/weak_ptr.h"
#include "base/types/expected.h"
#include "content/common/content_export.h"
#include "content/public/browser/digital_identity_provider.h"
#include "content/public/browser/document_service.h"
#include "content/public/browser/web_contents_observer.h"
#include "services/data_decoder/public/cpp/data_decoder.h"
#include "third_party/blink/public/mojom/webid/digital_identity_request.mojom.h"
#include "url/gurl.h"
namespace content {
class DigitalIdentityProvider;
class RenderFrameHost;
using ProtocolAndParsedRequest =
std::pair<std::string, data_decoder::DataDecoder::ValueOrError>;
// DigitalIdentityRequestImpl handles mojo connections from the renderer to
// fulfill digital identity requests.
//
// In practice, it is owned and managed by a RenderFrameHost. It accomplishes
// that via subclassing DocumentService, which observes the lifecycle of a
// RenderFrameHost and manages its own memory.
// Create() creates a self-managed instance of DigitalIdentityRequestImpl and
// binds it to the receiver.
class CONTENT_EXPORT DigitalIdentityRequestImpl
: public DocumentService<blink::mojom::DigitalIdentityRequest> {
public:
// The return value is only intended to be used in tests.
static base::WeakPtr<DigitalIdentityRequestImpl> CreateInstance(
RenderFrameHost&,
mojo::PendingReceiver<blink::mojom::DigitalIdentityRequest>);
// Returns the type of interstitial to show based on the request contents and
// the origin of the request.
static std::optional<DigitalIdentityInterstitialType> ComputeInterstitialType(
RenderFrameHost& render_frame_host,
const DigitalIdentityProvider* provider,
const std::vector<ProtocolAndParsedRequest>& parsed_requests);
DigitalIdentityRequestImpl(const DigitalIdentityRequestImpl&) = delete;
DigitalIdentityRequestImpl& operator=(const DigitalIdentityRequestImpl&) =
delete;
~DigitalIdentityRequestImpl() override;
// blink::mojom::DigitalIdentityRequest:
void Get(std::vector<blink::mojom::DigitalCredentialGetRequestPtr>
digital_credential_requests,
blink::mojom::GetRequestFormat format,
GetCallback) override;
void Create(std::vector<blink::mojom::DigitalCredentialCreateRequestPtr>
digital_credential_requests,
CreateCallback) override;
void Abort() override;
private:
DigitalIdentityRequestImpl(
RenderFrameHost&,
mojo::PendingReceiver<blink::mojom::DigitalIdentityRequest>);
// Called when the get request JSON has been parsed.
void OnGetRequestJsonParsed(
std::optional<std::string> protocol,
base::Value request_to_send,
const std::vector<ProtocolAndParsedRequest>& parsed_requests);
// Called when the create request JSON has been parsed.
void OnCreateRequestJsonParsed(
std::string protocol,
base::Value request_to_send,
data_decoder::DataDecoder::ValueOrError parsed_result);
// Called after fetching the user's identity. Shows an interstitial if needed.
void ShowInterstitialIfNeeded(
bool is_only_requesting_age,
base::expected<std::string,
DigitalIdentityProvider::RequestStatusForMetrics>
response);
// Called when the user has fulfilled the interstitial requirement. Will be
// called immediately after OnGetRequestJsonParsed() if no interstitial is
// needed.
void OnInterstitialDone(std::optional<std::string> protocol,
base::Value request_to_send,
DigitalIdentityProvider::RequestStatusForMetrics
status_after_interstitial);
// Infers blink::mojom::RequestDigitalIdentityStatus based on
// `status_for_metrics`.
void CompleteRequest(
std::optional<std::string> protocol,
base::expected<DigitalIdentityProvider::DigitalCredential,
DigitalIdentityProvider::RequestStatusForMetrics>
status_for_metrics);
void CompleteRequestWithError(
DigitalIdentityProvider::RequestStatusForMetrics status_for_metrics);
void CompleteRequestWithStatus(
std::optional<std::string> protocol,
blink::mojom::RequestDigitalIdentityStatus status,
base::expected<DigitalIdentityProvider::DigitalCredential,
DigitalIdentityProvider::RequestStatusForMetrics>
response);
std::unique_ptr<DigitalIdentityProvider> provider_;
GetCallback callback_;
// Callback which updates interstitial to inform user that the credential
// request has been aborted.
DigitalIdentityProvider::DigitalIdentityInterstitialAbortCallback
update_interstitial_on_abort_callback_;
base::WeakPtrFactory<DigitalIdentityRequestImpl> weak_ptr_factory_{this};
};
} // namespace content
#endif // CONTENT_BROWSER_WEBID_DIGITAL_CREDENTIALS_DIGITAL_IDENTITY_REQUEST_IMPL_H_
|