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. 4 5#include "chrome/browser/chromeos/login/signin/merge_session_xhr_request_waiter.h" 6 7#include "base/bind.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" 15 16using content::BrowserThread; 17 18namespace chromeos { 19 20namespace { 21 22// Maximum time for delaying XHR requests. 23const int kMaxRequestWaitTimeMS = 10000; 24 25} 26 27MergeSessionXHRRequestWaiter::MergeSessionXHRRequestWaiter( 28 Profile* profile, 29 const MergeSessionThrottle::CompletionCallback& callback) 30 : profile_(profile), 31 callback_(callback), 32 weak_ptr_factory_(this) { 33} 34 35MergeSessionXHRRequestWaiter::~MergeSessionXHRRequestWaiter() { 36 chromeos::OAuth2LoginManager* manager = 37 chromeos::OAuth2LoginManagerFactory::GetInstance()->GetForProfile( 38 profile_); 39 if (manager) 40 manager->RemoveObserver(this); 41} 42 43void 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)); 54 } else { 55 NotifyBlockingDone(); 56 } 57} 58 59void MergeSessionXHRRequestWaiter::OnSessionRestoreStateChanged( 60 Profile* user_profile, 61 OAuth2LoginManager::SessionRestoreState state) { 62 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 63 64 OAuth2LoginManager* manager = 65 OAuth2LoginManagerFactory::GetInstance()->GetForProfile(profile_); 66 DVLOG(1) << "Merge session throttle should " 67 << (!manager->ShouldBlockTabLoading() ? 68 " NOT" : "") 69 << " be blocking now, " 70 << state; 71 if (!manager->ShouldBlockTabLoading()) { 72 DVLOG(1) << "Unblocking XHR request throttle due to session merge"; 73 manager->RemoveObserver(this); 74 NotifyBlockingDone(); 75 } 76} 77 78void MergeSessionXHRRequestWaiter::OnTimeout() { 79 DVLOG(1) << "Unblocking XHR request throttle due to timeout"; 80 NotifyBlockingDone(); 81} 82 83void MergeSessionXHRRequestWaiter::NotifyBlockingDone() { 84 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 85 if (!callback_.is_null()) { 86 BrowserThread::PostTask( 87 BrowserThread::IO, FROM_HERE, callback_); 88 } 89 weak_ptr_factory_.InvalidateWeakPtrs(); 90 base::MessageLoop::current()->DeleteSoon(FROM_HERE, this); 91} 92 93} // namespace chromeos 94