summaryrefslogtreecommitdiff
path: root/chromium/services/device/fingerprint/fingerprint_chromeos.h
blob: 2b5b2dd8e0b6ac94f4105a5b0bb2dada6a2e1394 (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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef SERVICES_DEVICE_FINGERPRINT_FINGERPRINT_CHROMEOS_H_
#define SERVICES_DEVICE_FINGERPRINT_FINGERPRINT_CHROMEOS_H_

#include <stdint.h>

#include "base/containers/flat_map.h"
#include "base/containers/queue.h"
#include "base/macros.h"
#include "chromeos/dbus/biod/biod_client.h"
#include "mojo/public/cpp/bindings/pending_remote.h"
#include "mojo/public/cpp/bindings/remote.h"
#include "services/device/fingerprint/fingerprint_export.h"
#include "services/device/public/mojom/fingerprint.mojom.h"

namespace dbus {
class ObjectPath;
}

namespace device {

// Implementation of Fingerprint interface for ChromeOS platform.
// This is used to connect to biod(through dbus) and perform fingerprint related
// operations. It observes signals from biod. This class requires that
// chromeos::BiodClient has been initialized.
class SERVICES_DEVICE_FINGERPRINT_EXPORT FingerprintChromeOS
    : public mojom::Fingerprint,
      public chromeos::BiodClient::Observer {
 public:
  enum class FingerprintSession {
    NONE,
    AUTH,
    ENROLL,
  };

  explicit FingerprintChromeOS();

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

  ~FingerprintChromeOS() override;

  // mojom::Fingerprint:
  void GetRecordsForUser(const std::string& user_id,
                         GetRecordsForUserCallback callback) override;
  void StartEnrollSession(const std::string& user_id,
                          const std::string& label) override;
  void CancelCurrentEnrollSession(
      CancelCurrentEnrollSessionCallback callback) override;
  void RequestRecordLabel(const std::string& record_path,
                          RequestRecordLabelCallback callback) override;
  void SetRecordLabel(const std::string& record_path,
                      const std::string& new_label,
                      SetRecordLabelCallback callback) override;
  void RemoveRecord(const std::string& record_path,
                    RemoveRecordCallback callback) override;
  void StartAuthSession() override;
  void EndCurrentAuthSession(EndCurrentAuthSessionCallback callback) override;
  void DestroyAllRecords(DestroyAllRecordsCallback callback) override;
  void RequestType(RequestTypeCallback callback) override;
  void AddFingerprintObserver(mojo::PendingRemote<mojom::FingerprintObserver>
                                  pending_observer) override;

 private:
  friend class FingerprintChromeOSTest;

  // chromeos::BiodClient::Observer:
  void BiodServiceRestarted() override;
  void BiodEnrollScanDoneReceived(biod::ScanResult scan_result,
                                  bool enroll_session_complete,
                                  int percent_complete) override;
  void BiodAuthScanDoneReceived(
      biod::ScanResult scan_result,
      const chromeos::AuthScanMatches& matches) override;
  void BiodSessionFailedReceived() override;

  void OnFingerprintObserverDisconnected(mojom::FingerprintObserver* observer);
  void OnStartEnrollSession(const dbus::ObjectPath& enroll_path);
  void OnStartAuthSession(const dbus::ObjectPath& auth_path);
  void OnGetRecordsForUser(GetRecordsForUserCallback callback,
                           const std::vector<dbus::ObjectPath>& record_paths);
  void OnGetLabelFromRecordPath(size_t num_records,
                                const dbus::ObjectPath& record_path,
                                const std::string& label);

  void OnCloseEnrollSessionForAuth(bool result);
  void OnCloseAuthSessionForEnroll(const std::string& user_id,
                                   const std::string& label,
                                   bool result);

  void RunGetRecordsForUser(const std::string& user_id,
                            GetRecordsForUserCallback callback);

  // Start next request of GetRecordsForUser.
  void StartNextRequest();

  std::vector<mojo::Remote<mojom::FingerprintObserver>> observers_;

  // Saves record object path to label mapping for current GetRecordsForUser
  // request, and reset after the request is done.
  base::flat_map<std::string, std::string> records_path_to_label_;

  // Callback for current GetRecordsForUser request.
  GetRecordsForUserCallback on_get_records_;

  // Pending requests of GetRecordsForUser.
  base::queue<base::OnceClosure> get_records_pending_requests_;

  // Whether a GetRecordsForUser request is in process.
  bool is_request_running_ = false;

  // Session opened by current service.
  FingerprintSession opened_session_ = FingerprintSession::NONE;

  base::WeakPtrFactory<FingerprintChromeOS> weak_ptr_factory_{this};
};

}  // namespace device

#endif  // SERVICES_DEVICE_FINGERPRINT_FINGERPRINT_CHROMEOS_H_