File: account_checker.h

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (144 lines) | stat: -rw-r--r-- 5,191 bytes parent folder | download | duplicates (3)
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
// 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 <string>

#include "base/memory/scoped_refptr.h"
#include "components/endpoint_fetcher/endpoint_fetcher.h"
#include "components/signin/public/identity_manager/identity_manager.h"
#include "components/sync/base/data_type.h"
#include "components/sync/base/user_selectable_type.h"
#include "components/sync/service/sync_service.h"
#include "net/traffic_annotation/network_traffic_annotation.h"
#include "services/data_decoder/public/cpp/data_decoder.h"
#include "services/network/public/cpp/shared_url_loader_factory.h"

class GURL;
class PrefService;
class PrefChangeRegistrar;

namespace base {
class TimeDelta;
}  // namespace base

namespace commerce {

extern const char kNotificationsPrefUrl[];

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

  virtual bool IsSignedIn();

  // Check whether a specific sync entity is enabled by the user. This means
  // the user has chosen to sync the provided model type and does not
  // necessarily mean sync is active.
  virtual bool IsSyncTypeEnabled(syncer::UserSelectableType type);

  // Check whether sync is available for the user.
  virtual bool IsSyncAvailable();

  virtual bool IsAnonymizedUrlDataCollectionEnabled();

  virtual bool IsSubjectToParentalControls();

  // Whether a user is allowed to use model execution features.
  virtual bool CanUseModelExecutionFeatures();

  // Gets the user's country as determined at startup.
  virtual std::string GetCountry();

  // Gets the user's locale as determine at startup.
  virtual std::string GetLocale();

  virtual PrefService* GetPrefs();

 protected:
  friend class ShoppingService;
  friend class MockAccountChecker;

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

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

  // This method could be overridden in tests.
  virtual std::unique_ptr<endpoint_fetcher::EndpointFetcher>
  CreateEndpointFetcher(const std::string& oauth_consumer_name,
                        const GURL& url,
                        const endpoint_fetcher::HttpMethod http_method,
                        const std::string& content_type,
                        const std::vector<std::string>& scopes,
                        const base::TimeDelta& timeout,
                        const std::string& post_data,
                        const net::NetworkTrafficAnnotationTag& annotation_tag);

 private:
  // Called when the pref value on whether to receive price tracking emails
  // changes. We need to send the new value to server unless the change is
  // triggered by aligning with the server fetched value.
  void OnPriceEmailPrefChanged();

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

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

  std::string country_;

  std::string locale_;

  raw_ptr<PrefService> pref_service_;

  raw_ptr<signin::IdentityManager> identity_manager_;

  raw_ptr<syncer::SyncService> sync_service_;

  const scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory_;

  std::unique_ptr<PrefChangeRegistrar> pref_change_registrar_;

  bool is_waiting_for_pref_fetch_completion_;

  // Indicate whether we should ignore the next email pref change. This is true
  // only if the change is triggered by aligning with the server fetched value,
  // in which case we don't need to send the new value to the server again.
  bool ignore_next_email_pref_change_{false};

  base::WeakPtrFactory<AccountChecker> weak_ptr_factory_;
};

}  // namespace commerce

#endif  // COMPONENTS_COMMERCE_CORE_ACCOUNT_CHECKER_H_