OLD | NEW |
---|---|
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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_auth_service.h" |
6 | 6 |
7 #include <utility> | 7 #include <utility> |
8 | 8 |
9 #include "chrome/browser/chromeos/arc/arc_auth_ui.h" | |
10 #include "chrome/browser/profiles/profile.h" | |
11 #include "components/arc/arc_bridge_service.h" | |
12 | |
9 namespace arc { | 13 namespace arc { |
10 | 14 |
15 namespace { | |
16 | |
17 // Weak pointer. This class is owned by ArcServiceManager. | |
18 ArcAuthService* g_arc_auth_service = nullptr; | |
oshima
2016/01/28 17:46:40
optional: you don't need g_ because this is file s
khmel
2016/01/28 19:24:01
Done.
khmel
2016/01/28 19:24:01
Done.
| |
19 | |
20 // Skip creating UI in unit tests | |
21 bool g_disable_ui_for_testing = false; | |
22 | |
23 } // namespace | |
24 | |
11 ArcAuthService::ArcAuthService(ArcBridgeService* bridge_service) | 25 ArcAuthService::ArcAuthService(ArcBridgeService* bridge_service) |
12 : ArcService(bridge_service), binding_(this) { | 26 : ArcService(bridge_service), binding_(this) { |
27 DCHECK(!g_arc_auth_service); | |
28 g_arc_auth_service = this; | |
29 | |
13 arc_bridge_service()->AddObserver(this); | 30 arc_bridge_service()->AddObserver(this); |
14 } | 31 } |
15 | 32 |
16 ArcAuthService::~ArcAuthService() { | 33 ArcAuthService::~ArcAuthService() { |
17 arc_bridge_service()->RemoveObserver(this); | 34 arc_bridge_service()->RemoveObserver(this); |
35 CloseUI(); | |
36 | |
37 DCHECK(g_arc_auth_service == this); | |
38 g_arc_auth_service = nullptr; | |
39 } | |
40 | |
41 // static | |
42 ArcAuthService* ArcAuthService::Get() { | |
43 DCHECK(g_arc_auth_service); | |
44 DCHECK(g_arc_auth_service->thread_checker_.CalledOnValidThread()); | |
45 return g_arc_auth_service; | |
46 } | |
47 | |
48 // static | |
49 void ArcAuthService::DisableUIForTesting() { | |
50 g_disable_ui_for_testing = true; | |
18 } | 51 } |
19 | 52 |
20 void ArcAuthService::OnAuthInstanceReady() { | 53 void ArcAuthService::OnAuthInstanceReady() { |
21 arc_bridge_service()->auth_instance()->Init( | 54 arc_bridge_service()->auth_instance()->Init( |
22 binding_.CreateInterfacePtrAndBind()); | 55 binding_.CreateInterfacePtrAndBind()); |
23 } | 56 } |
24 | 57 |
58 std::string ArcAuthService::GetAuthCode() { | |
oshima
2016/01/28 17:46:40
GetAndResetAutoCode ?
khmel
2016/01/28 19:24:01
Yes, more clear
| |
59 DCHECK(thread_checker_.CalledOnValidThread()); | |
60 std::string auth_code; | |
61 auth_code_.swap(auth_code); | |
62 return auth_code; | |
63 } | |
64 | |
25 void ArcAuthService::GetAuthCode(const GetAuthCodeCallback& callback) { | 65 void ArcAuthService::GetAuthCode(const GetAuthCodeCallback& callback) { |
26 // TODO(victorhsieh): request auth code from LSO (crbug.com/571146). | 66 DCHECK(thread_checker_.CalledOnValidThread()); |
27 callback.Run(mojo::String("fake auth code from ArcAuthService in Chrome")); | 67 callback.Run(mojo::String(GetAuthCode())); |
68 } | |
69 | |
70 void ArcAuthService::SetState(State state) { | |
71 DCHECK(state_ != state); | |
72 state_ = state; | |
73 FOR_EACH_OBSERVER(Observer, observer_list_, OnOptInChanged(state_)); | |
74 } | |
75 | |
76 void ArcAuthService::OnPrimaryUserProfilePrepared(Profile* profile) { | |
77 DCHECK(profile != nullptr && profile != profile_); | |
78 DCHECK(thread_checker_.CalledOnValidThread()); | |
79 | |
80 Shutdown(); | |
81 | |
82 profile_ = profile; | |
83 | |
84 // TODO(khmel). At this moment UI to handle ARC OptIn is not ready yet. Assume | |
85 // we opted in by default. When UI is ready, this should be synced with | |
86 // user's prefs. | |
87 FetchAuthCode(); | |
88 } | |
89 | |
90 void ArcAuthService::Shutdown() { | |
91 profile_ = nullptr; | |
92 ArcBridgeService::Get()->Shutdown(); | |
93 if (state_ != State::DISABLE) { | |
94 auth_fetcher_.reset(); | |
95 SetState(State::DISABLE); | |
96 } | |
97 } | |
98 | |
99 void ArcAuthService::AddObserver(Observer* observer) { | |
100 DCHECK(thread_checker_.CalledOnValidThread()); | |
101 observer_list_.AddObserver(observer); | |
102 } | |
103 | |
104 void ArcAuthService::RemoveObserver(Observer* observer) { | |
105 DCHECK(thread_checker_.CalledOnValidThread()); | |
106 observer_list_.RemoveObserver(observer); | |
107 } | |
108 | |
109 void ArcAuthService::CloseUI() { | |
110 if (auth_ui_) { | |
111 auth_ui_->Close(); | |
112 DCHECK(auth_ui_ == nullptr); | |
oshima
2016/01/28 17:46:41
DCHEK(!auth_ui_)
khmel
2016/01/28 19:24:01
Done.
| |
113 } | |
114 } | |
115 | |
116 void ArcAuthService::SetAuthCodeAndStartArc(const std::string auth_code) { | |
117 DCHECK(thread_checker_.CalledOnValidThread()); | |
118 DCHECK(!auth_code.empty()); | |
119 DCHECK(state_ != State::ENABLE); | |
oshima
2016/01/28 17:46:40
DCHECK_EQ. same for others
khmel
2016/01/28 19:24:02
I cannot use DCHECK_EQ(NE) with State. It cannot b
oshima
2016/01/28 20:39:54
that's probably because you use enum class (instea
khmel
2016/01/28 21:06:01
Yes, you are right. This works fine. Thanks for he
| |
120 | |
121 CloseUI(); | |
122 auth_fetcher_.reset(); | |
123 auth_code_ = auth_code; | |
124 ArcBridgeService::Get()->HandleStartup(); | |
125 | |
126 SetState(State::ENABLE); | |
127 } | |
128 | |
129 void ArcAuthService::FetchAuthCode() { | |
130 DCHECK(thread_checker_.CalledOnValidThread()); | |
131 DCHECK(state_ == State::DISABLE); | |
132 | |
133 CloseUI(); | |
134 auth_code_ = ""; | |
oshima
2016/01/28 17:46:40
auth_code_.clear()
khmel
2016/01/28 19:24:01
Done.
| |
135 | |
136 SetState(State::FETCHING_CODE); | |
137 | |
138 auth_fetcher_.reset(new ArcAuthFetcher(profile_->GetRequestContext(), this)); | |
139 } | |
140 | |
141 void ArcAuthService::OnAuthCodeFetched(const std::string& auth_code) { | |
142 DCHECK(state_ == State::FETCHING_CODE); | |
143 SetAuthCodeAndStartArc(auth_code); | |
144 } | |
145 | |
146 void ArcAuthService::OnAuthCodeNeedUI() { | |
147 CloseUI(); | |
148 if (!g_disable_ui_for_testing) | |
149 auth_ui_ = new ArcAuthUI(profile_, this); | |
150 } | |
151 | |
152 void ArcAuthService::OnAuthCodeFailed() { | |
153 DCHECK(state_ == State::FETCHING_CODE); | |
154 CloseUI(); | |
155 | |
156 SetState(State::NO_CODE); | |
157 } | |
158 | |
159 void ArcAuthService::OnAuthUIClosed() { | |
160 DCHECK(auth_ui_ != nullptr); | |
oshima
2016/01/28 17:46:40
DCHECK(auth_ui_)
khmel
2016/01/28 19:24:01
Done.
| |
161 auth_ui_ = nullptr; | |
28 } | 162 } |
29 | 163 |
30 } // namespace arc | 164 } // namespace arc |
OLD | NEW |