Commit 0dd7eee5 authored by Kurt Horimoto's avatar Kurt Horimoto Committed by Commit Bot

[iOS] Move HTTP auth completion callback to standalone C function.

This was previously handled by a WebStateDelegateTabHelper instance
method that was bound with a weak pointer.  If the WebState is destroyed
before the completion callback is executed, this means that the
WebKit-provided completion block will not be executed since the weak
pointer makes the callback a no-op.  This CL moves this logic into a
standalone C function that will execute the WebKit callback regardless
of whether the WebState has been destroyed.

Bug: 1087858
Change-Id: Id1a8ccfbead397d04b923b04e069589ea8e83967
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2225461
Commit-Queue: Kurt Horimoto <kkhorimoto@chromium.org>
Reviewed-by: default avatarMike Dougherty <michaeldo@chromium.org>
Reviewed-by: default avatarEugene But <eugenebut@chromium.org>
Cr-Commit-Position: refs/heads/master@{#773886}
parent ad33b373
...@@ -5,14 +5,11 @@ ...@@ -5,14 +5,11 @@
#ifndef IOS_CHROME_BROWSER_WEB_WEB_STATE_DELEGATE_TAB_HELPER_H_ #ifndef IOS_CHROME_BROWSER_WEB_WEB_STATE_DELEGATE_TAB_HELPER_H_
#define IOS_CHROME_BROWSER_WEB_WEB_STATE_DELEGATE_TAB_HELPER_H_ #define IOS_CHROME_BROWSER_WEB_WEB_STATE_DELEGATE_TAB_HELPER_H_
#include "base/memory/weak_ptr.h"
#import "ios/chrome/browser/ui/dialogs/overlay_java_script_dialog_presenter.h" #import "ios/chrome/browser/ui/dialogs/overlay_java_script_dialog_presenter.h"
#import "ios/web/public/web_state_delegate.h" #import "ios/web/public/web_state_delegate.h"
#include "ios/web/public/web_state_observer.h" #include "ios/web/public/web_state_observer.h"
#import "ios/web/public/web_state_user_data.h" #import "ios/web/public/web_state_user_data.h"
class OverlayResponse;
// Tab helper that handles the WebStateDelegate implementation. // Tab helper that handles the WebStateDelegate implementation.
class WebStateDelegateTabHelper class WebStateDelegateTabHelper
: public web::WebStateDelegate, : public web::WebStateDelegate,
...@@ -41,12 +38,7 @@ class WebStateDelegateTabHelper ...@@ -41,12 +38,7 @@ class WebStateDelegateTabHelper
// WebStateObserver: // WebStateObserver:
void WebStateDestroyed(web::WebState* web_state) override; void WebStateDestroyed(web::WebState* web_state) override;
// Callback for HTTP authentication dialogs.
void OnHTTPAuthOverlayFinished(web::WebStateDelegate::AuthCallback callback,
OverlayResponse* response);
OverlayJavaScriptDialogPresenter java_script_dialog_presenter_; OverlayJavaScriptDialogPresenter java_script_dialog_presenter_;
base::WeakPtrFactory<WebStateDelegateTabHelper> weak_factory_;
}; };
#endif // IOS_CHROME_BROWSER_WEB_WEB_STATE_DELEGATE_TAB_HELPER_H_ #endif // IOS_CHROME_BROWSER_WEB_WEB_STATE_DELEGATE_TAB_HELPER_H_
...@@ -18,10 +18,28 @@ ...@@ -18,10 +18,28 @@
#error "This file requires ARC support." #error "This file requires ARC support."
#endif #endif
namespace {
// Callback for HTTP authentication dialogs. This callback is a standalone
// function rather than an instance method. This is to ensure that the callback
// can be executed regardless of whether the tab helper has been destroyed.
void OnHTTPAuthOverlayFinished(web::WebStateDelegate::AuthCallback callback,
OverlayResponse* response) {
if (response) {
HTTPAuthOverlayResponseInfo* auth_info =
response->GetInfo<HTTPAuthOverlayResponseInfo>();
if (auth_info) {
callback.Run(base::SysUTF8ToNSString(auth_info->username()),
base::SysUTF8ToNSString(auth_info->password()));
return;
}
}
callback.Run(nil, nil);
}
} // namespace
WEB_STATE_USER_DATA_KEY_IMPL(WebStateDelegateTabHelper) WEB_STATE_USER_DATA_KEY_IMPL(WebStateDelegateTabHelper)
WebStateDelegateTabHelper::WebStateDelegateTabHelper(web::WebState* web_state) WebStateDelegateTabHelper::WebStateDelegateTabHelper(web::WebState* web_state) {
: weak_factory_(this) {
web_state->AddObserver(this); web_state->AddObserver(this);
} }
...@@ -48,8 +66,7 @@ void WebStateDelegateTabHelper::OnAuthRequired( ...@@ -48,8 +66,7 @@ void WebStateDelegateTabHelper::OnAuthRequired(
nsurlprotectionspace_util::RequesterOrigin(protection_space), message, nsurlprotectionspace_util::RequesterOrigin(protection_space), message,
default_username); default_username);
request->GetCallbackManager()->AddCompletionCallback( request->GetCallbackManager()->AddCompletionCallback(
base::BindOnce(&WebStateDelegateTabHelper::OnHTTPAuthOverlayFinished, base::BindOnce(&OnHTTPAuthOverlayFinished, callback));
weak_factory_.GetWeakPtr(), callback));
OverlayRequestQueue::FromWebState(source, OverlayModality::kWebContentArea) OverlayRequestQueue::FromWebState(source, OverlayModality::kWebContentArea)
->AddRequest(std::move(request)); ->AddRequest(std::move(request));
} }
...@@ -60,21 +77,3 @@ void WebStateDelegateTabHelper::WebStateDestroyed(web::WebState* web_state) { ...@@ -60,21 +77,3 @@ void WebStateDelegateTabHelper::WebStateDestroyed(web::WebState* web_state) {
web_state->RemoveObserver(this); web_state->RemoveObserver(this);
} }
#pragma mark - Overlay Callbacks
void WebStateDelegateTabHelper::OnHTTPAuthOverlayFinished(
web::WebStateDelegate::AuthCallback callback,
OverlayResponse* response) {
if (!response) {
callback.Run(nil, nil);
return;
}
HTTPAuthOverlayResponseInfo* auth_info =
response->GetInfo<HTTPAuthOverlayResponseInfo>();
if (!auth_info) {
callback.Run(nil, nil);
return;
}
callback.Run(base::SysUTF8ToNSString(auth_info->username()),
base::SysUTF8ToNSString(auth_info->password()));
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment