Bug 1657404 - Use WeakPtr for observer pattern in GamepadPlatformService r=handyman

Differential Revision: https://phabricator.services.mozilla.com/D93085
This commit is contained in:
Chris Martin 2020-10-09 16:42:53 +00:00
Родитель 70a97d98be
Коммит d3e711c87d
3 изменённых файлов: 10 добавлений и 4 удалений

Просмотреть файл

@ -34,7 +34,7 @@ GamepadPlatformService::MonitoringState::~MonitoringState() {
}
void GamepadPlatformService::MonitoringState::AddObserver(
RefPtr<GamepadTestChannelParent> aParent) {
WeakPtr<GamepadTestChannelParent> aParent) {
AssertIsOnBackgroundThread();
MOZ_ASSERT(aParent);
MOZ_ASSERT(!mObservers.Contains(aParent));
@ -60,6 +60,9 @@ void GamepadPlatformService::MonitoringState::Set(bool aIsMonitoring) {
if (mIsMonitoring != aIsMonitoring) {
mIsMonitoring = aIsMonitoring;
for (auto& observer : mObservers) {
// Since each GamepadTestChannelParent removes itself in its dtor, this
// should never be nullptr
MOZ_RELEASE_ASSERT(observer);
observer->OnMonitoringStateChanged(aIsMonitoring);
}
}

Просмотреть файл

@ -12,6 +12,7 @@
#include <map>
#include "mozilla/Mutex.h"
#include "mozilla/StaticPtr.h"
#include "mozilla/WeakPtr.h"
namespace mozilla {
namespace dom {
@ -39,7 +40,7 @@ class GamepadPlatformService final {
MonitoringState() = default;
~MonitoringState();
void AddObserver(RefPtr<GamepadTestChannelParent> aParent);
void AddObserver(WeakPtr<GamepadTestChannelParent> aParent);
void RemoveObserver(GamepadTestChannelParent* aParent);
bool IsMonitoring() const;
@ -53,7 +54,7 @@ class GamepadPlatformService final {
void Set(bool aIsMonitoring);
bool mIsMonitoring{false};
nsTArray<RefPtr<GamepadTestChannelParent>> mObservers;
nsTArray<WeakPtr<GamepadTestChannelParent>> mObservers;
friend class GamepadPlatformService;
};

Просмотреть файл

@ -5,6 +5,7 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "mozilla/dom/PGamepadTestChannelParent.h"
#include "mozilla/WeakPtr.h"
#ifndef mozilla_dom_GamepadTestChannelParent_h_
# define mozilla_dom_GamepadTestChannelParent_h_
@ -12,7 +13,8 @@
namespace mozilla {
namespace dom {
class GamepadTestChannelParent final : public PGamepadTestChannelParent {
class GamepadTestChannelParent final : public PGamepadTestChannelParent,
public SupportsWeakPtr {
public:
NS_INLINE_DECL_THREADSAFE_REFCOUNTING(GamepadTestChannelParent)