File: browser_dm_token_storage.h

package info (click to toggle)
chromium 139.0.7258.127-2
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 6,122,156 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 (153 lines) | stat: -rw-r--r-- 5,986 bytes parent folder | download | duplicates (7)
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
145
146
147
148
149
150
151
152
153
// Copyright 2018 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_ENTERPRISE_BROWSER_CONTROLLER_BROWSER_DM_TOKEN_STORAGE_H_
#define COMPONENTS_ENTERPRISE_BROWSER_CONTROLLER_BROWSER_DM_TOKEN_STORAGE_H_

#include <memory>
#include <string>

#include "base/functional/callback_forward.h"
#include "base/memory/scoped_refptr.h"
#include "base/memory/weak_ptr.h"
#include "base/no_destructor.h"
#include "base/run_loop.h"
#include "base/sequence_checker.h"
#include "base/system/sys_info.h"
#include "base/task/single_thread_task_runner.h"
#include "components/policy/core/common/cloud/dm_token.h"

namespace base {
class TaskRunner;
}

namespace policy {

// Manages storing and retrieving tokens and client ID used to enroll browser
// instances for enterprise management. The tokens are read from disk or
// registry once and cached values are returned in subsequent calls.
//
// All calls to member functions must be sequenced. It is an error to attempt
// concurrent store operations. RetrieveClientId must be the first method
// called.
class BrowserDMTokenStorage {
 public:
  using StoreTask = base::OnceCallback<bool()>;
  using StoreCallback = base::OnceCallback<void(bool success)>;

  // Delegate pattern for platform-dependant operations.
  class Delegate {
   public:
    virtual ~Delegate() = default;

    // Gets the client ID and returns it.
    virtual std::string InitClientId() = 0;
    // Gets the enrollment token and returns it.
    virtual std::string InitEnrollmentToken() = 0;
    // Gets the DM token and returns it.
    virtual std::string InitDMToken() = 0;
    // Gets the boolean value that determines if error message will be
    // displayed when enrollment fails.
    virtual bool InitEnrollmentErrorOption() = 0;
    // Returns whether the enrollment token can be initialized (if it is not
    // already) when `InitIfNeeded` is called.
    virtual bool CanInitEnrollmentToken() const = 0;
    // Function called by `SaveDMToken()` that returns if the operation was a
    // success.
    virtual StoreTask SaveDMTokenTask(const std::string& token,
                                      const std::string& client_id) = 0;
    // Function called by `DeleteDMToken()` that returns if the operation was a
    // success.
    virtual StoreTask DeleteDMTokenTask(const std::string& client_id) = 0;
    // Gets the specific task runner that should be used by |SaveDMToken|.
    virtual scoped_refptr<base::TaskRunner> SaveDMTokenTaskRunner() = 0;
  };

  // Returns the global singleton object. Must be called from the UI thread. The
  // first caller must set the platform-specific delegate via SetDelegate().
  static BrowserDMTokenStorage* Get();

  BrowserDMTokenStorage(const BrowserDMTokenStorage&) = delete;
  BrowserDMTokenStorage& operator=(const BrowserDMTokenStorage&) = delete;

  // Sets the delegate to use for platform-specific operations.
  static void SetDelegate(std::unique_ptr<Delegate> delegate);

  // Returns a client ID unique to the machine.
  std::string RetrieveClientId();
  // Returns the enrollment token, or an empty string if there is none.
  std::string RetrieveEnrollmentToken();
  // Asynchronously stores |dm_token| and calls |callback| with a boolean to
  // indicate success or failure. It is an error to attempt concurrent store
  // operations.
  void StoreDMToken(const std::string& dm_token, StoreCallback callback);
  // Asynchronously invalidates |dm_token_| and calls |callback| with a boolean
  // to indicate success or failure. It is an error to attempt concurrent store
  // operations.
  void InvalidateDMToken(StoreCallback callback);
  // Asynchronously clears |dm_token_| and calls |callback| with a boolean to
  // indicate success or failure. It is an error to attempt concurrent store
  // operations.
  void ClearDMToken(StoreCallback callback);
  // Returns an already stored DM token. An empty token is returned if no DM
  // token exists on the system or an error is encountered.
  DMToken RetrieveDMToken();
  // Must be called after the DM token is saved, to ensure that the callback is
  // invoked.
  void OnDMTokenStored(bool success);

  // Return true if we display error message dialog when enrollment process
  // fails.
  virtual bool ShouldDisplayErrorMessageOnFailure();

  // Set the BrowserDMTokenStorage instance for testing. The caller owns the
  // instance of the storage.
  static void SetForTesting(BrowserDMTokenStorage* storage) {
    storage_for_testing_ = storage;
  }
  // Force the class to initialize again. Use it when some fields are changed
  // during test.
  void ResetForTesting() { is_initialized_ = false; }

 protected:
  friend class base::NoDestructor<BrowserDMTokenStorage>;

  // The platform-specific delegate. Must be set via
  // BrowserDMTokenStorage::SetDelegate() before other methods can be called.
  std::unique_ptr<Delegate> delegate_;

  // Get the global singleton instance by calling BrowserDMTokenStorage::Get().
  BrowserDMTokenStorage();
  virtual ~BrowserDMTokenStorage();

 private:
  static BrowserDMTokenStorage* storage_for_testing_;

  // Initializes the DMTokenStorage object and caches the ids and tokens. This
  // is called the first time the BrowserDMTokenStorage is interacted with.
  void InitIfNeeded();

  // Saves the DM token.
  void SaveDMToken(const std::string& token);
  // Deletes the DM token.
  void DeleteDMToken();

  // Will be called after the DM token is stored.
  StoreCallback store_callback_;

  bool is_initialized_{false};
  bool is_init_enrollment_token_skipped_{true};

  std::string client_id_;
  std::string enrollment_token_;
  DMToken dm_token_;
  bool should_display_error_message_on_failure_;

  SEQUENCE_CHECKER(sequence_checker_);

  base::WeakPtrFactory<BrowserDMTokenStorage> weak_factory_{this};
};

}  // namespace policy
#endif  // COMPONENTS_ENTERPRISE_BROWSER_CONTROLLER_BROWSER_DM_TOKEN_STORAGE_H_