This source file includes following definitions.
- FetchUsernameThroughSigninManager
- infobar_shown_
- ShowInfoBarIfPossible
- ShowInfoBarUIThread
- DidStopLoading
- WebContentsDestroyed
- AddInfoBarToWebContents
#include "chrome/browser/ui/auto_login_prompter.h"
#include "base/bind.h"
#include "base/command_line.h"
#include "base/logging.h"
#include "base/prefs/pref_service.h"
#include "chrome/browser/google/google_url_tracker.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
#include "chrome/browser/signin/signin_manager_factory.h"
#include "chrome/browser/tab_contents/tab_util.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/pref_names.h"
#include "components/auto_login_parser/auto_login_parser.h"
#include "components/signin/core/browser/profile_oauth2_token_service.h"
#include "components/signin/core/browser/signin_manager.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/web_contents.h"
#include "net/url_request/url_request.h"
#include "url/gurl.h"
using content::BrowserThread;
using content::WebContents;
namespace {
#if !defined(OS_ANDROID)
bool FetchUsernameThroughSigninManager(Profile* profile, std::string* output) {
SigninManagerBase* signin_manager =
SigninManagerFactory::GetInstance()->GetForProfile(profile);
if (!signin_manager)
return false;
ProfileOAuth2TokenService* token_service =
ProfileOAuth2TokenServiceFactory::GetForProfile(profile);
if (!token_service || !token_service->RefreshTokenIsAvailable(
signin_manager->GetAuthenticatedAccountId())) {
return false;
}
*output = signin_manager->GetAuthenticatedUsername();
return true;
}
#endif
}
AutoLoginPrompter::AutoLoginPrompter(WebContents* web_contents,
const Params& params,
const GURL& url)
: WebContentsObserver(web_contents),
params_(params),
url_(url),
infobar_shown_(false) {
if (!web_contents->IsLoading()) {
AddInfoBarToWebContents();
}
}
AutoLoginPrompter::~AutoLoginPrompter() {
}
void AutoLoginPrompter::ShowInfoBarIfPossible(net::URLRequest* request,
int child_id,
int route_id) {
if (!CommandLine::ForCurrentProcess()->HasSwitch(switches::kEnableAutologin))
return;
Params params;
if (!auto_login_parser::ParserHeaderInResponse(
request, auto_login_parser::ONLY_GOOGLE_COM, ¶ms.header))
return;
BrowserThread::PostTask(
BrowserThread::UI, FROM_HERE,
base::Bind(&ShowInfoBarUIThread,
params, request->url(), child_id, route_id));
}
void AutoLoginPrompter::ShowInfoBarUIThread(Params params,
const GURL& url,
int child_id,
int route_id) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
WebContents* web_contents = tab_util::GetWebContentsByID(child_id, route_id);
if (!web_contents)
return;
Profile* profile =
Profile::FromBrowserContext(web_contents->GetBrowserContext());
if (!profile->GetPrefs()->GetBoolean(prefs::kAutologinEnabled))
return;
#if !defined(OS_ANDROID)
if (!FetchUsernameThroughSigninManager(profile, ¶ms.username))
return;
#endif
if (!params.username.empty() && !params.header.account.empty() &&
params.username != params.header.account)
return;
new AutoLoginPrompter(web_contents, params, url);
}
void AutoLoginPrompter::DidStopLoading(
content::RenderViewHost* render_view_host) {
AddInfoBarToWebContents();
delete this;
}
void AutoLoginPrompter::WebContentsDestroyed(WebContents* web_contents) {
delete this;
}
void AutoLoginPrompter::AddInfoBarToWebContents() {
if (!infobar_shown_)
infobar_shown_ = AutoLoginInfoBarDelegate::Create(web_contents(), params_);
}