From 0768c4960f31ebc4d620ac797f4b212739b43fc6 Mon Sep 17 00:00:00 2001 From: Sammie Quon Date: Wed, 13 Oct 2021 16:45:27 +0000 Subject: [PATCH] desk_templates: Fix crash when shut down DesksTemplatesPresenter. The DesksClient shuts down before Shell, making the model unusable during Shell shutdown. This adds a observer to check when the model is destroying, to give observers a chance to remove themselves. Fixed: 1258979 Test: manual Change-Id: Iee5e22485c180c09511ac4da4105f9f8f0e73985 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3218653 Reviewed-by: Colin Blundell Commit-Queue: Sammie Quon Cr-Commit-Position: refs/heads/main@{#931088} --- ash/wm/desks/templates/desks_templates_presenter.cc | 4 ++++ ash/wm/desks/templates/desks_templates_presenter.h | 1 + components/desks_storage/core/desk_model.cc | 7 +++++-- components/desks_storage/core/desk_model_observer.h | 7 ++++++- components/desks_storage/core/desk_sync_bridge_unittest.cc | 2 +- 5 files changed, 17 insertions(+), 4 deletions(-) diff --git a/ash/wm/desks/templates/desks_templates_presenter.cc b/ash/wm/desks/templates/desks_templates_presenter.cc index 09c9e5246b98a9..175bb172d84478 100644 --- a/ash/wm/desks/templates/desks_templates_presenter.cc +++ b/ash/wm/desks/templates/desks_templates_presenter.cc @@ -44,6 +44,10 @@ DesksTemplatesPresenter::DesksTemplatesPresenter( DesksTemplatesPresenter::~DesksTemplatesPresenter() = default; +void DesksTemplatesPresenter::OnDeskModelDestroying() { + desk_model_observation_.Reset(); +} + void DesksTemplatesPresenter::OnGetAllEntries( desks_storage::DeskModel::GetAllEntriesStatus status, std::vector entries) { diff --git a/ash/wm/desks/templates/desks_templates_presenter.h b/ash/wm/desks/templates/desks_templates_presenter.h index b92ee6a8cbdb27..5c0523a55a2a3a 100644 --- a/ash/wm/desks/templates/desks_templates_presenter.h +++ b/ash/wm/desks/templates/desks_templates_presenter.h @@ -34,6 +34,7 @@ class ASH_EXPORT DesksTemplatesPresenter : desks_storage::DeskModelObserver { // TODO(sammiequon): Implement these once the model starts sending these // messages. void DeskModelLoaded() override {} + void OnDeskModelDestroying() override; void EntriesAddedOrUpdatedRemotely( const std::vector& new_entries) override {} void EntriesRemovedRemotely(const std::vector& uuids) override {} diff --git a/components/desks_storage/core/desk_model.cc b/components/desks_storage/core/desk_model.cc index 77e10a7089a651..e93e2fc364b731 100644 --- a/components/desks_storage/core/desk_model.cc +++ b/components/desks_storage/core/desk_model.cc @@ -14,7 +14,10 @@ namespace desks_storage { DeskModel::DeskModel() = default; -DeskModel::~DeskModel() = default; +DeskModel::~DeskModel() { + for (DeskModelObserver& observer : observers_) + observer.OnDeskModelDestroying(); +} void DeskModel::AddObserver(DeskModelObserver* observer) { DCHECK(observer); @@ -42,4 +45,4 @@ void DeskModel::SetPolicyDeskTemplates(const std::string& policyJson) { } } -} // namespace desks_storage \ No newline at end of file +} // namespace desks_storage diff --git a/components/desks_storage/core/desk_model_observer.h b/components/desks_storage/core/desk_model_observer.h index bcabeef65249e5..65a1f0dba8f9ce 100644 --- a/components/desks_storage/core/desk_model_observer.h +++ b/components/desks_storage/core/desk_model_observer.h @@ -26,6 +26,11 @@ class DeskModelObserver { // is unsafe to use the model. virtual void DeskModelLoaded() = 0; + // Invoked when the model is about to be destroyed. Gives observes which may + // outlive the model a chance to stop observing. Not pure virtual because it + // needs to be called from the destructor. + virtual void OnDeskModelDestroying() {} + // Invoked when desk templates are added/updated, removed remotely via sync. // This is the mechanism for the sync server to push changes in the state of // the model to clients. @@ -45,4 +50,4 @@ class DeskModelObserver { } // namespace desks_storage -#endif // COMPONENTS_DESKS_STORAGE_CORE_DESK_MODEL_OBSERVER_H_ \ No newline at end of file +#endif // COMPONENTS_DESKS_STORAGE_CORE_DESK_MODEL_OBSERVER_H_ diff --git a/components/desks_storage/core/desk_sync_bridge_unittest.cc b/components/desks_storage/core/desk_sync_bridge_unittest.cc index b3e0c852338244..1186f1bf732976 100644 --- a/components/desks_storage/core/desk_sync_bridge_unittest.cc +++ b/components/desks_storage/core/desk_sync_bridge_unittest.cc @@ -953,4 +953,4 @@ TEST_F(DeskSyncBridgeTest, MergeSyncDataUploadsLocalOnlyEntries) { } // namespace -} // namespace desks_storage \ No newline at end of file +} // namespace desks_storage