OLD | NEW |
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 <ostream> |
| 9 |
8 #include "base/macros.h" | 10 #include "base/macros.h" |
| 11 #include "base/observer_list.h" |
| 12 #include "base/threading/thread_checker.h" |
| 13 #include "chrome/browser/chromeos/arc/arc_auth_ui.h" |
9 #include "components/arc/arc_bridge_service.h" | 14 #include "components/arc/arc_bridge_service.h" |
10 #include "components/arc/arc_service.h" | 15 #include "components/arc/arc_service.h" |
| 16 #include "components/arc/auth/arc_auth_fetcher.h" |
11 #include "components/arc/common/auth.mojom.h" | 17 #include "components/arc/common/auth.mojom.h" |
12 #include "mojo/public/cpp/bindings/binding.h" | 18 #include "mojo/public/cpp/bindings/binding.h" |
13 | 19 |
| 20 class Profile; |
| 21 |
14 namespace arc { | 22 namespace arc { |
15 | 23 |
16 // This class proxies the request from the client to fetch an auth code from | 24 // This class proxies the request from the client to fetch an auth code from |
17 // LSO. | 25 // LSO. |
18 class ArcAuthService : public ArcService, | 26 class ArcAuthService : public ArcService, |
19 public AuthHost, | 27 public AuthHost, |
20 public ArcBridgeService::Observer { | 28 public ArcBridgeService::Observer, |
| 29 public ArcAuthFetcher::Delegate, |
| 30 public ArcAuthUI::Delegate { |
21 public: | 31 public: |
| 32 enum class State { |
| 33 DISABLE, // ARC is not allowed to run (default). |
| 34 FETCHING_CODE, // ARC is allowed, receiving auth_2 code. |
| 35 NO_CODE, // ARC is allowed, auth_2 code was not received. |
| 36 ENABLE, // ARC is allowed, auth_2 code was received. |
| 37 }; |
| 38 |
| 39 class Observer { |
| 40 public: |
| 41 virtual ~Observer() = default; |
| 42 |
| 43 // Called whenever Opt-In state of the ARC has been changed. |
| 44 virtual void OnOptInChanged(State state) = 0; |
| 45 }; |
| 46 |
22 explicit ArcAuthService(ArcBridgeService* bridge_service); | 47 explicit ArcAuthService(ArcBridgeService* bridge_service); |
23 ~ArcAuthService() override; | 48 ~ArcAuthService() override; |
24 | 49 |
25 private: | 50 static ArcAuthService* Get(); |
26 // Overrides ArcBridgeService::Observer. | 51 |
| 52 static void DisableUIForTesting(); |
| 53 |
| 54 void OnPrimaryUserProfilePrepared(Profile* profile); |
| 55 void Shutdown(); |
| 56 |
| 57 State state() const { return state_; } |
| 58 |
| 59 // Sets the auth code. Can be set from internally or from external component |
| 60 // that accepts user's credentials. This actually starts ARC bridge service. |
| 61 void SetAuthCodeAndStartArc(const std::string& auth_code); |
| 62 |
| 63 std::string GetAndResetAutoCode(); |
| 64 |
| 65 // Adds or removes observers. |
| 66 void AddObserver(Observer* observer); |
| 67 void RemoveObserver(Observer* observer); |
| 68 |
| 69 // ArcBridgeService::Observer: |
27 void OnAuthInstanceReady() override; | 70 void OnAuthInstanceReady() override; |
28 | 71 |
29 // Overrides AuthHost. | 72 // Overrides AuthHost. For security reason this code can be used only |
| 73 // once and exists for specific period of time. |
30 void GetAuthCode(const GetAuthCodeCallback& callback) override; | 74 void GetAuthCode(const GetAuthCodeCallback& callback) override; |
31 | 75 |
| 76 // ArcAuthFetcher::Delegate: |
| 77 void OnAuthCodeFetched(const std::string& auth_code) override; |
| 78 void OnAuthCodeNeedUI() override; |
| 79 void OnAuthCodeFailed() override; |
| 80 |
| 81 // ArcAuthUI::Delegate: |
| 82 void OnAuthUIClosed() override; |
| 83 |
| 84 private: |
| 85 void FetchAuthCode(); |
| 86 void CloseUI(); |
| 87 void SetState(State state); |
| 88 |
| 89 // Unowned pointer. Keeps current profile. |
| 90 Profile* profile_ = nullptr; |
| 91 |
| 92 // Owned by view hierarchy. |
| 93 ArcAuthUI* auth_ui_ = nullptr; |
| 94 |
32 mojo::Binding<AuthHost> binding_; | 95 mojo::Binding<AuthHost> binding_; |
| 96 base::ThreadChecker thread_checker_; |
| 97 State state_ = State::DISABLE; |
| 98 base::ObserverList<Observer> observer_list_; |
| 99 scoped_ptr<ArcAuthFetcher> auth_fetcher_; |
| 100 std::string auth_code_; |
33 | 101 |
34 DISALLOW_COPY_AND_ASSIGN(ArcAuthService); | 102 DISALLOW_COPY_AND_ASSIGN(ArcAuthService); |
35 }; | 103 }; |
36 | 104 |
| 105 std::ostream& operator<<(std::ostream& os, const ArcAuthService::State& state); |
| 106 |
37 } // namespace arc | 107 } // namespace arc |
38 | 108 |
39 #endif // CHROME_BROWSER_CHROMEOS_ARC_ARC_AUTH_SERVICE_H_ | 109 #endif // CHROME_BROWSER_CHROMEOS_ARC_ARC_AUTH_SERVICE_H_ |
OLD | NEW |