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

Unified Diff: chrome/browser/chromeos/arc/arc_auth_service.cc

Issue 1618193003: arc: Pass auth token from Chrome to ARC instance. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: rebased Created 4 years, 11 months 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 side-by-side diff with in-line comments
Download patch
Index: chrome/browser/chromeos/arc/arc_auth_service.cc
diff --git a/chrome/browser/chromeos/arc/arc_auth_service.cc b/chrome/browser/chromeos/arc/arc_auth_service.cc
index 46a2cea720f2f280f36d5bb591665d685f3cd213..03257469d6eaae857f46a25c3054ace8b04393b6 100644
--- a/chrome/browser/chromeos/arc/arc_auth_service.cc
+++ b/chrome/browser/chromeos/arc/arc_auth_service.cc
@@ -6,15 +6,48 @@
#include <utility>
+#include "chrome/browser/chromeos/arc/arc_auth_ui.h"
+#include "chrome/browser/profiles/profile.h"
+#include "components/arc/arc_bridge_service.h"
+
namespace arc {
+namespace {
+
+// Weak pointer. This class is owned by ArcServiceManager.
+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.
+
+// Skip creating UI in unit tests
+bool g_disable_ui_for_testing = false;
+
+} // namespace
+
ArcAuthService::ArcAuthService(ArcBridgeService* bridge_service)
: ArcService(bridge_service), binding_(this) {
+ DCHECK(!g_arc_auth_service);
+ g_arc_auth_service = this;
+
arc_bridge_service()->AddObserver(this);
}
ArcAuthService::~ArcAuthService() {
arc_bridge_service()->RemoveObserver(this);
+ CloseUI();
+
+ DCHECK(g_arc_auth_service == this);
+ g_arc_auth_service = nullptr;
+}
+
+// static
+ArcAuthService* ArcAuthService::Get() {
+ DCHECK(g_arc_auth_service);
+ DCHECK(g_arc_auth_service->thread_checker_.CalledOnValidThread());
+ return g_arc_auth_service;
+}
+
+// static
+void ArcAuthService::DisableUIForTesting() {
+ g_disable_ui_for_testing = true;
}
void ArcAuthService::OnAuthInstanceReady() {
@@ -22,9 +55,110 @@ void ArcAuthService::OnAuthInstanceReady() {
binding_.CreateInterfacePtrAndBind());
}
+std::string ArcAuthService::GetAuthCode() {
oshima 2016/01/28 17:46:40 GetAndResetAutoCode ?
khmel 2016/01/28 19:24:01 Yes, more clear
+ DCHECK(thread_checker_.CalledOnValidThread());
+ std::string auth_code;
+ auth_code_.swap(auth_code);
+ return auth_code;
+}
+
void ArcAuthService::GetAuthCode(const GetAuthCodeCallback& callback) {
- // TODO(victorhsieh): request auth code from LSO (crbug.com/571146).
- callback.Run(mojo::String("fake auth code from ArcAuthService in Chrome"));
+ DCHECK(thread_checker_.CalledOnValidThread());
+ callback.Run(mojo::String(GetAuthCode()));
+}
+
+void ArcAuthService::SetState(State state) {
+ DCHECK(state_ != state);
+ state_ = state;
+ FOR_EACH_OBSERVER(Observer, observer_list_, OnOptInChanged(state_));
+}
+
+void ArcAuthService::OnPrimaryUserProfilePrepared(Profile* profile) {
+ DCHECK(profile != nullptr && profile != profile_);
+ DCHECK(thread_checker_.CalledOnValidThread());
+
+ Shutdown();
+
+ profile_ = profile;
+
+ // TODO(khmel). At this moment UI to handle ARC OptIn is not ready yet. Assume
+ // we opted in by default. When UI is ready, this should be synced with
+ // user's prefs.
+ FetchAuthCode();
+}
+
+void ArcAuthService::Shutdown() {
+ profile_ = nullptr;
+ ArcBridgeService::Get()->Shutdown();
+ if (state_ != State::DISABLE) {
+ auth_fetcher_.reset();
+ SetState(State::DISABLE);
+ }
+}
+
+void ArcAuthService::AddObserver(Observer* observer) {
+ DCHECK(thread_checker_.CalledOnValidThread());
+ observer_list_.AddObserver(observer);
+}
+
+void ArcAuthService::RemoveObserver(Observer* observer) {
+ DCHECK(thread_checker_.CalledOnValidThread());
+ observer_list_.RemoveObserver(observer);
+}
+
+void ArcAuthService::CloseUI() {
+ if (auth_ui_) {
+ auth_ui_->Close();
+ DCHECK(auth_ui_ == nullptr);
oshima 2016/01/28 17:46:41 DCHEK(!auth_ui_)
khmel 2016/01/28 19:24:01 Done.
+ }
+}
+
+void ArcAuthService::SetAuthCodeAndStartArc(const std::string auth_code) {
+ DCHECK(thread_checker_.CalledOnValidThread());
+ DCHECK(!auth_code.empty());
+ 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
+
+ CloseUI();
+ auth_fetcher_.reset();
+ auth_code_ = auth_code;
+ ArcBridgeService::Get()->HandleStartup();
+
+ SetState(State::ENABLE);
+}
+
+void ArcAuthService::FetchAuthCode() {
+ DCHECK(thread_checker_.CalledOnValidThread());
+ DCHECK(state_ == State::DISABLE);
+
+ CloseUI();
+ auth_code_ = "";
oshima 2016/01/28 17:46:40 auth_code_.clear()
khmel 2016/01/28 19:24:01 Done.
+
+ SetState(State::FETCHING_CODE);
+
+ auth_fetcher_.reset(new ArcAuthFetcher(profile_->GetRequestContext(), this));
+}
+
+void ArcAuthService::OnAuthCodeFetched(const std::string& auth_code) {
+ DCHECK(state_ == State::FETCHING_CODE);
+ SetAuthCodeAndStartArc(auth_code);
+}
+
+void ArcAuthService::OnAuthCodeNeedUI() {
+ CloseUI();
+ if (!g_disable_ui_for_testing)
+ auth_ui_ = new ArcAuthUI(profile_, this);
+}
+
+void ArcAuthService::OnAuthCodeFailed() {
+ DCHECK(state_ == State::FETCHING_CODE);
+ CloseUI();
+
+ SetState(State::NO_CODE);
+}
+
+void ArcAuthService::OnAuthUIClosed() {
+ DCHECK(auth_ui_ != nullptr);
oshima 2016/01/28 17:46:40 DCHECK(auth_ui_)
khmel 2016/01/28 19:24:01 Done.
+ auth_ui_ = nullptr;
}
} // namespace arc

Powered by Google App Engine
This is Rietveld 408576698