1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef CHROMEOS_COMPONENTS_PROXIMITY_AUTH_SCREENLOCK_BRIDGE_H_
6 #define CHROMEOS_COMPONENTS_PROXIMITY_AUTH_SCREENLOCK_BRIDGE_H_
7 
8 #include <memory>
9 #include <string>
10 
11 #include "base/lazy_instance.h"
12 #include "base/macros.h"
13 #include "base/observer_list.h"
14 #include "base/strings/string16.h"
15 #include "base/values.h"
16 #include "chromeos/components/proximity_auth/public/mojom/auth_type.mojom.h"
17 #include "components/account_id/account_id.h"
18 
19 namespace proximity_auth {
20 
21 // ScreenlockBridge brings together the screenLockPrivate API and underlying
22 // support. It delegates calls to the ScreenLocker.
23 // TODO(tbarzic): Rename ScreenlockBridge to SignInScreenBridge, as this is not
24 // used solely for the lock screen anymore.
25 // TODO(jhawkins): Rationalize this class now that it is CrOS only and most of
26 // its functionality is not useful.
27 class ScreenlockBridge {
28  public:
29   // User pod icons supported by lock screen / signin screen UI.
30   enum UserPodCustomIcon {
31     USER_POD_CUSTOM_ICON_NONE,
32     USER_POD_CUSTOM_ICON_HARDLOCKED,
33     USER_POD_CUSTOM_ICON_LOCKED,
34     USER_POD_CUSTOM_ICON_LOCKED_TO_BE_ACTIVATED,
35     // TODO(isherman): The "locked with proximity hint" icon is currently the
36     // same as the "locked" icon. It's treated as a separate case to allow an
37     // easy asset swap without changing the code, in case we decide to use a
38     // different icon for this case. If we definitely decide against that, then
39     // this enum entry should be removed.
40     USER_POD_CUSTOM_ICON_LOCKED_WITH_PROXIMITY_HINT,
41     USER_POD_CUSTOM_ICON_UNLOCKED,
42     USER_POD_CUSTOM_ICON_SPINNER
43   };
44 
45   // Class containing parameters describing the custom icon that should be
46   // shown on a user's screen lock pod next to the input field.
47   class UserPodCustomIconOptions {
48    public:
49     UserPodCustomIconOptions();
50     ~UserPodCustomIconOptions();
51 
52     // Converts parameters to a dictionary values that can be sent to the
53     // screenlock web UI.
54     std::unique_ptr<base::DictionaryValue> ToDictionaryValue() const;
55 
56     // Sets the icon that should be shown in the UI.
57     void SetIcon(UserPodCustomIcon icon);
58 
59     // Sets the icon tooltip. If |autoshow| is set the tooltip is automatically
60     // shown with the icon.
61     void SetTooltip(const base::string16& tooltip, bool autoshow);
62 
63     // Sets the accessibility label of the icon. If this attribute is not
64     // provided, then the tooltip will be used.
65     void SetAriaLabel(const base::string16& aria_label);
66 
67     // If hardlock on click is set, clicking the icon in the screenlock will
68     // go to state where password is required for unlock.
69     void SetHardlockOnClick();
70 
71     std::string GetIDString() const;
72 
icon()73     UserPodCustomIcon icon() const { return icon_; }
74 
tooltip()75     const base::string16 tooltip() const { return tooltip_; }
76 
autoshow_tooltip()77     bool autoshow_tooltip() const { return autoshow_tooltip_; }
78 
aria_label()79     const base::string16 aria_label() const { return aria_label_; }
80 
hardlock_on_click()81     bool hardlock_on_click() const { return hardlock_on_click_; }
82 
83    private:
84     UserPodCustomIcon icon_;
85 
86     base::string16 tooltip_;
87     bool autoshow_tooltip_;
88 
89     base::string16 aria_label_;
90 
91     bool hardlock_on_click_;
92 
93     DISALLOW_COPY_AND_ASSIGN(UserPodCustomIconOptions);
94   };
95 
96   class LockHandler {
97    public:
98     enum ScreenType { SIGNIN_SCREEN = 0, LOCK_SCREEN = 1, OTHER_SCREEN = 2 };
99 
100     // Displays |message| in a banner on the lock screen.
101     virtual void ShowBannerMessage(const base::string16& message,
102                                    bool is_warning) = 0;
103 
104     // Shows a custom icon in the user pod on the lock screen.
105     virtual void ShowUserPodCustomIcon(
106         const AccountId& account_id,
107         const UserPodCustomIconOptions& icon) = 0;
108 
109     // Hides the custom icon in user pod for a user.
110     virtual void HideUserPodCustomIcon(const AccountId& account_id) = 0;
111 
112     // (Re)enable lock screen UI.
113     virtual void EnableInput() = 0;
114 
115     // Set the authentication type to be used on the lock screen.
116     virtual void SetAuthType(const AccountId& account_id,
117                              proximity_auth::mojom::AuthType auth_type,
118                              const base::string16& auth_value) = 0;
119 
120     // Returns the authentication type used for a user.
121     virtual proximity_auth::mojom::AuthType GetAuthType(
122         const AccountId& account_id) const = 0;
123 
124     // Returns the type of the screen -- a signin or a lock screen.
125     virtual ScreenType GetScreenType() const = 0;
126 
127     // Unlocks from easy unlock app for a user.
128     virtual void Unlock(const AccountId& account_id) = 0;
129 
130     // Attempts to login the user using an easy unlock key.
131     virtual void AttemptEasySignin(const AccountId& account_id,
132                                    const std::string& secret,
133                                    const std::string& key_label) = 0;
134 
135    protected:
~LockHandler()136     virtual ~LockHandler() {}
137   };
138 
139   class Observer {
140    public:
141     // Invoked after the screen is locked.
142     virtual void OnScreenDidLock(LockHandler::ScreenType screen_type) = 0;
143 
144     // Invoked after the screen lock is dismissed.
145     virtual void OnScreenDidUnlock(LockHandler::ScreenType screen_type) = 0;
146 
147     // Invoked when the user focused on the lock screen changes.
148     virtual void OnFocusedUserChanged(const AccountId& account_id) = 0;
149 
150    protected:
~Observer()151     virtual ~Observer() {}
152   };
153 
154   static ScreenlockBridge* Get();
155 
156   void SetLockHandler(LockHandler* lock_handler);
157   void SetFocusedUser(const AccountId& account_id);
158 
159   bool IsLocked() const;
160   void Lock();
161 
162   // Unlocks the screen for the authenticated user with the given |user_id|.
163   void Unlock(const AccountId& account_id);
164 
165   void AddObserver(Observer* observer);
166   void RemoveObserver(Observer* observer);
167 
lock_handler()168   LockHandler* lock_handler() { return lock_handler_; }
169 
focused_account_id()170   const AccountId& focused_account_id() const { return focused_account_id_; }
171 
172  private:
173   friend struct base::LazyInstanceTraitsBase<ScreenlockBridge>;
174   friend std::default_delete<ScreenlockBridge>;
175 
176   ScreenlockBridge();
177   ~ScreenlockBridge();
178 
179   LockHandler* lock_handler_ = nullptr;  // Not owned
180 
181   // The last focused user's id.
182   AccountId focused_account_id_;
183   base::ObserverList<Observer, true>::Unchecked observers_;
184 
185   DISALLOW_COPY_AND_ASSIGN(ScreenlockBridge);
186 };
187 
188 }  // namespace proximity_auth
189 
190 #endif  // CHROMEOS_COMPONENTS_PROXIMITY_AUTH_SCREENLOCK_BRIDGE_H_
191