summaryrefslogtreecommitdiffstats
path: root/chromium/components/commerce/core/account_checker.h
blob: 169e4555fc102ce623737dcf88872c93202dfc1e (plain)
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
// 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_ACCOUNT_CHECKER_H_
#define COMPONENTS_COMMERCE_CORE_ACCOUNT_CHECKER_H_

#include "base/memory/scoped_refptr.h"
#include "base/scoped_observation.h"
#include "components/endpoint_fetcher/endpoint_fetcher.h"
#include "components/signin/public/identity_manager/identity_manager.h"
#include "components/signin/public/identity_manager/primary_account_change_event.h"
#include "services/data_decoder/public/cpp/data_decoder.h"
#include "services/network/public/cpp/shared_url_loader_factory.h"

class PrefService;
class PrefChangeRegistrar;

namespace commerce {

// Used to check user account status.
class AccountChecker : public signin::IdentityManager::Observer {
 public:
  AccountChecker(const AccountChecker&) = delete;
  ~AccountChecker() override;

  virtual bool IsSignedIn();

  virtual bool IsAnonymizedUrlDataCollectionEnabled();

  virtual bool IsWebAndAppActivityEnabled();

 protected:
  friend class ShoppingService;
  friend class MockAccountChecker;

  // This class should only be initialized in ShoppingService.
  AccountChecker(
      PrefService* pref_service,
      signin::IdentityManager* identity_manager,
      scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory);

  // Fetch users' pref from server on whether to receive price tracking emails.
  void FetchPriceEmailPref();

 private:
  void OnPrimaryAccountChanged(
      const signin::PrimaryAccountChangeEvent& event_details) override;

  // Fetch users' consent status on web and app activity.
  void FetchWaaStatus();

  // Handle the responses for fetching users' web and app activity consent
  // status.
  void HandleFetchWaaResponse(
      // 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/1362026): Avoid passing this fetcher.
      std::unique_ptr<EndpointFetcher> endpoint_fetcher,
      std::unique_ptr<EndpointResponse> responses);

  void OnFetchWaaJsonParsed(data_decoder::DataDecoder::ValueOrError result);

  // Send users' pref to server on whether to receive price tracking emails.
  void SendPriceEmailPref();

  void HandleSendPriceEmailPrefResponse(
      // 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/1362026): Avoid passing this fetcher.
      std::unique_ptr<EndpointFetcher> endpoint_fetcher,
      std::unique_ptr<EndpointResponse> responses);

  void OnSendPriceEmailPrefJsonParsed(
      data_decoder::DataDecoder::ValueOrError result);

  void HandleFetchPriceEmailPrefResponse(
      // 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/1362026): Avoid passing this fetcher.
      std::unique_ptr<EndpointFetcher> endpoint_fetcher,
      std::unique_ptr<EndpointResponse> responses);

  void OnFetchPriceEmailPrefJsonParsed(
      data_decoder::DataDecoder::ValueOrError result);

  raw_ptr<PrefService> pref_service_;

  raw_ptr<signin::IdentityManager> identity_manager_;

  base::ScopedObservation<signin::IdentityManager,
                          signin::IdentityManager::Observer>
      scoped_identity_manager_observation_{this};

  const scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory_;

  std::unique_ptr<PrefChangeRegistrar> pref_change_registrar_;

  bool is_waiting_for_pref_fetch_completion_;

  base::WeakPtrFactory<AccountChecker> weak_ptr_factory_;
};

}  // namespace commerce

#endif  // COMPONENTS_COMMERCE_CORE_ACCOUNT_CHECKER_H_