| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "android_webview/browser/aw_login_delegate.h" | 5 #include "android_webview/browser/aw_login_delegate.h" |
| 6 | 6 |
| 7 #include "base/android/jni_android.h" |
| 7 #include "base/logging.h" | 8 #include "base/logging.h" |
| 8 #include "base/android/jni_android.h" | 9 #include "base/supports_user_data.h" |
| 9 #include "content/public/browser/browser_thread.h" | 10 #include "content/public/browser/browser_thread.h" |
| 10 #include "content/public/browser/render_view_host.h" | 11 #include "content/public/browser/render_view_host.h" |
| 11 #include "content/public/browser/resource_dispatcher_host.h" | 12 #include "content/public/browser/resource_dispatcher_host.h" |
| 12 #include "content/public/browser/resource_request_info.h" | 13 #include "content/public/browser/resource_request_info.h" |
| 13 #include "content/public/browser/web_contents.h" | 14 #include "content/public/browser/web_contents.h" |
| 14 #include "net/base/auth.h" | 15 #include "net/base/auth.h" |
| 15 #include "net/url_request/url_request.h" | 16 #include "net/url_request/url_request.h" |
| 16 | 17 |
| 17 using namespace base::android; | 18 using namespace base::android; |
| 18 | 19 |
| 19 using content::BrowserThread; | 20 using content::BrowserThread; |
| 20 using content::RenderViewHost; | 21 using content::RenderViewHost; |
| 21 using content::ResourceDispatcherHost; | 22 using content::ResourceDispatcherHost; |
| 22 using content::ResourceRequestInfo; | 23 using content::ResourceRequestInfo; |
| 23 using content::WebContents; | 24 using content::WebContents; |
| 24 | 25 |
| 26 namespace { |
| 27 const char* kAuthAttemptsKey = "android_webview_auth_attempts"; |
| 28 |
| 29 class UrlRequestAuthAttemptsData : public base::SupportsUserData::Data { |
| 30 public: |
| 31 UrlRequestAuthAttemptsData() : auth_attempts_(0) { } |
| 32 int auth_attempts_; |
| 33 }; |
| 34 |
| 35 } // namespace |
| 36 |
| 25 namespace android_webview { | 37 namespace android_webview { |
| 26 | 38 |
| 27 AwLoginDelegate::AwLoginDelegate(net::AuthChallengeInfo* auth_info, | 39 AwLoginDelegate::AwLoginDelegate(net::AuthChallengeInfo* auth_info, |
| 28 net::URLRequest* request) | 40 net::URLRequest* request) |
| 29 : auth_info_(auth_info), | 41 : auth_info_(auth_info), |
| 30 request_(request), | 42 request_(request), |
| 31 render_process_id_(0), | 43 render_process_id_(0), |
| 32 render_view_id_(0) { | 44 render_view_id_(0) { |
| 33 // TODO(benm): We need to track whether the last auth request for this host | |
| 34 // was successful (for HttpAuthHandler.useHttpAuthUsernamePassword()). We | |
| 35 // could attach that to the URLRequests SupportsUserData::Data and bubble | |
| 36 // that up to Java for the purpose of the API. | |
| 37 ResourceRequestInfo::GetRenderViewForRequest( | 45 ResourceRequestInfo::GetRenderViewForRequest( |
| 38 request, &render_process_id_, &render_view_id_); | 46 request, &render_process_id_, &render_view_id_); |
| 39 | 47 |
| 48 UrlRequestAuthAttemptsData* count = |
| 49 static_cast<UrlRequestAuthAttemptsData*>( |
| 50 request->GetUserData(kAuthAttemptsKey)); |
| 51 |
| 52 if (count == NULL) { |
| 53 count = new UrlRequestAuthAttemptsData(); |
| 54 request->SetUserData(kAuthAttemptsKey, count); |
| 55 } |
| 56 |
| 40 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 57 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| 41 base::Bind(&AwLoginDelegate::HandleHttpAuthRequestOnUIThread, | 58 base::Bind(&AwLoginDelegate::HandleHttpAuthRequestOnUIThread, |
| 42 make_scoped_refptr(this))); | 59 make_scoped_refptr(this), (count->auth_attempts_ == 0))); |
| 60 count->auth_attempts_++; |
| 43 } | 61 } |
| 44 | 62 |
| 45 AwLoginDelegate::~AwLoginDelegate() { | 63 AwLoginDelegate::~AwLoginDelegate() { |
| 46 } | 64 } |
| 47 | 65 |
| 48 void AwLoginDelegate::Proceed(const string16& user, | 66 void AwLoginDelegate::Proceed(const string16& user, |
| 49 const string16& password) { | 67 const string16& password) { |
| 50 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 68 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 51 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, | 69 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, |
| 52 base::Bind(&AwLoginDelegate::ProceedOnIOThread, | 70 base::Bind(&AwLoginDelegate::ProceedOnIOThread, |
| 53 make_scoped_refptr(this), user, password)); | 71 make_scoped_refptr(this), user, password)); |
| 54 } | 72 } |
| 55 | 73 |
| 56 void AwLoginDelegate::Cancel() { | 74 void AwLoginDelegate::Cancel() { |
| 57 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 75 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 58 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, | 76 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, |
| 59 base::Bind(&AwLoginDelegate::CancelOnIOThread, | 77 base::Bind(&AwLoginDelegate::CancelOnIOThread, |
| 60 make_scoped_refptr(this))); | 78 make_scoped_refptr(this))); |
| 61 } | 79 } |
| 62 | 80 |
| 63 void AwLoginDelegate::HandleHttpAuthRequestOnUIThread() { | 81 void AwLoginDelegate::HandleHttpAuthRequestOnUIThread( |
| 82 bool first_auth_attempt) { |
| 64 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 83 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 65 | 84 |
| 66 aw_http_auth_handler_.reset(AwHttpAuthHandlerBase::Create(this, auth_info_)); | 85 aw_http_auth_handler_.reset(AwHttpAuthHandlerBase::Create( |
| 86 this, auth_info_, first_auth_attempt)); |
| 67 | 87 |
| 68 RenderViewHost* render_view_host = RenderViewHost::FromID( | 88 RenderViewHost* render_view_host = RenderViewHost::FromID( |
| 69 render_process_id_, render_view_id_); | 89 render_process_id_, render_view_id_); |
| 70 if (!render_view_host) { | 90 if (!render_view_host) { |
| 71 Cancel(); | 91 Cancel(); |
| 72 return; | 92 return; |
| 73 } | 93 } |
| 74 | 94 |
| 75 WebContents* web_contents = WebContents::FromRenderViewHost( | 95 WebContents* web_contents = WebContents::FromRenderViewHost( |
| 76 render_view_host); | 96 render_view_host); |
| (...skipping 17 matching lines...) Expand all Loading... |
| 94 } | 114 } |
| 95 } | 115 } |
| 96 | 116 |
| 97 void AwLoginDelegate::OnRequestCancelled() { | 117 void AwLoginDelegate::OnRequestCancelled() { |
| 98 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 118 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 99 request_ = NULL; | 119 request_ = NULL; |
| 100 aw_http_auth_handler_.reset(); | 120 aw_http_auth_handler_.reset(); |
| 101 } | 121 } |
| 102 | 122 |
| 103 } // namespace android_webview | 123 } // namespace android_webview |
| OLD | NEW |