login_prompt_android.cc revision 2a99a7e74a7f215066514fe81d2bfa6639d9eddd
1// Copyright (c) 2012 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/ui/login/login_prompt.h"
6
7#include "base/logging.h"
8#include "base/memory/scoped_ptr.h"
9#include "base/string16.h"
10#include "base/utf_string_conversions.h"
11#include "chrome/browser/android/tab_android.h"
12#include "chrome/browser/ui/android/chrome_http_auth_handler.h"
13#include "chrome/browser/ui/login/login_prompt.h"
14#include "content/public/browser/browser_thread.h"
15#include "content/public/browser/web_contents.h"
16#include "net/base/auth.h"
17
18using content::BrowserThread;
19using net::URLRequest;
20using net::AuthChallengeInfo;
21
22class LoginHandlerAndroid : public LoginHandler {
23 public:
24  LoginHandlerAndroid(AuthChallengeInfo* auth_info, URLRequest* request)
25      : LoginHandler(auth_info, request) {
26  }
27
28  // LoginHandler methods:
29
30  virtual void OnAutofillDataAvailable(
31      const string16& username,
32      const string16& password) OVERRIDE {
33    DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
34    DCHECK(chrome_http_auth_handler_.get() != NULL);
35    chrome_http_auth_handler_->OnAutofillDataAvailable(
36        username, password);
37  }
38
39  virtual void BuildViewForPasswordManager(
40      PasswordManager* manager,
41      const string16& explanation) OVERRIDE {
42    DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
43
44    // Get pointer to TabAndroid
45    content::WebContents* web_contents = GetWebContentsForLogin();
46    CHECK(web_contents);
47    TabAndroid* tab_android = TabAndroid::FromWebContents(web_contents);
48
49    // Notify TabAndroid that HTTP authentication is required for current page.
50    if (tab_android) {
51      chrome_http_auth_handler_.reset(new ChromeHttpAuthHandler(explanation));
52      chrome_http_auth_handler_->Init();
53      chrome_http_auth_handler_->SetObserver(this);
54
55      tab_android->OnReceivedHttpAuthRequest(
56          chrome_http_auth_handler_.get()->GetJavaObject(),
57          ASCIIToUTF16(auth_info()->challenger.ToString()),
58          UTF8ToUTF16(auth_info()->realm));
59
60      // Register to receive a callback to OnAutofillDataAvailable().
61      SetModel(manager);
62      NotifyAuthNeeded();
63    } else {
64      CancelAuth();
65      LOG(WARNING) << "HTTP Authentication failed because TabAndroid is "
66          "missing";
67    }
68  }
69
70 protected:
71  virtual ~LoginHandlerAndroid() {}
72
73  virtual void CloseDialog() OVERRIDE {}
74
75 private:
76  scoped_ptr<ChromeHttpAuthHandler> chrome_http_auth_handler_;
77};
78
79// static
80LoginHandler* LoginHandler::Create(net::AuthChallengeInfo* auth_info,
81                                   net::URLRequest* request) {
82  return new LoginHandlerAndroid(auth_info, request);
83}
84