Commit 20b0fd10 authored by Clemens Arbesser's avatar Clemens Arbesser Committed by Commit Bot

[Autofill Assistant] DetachUi when necessary to avoid race condition.

Bug: b/166266031
Change-Id: I2ab6523282c7eb271c4874abe6b4238e7ff1dffd
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2375170
Commit-Queue: Clemens Arbesser <arbesser@google.com>
Reviewed-by: default avatarLuca Hunkeler <hluca@google.com>
Cr-Commit-Position: refs/heads/master@{#801718}
parent 99ecd216
......@@ -587,12 +587,19 @@ void ClientAndroid::InvalidateAccessToken(const std::string& access_token) {
}
void ClientAndroid::CreateController(std::unique_ptr<Service> service) {
if (controller_) {
DestroyController();
}
controller_ = std::make_unique<Controller>(
web_contents_, /* client= */ this, base::DefaultTickClock::GetInstance(),
std::move(service));
}
void ClientAndroid::DestroyController() {
if (controller_ && ui_controller_android_ &&
ui_controller_android_->IsAttachedTo(controller_.get())) {
ui_controller_android_->Detach();
}
controller_.reset();
started_ = false;
}
......
......@@ -315,8 +315,7 @@ void UiControllerAndroid::Attach(content::WebContents* web_contents,
client_ = client;
// Detach from the current ui_delegate, if one was set previously.
if (ui_delegate_)
ui_delegate_->RemoveObserver(this);
Detach();
// Attach to the new ui_delegate.
ui_delegate_ = ui_delegate;
......@@ -385,12 +384,17 @@ void UiControllerAndroid::Attach(content::WebContents* web_contents,
SetVisible(true);
}
void UiControllerAndroid::Detach() {
if (ui_delegate_) {
ui_delegate_->RemoveObserver(this);
}
ui_delegate_ = nullptr;
}
UiControllerAndroid::~UiControllerAndroid() {
Java_AutofillAssistantUiController_clearNativePtr(AttachCurrentThread(),
java_object_);
if (ui_delegate_)
ui_delegate_->RemoveObserver(this);
Detach();
}
base::android::ScopedJavaLocalRef<jobject> UiControllerAndroid::GetModel() {
......
......@@ -66,6 +66,10 @@ class UiControllerAndroid : public ControllerObserver {
Client* client,
UiDelegate* ui_delegate);
// Detaches the UI from |ui_delegate_|. It will stop receiving notifications
// from the delegate until it is attached again.
void Detach();
// Returns true if the UI is attached to a delegate.
bool IsAttached() { return ui_delegate_ != nullptr; }
......
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