Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(192)

Side by Side Diff: chrome/browser/chromeos/arc/arc_auth_service.h

Issue 2507073002: Split ArcSessionManager from ArcAuthService. (Closed)
Patch Set: Split ArcSessionManager from ArcAuthService Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef CHROME_BROWSER_CHROMEOS_ARC_ARC_AUTH_SERVICE_H_ 5 #ifndef CHROME_BROWSER_CHROMEOS_ARC_ARC_AUTH_SERVICE_H_
6 #define CHROME_BROWSER_CHROMEOS_ARC_ARC_AUTH_SERVICE_H_ 6 #define CHROME_BROWSER_CHROMEOS_ARC_ARC_AUTH_SERVICE_H_
7 7
8 #include <memory> 8 #include <memory>
9 #include <ostream>
10 #include <string> 9 #include <string>
11 10
12 #include "base/macros.h" 11 #include "base/macros.h"
13 #include "base/memory/weak_ptr.h" 12 #include "base/memory/weak_ptr.h"
14 #include "base/observer_list.h"
15 #include "base/timer/timer.h"
16 #include "chrome/browser/chromeos/arc/arc_support_host.h" 13 #include "chrome/browser/chromeos/arc/arc_support_host.h"
17 #include "chrome/browser/chromeos/arc/optin/arc_optin_preference_handler_observe r.h"
18 #include "chrome/browser/chromeos/policy/android_management_client.h"
19 #include "components/arc/arc_bridge_service.h"
20 #include "components/arc/arc_service.h" 14 #include "components/arc/arc_service.h"
21 #include "components/arc/common/auth.mojom.h" 15 #include "components/arc/common/auth.mojom.h"
22 #include "components/arc/instance_holder.h" 16 #include "components/arc/instance_holder.h"
23 #include "components/prefs/pref_change_registrar.h"
24 #include "components/sync_preferences/pref_service_syncable_observer.h"
25 #include "components/sync_preferences/synced_pref_observer.h"
26 #include "mojo/public/cpp/bindings/binding.h" 17 #include "mojo/public/cpp/bindings/binding.h"
27 18
28 class ArcAppLauncher;
29 class Profile;
30
31 namespace ash {
32 class ShelfDelegate;
33 }
34
35 namespace user_prefs {
36 class PrefRegistrySyncable;
37 }
38
39 namespace arc { 19 namespace arc {
40 20
41 class ArcAndroidManagementChecker;
42 class ArcAuthCodeFetcher; 21 class ArcAuthCodeFetcher;
43 class ArcAuthContext; 22 class ArcAuthContext;
44 class ArcOptInPreferenceHandler;
45 class ArcRobotAuth; 23 class ArcRobotAuth;
46 enum class ProvisioningResult : int;
47 24
48 // This class proxies the request from the client to fetch an auth code from 25 // Implementation of ARC authorization.
49 // LSO. It lives on the UI thread. 26 // TODO(hidehiko): Move to c/b/c/arc/auth with adding tests.
50 class ArcAuthService : public ArcService, 27 class ArcAuthService : public ArcService,
51 public mojom::AuthHost, 28 public mojom::AuthHost,
52 public ArcBridgeService::Observer,
53 public InstanceHolder<mojom::AuthInstance>::Observer, 29 public InstanceHolder<mojom::AuthInstance>::Observer,
54 public ArcSupportHost::Observer, 30 public ArcSupportHost::Observer {
55 public ArcOptInPreferenceHandlerObserver,
56 public sync_preferences::PrefServiceSyncableObserver,
57 public sync_preferences::SyncedPrefObserver {
58 public: 31 public:
59 // Represents each State of ARC session.
60 // NOT_INITIALIZED: represents the state that the Profile is not yet ready
61 // so that this service is not yet initialized, or Chrome is being shut
62 // down so that this is destroyed.
63 // STOPPED: ARC session is not running, or being terminated.
64 // SHOWING_TERMS_OF_SERVICE: "Terms Of Service" page is shown on ARC support
65 // Chrome app.
66 // CHECKING_ANDROID_MANAGEMENT: Checking Android management status. Note that
67 // the status is checked for each ARC session starting, but this is the
68 // state only for the first boot case (= opt-in case). The second time and
69 // later the management check is running in parallel with ARC session
70 // starting, and in such a case, State is ACTIVE, instead.
71 // FETCHING_CODE: Fetching an auth token. Similar to
72 // CHECKING_ANDROID_MANAGEMENT case, this is only for the first boot case.
73 // In re-auth flow (fetching an auth token while ARC is running), the
74 // State should be ACTIVE.
75 // TODO(hidehiko): Migrate into re-auth flow, then remove this state.
76 // ACTIVE: ARC is running.
77 //
78 // State transition should be as follows:
79 //
80 // NOT_INITIALIZED -> STOPPED: when the primary Profile gets ready.
81 // ...(any)... -> NOT_INITIALIZED: when the Chrome is being shutdown.
82 // ...(any)... -> STOPPED: on error.
83 //
84 // In the first boot case (no OOBE case):
85 // STOPPED -> SHOWING_TERMS_OF_SERVICE: when arc.enabled preference is set.
86 // SHOWING_TERMS_OF_SERVICE -> CHECKING_ANDROID_MANAGEMENT: when a user
87 // agree with "Terms Of Service"
88 // CHECKING_ANDROID_MANAGEMENT -> FETCHING_CODE: when Android management
89 // check passes.
90 // FETCHING_CODE -> ACTIVE: when the auth token is successfully fetched.
91 //
92 // In the first boot case (OOBE case):
93 // STOPPED -> FETCHING_CODE: When arc.enabled preference is set.
94 // FETCHING_CODE -> ACTIVE: when the auth token is successfully fetched.
95 //
96 // In the second (or later) boot case:
97 // STOPPED -> ACTIVE: when arc.enabled preference is checked that it is
98 // true. Practically, this is when the primary Profile gets ready.
99 enum class State {
100 NOT_INITIALIZED,
101 STOPPED,
102 SHOWING_TERMS_OF_SERVICE,
103 CHECKING_ANDROID_MANAGEMENT,
104 ACTIVE,
105 };
106
107 class Observer {
108 public:
109 virtual ~Observer() = default;
110
111 // Called to notify that ARC bridge is shut down.
112 virtual void OnShutdownBridge() {}
113
114 // Called to notify that ARC enabled state has been updated.
115 virtual void OnOptInEnabled(bool enabled) {}
116
117 // Called to notify that ARC has been initialized successfully.
118 virtual void OnInitialStart() {}
119 };
120
121 explicit ArcAuthService(ArcBridgeService* bridge_service); 32 explicit ArcAuthService(ArcBridgeService* bridge_service);
122 ~ArcAuthService() override; 33 ~ArcAuthService() override;
123 34
124 static ArcAuthService* Get(); 35 // This is introduced to work with existing tests.
125 36 // TODO(crbug.com/664095): Clean up the test and remove this method.
126 // It is called from chrome/browser/prefs/browser_prefs.cc. 37 static ArcAuthService* GetForTest();
127 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry);
128
129 static void DisableUIForTesting();
130 static void SetShelfDelegateForTesting(ash::ShelfDelegate* shelf_delegate);
131
132 // Checks if OptIn verification was disabled by switch in command line.
133 static bool IsOptInVerificationDisabled();
134
135 static void EnableCheckAndroidManagementForTesting();
136
137 // Returns true if Arc is allowed to run for the given profile.
138 static bool IsAllowedForProfile(const Profile* profile);
139
140 // Returns true if Arc is allowed to run for the current session.
141 bool IsAllowed() const;
142
143 void OnPrimaryUserProfilePrepared(Profile* profile);
144 void Shutdown();
145
146 Profile* profile() { return profile_; }
147 const Profile* profile() const { return profile_; }
148
149 State state() const { return state_; }
150
151 // Adds or removes observers.
152 void AddObserver(Observer* observer);
153 void RemoveObserver(Observer* observer);
154
155 // ArcBridgeService::Observer:
156 void OnBridgeStopped(ArcBridgeService::StopReason reason) override;
157 38
158 // InstanceHolder<mojom::AuthInstance>::Observer: 39 // InstanceHolder<mojom::AuthInstance>::Observer:
159 void OnInstanceReady() override; 40 void OnInstanceReady() override;
41 void OnInstanceClosed() override;
160 42
161 // AuthHost: 43 // mojom::AuthHost:
162 void OnSignInComplete() override; 44 void OnSignInComplete() override;
163 void OnSignInFailed(mojom::ArcSignInFailureReason reason) override; 45 void OnSignInFailed(mojom::ArcSignInFailureReason reason) override;
164 void RequestAccountInfo() override; 46 void RequestAccountInfo() override;
165 47
166 // Deprecated methods: 48 // Deprecated methods:
167 // For security reason this code can be used only once and exists for specific 49 // For security reason this code can be used only once and exists for specific
168 // period of time. 50 // period of time.
169 void GetAuthCodeDeprecated0( 51 void GetAuthCodeDeprecated0(
170 const GetAuthCodeDeprecated0Callback& callback) override; 52 const GetAuthCodeDeprecated0Callback& callback) override;
171 void GetAuthCodeDeprecated( 53 void GetAuthCodeDeprecated(
172 const GetAuthCodeDeprecatedCallback& callback) override; 54 const GetAuthCodeDeprecatedCallback& callback) override;
173 void GetAuthCodeAndAccountTypeDeprecated( 55 void GetAuthCodeAndAccountTypeDeprecated(
174 const GetAuthCodeAndAccountTypeDeprecatedCallback& callback) override; 56 const GetAuthCodeAndAccountTypeDeprecatedCallback& callback) override;
175 // Callback is called with a bool that indicates the management status of the
176 // user.
177 void GetIsAccountManagedDeprecated( 57 void GetIsAccountManagedDeprecated(
178 const GetIsAccountManagedDeprecatedCallback& callback) override; 58 const GetIsAccountManagedDeprecatedCallback& callback) override;
179 59
180 // Called from Arc support platform app when user cancels signing.
181 void CancelAuthCode();
182
183 bool IsArcManaged() const;
184 bool IsArcEnabled() const;
185
186 // This requires Arc to be allowed (|IsAllowed|)for current profile.
187 void EnableArc();
188 void DisableArc();
189
190 // Called from the Chrome OS metrics provider to record Arc.State
191 // periodically.
192 void RecordArcState();
193
194 // sync_preferences::PrefServiceSyncableObserver
195 void OnIsSyncingChanged() override;
196
197 // sync_preferences::SyncedPrefObserver
198 void OnSyncedPrefChanged(const std::string& path, bool from_sync) override;
199
200 // ArcSupportHost::Observer: 60 // ArcSupportHost::Observer:
201 void OnWindowClosed() override;
202 void OnTermsAgreed(bool is_metrics_enabled,
203 bool is_backup_and_restore_enabled,
204 bool is_location_service_enabled) override;
205 void OnAuthSucceeded(const std::string& auth_code) override; 61 void OnAuthSucceeded(const std::string& auth_code) override;
206 void OnRetryClicked() override; 62 void OnRetryClicked() override;
207 void OnSendFeedbackClicked() override;
208
209 // arc::ArcOptInPreferenceHandlerObserver:
210 void OnMetricsModeChanged(bool enabled, bool managed) override;
211 void OnBackupAndRestoreModeChanged(bool enabled, bool managed) override;
212 void OnLocationServicesModeChanged(bool enabled, bool managed) override;
213
214 // Stops ARC without changing ArcEnabled preference.
215 void StopArc();
216
217 // StopArc(), then EnableArc(). Between them data clear may happens.
218 // This is a special method to support enterprise device lost case.
219 // This can be called only when ARC is running.
220 void StopAndEnableArc();
221
222 // Removes the data if ARC is stopped. Otherwise, queue to remove the data
223 // on ARC is stopped.
224 void RemoveArcData();
225
226 ArcSupportHost* support_host() { return support_host_.get(); }
227
228 void StartArc();
229
230 void OnProvisioningFinished(ProvisioningResult result);
231 63
232 private: 64 private:
233 using AccountInfoCallback = base::Callback<void(mojom::AccountInfoPtr)>; 65 using AccountInfoCallback = base::Callback<void(mojom::AccountInfoPtr)>;
234 class AccountInfoNotifier; 66 class AccountInfoNotifier;
235 67
236 // TODO(hidehiko): move UI methods/fields to ArcSupportHost. 68 // Starts to request account info.
237 void SetState(State state);
238 void ShutdownBridge();
239 void OnOptInPreferenceChanged();
240 void StartUI();
241 void OnAndroidManagementPassed();
242 void OnArcDataRemoved(bool success);
243 void OnArcSignInTimeout();
244 void FetchAuthCode();
245 void PrepareContextForAuthCodeRequest();
246 void RequestAccountInfoInternal( 69 void RequestAccountInfoInternal(
247 std::unique_ptr<AccountInfoNotifier> account_info_notifier); 70 std::unique_ptr<AccountInfoNotifier> account_info_notifier);
248 void OnAccountInfoReady(mojom::AccountInfoPtr account_info); 71 void OnAccountInfoReady(mojom::AccountInfoPtr account_info);
249 72
250 // Callback for Robot auth in Kiosk mode. 73 // Callback for Robot auth in Kiosk mode.
251 void OnRobotAuthCodeFetched(const std::string& auth_code); 74 void OnRobotAuthCodeFetched(const std::string& auth_code);
252 75
253 // Callback for automatic auth code fetching when --arc-user-auth-endpoint 76 // Callback for automatic auth code fetching when --arc-user-auth-endpoint
254 // flag is set. 77 // flag is set.
255 void OnAuthCodeFetched(const std::string& auth_code); 78 void OnAuthCodeFetched(const std::string& auth_code);
256 79
257 // Common procedure across LSO auth code fetching, automatic auth code 80 // Common procedure across LSO auth code fetching, automatic auth code
258 // fetching, and Robot auth. 81 // fetching, and Robot auth.
259 void OnAuthCodeObtained(const std::string& auth_code); 82 void OnAuthCodeObtained(const std::string& auth_code);
260 83
261 void StartArcAndroidManagementCheck(); 84 mojo::Binding<mojom::AuthHost> binding_;
262
263 // Called when the Android management check is done in opt-in flow or
264 // re-auth flow.
265 void OnAndroidManagementChecked(
266 policy::AndroidManagementClient::Result result);
267
268 // Called when the background Android management check is done. It is
269 // triggered when the second or later ARC boot timing.
270 void OnBackgroundAndroidManagementChecked(
271 policy::AndroidManagementClient::Result result);
272
273 // Unowned pointer. Keeps current profile.
274 Profile* profile_ = nullptr;
275
276 // Registrar used to monitor ARC enabled state.
277 PrefChangeRegistrar pref_change_registrar_;
278
279 mojo::Binding<AuthHost> binding_;
280 State state_ = State::NOT_INITIALIZED;
281 base::ObserverList<Observer> observer_list_;
282 std::unique_ptr<ArcAppLauncher> playstore_launcher_;
283 bool clear_required_ = false;
284 bool reenable_arc_ = false;
285 base::OneShotTimer arc_sign_in_timer_;
286
287 // Notifies the correct callback whenever the auth_code is ready.
288 std::unique_ptr<AccountInfoNotifier> account_info_notifier_;
289
290 // Temporarily keeps the ArcSupportHost instance.
291 // This should be moved to ArcSessionManager when the refactoring is
292 // done.
293 std::unique_ptr<ArcSupportHost> support_host_;
294 // Handles preferences and metrics mode.
295 std::unique_ptr<arc::ArcOptInPreferenceHandler> preference_handler_;
296 85
297 std::unique_ptr<ArcAuthContext> context_; 86 std::unique_ptr<ArcAuthContext> context_;
298 std::unique_ptr<ArcAuthCodeFetcher> auth_code_fetcher_; 87 std::unique_ptr<ArcAuthCodeFetcher> auth_code_fetcher_;
299 std::unique_ptr<ArcAndroidManagementChecker> android_management_checker_;
300 std::unique_ptr<ArcRobotAuth> arc_robot_auth_; 88 std::unique_ptr<ArcRobotAuth> arc_robot_auth_;
301 89
302 base::Time sign_in_time_; 90 std::unique_ptr<AccountInfoNotifier> notifier_;
303 91
304 base::WeakPtrFactory<ArcAuthService> weak_ptr_factory_; 92 base::WeakPtrFactory<ArcAuthService> weak_ptr_factory_;
305 93
306 DISALLOW_COPY_AND_ASSIGN(ArcAuthService); 94 DISALLOW_COPY_AND_ASSIGN(ArcAuthService);
307 }; 95 };
308 96
309 // Outputs the stringified |state| to |os|. This is only for logging purposes.
310 std::ostream& operator<<(std::ostream& os, const ArcAuthService::State& state);
311
312 } // namespace arc 97 } // namespace arc
313 98
314 #endif // CHROME_BROWSER_CHROMEOS_ARC_ARC_AUTH_SERVICE_H_ 99 #endif // CHROME_BROWSER_CHROMEOS_ARC_ARC_AUTH_SERVICE_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698