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/ui/android/chrome_http_auth_handler.h"
     12 #include "chrome/browser/ui/android/window_android_helper.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 #include "ui/base/android/window_android.h"
     18 
     19 using content::BrowserThread;
     20 using net::URLRequest;
     21 using net::AuthChallengeInfo;
     22 
     23 class LoginHandlerAndroid : public LoginHandler {
     24  public:
     25   LoginHandlerAndroid(AuthChallengeInfo* auth_info, URLRequest* request)
     26       : LoginHandler(auth_info, request) {
     27   }
     28 
     29   // LoginHandler methods:
     30 
     31   virtual void OnAutofillDataAvailable(
     32       const base::string16& username,
     33       const base::string16& password) OVERRIDE {
     34     DCHECK_CURRENTLY_ON(BrowserThread::UI);
     35     DCHECK(chrome_http_auth_handler_.get() != NULL);
     36     chrome_http_auth_handler_->OnAutofillDataAvailable(
     37         username, password);
     38   }
     39   virtual void OnLoginModelDestroying() OVERRIDE {}
     40 
     41   virtual void BuildViewForPasswordManager(
     42       password_manager::PasswordManager* manager,
     43       const base::string16& explanation) OVERRIDE {
     44     DCHECK_CURRENTLY_ON(BrowserThread::UI);
     45 
     46     // Get pointer to TabAndroid
     47     content::WebContents* web_contents = GetWebContentsForLogin();
     48     CHECK(web_contents);
     49     WindowAndroidHelper* window_helper = WindowAndroidHelper::FromWebContents(
     50         web_contents);
     51 
     52     // Notify WindowAndroid that HTTP authentication is required.
     53     if (window_helper->GetWindowAndroid()) {
     54       chrome_http_auth_handler_.reset(new ChromeHttpAuthHandler(explanation));
     55       chrome_http_auth_handler_->Init();
     56       chrome_http_auth_handler_->SetObserver(this);
     57       chrome_http_auth_handler_->ShowDialog(
     58           window_helper->GetWindowAndroid()->GetJavaObject().obj());
     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
     80 LoginHandler* LoginHandler::Create(net::AuthChallengeInfo* auth_info,
     81                                    net::URLRequest* request) {
     82   return new LoginHandlerAndroid(auth_info, request);
     83 }
     84