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.
5 #ifndef CHROME_BROWSER_CHROMEOS_LOGIN_EASY_UNLOCK_BOOTSTRAP_USER_CONTEXT_INITIALIZER_H_
6 #define CHROME_BROWSER_CHROMEOS_LOGIN_EASY_UNLOCK_BOOTSTRAP_USER_CONTEXT_INITIALIZER_H_
10 #include "base/callback.h"
11 #include "base/macros.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/memory/weak_ptr.h"
14 #include "chrome/browser/chromeos/login/easy_unlock/easy_unlock_types.h"
15 #include "chrome/browser/signin/easy_unlock_auth_attempt.h"
16 #include "chrome/browser/signin/easy_unlock_service_observer.h"
17 #include "chromeos/login/auth/user_context.h"
18 #include "google_apis/gaia/gaia_oauth_client.h"
22 // Performs initialization work for adding a new account via Easy bootstrap.
23 // The class takes an authorization code then prepares a UserContext that
24 // could be used to create a new account.
25 class BootstrapUserContextInitializer final
26 : public gaia::GaiaOAuthClient::Delegate
,
27 public EasyUnlockServiceObserver
{
29 // Callback type to be invoked after initialization work is done.
30 typedef base::Callback
<void(bool success
, const UserContext
& user_context
)>
33 BootstrapUserContextInitializer();
34 ~BootstrapUserContextInitializer() override
;
36 // Run initialization work with given |auth_code|. |callback| will be invoked
37 // when the work is done.
38 void Start(const std::string
& auth_code
, const CompleteCallback
& callback
);
40 const UserContext
& user_context() const { return user_context_
; }
41 bool random_key_used() const { return random_key_used_
; }
43 static void SetCompleteCallbackForTesting(CompleteCallback
* callback
);
46 void Notify(bool success
);
48 // Starts to check existing Smart lock keys to use.
49 void StartCheckExistingKeys();
51 void OnGetEasyUnlockData(bool success
,
52 const EasyUnlockDeviceKeyDataList
& data_list
);
53 void OnEasyUnlockAuthenticated(EasyUnlockAuthAttempt::Type auth_attempt_type
,
55 const std::string
& user_id
,
56 const std::string
& key_secret
,
57 const std::string
& key_label
);
58 void CreateRandomKey();
60 // Start refresh token and user info fetching.
61 void StartTokenFetch(const std::string
& auth_code
);
63 // gaia::GaiaOAuthClient::Delegate
64 void OnGetTokensResponse(const std::string
& refresh_token
,
65 const std::string
& access_token
,
66 int expires_in_seconds
) override
;
67 void OnRefreshTokenResponse(const std::string
& access_token
,
68 int expires_in_seconds
) override
;
69 void OnGetUserInfoResponse(
70 scoped_ptr
<base::DictionaryValue
> user_info
) override
;
71 void OnOAuthError() override
;
72 void OnNetworkError(int response_code
) override
;
74 // EasyUnlockServiceObserver:
75 void OnScreenlockStateChanged(proximity_auth::ScreenlockState state
) override
;
77 CompleteCallback callback_
;
78 scoped_ptr
<gaia::GaiaOAuthClient
> token_fetcher_
;
80 UserContext user_context_
;
81 bool random_key_used_
;
83 base::WeakPtrFactory
<BootstrapUserContextInitializer
> weak_ptr_factory_
;
85 static CompleteCallback
* complete_callback_for_testing_
;
87 DISALLOW_COPY_AND_ASSIGN(BootstrapUserContextInitializer
);
90 } // namespace chromeos
92 #endif // CHROME_BROWSER_CHROMEOS_LOGIN_EASY_UNLOCK_BOOTSTRAP_USER_CONTEXT_INITIALIZER_H_