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
|
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef COMPONENTS_COMMERCE_CORE_SUBSCRIPTIONS_SUBSCRIPTIONS_SERVER_PROXY_H_
#define COMPONENTS_COMMERCE_CORE_SUBSCRIPTIONS_SUBSCRIPTIONS_SERVER_PROXY_H_
#include <string>
#include <unordered_map>
#include "base/check.h"
#include "base/functional/callback.h"
#include "base/values.h"
#include "components/commerce/core/subscriptions/subscriptions_manager.h"
#include "net/traffic_annotation/network_traffic_annotation.h"
#include "services/data_decoder/public/cpp/data_decoder.h"
namespace network {
class SharedURLLoaderFactory;
} // namespace network
namespace signin {
class IdentityManager;
} // namespace signin
namespace endpoint_fetcher {
class EndpointFetcher;
struct EndpointResponse;
} // namespace endpoint_fetcher
namespace commerce {
enum class SubscriptionType;
struct CommerceSubscription;
using ManageSubscriptionsFetcherCallback = base::OnceCallback<void(
SubscriptionsRequestStatus,
std::unique_ptr<std::vector<CommerceSubscription>>)>;
using GetSubscriptionsFetcherCallback = base::OnceCallback<void(
SubscriptionsRequestStatus,
std::unique_ptr<std::vector<CommerceSubscription>>)>;
class SubscriptionsServerProxy {
public:
SubscriptionsServerProxy(
signin::IdentityManager* identity_manager,
scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory);
SubscriptionsServerProxy(const SubscriptionsServerProxy&) = delete;
SubscriptionsServerProxy& operator=(const SubscriptionsServerProxy&) = delete;
virtual ~SubscriptionsServerProxy();
// Make an HTTPS call to backend to create the new |subscriptions|.
virtual void Create(
std::unique_ptr<std::vector<CommerceSubscription>> subscriptions,
ManageSubscriptionsFetcherCallback callback);
// Make an HTTPS call to backend to delete the existing |subscriptions|.
virtual void Delete(
std::unique_ptr<std::vector<CommerceSubscription>> subscriptions,
ManageSubscriptionsFetcherCallback callback);
// Make an HTTP call to backend to get all subscriptions for specified type.
virtual void Get(SubscriptionType type,
GetSubscriptionsFetcherCallback callback);
protected:
// This method could be overridden in tests.
virtual std::unique_ptr<endpoint_fetcher::EndpointFetcher>
CreateEndpointFetcher(const GURL& url,
const endpoint_fetcher::HttpMethod http_method,
const std::string& post_data,
const net::NetworkTrafficAnnotationTag& annotation_tag);
private:
// Handle Create or Delete response.
void HandleManageSubscriptionsResponses(
ManageSubscriptionsFetcherCallback callback,
// Passing the endpoint_fetcher ensures the endpoint_fetcher's
// lifetime extends to the callback and is not destroyed
// prematurely (which would result in cancellation of the request).
// TODO(crbug.com/40238190): Avoid passing this fetcher.
std::unique_ptr<endpoint_fetcher::EndpointFetcher> endpoint_fetcher,
std::unique_ptr<endpoint_fetcher::EndpointResponse> responses);
// This is called when Create or Delete response is parsed.
void OnManageSubscriptionsJsonParsed(
ManageSubscriptionsFetcherCallback callback,
data_decoder::DataDecoder::ValueOrError result);
// Handle Get response.
void HandleGetSubscriptionsResponses(
GetSubscriptionsFetcherCallback callback,
// Passing the endpoint_fetcher ensures the endpoint_fetcher's
// lifetime extends to the callback and is not destroyed
// prematurely (which would result in cancellation of the request).
// TODO(crbug.com/40238190): Avoid passing this fetcher.
std::unique_ptr<endpoint_fetcher::EndpointFetcher> endpoint_fetcher,
std::unique_ptr<endpoint_fetcher::EndpointResponse> responses);
// This is called when Get response is parsed.
void OnGetSubscriptionsJsonParsed(
GetSubscriptionsFetcherCallback callback,
data_decoder::DataDecoder::ValueOrError result);
std::unique_ptr<std::vector<CommerceSubscription>>
GetSubscriptionsFromParsedJson(
const data_decoder::DataDecoder::ValueOrError& result);
bool IsPriceTrackingLocaleKeyEnabled();
base::Value::Dict Serialize(const CommerceSubscription& subscription);
std::optional<CommerceSubscription> Deserialize(const base::Value& value);
const scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory_;
const raw_ptr<signin::IdentityManager> identity_manager_;
base::WeakPtrFactory<SubscriptionsServerProxy> weak_ptr_factory_;
};
} // namespace commerce
#endif // COMPONENTS_COMMERCE_CORE_SUBSCRIPTIONS_SUBSCRIPTIONS_SERVER_PROXY_H_
|