Home | History | Annotate | Download | only in android
      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/strings/string16.h"
     10 #include "base/strings/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 
     18 using content::BrowserThread;
     19 using net::URLRequest;
     20 using net::AuthChallengeInfo;
     21 
     22 class 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   virtual void OnLoginModelDestroying() OVERRIDE {}
     39 
     40   virtual void BuildViewForPasswordManager(
     41       PasswordManager* manager,
     42       const string16& explanation) OVERRIDE {
     43     DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
     44 
     45     // Get pointer to TabAndroid
     46     content::WebContents* web_contents = GetWebContentsForLogin();
     47     CHECK(web_contents);
     48     TabAndroid* tab_android = TabAndroid::FromWebContents(web_contents);
     49 
     50     // Notify TabAndroid that HTTP authentication is required for current page.
     51     if (tab_android) {
     52       chrome_http_auth_handler_.reset(new ChromeHttpAuthHandler(explanation));
     53       chrome_http_auth_handler_->Init();
     54       chrome_http_auth_handler_->SetObserver(this);
     55 
     56       tab_android->OnReceivedHttpAuthRequest(
     57           chrome_http_auth_handler_.get()->GetJavaObject(),
     58           ASCIIToUTF16(auth_info()->challenger.ToString()),
     59           UTF8ToUTF16(auth_info()->realm));
     60 
     61       // Register to receive a callback to OnAutofillDataAvailable().
     62       SetModel(manager);
     63       NotifyAuthNeeded();
     64     } else {
     65       CancelAuth();
     66       LOG(WARNING) << "HTTP Authentication failed because TabAndroid is "
     67           "missing";
     68     }
     69   }
     70 
     71  protected:
     72   virtual ~LoginHandlerAndroid() {}
     73 
     74   virtual void CloseDialog() OVERRIDE {}
     75 
     76  private:
     77   scoped_ptr<ChromeHttpAuthHandler> chrome_http_auth_handler_;
     78 };
     79 
     80 // static
     81 LoginHandler* LoginHandler::Create(net::AuthChallengeInfo* auth_info,
     82                                    net::URLRequest* request) {
     83   return new LoginHandlerAndroid(auth_info, request);
     84 }
     85