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

Side by Side Diff: chrome/browser/chromeos/arc/arc_session_manager.cc

Issue 2507073002: Split ArcSessionManager from ArcAuthService. (Closed)
Patch Set: Fix rebase mistake 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 2015 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 #include "chrome/browser/chromeos/arc/arc_auth_service.h" 5 #include "chrome/browser/chromeos/arc/arc_session_manager.h"
6 6
7 #include <utility> 7 #include <utility>
8 8
9 #include "ash/common/shelf/shelf_delegate.h" 9 #include "ash/common/shelf/shelf_delegate.h"
10 #include "ash/common/wm_shell.h" 10 #include "ash/common/wm_shell.h"
11 #include "base/bind.h" 11 #include "base/bind.h"
12 #include "base/callback_helpers.h" 12 #include "base/callback_helpers.h"
13 #include "base/command_line.h" 13 #include "base/command_line.h"
14 #include "base/logging.h" 14 #include "base/logging.h"
15 #include "base/strings/string16.h" 15 #include "base/strings/string16.h"
(...skipping 27 matching lines...) Expand all
43 #include "components/prefs/pref_service.h" 43 #include "components/prefs/pref_service.h"
44 #include "components/sync_preferences/pref_service_syncable.h" 44 #include "components/sync_preferences/pref_service_syncable.h"
45 #include "components/user_manager/user.h" 45 #include "components/user_manager/user.h"
46 #include "content/public/browser/browser_thread.h" 46 #include "content/public/browser/browser_thread.h"
47 #include "extensions/browser/extension_prefs.h" 47 #include "extensions/browser/extension_prefs.h"
48 48
49 namespace arc { 49 namespace arc {
50 50
51 namespace { 51 namespace {
52 52
53 constexpr size_t kMinVersionForOnAccountInfoReady = 5;
54
55 // Weak pointer. This class is owned by ArcServiceManager. 53 // Weak pointer. This class is owned by ArcServiceManager.
56 ArcAuthService* g_arc_auth_service = nullptr; 54 ArcSessionManager* g_arc_session_manager = nullptr;
57 55
58 // Skip creating UI in unit tests 56 // Skip creating UI in unit tests
59 bool g_disable_ui_for_testing = false; 57 bool g_disable_ui_for_testing = false;
60 58
61 // Use specified ash::ShelfDelegate for unit tests. 59 // Use specified ash::ShelfDelegate for unit tests.
62 ash::ShelfDelegate* g_shelf_delegate_for_testing = nullptr; 60 ash::ShelfDelegate* g_shelf_delegate_for_testing = nullptr;
63 61
64 // The Android management check is disabled by default, it's used only for 62 // The Android management check is disabled by default, it's used only for
65 // testing. 63 // testing.
66 bool g_enable_check_android_management_for_testing = false; 64 bool g_enable_check_android_management_for_testing = false;
67 65
68 // Maximum amount of time we'll wait for ARC to finish booting up. Once this 66 // Maximum amount of time we'll wait for ARC to finish booting up. Once this
69 // timeout expires, keep ARC running in case the user wants to file feedback, 67 // timeout expires, keep ARC running in case the user wants to file feedback,
70 // but present the UI to try again. 68 // but present the UI to try again.
71 constexpr base::TimeDelta kArcSignInTimeout = base::TimeDelta::FromMinutes(5); 69 constexpr base::TimeDelta kArcSignInTimeout = base::TimeDelta::FromMinutes(5);
72 70
73 ash::ShelfDelegate* GetShelfDelegate() { 71 ash::ShelfDelegate* GetShelfDelegate() {
74 if (g_shelf_delegate_for_testing) 72 if (g_shelf_delegate_for_testing)
75 return g_shelf_delegate_for_testing; 73 return g_shelf_delegate_for_testing;
76 if (ash::WmShell::HasInstance()) { 74 if (ash::WmShell::HasInstance()) {
77 DCHECK(ash::WmShell::Get()->shelf_delegate()); 75 DCHECK(ash::WmShell::Get()->shelf_delegate());
78 return ash::WmShell::Get()->shelf_delegate(); 76 return ash::WmShell::Get()->shelf_delegate();
79 } 77 }
80 return nullptr; 78 return nullptr;
81 } 79 }
82 80
83 ProvisioningResult ConvertArcSignInFailureReasonToProvisioningResult(
84 mojom::ArcSignInFailureReason reason) {
85 using ArcSignInFailureReason = mojom::ArcSignInFailureReason;
86
87 #define MAP_PROVISIONING_RESULT(name) \
88 case ArcSignInFailureReason::name: \
89 return ProvisioningResult::name
90
91 switch (reason) {
92 MAP_PROVISIONING_RESULT(UNKNOWN_ERROR);
93 MAP_PROVISIONING_RESULT(MOJO_VERSION_MISMATCH);
94 MAP_PROVISIONING_RESULT(MOJO_CALL_TIMEOUT);
95 MAP_PROVISIONING_RESULT(DEVICE_CHECK_IN_FAILED);
96 MAP_PROVISIONING_RESULT(DEVICE_CHECK_IN_TIMEOUT);
97 MAP_PROVISIONING_RESULT(DEVICE_CHECK_IN_INTERNAL_ERROR);
98 MAP_PROVISIONING_RESULT(GMS_NETWORK_ERROR);
99 MAP_PROVISIONING_RESULT(GMS_SERVICE_UNAVAILABLE);
100 MAP_PROVISIONING_RESULT(GMS_BAD_AUTHENTICATION);
101 MAP_PROVISIONING_RESULT(GMS_SIGN_IN_FAILED);
102 MAP_PROVISIONING_RESULT(GMS_SIGN_IN_TIMEOUT);
103 MAP_PROVISIONING_RESULT(GMS_SIGN_IN_INTERNAL_ERROR);
104 MAP_PROVISIONING_RESULT(CLOUD_PROVISION_FLOW_FAILED);
105 MAP_PROVISIONING_RESULT(CLOUD_PROVISION_FLOW_TIMEOUT);
106 MAP_PROVISIONING_RESULT(CLOUD_PROVISION_FLOW_INTERNAL_ERROR);
107 }
108 #undef MAP_PROVISIONING_RESULT
109
110 NOTREACHED() << "unknown reason: " << static_cast<int>(reason);
111 return ProvisioningResult::UNKNOWN_ERROR;
112 }
113
114 bool IsArcKioskMode() {
115 return user_manager::UserManager::Get()->IsLoggedInAsArcKioskApp();
116 }
117
118 mojom::ChromeAccountType GetAccountType() {
119 if (IsArcKioskMode())
120 return mojom::ChromeAccountType::ROBOT_ACCOUNT;
121 return mojom::ChromeAccountType::USER_ACCOUNT;
122 }
123
124 } // namespace 81 } // namespace
125 82
126 // TODO(lhchavez): Get rid of this class once we can safely remove all the 83 ArcSessionManager::ArcSessionManager(ArcBridgeService* bridge_service)
127 // deprecated interfaces and only need to care about one type of callback. 84 : ArcService(bridge_service), weak_ptr_factory_(this) {
128 class ArcAuthService::AccountInfoNotifier {
129 public:
130 explicit AccountInfoNotifier(
131 const GetAuthCodeDeprecatedCallback& auth_callback)
132 : callback_type_(CallbackType::AUTH_CODE),
133 auth_callback_(auth_callback) {}
134
135 explicit AccountInfoNotifier(
136 const GetAuthCodeAndAccountTypeDeprecatedCallback& auth_account_callback)
137 : callback_type_(CallbackType::AUTH_CODE_AND_ACCOUNT),
138 auth_account_callback_(auth_account_callback) {}
139
140 explicit AccountInfoNotifier(const AccountInfoCallback& account_info_callback)
141 : callback_type_(CallbackType::ACCOUNT_INFO),
142 account_info_callback_(account_info_callback) {}
143
144 void Notify(bool is_enforced,
145 const std::string& auth_code,
146 mojom::ChromeAccountType account_type,
147 bool is_managed) {
148 switch (callback_type_) {
149 case CallbackType::AUTH_CODE:
150 DCHECK(!auth_callback_.is_null());
151 auth_callback_.Run(auth_code, is_enforced);
152 break;
153 case CallbackType::AUTH_CODE_AND_ACCOUNT:
154 DCHECK(!auth_account_callback_.is_null());
155 auth_account_callback_.Run(auth_code, is_enforced, account_type);
156 break;
157 case CallbackType::ACCOUNT_INFO:
158 DCHECK(!account_info_callback_.is_null());
159 mojom::AccountInfoPtr account_info = mojom::AccountInfo::New();
160 if (!is_enforced) {
161 account_info->auth_code = base::nullopt;
162 } else {
163 account_info->auth_code = auth_code;
164 }
165 account_info->account_type = account_type;
166 account_info->is_managed = is_managed;
167 account_info_callback_.Run(std::move(account_info));
168 break;
169 }
170 }
171
172 private:
173 enum class CallbackType { AUTH_CODE, AUTH_CODE_AND_ACCOUNT, ACCOUNT_INFO };
174
175 const CallbackType callback_type_;
176 const GetAuthCodeDeprecatedCallback auth_callback_;
177 const GetAuthCodeAndAccountTypeDeprecatedCallback auth_account_callback_;
178 const AccountInfoCallback account_info_callback_;
179 };
180
181 ArcAuthService::ArcAuthService(ArcBridgeService* bridge_service)
182 : ArcService(bridge_service), binding_(this), weak_ptr_factory_(this) {
183 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 85 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
184 DCHECK(!g_arc_auth_service); 86 DCHECK(!g_arc_session_manager);
185 87 g_arc_session_manager = this;
186 g_arc_auth_service = this;
187 88
188 arc_bridge_service()->AddObserver(this); 89 arc_bridge_service()->AddObserver(this);
189 arc_bridge_service()->auth()->AddObserver(this);
190 } 90 }
191 91
192 ArcAuthService::~ArcAuthService() { 92 ArcSessionManager::~ArcSessionManager() {
193 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 93 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
194 DCHECK_EQ(this, g_arc_auth_service);
195 94
196 Shutdown(); 95 Shutdown();
197 arc_bridge_service()->auth()->RemoveObserver(this);
198 arc_bridge_service()->RemoveObserver(this); 96 arc_bridge_service()->RemoveObserver(this);
199 97
200 g_arc_auth_service = nullptr; 98 DCHECK_EQ(this, g_arc_session_manager);
99 g_arc_session_manager = nullptr;
201 } 100 }
202 101
203 // static 102 // static
204 ArcAuthService* ArcAuthService::Get() { 103 ArcSessionManager* ArcSessionManager::Get() {
205 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 104 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
206 return g_arc_auth_service; 105 return g_arc_session_manager;
207 } 106 }
208 107
209 // static 108 // static
210 void ArcAuthService::RegisterProfilePrefs( 109 void ArcSessionManager::RegisterProfilePrefs(
211 user_prefs::PrefRegistrySyncable* registry) { 110 user_prefs::PrefRegistrySyncable* registry) {
212 // TODO(dspaid): Implement a mechanism to allow this to sync on first boot 111 // TODO(dspaid): Implement a mechanism to allow this to sync on first boot
213 // only. 112 // only.
214 registry->RegisterBooleanPref(prefs::kArcEnabled, false); 113 registry->RegisterBooleanPref(prefs::kArcEnabled, false);
215 registry->RegisterBooleanPref(prefs::kArcSignedIn, false); 114 registry->RegisterBooleanPref(prefs::kArcSignedIn, false);
216 registry->RegisterBooleanPref(prefs::kArcTermsAccepted, false); 115 registry->RegisterBooleanPref(prefs::kArcTermsAccepted, false);
217 registry->RegisterBooleanPref(prefs::kArcBackupRestoreEnabled, true); 116 registry->RegisterBooleanPref(prefs::kArcBackupRestoreEnabled, true);
218 registry->RegisterBooleanPref(prefs::kArcLocationServiceEnabled, true); 117 registry->RegisterBooleanPref(prefs::kArcLocationServiceEnabled, true);
219 } 118 }
220 119
221 // static 120 // static
222 void ArcAuthService::DisableUIForTesting() { 121 void ArcSessionManager::DisableUIForTesting() {
223 g_disable_ui_for_testing = true; 122 g_disable_ui_for_testing = true;
224 } 123 }
225 124
226 // static 125 // static
227 void ArcAuthService::SetShelfDelegateForTesting( 126 void ArcSessionManager::SetShelfDelegateForTesting(
228 ash::ShelfDelegate* shelf_delegate) { 127 ash::ShelfDelegate* shelf_delegate) {
229 g_shelf_delegate_for_testing = shelf_delegate; 128 g_shelf_delegate_for_testing = shelf_delegate;
230 } 129 }
231 130
232 // static 131 // static
233 bool ArcAuthService::IsOptInVerificationDisabled() { 132 bool ArcSessionManager::IsOptInVerificationDisabled() {
234 return base::CommandLine::ForCurrentProcess()->HasSwitch( 133 return base::CommandLine::ForCurrentProcess()->HasSwitch(
235 chromeos::switches::kDisableArcOptInVerification); 134 chromeos::switches::kDisableArcOptInVerification);
236 } 135 }
237 136
238 // static 137 // static
239 void ArcAuthService::EnableCheckAndroidManagementForTesting() { 138 void ArcSessionManager::EnableCheckAndroidManagementForTesting() {
240 g_enable_check_android_management_for_testing = true; 139 g_enable_check_android_management_for_testing = true;
241 } 140 }
242 141
243 // static 142 // static
244 bool ArcAuthService::IsAllowedForProfile(const Profile* profile) { 143 bool ArcSessionManager::IsAllowedForProfile(const Profile* profile) {
245 if (!ArcBridgeService::GetEnabled(base::CommandLine::ForCurrentProcess())) { 144 if (!ArcBridgeService::GetEnabled(base::CommandLine::ForCurrentProcess())) {
246 VLOG(1) << "Arc is not enabled."; 145 VLOG(1) << "Arc is not enabled.";
247 return false; 146 return false;
248 } 147 }
249 148
250 if (!profile) { 149 if (!profile) {
251 VLOG(1) << "ARC is not supported for systems without profile."; 150 VLOG(1) << "ARC is not supported for systems without profile.";
252 return false; 151 return false;
253 } 152 }
254 153
(...skipping 16 matching lines...) Expand all
271 170
272 if (user_manager::UserManager::Get() 171 if (user_manager::UserManager::Get()
273 ->IsCurrentUserCryptohomeDataEphemeral()) { 172 ->IsCurrentUserCryptohomeDataEphemeral()) {
274 VLOG(2) << "Users with ephemeral data are not supported in Arc."; 173 VLOG(2) << "Users with ephemeral data are not supported in Arc.";
275 return false; 174 return false;
276 } 175 }
277 176
278 return true; 177 return true;
279 } 178 }
280 179
281 void ArcAuthService::OnInstanceReady() { 180 // static
282 auto* instance = arc_bridge_service()->auth()->GetInstanceForMethod("Init"); 181 bool ArcSessionManager::IsArcKioskMode() {
283 DCHECK(instance); 182 return user_manager::UserManager::Get()->IsLoggedInAsArcKioskApp();
284 instance->Init(binding_.CreateInterfacePtrAndBind());
285 } 183 }
286 184
287 void ArcAuthService::OnBridgeStopped(ArcBridgeService::StopReason reason) { 185 void ArcSessionManager::OnBridgeStopped(ArcBridgeService::StopReason reason) {
288 // TODO(crbug.com/625923): Use |reason| to report more detailed errors. 186 // TODO(crbug.com/625923): Use |reason| to report more detailed errors.
289 if (arc_sign_in_timer_.IsRunning()) { 187 if (arc_sign_in_timer_.IsRunning()) {
290 OnProvisioningFinished(ProvisioningResult::ARC_STOPPED); 188 OnProvisioningFinished(ProvisioningResult::ARC_STOPPED);
291 } 189 }
292 190
293 if (clear_required_) { 191 if (clear_required_) {
294 // This should be always true, but just in case as this is looked at 192 // This should be always true, but just in case as this is looked at
295 // inside RemoveArcData() at first. 193 // inside RemoveArcData() at first.
296 DCHECK(arc_bridge_service()->stopped()); 194 DCHECK(arc_bridge_service()->stopped());
297 RemoveArcData(); 195 RemoveArcData();
298 } else { 196 } else {
299 // To support special "Stop and enable ARC" procedure for enterprise, 197 // To support special "Stop and enable ARC" procedure for enterprise,
300 // here call OnArcDataRemoved(true) as if the data removal is successfully 198 // here call OnArcDataRemoved(true) as if the data removal is successfully
301 // done. 199 // done.
302 // TODO(hidehiko): Restructure the code. crbug.com/665316 200 // TODO(hidehiko): Restructure the code. crbug.com/665316
303 base::ThreadTaskRunnerHandle::Get()->PostTask( 201 base::ThreadTaskRunnerHandle::Get()->PostTask(
304 FROM_HERE, base::Bind(&ArcAuthService::OnArcDataRemoved, 202 FROM_HERE, base::Bind(&ArcSessionManager::OnArcDataRemoved,
305 weak_ptr_factory_.GetWeakPtr(), true)); 203 weak_ptr_factory_.GetWeakPtr(), true));
306 } 204 }
307 } 205 }
308 206
309 void ArcAuthService::RemoveArcData() { 207 void ArcSessionManager::RemoveArcData() {
310 if (!arc_bridge_service()->stopped()) { 208 if (!arc_bridge_service()->stopped()) {
311 // Just set a flag. On bridge stopped, this will be re-called, 209 // Just set a flag. On bridge stopped, this will be re-called,
312 // then session manager should remove the data. 210 // then session manager should remove the data.
313 clear_required_ = true; 211 clear_required_ = true;
314 return; 212 return;
315 } 213 }
316 clear_required_ = false; 214 clear_required_ = false;
317 chromeos::DBusThreadManager::Get()->GetSessionManagerClient()->RemoveArcData( 215 chromeos::DBusThreadManager::Get()->GetSessionManagerClient()->RemoveArcData(
318 cryptohome::Identification( 216 cryptohome::Identification(
319 multi_user_util::GetAccountIdFromProfile(profile_)), 217 multi_user_util::GetAccountIdFromProfile(profile_)),
320 base::Bind(&ArcAuthService::OnArcDataRemoved, 218 base::Bind(&ArcSessionManager::OnArcDataRemoved,
321 weak_ptr_factory_.GetWeakPtr())); 219 weak_ptr_factory_.GetWeakPtr()));
322 } 220 }
323 221
324 void ArcAuthService::OnArcDataRemoved(bool success) { 222 void ArcSessionManager::OnArcDataRemoved(bool success) {
325 LOG_IF(ERROR, !success) << "Required ARC user data wipe failed."; 223 LOG_IF(ERROR, !success) << "Required ARC user data wipe failed.";
326 224
327 // Here check if |reenable_arc_| is marked or not. 225 // Here check if |reenable_arc_| is marked or not.
328 // The only case this happens should be in the special case for enterprise 226 // The only case this happens should be in the special case for enterprise
329 // "on managed lost" case. In that case, OnBridgeStopped() should trigger 227 // "on managed lost" case. In that case, OnBridgeStopped() should trigger
330 // the RemoveArcData(), then this. 228 // the RemoveArcData(), then this.
331 // TODO(hidehiko): Restructure the code. 229 // TODO(hidehiko): Restructure the code.
332 if (!reenable_arc_) 230 if (!reenable_arc_)
333 return; 231 return;
334 232
335 // Restart ARC anyway. Let the enterprise reporting instance decide whether 233 // Restart ARC anyway. Let the enterprise reporting instance decide whether
336 // the ARC user data wipe is still required or not. 234 // the ARC user data wipe is still required or not.
337 reenable_arc_ = false; 235 reenable_arc_ = false;
338 VLOG(1) << "Reenable ARC"; 236 VLOG(1) << "Reenable ARC";
339 EnableArc(); 237 EnableArc();
340 } 238 }
341 239
342 void ArcAuthService::GetAuthCodeDeprecated0( 240 void ArcSessionManager::OnProvisioningFinished(ProvisioningResult result) {
343 const GetAuthCodeDeprecated0Callback& callback) {
344 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
345 NOTREACHED() << "GetAuthCodeDeprecated0() should no longer be callable";
346 }
347
348 void ArcAuthService::GetAuthCodeDeprecated(
349 const GetAuthCodeDeprecatedCallback& callback) {
350 // For robot account we must use RequestAccountInfo because it allows
351 // to specify account type.
352 DCHECK(!IsArcKioskMode());
353 RequestAccountInfoInternal(
354 base::MakeUnique<ArcAuthService::AccountInfoNotifier>(callback));
355 }
356
357 void ArcAuthService::GetAuthCodeAndAccountTypeDeprecated(
358 const GetAuthCodeAndAccountTypeDeprecatedCallback& callback) {
359 RequestAccountInfoInternal(
360 base::MakeUnique<ArcAuthService::AccountInfoNotifier>(callback));
361 }
362
363 void ArcAuthService::RequestAccountInfo() {
364 RequestAccountInfoInternal(
365 base::MakeUnique<ArcAuthService::AccountInfoNotifier>(
366 base::Bind(&ArcAuthService::OnAccountInfoReady,
367 weak_ptr_factory_.GetWeakPtr())));
368 }
369
370 void ArcAuthService::OnAccountInfoReady(mojom::AccountInfoPtr account_info) {
371 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
372 auto* instance = arc_bridge_service()->auth()->GetInstanceForMethod(
373 "OnAccountInfoReady", kMinVersionForOnAccountInfoReady);
374 DCHECK(instance);
375 instance->OnAccountInfoReady(std::move(account_info));
376 }
377
378 void ArcAuthService::RequestAccountInfoInternal(
379 std::unique_ptr<ArcAuthService::AccountInfoNotifier>
380 account_info_notifier) {
381 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
382 // No other auth code-related operation may be in progress.
383 DCHECK(!account_info_notifier_);
384
385 if (IsOptInVerificationDisabled()) {
386 account_info_notifier->Notify(false /* = is_enforced */, std::string(),
387 GetAccountType(),
388 policy_util::IsAccountManaged(profile_));
389 return;
390 }
391
392 // Hereafter asynchronous operation. Remember the notifier.
393 account_info_notifier_ = std::move(account_info_notifier);
394
395 // In Kiosk mode, use Robot auth code fetching.
396 if (IsArcKioskMode()) {
397 arc_robot_auth_.reset(new ArcRobotAuth());
398 arc_robot_auth_->FetchRobotAuthCode(
399 base::Bind(&ArcAuthService::OnRobotAuthCodeFetched,
400 weak_ptr_factory_.GetWeakPtr()));
401 return;
402 }
403
404 // If endpoint is passed via command line flag, use automatic auth code
405 // fetching.
406 const base::CommandLine* command_line =
407 base::CommandLine::ForCurrentProcess();
408 if (command_line->HasSwitch(chromeos::switches::kArcUseAuthEndpoint)) {
409 std::string auth_endpoint = command_line->GetSwitchValueASCII(
410 chromeos::switches::kArcUseAuthEndpoint);
411 if (!auth_endpoint.empty()) {
412 DCHECK(!auth_code_fetcher_);
413 auth_code_fetcher_ = base::MakeUnique<ArcAuthCodeFetcher>(
414 profile_, context_.get(), auth_endpoint);
415 auth_code_fetcher_->Fetch(base::Bind(&ArcAuthService::OnAuthCodeFetched,
416 weak_ptr_factory_.GetWeakPtr()));
417 return;
418 }
419 }
420
421 // Otherwise, show LSO page to user after HTTP context preparation, and let
422 // them click "Sign in" button.
423 DCHECK(context_);
424 context_->Prepare(base::Bind(&ArcAuthService::OnContextPrepared,
425 weak_ptr_factory_.GetWeakPtr()));
426 }
427
428 void ArcAuthService::OnContextPrepared(
429 net::URLRequestContextGetter* request_context_getter) {
430 if (!support_host_)
431 return;
432
433 if (request_context_getter) {
434 support_host_->ShowLso();
435 } else {
436 support_host_->ShowError(ArcSupportHost::Error::SIGN_IN_NETWORK_ERROR,
437 false);
438 }
439 }
440
441 void ArcAuthService::OnRobotAuthCodeFetched(
442 const std::string& robot_auth_code) {
443 // We fetching robot auth code for ARC kiosk only.
444 DCHECK(IsArcKioskMode());
445
446 // Current instance of ArcRobotAuth became useless.
447 arc_robot_auth_.reset();
448
449 if (robot_auth_code.empty()) {
450 VLOG(1) << "Robot account auth code fetching error";
451 // Log out the user. All the cleanup will be done in Shutdown() method.
452 // The callback is not called because auth code is empty.
453 chrome::AttemptUserExit();
454 return;
455 }
456
457 OnAuthCodeObtained(robot_auth_code);
458 }
459
460 void ArcAuthService::OnAuthCodeFetched(const std::string& auth_code) {
461 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
462 auth_code_fetcher_.reset();
463
464 if (auth_code.empty()) {
465 OnProvisioningFinished(
466 ProvisioningResult::CHROME_SERVER_COMMUNICATION_ERROR);
467 return;
468 }
469
470 OnAuthCodeObtained(auth_code);
471 }
472
473 void ArcAuthService::OnSignInComplete() {
474 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
475 DCHECK_EQ(state_, State::ACTIVE);
476 OnProvisioningFinished(ProvisioningResult::SUCCESS);
477 }
478
479 void ArcAuthService::OnSignInFailed(mojom::ArcSignInFailureReason reason) {
480 OnProvisioningFinished(
481 ConvertArcSignInFailureReasonToProvisioningResult(reason));
482 }
483
484 void ArcAuthService::OnProvisioningFinished(ProvisioningResult result) {
485 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 241 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
486 DCHECK_EQ(state_, State::ACTIVE); 242 DCHECK_EQ(state_, State::ACTIVE);
487 243
488 if (result == ProvisioningResult::CHROME_SERVER_COMMUNICATION_ERROR) { 244 if (result == ProvisioningResult::CHROME_SERVER_COMMUNICATION_ERROR) {
489 // For backwards compatibility, use NETWORK_ERROR for 245 // For backwards compatibility, use NETWORK_ERROR for
490 // CHROME_SERVER_COMMUNICATION_ERROR case. 246 // CHROME_SERVER_COMMUNICATION_ERROR case.
491 UpdateOptInCancelUMA(OptInCancelReason::NETWORK_ERROR); 247 UpdateOptInCancelUMA(OptInCancelReason::NETWORK_ERROR);
492 } else if (!sign_in_time_.is_null()) { 248 } else if (!sign_in_time_.is_null()) {
493 arc_sign_in_timer_.Stop(); 249 arc_sign_in_timer_.Stop();
494 250
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after
573 result == ProvisioningResult::UNKNOWN_ERROR) { 329 result == ProvisioningResult::UNKNOWN_ERROR) {
574 RemoveArcData(); 330 RemoveArcData();
575 } 331 }
576 332
577 // We'll delay shutting down the bridge in this case to allow people to send 333 // We'll delay shutting down the bridge in this case to allow people to send
578 // feedback. 334 // feedback.
579 if (support_host_) 335 if (support_host_)
580 support_host_->ShowError(error, true /* = show send feedback button */); 336 support_host_->ShowError(error, true /* = show send feedback button */);
581 } 337 }
582 338
583 void ArcAuthService::GetIsAccountManagedDeprecated( 339 void ArcSessionManager::SetState(State state) {
584 const GetIsAccountManagedDeprecatedCallback& callback) {
585 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
586
587 callback.Run(policy_util::IsAccountManaged(profile_));
588 }
589
590 void ArcAuthService::SetState(State state) {
591 state_ = state; 340 state_ = state;
592 } 341 }
593 342
594 bool ArcAuthService::IsAllowed() const { 343 bool ArcSessionManager::IsAllowed() const {
595 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 344 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
596 return profile_ != nullptr; 345 return profile_ != nullptr;
597 } 346 }
598 347
599 void ArcAuthService::OnPrimaryUserProfilePrepared(Profile* profile) { 348 void ArcSessionManager::OnPrimaryUserProfilePrepared(Profile* profile) {
600 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 349 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
601 DCHECK(profile && profile != profile_); 350 DCHECK(profile && profile != profile_);
602 351
603 Shutdown(); 352 Shutdown();
604 353
605 if (!IsAllowedForProfile(profile)) 354 if (!IsAllowedForProfile(profile))
606 return; 355 return;
607 356
608 // TODO(khmel): Move this to IsAllowedForProfile. 357 // TODO(khmel): Move this to IsAllowedForProfile.
609 if (policy_util::IsArcDisabledForEnterprise() && 358 if (policy_util::IsArcDisabledForEnterprise() &&
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
642 prefs::kArcEnabled, this); 391 prefs::kArcEnabled, this);
643 392
644 context_.reset(new ArcAuthContext(profile_)); 393 context_.reset(new ArcAuthContext(profile_));
645 394
646 if (!g_disable_ui_for_testing || 395 if (!g_disable_ui_for_testing ||
647 g_enable_check_android_management_for_testing) { 396 g_enable_check_android_management_for_testing) {
648 ArcAndroidManagementChecker::StartClient(); 397 ArcAndroidManagementChecker::StartClient();
649 } 398 }
650 pref_change_registrar_.Init(profile_->GetPrefs()); 399 pref_change_registrar_.Init(profile_->GetPrefs());
651 pref_change_registrar_.Add( 400 pref_change_registrar_.Add(
652 prefs::kArcEnabled, base::Bind(&ArcAuthService::OnOptInPreferenceChanged, 401 prefs::kArcEnabled,
653 weak_ptr_factory_.GetWeakPtr())); 402 base::Bind(&ArcSessionManager::OnOptInPreferenceChanged,
403 weak_ptr_factory_.GetWeakPtr()));
654 if (profile_->GetPrefs()->GetBoolean(prefs::kArcEnabled)) { 404 if (profile_->GetPrefs()->GetBoolean(prefs::kArcEnabled)) {
655 OnOptInPreferenceChanged(); 405 OnOptInPreferenceChanged();
656 } else { 406 } else {
657 RemoveArcData(); 407 RemoveArcData();
658 PrefServiceSyncableFromProfile(profile_)->AddObserver(this); 408 PrefServiceSyncableFromProfile(profile_)->AddObserver(this);
659 OnIsSyncingChanged(); 409 OnIsSyncingChanged();
660 } 410 }
661 } 411 }
662 412
663 void ArcAuthService::OnIsSyncingChanged() { 413 void ArcSessionManager::OnIsSyncingChanged() {
664 sync_preferences::PrefServiceSyncable* const pref_service_syncable = 414 sync_preferences::PrefServiceSyncable* const pref_service_syncable =
665 PrefServiceSyncableFromProfile(profile_); 415 PrefServiceSyncableFromProfile(profile_);
666 if (!pref_service_syncable->IsSyncing()) 416 if (!pref_service_syncable->IsSyncing())
667 return; 417 return;
668 418
669 pref_service_syncable->RemoveObserver(this); 419 pref_service_syncable->RemoveObserver(this);
670 420
671 if (IsArcEnabled()) 421 if (IsArcEnabled())
672 OnOptInPreferenceChanged(); 422 OnOptInPreferenceChanged();
673 } 423 }
674 424
675 void ArcAuthService::Shutdown() { 425 void ArcSessionManager::Shutdown() {
676 ShutdownBridge(); 426 ShutdownBridge();
677 if (support_host_) { 427 if (support_host_) {
678 support_host_->Close(); 428 support_host_->Close();
679 support_host_->RemoveObserver(this); 429 support_host_->RemoveObserver(this);
680 support_host_.reset(); 430 support_host_.reset();
681 } 431 }
682 if (profile_) { 432 if (profile_) {
683 sync_preferences::PrefServiceSyncable* pref_service_syncable = 433 sync_preferences::PrefServiceSyncable* pref_service_syncable =
684 PrefServiceSyncableFromProfile(profile_); 434 PrefServiceSyncableFromProfile(profile_);
685 pref_service_syncable->RemoveObserver(this); 435 pref_service_syncable->RemoveObserver(this);
686 pref_service_syncable->RemoveSyncedPrefObserver(prefs::kArcEnabled, this); 436 pref_service_syncable->RemoveSyncedPrefObserver(prefs::kArcEnabled, this);
687 } 437 }
688 pref_change_registrar_.RemoveAll(); 438 pref_change_registrar_.RemoveAll();
689 context_.reset(); 439 context_.reset();
690 profile_ = nullptr; 440 profile_ = nullptr;
691 arc_robot_auth_.reset();
692 SetState(State::NOT_INITIALIZED); 441 SetState(State::NOT_INITIALIZED);
693 } 442 }
694 443
695 void ArcAuthService::OnSyncedPrefChanged(const std::string& path, 444 void ArcSessionManager::OnSyncedPrefChanged(const std::string& path,
696 bool from_sync) { 445 bool from_sync) {
697 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 446 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
698 447
699 // Update UMA only for local changes 448 // Update UMA only for local changes
700 if (!from_sync) { 449 if (!from_sync) {
701 const bool arc_enabled = 450 const bool arc_enabled =
702 profile_->GetPrefs()->GetBoolean(prefs::kArcEnabled); 451 profile_->GetPrefs()->GetBoolean(prefs::kArcEnabled);
703 UpdateOptInActionUMA(arc_enabled ? OptInActionType::OPTED_IN 452 UpdateOptInActionUMA(arc_enabled ? OptInActionType::OPTED_IN
704 : OptInActionType::OPTED_OUT); 453 : OptInActionType::OPTED_OUT);
705 454
706 if (!arc_enabled && !IsArcManaged()) { 455 if (!arc_enabled && !IsArcManaged()) {
707 ash::ShelfDelegate* shelf_delegate = GetShelfDelegate(); 456 ash::ShelfDelegate* shelf_delegate = GetShelfDelegate();
708 if (shelf_delegate) 457 if (shelf_delegate)
709 shelf_delegate->UnpinAppWithID(ArcSupportHost::kHostAppId); 458 shelf_delegate->UnpinAppWithID(ArcSupportHost::kHostAppId);
710 } 459 }
711 } 460 }
712 } 461 }
713 462
714 void ArcAuthService::StopArc() { 463 void ArcSessionManager::StopArc() {
715 if (state_ != State::STOPPED) { 464 if (state_ != State::STOPPED) {
716 profile_->GetPrefs()->SetBoolean(prefs::kArcSignedIn, false); 465 profile_->GetPrefs()->SetBoolean(prefs::kArcSignedIn, false);
717 profile_->GetPrefs()->SetBoolean(prefs::kArcTermsAccepted, false); 466 profile_->GetPrefs()->SetBoolean(prefs::kArcTermsAccepted, false);
718 } 467 }
719 ShutdownBridge(); 468 ShutdownBridge();
720 if (support_host_) 469 if (support_host_)
721 support_host_->Close(); 470 support_host_->Close();
722 } 471 }
723 472
724 void ArcAuthService::OnOptInPreferenceChanged() { 473 void ArcSessionManager::OnOptInPreferenceChanged() {
725 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 474 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
726 DCHECK(profile_); 475 DCHECK(profile_);
727 476
728 // TODO(dspaid): Move code from OnSyncedPrefChanged into this method. 477 // TODO(dspaid): Move code from OnSyncedPrefChanged into this method.
729 OnSyncedPrefChanged(prefs::kArcEnabled, IsArcManaged()); 478 OnSyncedPrefChanged(prefs::kArcEnabled, IsArcManaged());
730 479
731 const bool arc_enabled = IsArcEnabled(); 480 const bool arc_enabled = IsArcEnabled();
732 for (auto& observer : observer_list_) 481 for (auto& observer : observer_list_)
733 observer.OnOptInEnabled(arc_enabled); 482 observer.OnOptInEnabled(arc_enabled);
734 483
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
771 // Note: Because the callback may be called in synchronous way (i.e. called 520 // Note: Because the callback may be called in synchronous way (i.e. called
772 // on the same stack), StartCheck() needs to be called *after* StartArc(). 521 // on the same stack), StartCheck() needs to be called *after* StartArc().
773 // Otherwise, DisableArc() which may be called in 522 // Otherwise, DisableArc() which may be called in
774 // OnBackgroundAndroidManagementChecked() could be ignored. 523 // OnBackgroundAndroidManagementChecked() could be ignored.
775 if (!g_disable_ui_for_testing || 524 if (!g_disable_ui_for_testing ||
776 g_enable_check_android_management_for_testing) { 525 g_enable_check_android_management_for_testing) {
777 android_management_checker_.reset(new ArcAndroidManagementChecker( 526 android_management_checker_.reset(new ArcAndroidManagementChecker(
778 profile_, context_->token_service(), context_->account_id(), 527 profile_, context_->token_service(), context_->account_id(),
779 true /* retry_on_error */)); 528 true /* retry_on_error */));
780 android_management_checker_->StartCheck( 529 android_management_checker_->StartCheck(
781 base::Bind(&ArcAuthService::OnBackgroundAndroidManagementChecked, 530 base::Bind(&ArcSessionManager::OnBackgroundAndroidManagementChecked,
782 weak_ptr_factory_.GetWeakPtr())); 531 weak_ptr_factory_.GetWeakPtr()));
783 } 532 }
784 } 533 }
785 } 534 }
786 535
787 void ArcAuthService::ShutdownBridge() { 536 void ArcSessionManager::ShutdownBridge() {
788 arc_sign_in_timer_.Stop(); 537 arc_sign_in_timer_.Stop();
789 playstore_launcher_.reset(); 538 playstore_launcher_.reset();
790 account_info_notifier_.reset();
791 android_management_checker_.reset(); 539 android_management_checker_.reset();
792 auth_code_fetcher_.reset();
793 arc_bridge_service()->RequestStop(); 540 arc_bridge_service()->RequestStop();
794 if (state_ != State::NOT_INITIALIZED) 541 if (state_ != State::NOT_INITIALIZED)
795 SetState(State::STOPPED); 542 SetState(State::STOPPED);
796 for (auto& observer : observer_list_) 543 for (auto& observer : observer_list_)
797 observer.OnShutdownBridge(); 544 observer.OnShutdownBridge();
798 } 545 }
799 546
800 void ArcAuthService::AddObserver(Observer* observer) { 547 void ArcSessionManager::AddObserver(Observer* observer) {
801 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 548 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
802 observer_list_.AddObserver(observer); 549 observer_list_.AddObserver(observer);
803 } 550 }
804 551
805 void ArcAuthService::RemoveObserver(Observer* observer) { 552 void ArcSessionManager::RemoveObserver(Observer* observer) {
806 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 553 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
807 observer_list_.RemoveObserver(observer); 554 observer_list_.RemoveObserver(observer);
808 } 555 }
809 556
810 // This is the special method to support enterprise mojo API. 557 // This is the special method to support enterprise mojo API.
811 // TODO(hidehiko): Remove this. 558 // TODO(hidehiko): Remove this.
812 void ArcAuthService::StopAndEnableArc() { 559 void ArcSessionManager::StopAndEnableArc() {
813 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 560 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
814 DCHECK(!arc_bridge_service()->stopped()); 561 DCHECK(!arc_bridge_service()->stopped());
815 reenable_arc_ = true; 562 reenable_arc_ = true;
816 StopArc(); 563 StopArc();
817 } 564 }
818 565
819 void ArcAuthService::StartArc() { 566 void ArcSessionManager::StartArc() {
820 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 567 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
821 arc_bridge_service()->RequestStart(); 568 arc_bridge_service()->RequestStart();
822 SetState(State::ACTIVE); 569 SetState(State::ACTIVE);
823 } 570 }
824 571
825 void ArcAuthService::OnAuthCodeObtained(const std::string& auth_code) { 572 void ArcSessionManager::OnArcSignInTimeout() {
826 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
827 DCHECK(!auth_code.empty());
828
829 account_info_notifier_->Notify(!IsOptInVerificationDisabled(), auth_code,
830 GetAccountType(),
831 policy_util::IsAccountManaged(profile_));
832 account_info_notifier_.reset();
833 }
834
835 void ArcAuthService::OnArcSignInTimeout() {
836 LOG(ERROR) << "Timed out waiting for first sign in."; 573 LOG(ERROR) << "Timed out waiting for first sign in.";
837 OnProvisioningFinished(ProvisioningResult::OVERALL_SIGN_IN_TIMEOUT); 574 OnProvisioningFinished(ProvisioningResult::OVERALL_SIGN_IN_TIMEOUT);
838 } 575 }
839 576
840 void ArcAuthService::CancelAuthCode() { 577 void ArcSessionManager::CancelAuthCode() {
841 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 578 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
842 579
843 if (state_ == State::NOT_INITIALIZED) { 580 if (state_ == State::NOT_INITIALIZED) {
844 NOTREACHED(); 581 NOTREACHED();
845 return; 582 return;
846 } 583 }
847 584
848 // In case |state_| is ACTIVE, UI page can be ARC_LOADING (which means normal 585 // In case |state_| is ACTIVE, UI page can be ARC_LOADING (which means normal
849 // ARC booting) or ERROR (in case ARC can not be started). If ARC is booting 586 // ARC booting) or ERROR (in case ARC can not be started). If ARC is booting
850 // normally don't stop it on progress close. 587 // normally don't stop it on progress close.
(...skipping 12 matching lines...) Expand all
863 } 600 }
864 601
865 StopArc(); 602 StopArc();
866 603
867 if (IsArcManaged()) 604 if (IsArcManaged())
868 return; 605 return;
869 606
870 DisableArc(); 607 DisableArc();
871 } 608 }
872 609
873 bool ArcAuthService::IsArcManaged() const { 610 bool ArcSessionManager::IsArcManaged() const {
874 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 611 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
875 DCHECK(profile_); 612 DCHECK(profile_);
876 return profile_->GetPrefs()->IsManagedPreference(prefs::kArcEnabled); 613 return profile_->GetPrefs()->IsManagedPreference(prefs::kArcEnabled);
877 } 614 }
878 615
879 bool ArcAuthService::IsArcEnabled() const { 616 bool ArcSessionManager::IsArcEnabled() const {
880 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 617 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
881 if (!IsAllowed()) 618 if (!IsAllowed())
882 return false; 619 return false;
883 620
884 DCHECK(profile_); 621 DCHECK(profile_);
885 return profile_->GetPrefs()->GetBoolean(prefs::kArcEnabled); 622 return profile_->GetPrefs()->GetBoolean(prefs::kArcEnabled);
886 } 623 }
887 624
888 void ArcAuthService::EnableArc() { 625 void ArcSessionManager::EnableArc() {
889 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 626 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
890 DCHECK(profile_); 627 DCHECK(profile_);
891 628
892 if (IsArcEnabled()) { 629 if (IsArcEnabled()) {
893 OnOptInPreferenceChanged(); 630 OnOptInPreferenceChanged();
894 return; 631 return;
895 } 632 }
896 633
897 if (!IsArcManaged()) 634 if (!IsArcManaged())
898 profile_->GetPrefs()->SetBoolean(prefs::kArcEnabled, true); 635 profile_->GetPrefs()->SetBoolean(prefs::kArcEnabled, true);
899 } 636 }
900 637
901 void ArcAuthService::DisableArc() { 638 void ArcSessionManager::DisableArc() {
902 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 639 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
903 DCHECK(profile_); 640 DCHECK(profile_);
904 profile_->GetPrefs()->SetBoolean(prefs::kArcEnabled, false); 641 profile_->GetPrefs()->SetBoolean(prefs::kArcEnabled, false);
905 } 642 }
906 643
907 void ArcAuthService::RecordArcState() { 644 void ArcSessionManager::RecordArcState() {
908 // Only record Enabled state if ARC is allowed in the first place, so we do 645 // Only record Enabled state if ARC is allowed in the first place, so we do
909 // not split the ARC population by devices that cannot run ARC. 646 // not split the ARC population by devices that cannot run ARC.
910 if (IsAllowed()) 647 if (IsAllowed())
911 UpdateEnabledStateUMA(IsArcEnabled()); 648 UpdateEnabledStateUMA(IsArcEnabled());
912 } 649 }
913 650
914 void ArcAuthService::StartUI() { 651 void ArcSessionManager::StartUI() {
915 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 652 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
916 653
917 if (!arc_bridge_service()->stopped()) { 654 if (!arc_bridge_service()->stopped()) {
918 // If the user attempts to re-enable ARC while the bridge is still running 655 // If the user attempts to re-enable ARC while the bridge is still running
919 // the user should not be able to continue until the bridge has stopped. 656 // the user should not be able to continue until the bridge has stopped.
920 if (support_host_) { 657 if (support_host_) {
921 support_host_->ShowError( 658 support_host_->ShowError(
922 ArcSupportHost::Error::SIGN_IN_SERVICE_UNAVAILABLE_ERROR, false); 659 ArcSupportHost::Error::SIGN_IN_SERVICE_UNAVAILABLE_ERROR, false);
923 } 660 }
924 return; 661 return;
925 } 662 }
926 663
927 SetState(State::SHOWING_TERMS_OF_SERVICE); 664 SetState(State::SHOWING_TERMS_OF_SERVICE);
928 if (support_host_) 665 if (support_host_)
929 support_host_->ShowTermsOfService(); 666 support_host_->ShowTermsOfService();
930 } 667 }
931 668
932 void ArcAuthService::StartArcAndroidManagementCheck() { 669 void ArcSessionManager::StartArcAndroidManagementCheck() {
933 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 670 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
934 DCHECK(arc_bridge_service()->stopped()); 671 DCHECK(arc_bridge_service()->stopped());
935 DCHECK(state_ == State::SHOWING_TERMS_OF_SERVICE || 672 DCHECK(state_ == State::SHOWING_TERMS_OF_SERVICE ||
936 state_ == State::CHECKING_ANDROID_MANAGEMENT); 673 state_ == State::CHECKING_ANDROID_MANAGEMENT);
937 SetState(State::CHECKING_ANDROID_MANAGEMENT); 674 SetState(State::CHECKING_ANDROID_MANAGEMENT);
938 675
939 android_management_checker_.reset(new ArcAndroidManagementChecker( 676 android_management_checker_.reset(new ArcAndroidManagementChecker(
940 profile_, context_->token_service(), context_->account_id(), 677 profile_, context_->token_service(), context_->account_id(),
941 false /* retry_on_error */)); 678 false /* retry_on_error */));
942 android_management_checker_->StartCheck( 679 android_management_checker_->StartCheck(
943 base::Bind(&ArcAuthService::OnAndroidManagementChecked, 680 base::Bind(&ArcSessionManager::OnAndroidManagementChecked,
944 weak_ptr_factory_.GetWeakPtr())); 681 weak_ptr_factory_.GetWeakPtr()));
945 } 682 }
946 683
947 void ArcAuthService::OnAndroidManagementChecked( 684 void ArcSessionManager::OnAndroidManagementChecked(
948 policy::AndroidManagementClient::Result result) { 685 policy::AndroidManagementClient::Result result) {
949 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 686 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
950 DCHECK_EQ(state_, State::CHECKING_ANDROID_MANAGEMENT); 687 DCHECK_EQ(state_, State::CHECKING_ANDROID_MANAGEMENT);
951 688
952 switch (result) { 689 switch (result) {
953 case policy::AndroidManagementClient::Result::UNMANAGED: 690 case policy::AndroidManagementClient::Result::UNMANAGED:
954 VLOG(1) << "Starting ARC for first sign in."; 691 VLOG(1) << "Starting ARC for first sign in.";
955 sign_in_time_ = base::Time::Now(); 692 sign_in_time_ = base::Time::Now();
956 arc_sign_in_timer_.Start(FROM_HERE, kArcSignInTimeout, 693 arc_sign_in_timer_.Start(
957 base::Bind(&ArcAuthService::OnArcSignInTimeout, 694 FROM_HERE, kArcSignInTimeout,
958 weak_ptr_factory_.GetWeakPtr())); 695 base::Bind(&ArcSessionManager::OnArcSignInTimeout,
696 weak_ptr_factory_.GetWeakPtr()));
959 StartArc(); 697 StartArc();
960 break; 698 break;
961 case policy::AndroidManagementClient::Result::MANAGED: 699 case policy::AndroidManagementClient::Result::MANAGED:
962 ShutdownBridge(); 700 ShutdownBridge();
963 if (support_host_) { 701 if (support_host_) {
964 support_host_->ShowError( 702 support_host_->ShowError(
965 ArcSupportHost::Error::ANDROID_MANAGEMENT_REQUIRED_ERROR, false); 703 ArcSupportHost::Error::ANDROID_MANAGEMENT_REQUIRED_ERROR, false);
966 } 704 }
967 UpdateOptInCancelUMA(OptInCancelReason::ANDROID_MANAGEMENT_REQUIRED); 705 UpdateOptInCancelUMA(OptInCancelReason::ANDROID_MANAGEMENT_REQUIRED);
968 break; 706 break;
969 case policy::AndroidManagementClient::Result::ERROR: 707 case policy::AndroidManagementClient::Result::ERROR:
970 ShutdownBridge(); 708 ShutdownBridge();
971 if (support_host_) { 709 if (support_host_) {
972 support_host_->ShowError( 710 support_host_->ShowError(
973 ArcSupportHost::Error::SERVER_COMMUNICATION_ERROR, false); 711 ArcSupportHost::Error::SERVER_COMMUNICATION_ERROR, false);
974 } 712 }
975 UpdateOptInCancelUMA(OptInCancelReason::NETWORK_ERROR); 713 UpdateOptInCancelUMA(OptInCancelReason::NETWORK_ERROR);
976 break; 714 break;
977 } 715 }
978 } 716 }
979 717
980 void ArcAuthService::OnBackgroundAndroidManagementChecked( 718 void ArcSessionManager::OnBackgroundAndroidManagementChecked(
981 policy::AndroidManagementClient::Result result) { 719 policy::AndroidManagementClient::Result result) {
982 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 720 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
983 switch (result) { 721 switch (result) {
984 case policy::AndroidManagementClient::Result::UNMANAGED: 722 case policy::AndroidManagementClient::Result::UNMANAGED:
985 // Do nothing. ARC should be started already. 723 // Do nothing. ARC should be started already.
986 break; 724 break;
987 case policy::AndroidManagementClient::Result::MANAGED: 725 case policy::AndroidManagementClient::Result::MANAGED:
988 DisableArc(); 726 DisableArc();
989 break; 727 break;
990 case policy::AndroidManagementClient::Result::ERROR: 728 case policy::AndroidManagementClient::Result::ERROR:
991 // This code should not be reached. For background check, 729 // This code should not be reached. For background check,
992 // retry_on_error should be set. 730 // retry_on_error should be set.
993 NOTREACHED(); 731 NOTREACHED();
994 } 732 }
995 } 733 }
996 734
997 void ArcAuthService::OnWindowClosed() { 735 void ArcSessionManager::OnWindowClosed() {
998 DCHECK(support_host_); 736 DCHECK(support_host_);
999 CancelAuthCode(); 737 CancelAuthCode();
1000 } 738 }
1001 739
1002 void ArcAuthService::OnTermsAgreed(bool is_metrics_enabled, 740 void ArcSessionManager::OnTermsAgreed(bool is_metrics_enabled,
1003 bool is_backup_and_restore_enabled, 741 bool is_backup_and_restore_enabled,
1004 bool is_location_service_enabled) { 742 bool is_location_service_enabled) {
1005 DCHECK(support_host_); 743 DCHECK(support_host_);
1006 744
1007 // Terms were accepted 745 // Terms were accepted
1008 profile_->GetPrefs()->SetBoolean(prefs::kArcTermsAccepted, true); 746 profile_->GetPrefs()->SetBoolean(prefs::kArcTermsAccepted, true);
1009 747
1010 // Since this is ARC support's UI event callback, preference_handler_ 748 // Since this is ARC support's UI event callback, preference_handler_
1011 // should be always created (see OnPrimaryUserProfilePrepared()). 749 // should be always created (see OnPrimaryUserProfilePrepared()).
1012 // TODO(hidehiko): Simplify the logic with the code restructuring. 750 // TODO(hidehiko): Simplify the logic with the code restructuring.
1013 DCHECK(preference_handler_); 751 DCHECK(preference_handler_);
1014 preference_handler_->EnableMetrics(is_metrics_enabled); 752 preference_handler_->EnableMetrics(is_metrics_enabled);
1015 preference_handler_->EnableBackupRestore(is_backup_and_restore_enabled); 753 preference_handler_->EnableBackupRestore(is_backup_and_restore_enabled);
1016 preference_handler_->EnableLocationService(is_location_service_enabled); 754 preference_handler_->EnableLocationService(is_location_service_enabled);
1017 support_host_->ShowArcLoading(); 755 support_host_->ShowArcLoading();
1018 StartArcAndroidManagementCheck(); 756 StartArcAndroidManagementCheck();
1019 } 757 }
1020 758
1021 void ArcAuthService::OnAuthSucceeded(const std::string& auth_code) { 759 void ArcSessionManager::OnRetryClicked() {
1022 DCHECK(support_host_);
1023 OnAuthCodeObtained(auth_code);
1024 }
1025
1026 void ArcAuthService::OnRetryClicked() {
1027 DCHECK(support_host_); 760 DCHECK(support_host_);
1028 761
1029 UpdateOptInActionUMA(OptInActionType::RETRY); 762 UpdateOptInActionUMA(OptInActionType::RETRY);
1030 763
1031 // TODO(hidehiko): Simplify the retry logic. 764 // TODO(hidehiko): Simplify the retry logic.
1032 if (!profile_->GetPrefs()->GetBoolean(prefs::kArcTermsAccepted)) { 765 if (!profile_->GetPrefs()->GetBoolean(prefs::kArcTermsAccepted)) {
1033 // If the user has not yet agreed on Terms of Service, then show it. 766 // If the user has not yet agreed on Terms of Service, then show it.
1034 support_host_->ShowTermsOfService(); 767 support_host_->ShowTermsOfService();
1035 } else if (support_host_->ui_page() == ArcSupportHost::UIPage::ERROR && 768 } else if (support_host_->ui_page() == ArcSupportHost::UIPage::ERROR &&
1036 !arc_bridge_service()->stopped()) { 769 !arc_bridge_service()->stopped()) {
1037 // ERROR_WITH_FEEDBACK is set in OnSignInFailed(). In the case, stopping 770 // ERROR_WITH_FEEDBACK is set in OnSignInFailed(). In the case, stopping
1038 // ARC was postponed to contain its internal state into the report. 771 // ARC was postponed to contain its internal state into the report.
1039 // Here, on retry, stop it, then restart. 772 // Here, on retry, stop it, then restart.
1040 DCHECK_EQ(State::ACTIVE, state_); 773 DCHECK_EQ(State::ACTIVE, state_);
1041 support_host_->ShowArcLoading(); 774 support_host_->ShowArcLoading();
1042 ShutdownBridge(); 775 ShutdownBridge();
1043 reenable_arc_ = true; 776 reenable_arc_ = true;
1044 } else if (state_ == State::ACTIVE) { 777 } else if (state_ == State::ACTIVE) {
1045 // This happens when ARC support Chrome app reports an error on "Sign in" 778 // This case is handled in ArcAuthService.
1046 // page. 779 // Do nothing.
1047 DCHECK(context_);
1048 context_->Prepare(base::Bind(&ArcAuthService::OnContextPrepared,
1049 weak_ptr_factory_.GetWeakPtr()));
1050 } else { 780 } else {
1051 // Otherwise, we restart ARC. Note: this is the first boot case. 781 // Otherwise, we restart ARC. Note: this is the first boot case.
1052 // For second or later boot, either ERROR_WITH_FEEDBACK case or ACTIVE 782 // For second or later boot, either ERROR_WITH_FEEDBACK case or ACTIVE
1053 // case must hit. 783 // case must hit.
1054 support_host_->ShowArcLoading(); 784 support_host_->ShowArcLoading();
1055 StartArcAndroidManagementCheck(); 785 StartArcAndroidManagementCheck();
1056 } 786 }
1057 } 787 }
1058 788
1059 void ArcAuthService::OnSendFeedbackClicked() { 789 void ArcSessionManager::OnSendFeedbackClicked() {
1060 DCHECK(support_host_); 790 DCHECK(support_host_);
1061 chrome::OpenFeedbackDialog(nullptr); 791 chrome::OpenFeedbackDialog(nullptr);
1062 } 792 }
1063 793
1064 void ArcAuthService::OnMetricsModeChanged(bool enabled, bool managed) { 794 void ArcSessionManager::OnMetricsModeChanged(bool enabled, bool managed) {
1065 if (!support_host_) 795 if (!support_host_)
1066 return; 796 return;
1067 support_host_->SetMetricsPreferenceCheckbox(enabled, managed); 797 support_host_->SetMetricsPreferenceCheckbox(enabled, managed);
1068 } 798 }
1069 799
1070 void ArcAuthService::OnBackupAndRestoreModeChanged(bool enabled, bool managed) { 800 void ArcSessionManager::OnBackupAndRestoreModeChanged(bool enabled,
801 bool managed) {
1071 if (!support_host_) 802 if (!support_host_)
1072 return; 803 return;
1073 support_host_->SetBackupAndRestorePreferenceCheckbox(enabled, managed); 804 support_host_->SetBackupAndRestorePreferenceCheckbox(enabled, managed);
1074 } 805 }
1075 806
1076 void ArcAuthService::OnLocationServicesModeChanged(bool enabled, bool managed) { 807 void ArcSessionManager::OnLocationServicesModeChanged(bool enabled,
808 bool managed) {
1077 if (!support_host_) 809 if (!support_host_)
1078 return; 810 return;
1079 support_host_->SetLocationServicesPreferenceCheckbox(enabled, managed); 811 support_host_->SetLocationServicesPreferenceCheckbox(enabled, managed);
1080 } 812 }
1081 813
1082 std::ostream& operator<<(std::ostream& os, const ArcAuthService::State& state) { 814 std::ostream& operator<<(std::ostream& os,
815 const ArcSessionManager::State& state) {
1083 switch (state) { 816 switch (state) {
1084 case ArcAuthService::State::NOT_INITIALIZED: 817 case ArcSessionManager::State::NOT_INITIALIZED:
1085 return os << "NOT_INITIALIZED"; 818 return os << "NOT_INITIALIZED";
1086 case ArcAuthService::State::STOPPED: 819 case ArcSessionManager::State::STOPPED:
1087 return os << "STOPPED"; 820 return os << "STOPPED";
1088 case ArcAuthService::State::SHOWING_TERMS_OF_SERVICE: 821 case ArcSessionManager::State::SHOWING_TERMS_OF_SERVICE:
1089 return os << "SHOWING_TERMS_OF_SERVICE"; 822 return os << "SHOWING_TERMS_OF_SERVICE";
1090 case ArcAuthService::State::CHECKING_ANDROID_MANAGEMENT: 823 case ArcSessionManager::State::CHECKING_ANDROID_MANAGEMENT:
1091 return os << "CHECKING_ANDROID_MANAGEMENT"; 824 return os << "CHECKING_ANDROID_MANAGEMENT";
1092 case ArcAuthService::State::ACTIVE: 825 case ArcSessionManager::State::ACTIVE:
1093 return os << "ACTIVE"; 826 return os << "ACTIVE";
1094 } 827 }
1095 828
1096 // Some compiler reports an error even if all values of an enum-class are 829 // Some compiler reports an error even if all values of an enum-class are
1097 // covered indivisually in a switch statement. 830 // covered indivisually in a switch statement.
1098 NOTREACHED(); 831 NOTREACHED();
1099 return os; 832 return os;
1100 } 833 }
1101 834
1102 } // namespace arc 835 } // namespace arc
OLDNEW
« no previous file with comments | « chrome/browser/chromeos/arc/arc_session_manager.h ('k') | chrome/browser/chromeos/arc/arc_session_manager_browsertest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698