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) { ...@@ -587,12 +587,19 @@ void ClientAndroid::InvalidateAccessToken(const std::string& access_token) {
} }
void ClientAndroid::CreateController(std::unique_ptr<Service> service) { void ClientAndroid::CreateController(std::unique_ptr<Service> service) {
if (controller_) {
DestroyController();
}
controller_ = std::make_unique<Controller>( controller_ = std::make_unique<Controller>(
web_contents_, /* client= */ this, base::DefaultTickClock::GetInstance(), web_contents_, /* client= */ this, base::DefaultTickClock::GetInstance(),
std::move(service)); std::move(service));
} }
void ClientAndroid::DestroyController() { void ClientAndroid::DestroyController() {
if (controller_ && ui_controller_android_ &&
ui_controller_android_->IsAttachedTo(controller_.get())) {
ui_controller_android_->Detach();
}
controller_.reset(); controller_.reset();
started_ = false; started_ = false;
} }
......
...@@ -315,8 +315,7 @@ void UiControllerAndroid::Attach(content::WebContents* web_contents, ...@@ -315,8 +315,7 @@ void UiControllerAndroid::Attach(content::WebContents* web_contents,
client_ = client; client_ = client;
// Detach from the current ui_delegate, if one was set previously. // Detach from the current ui_delegate, if one was set previously.
if (ui_delegate_) Detach();
ui_delegate_->RemoveObserver(this);
// Attach to the new ui_delegate. // Attach to the new ui_delegate.
ui_delegate_ = ui_delegate; ui_delegate_ = ui_delegate;
...@@ -385,12 +384,17 @@ void UiControllerAndroid::Attach(content::WebContents* web_contents, ...@@ -385,12 +384,17 @@ void UiControllerAndroid::Attach(content::WebContents* web_contents,
SetVisible(true); SetVisible(true);
} }
void UiControllerAndroid::Detach() {
if (ui_delegate_) {
ui_delegate_->RemoveObserver(this);
}
ui_delegate_ = nullptr;
}
UiControllerAndroid::~UiControllerAndroid() { UiControllerAndroid::~UiControllerAndroid() {
Java_AutofillAssistantUiController_clearNativePtr(AttachCurrentThread(), Java_AutofillAssistantUiController_clearNativePtr(AttachCurrentThread(),
java_object_); java_object_);
Detach();
if (ui_delegate_)
ui_delegate_->RemoveObserver(this);
} }
base::android::ScopedJavaLocalRef<jobject> UiControllerAndroid::GetModel() { base::android::ScopedJavaLocalRef<jobject> UiControllerAndroid::GetModel() {
......
...@@ -66,6 +66,10 @@ class UiControllerAndroid : public ControllerObserver { ...@@ -66,6 +66,10 @@ class UiControllerAndroid : public ControllerObserver {
Client* client, Client* client,
UiDelegate* ui_delegate); 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. // Returns true if the UI is attached to a delegate.
bool IsAttached() { return ui_delegate_ != nullptr; } 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