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 #include "chrome/browser/chromeos/login/signin/merge_session_xhr_request_waiter.h"
8 #include "base/lazy_instance.h"
9 #include "base/logging.h"
10 #include "base/memory/weak_ptr.h"
11 #include "base/message_loop/message_loop.h"
12 #include "chrome/browser/chromeos/login/signin/oauth2_login_manager.h"
13 #include "chrome/browser/chromeos/login/signin/oauth2_login_manager_factory.h"
14 #include "content/public/browser/browser_thread.h"
16 using content::BrowserThread
;
22 // Maximum time for delaying XHR requests.
23 const int kMaxRequestWaitTimeMS
= 10000;
27 MergeSessionXHRRequestWaiter::MergeSessionXHRRequestWaiter(
29 const MergeSessionThrottle::CompletionCallback
& callback
)
32 weak_ptr_factory_(this) {
35 MergeSessionXHRRequestWaiter::~MergeSessionXHRRequestWaiter() {
36 chromeos::OAuth2LoginManager
* manager
=
37 chromeos::OAuth2LoginManagerFactory::GetInstance()->GetForProfile(
40 manager
->RemoveObserver(this);
43 void MergeSessionXHRRequestWaiter::StartWaiting() {
44 OAuth2LoginManager
* manager
=
45 OAuth2LoginManagerFactory::GetInstance()->GetForProfile(profile_
);
46 if (manager
&& manager
->ShouldBlockTabLoading()) {
47 DVLOG(1) << "Waiting for XHR request throttle";
48 manager
->AddObserver(this);
49 BrowserThread::PostDelayedTask(
50 BrowserThread::UI
, FROM_HERE
,
51 base::Bind(&MergeSessionXHRRequestWaiter::OnTimeout
,
52 weak_ptr_factory_
.GetWeakPtr()),
53 base::TimeDelta::FromMilliseconds(kMaxRequestWaitTimeMS
));
59 void MergeSessionXHRRequestWaiter::OnSessionRestoreStateChanged(
60 Profile
* user_profile
,
61 OAuth2LoginManager::SessionRestoreState state
) {
62 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
64 OAuth2LoginManager
* manager
=
65 OAuth2LoginManagerFactory::GetInstance()->GetForProfile(profile_
);
66 DVLOG(1) << "Merge session throttle should "
67 << (!manager
->ShouldBlockTabLoading() ?
69 << " be blocking now, "
71 if (!manager
->ShouldBlockTabLoading()) {
72 DVLOG(1) << "Unblocking XHR request throttle due to session merge";
73 manager
->RemoveObserver(this);
78 void MergeSessionXHRRequestWaiter::OnTimeout() {
79 DVLOG(1) << "Unblocking XHR request throttle due to timeout";
83 void MergeSessionXHRRequestWaiter::NotifyBlockingDone() {
84 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
85 if (!callback_
.is_null()) {
86 BrowserThread::PostTask(
87 BrowserThread::IO
, FROM_HERE
, callback_
);
89 weak_ptr_factory_
.InvalidateWeakPtrs();
90 base::MessageLoop::current()->DeleteSoon(FROM_HERE
, this);
93 } // namespace chromeos