Commit e2e6c9ee authored by James Vecore's avatar James Vecore Committed by Commit Bot

[Nearby] Add OnShutdown event to the observer interface

Change-Id: I756d575d9ce93df26f52c31201274dab67f2c0ad
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2404391
Commit-Queue: James Vecore <vecore@google.com>
Auto-Submit: James Vecore <vecore@google.com>
Reviewed-by: default avatarRyan Hansberry <hansberry@chromium.org>
Cr-Commit-Position: refs/heads/master@{#805980}
parent cfe7cada
...@@ -65,6 +65,10 @@ class NearbySharingService : public KeyedService { ...@@ -65,6 +65,10 @@ class NearbySharingService : public KeyedService {
class Observer : public base::CheckedObserver { class Observer : public base::CheckedObserver {
public: public:
virtual void OnHighVisibilityChanged(bool in_high_visibility) = 0; virtual void OnHighVisibilityChanged(bool in_high_visibility) = 0;
// Called during the |KeyedService| shutdown, but before everything has been
// cleaned up. It is safe to remove any observers on this event.
virtual void OnShutdown() = 0;
}; };
using StatusCodesCallback = using StatusCodesCallback =
......
...@@ -252,6 +252,12 @@ NearbySharingServiceImpl::~NearbySharingServiceImpl() { ...@@ -252,6 +252,12 @@ NearbySharingServiceImpl::~NearbySharingServiceImpl() {
} }
void NearbySharingServiceImpl::Shutdown() { void NearbySharingServiceImpl::Shutdown() {
// Before we clean up, lets give observers a heads up we are shutting down.
for (auto& observer : observers_) {
observer.OnShutdown();
}
observers_.Clear();
// Clear in-progress transfers. // Clear in-progress transfers.
ClearOutgoingShareTargetInfoMap(); ClearOutgoingShareTargetInfoMap();
incoming_share_target_info_map_.clear(); incoming_share_target_info_map_.clear();
......
...@@ -922,11 +922,22 @@ class NearbySharingServiceImplSendFailureTest ...@@ -922,11 +922,22 @@ class NearbySharingServiceImplSendFailureTest
class TestObserver : public NearbySharingService::Observer { class TestObserver : public NearbySharingService::Observer {
public: public:
explicit TestObserver(NearbySharingService* service) : service_(service) {
service_->AddObserver(this);
}
void OnHighVisibilityChanged(bool in_high_visibility) override { void OnHighVisibilityChanged(bool in_high_visibility) override {
in_high_visibility_ = in_high_visibility; in_high_visibility_ = in_high_visibility;
} }
void OnShutdown() override {
shutdown_called_ = true;
service_->RemoveObserver(this);
}
bool in_high_visibility_ = false; bool in_high_visibility_ = false;
bool shutdown_called_ = false;
NearbySharingService* service_;
}; };
} // namespace } // namespace
...@@ -3432,14 +3443,13 @@ TEST_F(NearbySharingServiceImplTest, ProfileChangedControlsAdvertising) { ...@@ -3432,14 +3443,13 @@ TEST_F(NearbySharingServiceImplTest, ProfileChangedControlsAdvertising) {
TEST_F(NearbySharingServiceImplTest, TEST_F(NearbySharingServiceImplTest,
RegisterForegroundReceiveSurfaceEntersHighVisibility) { RegisterForegroundReceiveSurfaceEntersHighVisibility) {
TestObserver observer; TestObserver observer(service_.get());
NiceMock<MockTransferUpdateCallback> callback; NiceMock<MockTransferUpdateCallback> callback;
ui::ScopedSetIdleState unlocked(ui::IDLE_STATE_IDLE); ui::ScopedSetIdleState unlocked(ui::IDLE_STATE_IDLE);
SetConnectionType(net::NetworkChangeNotifier::CONNECTION_WIFI); SetConnectionType(net::NetworkChangeNotifier::CONNECTION_WIFI);
SetVisibility(nearby_share::mojom::Visibility::kAllContacts); SetVisibility(nearby_share::mojom::Visibility::kAllContacts);
local_device_data_manager()->SetDeviceName(kDeviceName); local_device_data_manager()->SetDeviceName(kDeviceName);
service_->AddObserver(&observer);
// To start, we should not be in high visibility state. // To start, we should not be in high visibility state.
EXPECT_FALSE(service_->IsInHighVisibility()); EXPECT_FALSE(service_->IsInHighVisibility());
...@@ -3460,5 +3470,19 @@ TEST_F(NearbySharingServiceImplTest, ...@@ -3460,5 +3470,19 @@ TEST_F(NearbySharingServiceImplTest,
EXPECT_FALSE(service_->IsInHighVisibility()); EXPECT_FALSE(service_->IsInHighVisibility());
EXPECT_FALSE(observer.in_high_visibility_); EXPECT_FALSE(observer.in_high_visibility_);
// Remove the observer before it goes out of scope.
service_->RemoveObserver(&observer); service_->RemoveObserver(&observer);
} }
TEST_F(NearbySharingServiceImplTest, ShutdownCallsObservers) {
TestObserver observer(service_.get());
EXPECT_FALSE(observer.shutdown_called_);
service_->Shutdown();
EXPECT_TRUE(observer.shutdown_called_);
// Prevent a double shutdown.
service_.reset();
}
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