forked from Pissandshittium/pissandshittium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdetachable_base_handler.h
158 lines (126 loc) · 6.52 KB
/
detachable_base_handler.h
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
154
155
156
157
158
// Copyright 2018 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 ASH_DETACHABLE_BASE_DETACHABLE_BASE_HANDLER_H_
#define ASH_DETACHABLE_BASE_DETACHABLE_BASE_HANDLER_H_
#include <map>
#include <string>
#include <vector>
#include "ash/ash_export.h"
#include "ash/detachable_base/detachable_base_pairing_status.h"
#include "base/macros.h"
#include "base/memory/weak_ptr.h"
#include "base/observer_list.h"
#include "base/optional.h"
#include "base/scoped_observer.h"
#include "chromeos/dbus/hammerd/hammerd_client.h"
#include "chromeos/dbus/power/power_manager_client.h"
#include "components/account_id/account_id.h"
class PrefRegistrySimple;
class PrefService;
namespace ash {
class DetachableBaseObserver;
struct UserInfo;
// Keeps track of the state of Chrome OS device's detachable base. It tracks
// whether:
// * a detachable base is paired, and authenticated
// * Detachable base is considered paired when hammerd connects Chrome OS
// with the base
// * During pairing, hammerd will issue a challenge to the detachable base
// to verify its identity. The base is authenticated if the challenge
// succeeds. If the pairing challenge fails, the base might still work,
// but it will not be identifiable.
// * the paired base requires a firmware update
// This information is used to detect when a detachable base related
// notification should be shown to the user.
// Note that this does not track active/existing users, but it provides methods
// to set and retrive per user state to and from local state - the
// DetachableBaseHandler clients are expected to determine for which users the
// detachable base state should be set or retrieved.
class ASH_EXPORT DetachableBaseHandler
: public chromeos::HammerdClient::Observer,
public chromeos::PowerManagerClient::Observer {
public:
// |local_state| - PrefService of Local state. May be null in tests.
explicit DetachableBaseHandler(PrefService* local_state);
~DetachableBaseHandler() override;
// Registers the local state prefs for detachable base devices.
static void RegisterPrefs(PrefRegistrySimple* registry);
void AddObserver(DetachableBaseObserver* observer);
void RemoveObserver(DetachableBaseObserver* observer);
// Removes the detachable base data associated with a user from local state.
void RemoveUserData(const UserInfo& user);
// Gets the detachable base pairing state.
DetachableBasePairingStatus GetPairingStatus() const;
// Whether the paired base matches the last base used by the user.
// Returns true only if the base has kAuthenticated pairing state.
// If this the user has not previously used any detachable bases (i.e. the
// last used detachable base is empty), this will return true.
bool PairedBaseMatchesLastUsedByUser(const UserInfo& user) const;
// Sets the currently paired base as the last used base for the user.
// If the user is not ephemeral, the last used base information will be
// persisted in the local state.
// No-op if a detachable base is not currently paired and authenticated.
// Returns whether the base state was successfully updated.
//
// Note that this will fail for non-ephemeral users if |local_state_| is not
// yet initialized. Observers will be notified that pairing status
// changed when local state is initialized (provided a base is still
// paired) - setting the last used base can be retried at that point.
bool SetPairedBaseAsLastUsedByUser(const UserInfo& user);
// chromeos::HammerdClient::Observer:
void BaseFirmwareUpdateNeeded() override;
void BaseFirmwareUpdateStarted() override;
void BaseFirmwareUpdateSucceeded() override;
void BaseFirmwareUpdateFailed() override;
void PairChallengeSucceeded(const std::vector<uint8_t>& base_id) override;
void PairChallengeFailed() override;
void InvalidBaseConnected() override;
// chromeos::PowerManagerClient::Observer:
void TabletModeEventReceived(chromeos::PowerManagerClient::TabletMode mode,
const base::TimeTicks& timestamp) override;
private:
// Identifier for a detachable base device - HEX encoded string created from
// data passed to PairChallengeSucceeded. It's known only if the base was
// successfully authenticated.
using DetachableBaseId = std::string;
// Callback for getting initial power manager switches - used to determine
// whether the tablet mode is on when the DetachableBaseHandler is created.
void OnGotPowerManagerSwitchStates(
base::Optional<chromeos::PowerManagerClient::SwitchStates> switch_states);
// Updates the tracked tablet mode state, and notifies observers about pairing
// status change if required.
void UpdateTabletMode(chromeos::PowerManagerClient::TabletMode mode);
// Retrieves the last known base used by a user. Returns an empty string if
// a detachable base usage was not previously recorded for the user.
DetachableBaseId GetLastUsedDeviceForUser(const UserInfo& user) const;
// Notifies observers that the detachable base pairing state has changed.
void NotifyPairingStatusChanged();
// Notifies observers about whether the detachable base requires a firmware
// update.
void NotifyBaseRequiresFirmwareUpdate(bool requires_update);
PrefService* local_state_ = nullptr;
// Tablet mode state currently reported by power manager - tablet mode getting
// turned on is used as a signal that the base is detached.
base::Optional<chromeos::PowerManagerClient::TabletMode> tablet_mode_;
// The HEX encoded ID of the authenticated paired base device. This will
// be non empty iff pairing_status_ is kAuthenticated.
DetachableBaseId authenticated_base_id_;
// The paired base device pairing status.
DetachableBasePairingStatus pairing_status_ =
DetachableBasePairingStatus::kNone;
ScopedObserver<chromeos::HammerdClient, chromeos::HammerdClient::Observer>
hammerd_observer_;
ScopedObserver<chromeos::PowerManagerClient,
chromeos::PowerManagerClient::Observer>
power_manager_observer_;
// In-memory map from a user account ID to last used device set for user using
// SetPairedBaseAsLastUsedByUser().
// Used for ephemeral users.
std::map<AccountId, DetachableBaseId> last_used_devices_;
base::ObserverList<DetachableBaseObserver>::Unchecked observers_;
base::WeakPtrFactory<DetachableBaseHandler> weak_ptr_factory_{this};
DISALLOW_COPY_AND_ASSIGN(DetachableBaseHandler);
};
} // namespace ash
#endif // ASH_DETACHABLE_BASE_DETACHABLE_BASE_HANDLER_H_