mirror of
https://github.com/mozilla/gecko-dev.git
synced 2024-10-08 02:14:43 +00:00
Bug 1047408 - Remove nsTWeakRef.h r=smaug
Differential Revision: https://phabricator.services.mozilla.com/D32319 --HG-- extra : moz-landing-system : lando
This commit is contained in:
parent
182c086abf
commit
00782757b5
@ -5,6 +5,7 @@
|
||||
|
||||
#include "mozilla/BasicEvents.h"
|
||||
#include "mozilla/DebugOnly.h"
|
||||
#include "mozilla/WeakPtr.h"
|
||||
|
||||
#include "windows.h"
|
||||
#include "windowsx.h"
|
||||
@ -21,7 +22,6 @@
|
||||
#include "nsWindowsDllInterceptor.h"
|
||||
#include "nsPluginNativeWindow.h"
|
||||
#include "nsThreadUtils.h"
|
||||
#include "nsTWeakRef.h"
|
||||
#include "nsCrashOnException.h"
|
||||
|
||||
using namespace mozilla;
|
||||
@ -40,7 +40,7 @@ using namespace mozilla;
|
||||
#define WM_USER_FLASH WM_USER + 1
|
||||
static UINT sWM_FLASHBOUNCEMSG = 0;
|
||||
|
||||
typedef nsTWeakRef<class nsPluginNativeWindowWin> PluginWindowWeakRef;
|
||||
class nsPluginNativeWindowWin;
|
||||
|
||||
/**
|
||||
* PLEvent handling code
|
||||
@ -48,8 +48,8 @@ typedef nsTWeakRef<class nsPluginNativeWindowWin> PluginWindowWeakRef;
|
||||
class PluginWindowEvent : public Runnable {
|
||||
public:
|
||||
PluginWindowEvent();
|
||||
void Init(const PluginWindowWeakRef& ref, HWND hWnd, UINT msg, WPARAM wParam,
|
||||
LPARAM lParam);
|
||||
void Init(WeakPtr<nsPluginNativeWindowWin> aRef, HWND aWnd, UINT aMsg,
|
||||
WPARAM aParam, LPARAM aLParam);
|
||||
void Clear();
|
||||
HWND GetWnd() { return mWnd; };
|
||||
UINT GetMsg() { return mMsg; };
|
||||
@ -60,7 +60,7 @@ class PluginWindowEvent : public Runnable {
|
||||
NS_DECL_NSIRUNNABLE
|
||||
|
||||
protected:
|
||||
PluginWindowWeakRef mPluginWindowRef;
|
||||
WeakPtr<nsPluginNativeWindowWin> mPluginWindowRef;
|
||||
HWND mWnd;
|
||||
UINT mMsg;
|
||||
WPARAM mWParam;
|
||||
@ -78,11 +78,11 @@ void PluginWindowEvent::Clear() {
|
||||
mLParam = 0;
|
||||
}
|
||||
|
||||
void PluginWindowEvent::Init(const PluginWindowWeakRef& ref, HWND aWnd,
|
||||
void PluginWindowEvent::Init(WeakPtr<nsPluginNativeWindowWin> aRef, HWND aWnd,
|
||||
UINT aMsg, WPARAM aWParam, LPARAM aLParam) {
|
||||
NS_ASSERTION(aWnd != nullptr, "invalid plugin event value");
|
||||
NS_ASSERTION(mWnd == nullptr, "event already in use");
|
||||
mPluginWindowRef = ref;
|
||||
mPluginWindowRef = aRef;
|
||||
mWnd = aWnd;
|
||||
mMsg = aMsg;
|
||||
mWParam = aWParam;
|
||||
@ -93,10 +93,12 @@ void PluginWindowEvent::Init(const PluginWindowWeakRef& ref, HWND aWnd,
|
||||
* nsPluginNativeWindow Windows specific class declaration
|
||||
*/
|
||||
|
||||
class nsPluginNativeWindowWin : public nsPluginNativeWindow {
|
||||
class nsPluginNativeWindowWin
|
||||
: public nsPluginNativeWindow,
|
||||
public SupportsWeakPtr<nsPluginNativeWindowWin> {
|
||||
public:
|
||||
MOZ_DECLARE_WEAKREFERENCE_TYPENAME(nsPluginNativeWindowWin)
|
||||
nsPluginNativeWindowWin();
|
||||
virtual ~nsPluginNativeWindowWin();
|
||||
|
||||
virtual nsresult CallSetWindow(
|
||||
RefPtr<nsNPAPIPluginInstance>& aPluginInstance);
|
||||
@ -116,7 +118,7 @@ class nsPluginNativeWindowWin : public nsPluginNativeWindow {
|
||||
private:
|
||||
WNDPROC mPluginWinProc;
|
||||
WNDPROC mPrevWinProc;
|
||||
PluginWindowWeakRef mWeakRef;
|
||||
WeakPtr<nsPluginNativeWindowWin> mWeakRef;
|
||||
RefPtr<PluginWindowEvent> mCachedPluginWindowEvent;
|
||||
|
||||
HWND mParentWnd;
|
||||
@ -458,18 +460,12 @@ nsPluginNativeWindowWin::nsPluginNativeWindowWin() : nsPluginNativeWindow() {
|
||||
}
|
||||
}
|
||||
|
||||
nsPluginNativeWindowWin::~nsPluginNativeWindowWin() {
|
||||
// clear weak reference to self to prevent any pending events from
|
||||
// dereferencing this.
|
||||
mWeakRef.forget();
|
||||
}
|
||||
|
||||
WNDPROC nsPluginNativeWindowWin::GetPrevWindowProc() { return mPrevWinProc; }
|
||||
|
||||
WNDPROC nsPluginNativeWindowWin::GetWindowProc() { return mPluginWinProc; }
|
||||
|
||||
NS_IMETHODIMP PluginWindowEvent::Run() {
|
||||
nsPluginNativeWindowWin* win = mPluginWindowRef.get();
|
||||
nsPluginNativeWindowWin* win = mPluginWindowRef;
|
||||
if (!win) return NS_OK;
|
||||
|
||||
HWND hWnd = GetWnd();
|
||||
|
@ -88,7 +88,6 @@ EXPORTS += [
|
||||
'nsQueryObject.h',
|
||||
'nsSystemInfo.h',
|
||||
'nsTraceRefcnt.h',
|
||||
'nsTWeakRef.h',
|
||||
'nsVersionComparator.h',
|
||||
'nsWeakReference.h',
|
||||
]
|
||||
|
@ -1,159 +0,0 @@
|
||||
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
||||
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
||||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
#ifndef nsTWeakRef_h__
|
||||
#define nsTWeakRef_h__
|
||||
|
||||
#ifndef nsDebug_h___
|
||||
# include "nsDebug.h"
|
||||
#endif
|
||||
|
||||
/**
|
||||
* A weak reference class for use with generic C++ objects. NOT THREADSAFE!
|
||||
*
|
||||
* Example usage:
|
||||
*
|
||||
* class A {
|
||||
* public:
|
||||
* A() : mWeakSelf(this) {
|
||||
* }
|
||||
* ~A() {
|
||||
* mWeakSelf.forget();
|
||||
* }
|
||||
* void Bar() { printf("Bar!\n"); }
|
||||
* const nsTWeakRef<A> &AsWeakRef() const { return mWeakSelf; }
|
||||
* private:
|
||||
* nsTWeakRef<A> mWeakSelf;
|
||||
* };
|
||||
*
|
||||
* class B {
|
||||
* public:
|
||||
* void SetA(const nsTWeakRef<A> &a) {
|
||||
* mA = a;
|
||||
* }
|
||||
* void Foo() {
|
||||
* if (mA)
|
||||
* mA->Bar();
|
||||
* }
|
||||
* private:
|
||||
* nsTWeakRef<A> mA;
|
||||
* };
|
||||
*
|
||||
* void Test() {
|
||||
* B b;
|
||||
* {
|
||||
* A a;
|
||||
* b.SetA(a.AsWeakRef());
|
||||
* b.Foo(); // prints "Bar!"
|
||||
* }
|
||||
* b.Foo(); // prints nothing because |a| has already been destroyed
|
||||
* }
|
||||
*
|
||||
* One can imagine much more complex examples, especially when asynchronous
|
||||
* event processing is involved.
|
||||
*
|
||||
* Keep in mind that you should only ever need a class like this when you have
|
||||
* multiple instances of B, such that it is not possible for A and B to simply
|
||||
* have pointers to one another.
|
||||
*/
|
||||
template <class Type>
|
||||
class nsTWeakRef {
|
||||
public:
|
||||
~nsTWeakRef() {}
|
||||
|
||||
/**
|
||||
* Construct from an object pointer (may be null).
|
||||
*/
|
||||
explicit nsTWeakRef(Type* aObj = nullptr) {
|
||||
if (aObj) {
|
||||
mRef = new Inner(aObj);
|
||||
} else {
|
||||
mRef = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Construct from another weak reference object.
|
||||
*/
|
||||
explicit nsTWeakRef(const nsTWeakRef<Type>& aOther) : mRef(aOther.mRef) {}
|
||||
|
||||
/**
|
||||
* Assign from an object pointer.
|
||||
*/
|
||||
nsTWeakRef<Type>& operator=(Type* aObj) {
|
||||
if (aObj) {
|
||||
mRef = new Inner(aObj);
|
||||
} else {
|
||||
mRef = nullptr;
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Assign from another weak reference object.
|
||||
*/
|
||||
nsTWeakRef<Type>& operator=(const nsTWeakRef<Type>& aOther) {
|
||||
mRef = aOther.mRef;
|
||||
return *this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the referenced object. This method may return null if the reference
|
||||
* has been cleared or if an out-of-memory error occurred at assignment.
|
||||
*/
|
||||
Type* get() const { return mRef ? mRef->mObj : nullptr; }
|
||||
|
||||
/**
|
||||
* Called to "null out" the weak reference. Typically, the object referenced
|
||||
* by this weak reference calls this method when it is being destroyed.
|
||||
* @returns The former referenced object.
|
||||
*/
|
||||
Type* forget() {
|
||||
Type* obj;
|
||||
if (mRef) {
|
||||
obj = mRef->mObj;
|
||||
mRef->mObj = nullptr;
|
||||
mRef = nullptr;
|
||||
} else {
|
||||
obj = nullptr;
|
||||
}
|
||||
return obj;
|
||||
}
|
||||
|
||||
/**
|
||||
* Allow |*this| to be treated as a |Type*| for convenience.
|
||||
*/
|
||||
operator Type*() const { return get(); }
|
||||
|
||||
/**
|
||||
* Allow |*this| to be treated as a |Type*| for convenience. Use with
|
||||
* caution since this method will crash if the referenced object is null.
|
||||
*/
|
||||
Type* operator->() const MOZ_NO_ADDREF_RELEASE_ON_RETURN {
|
||||
NS_ASSERTION(
|
||||
mRef && mRef->mObj,
|
||||
"You can't dereference a null weak reference with operator->().");
|
||||
return get();
|
||||
}
|
||||
|
||||
private:
|
||||
struct Inner {
|
||||
int mCnt;
|
||||
Type* mObj;
|
||||
|
||||
explicit Inner(Type* aObj) : mCnt(1), mObj(aObj) {}
|
||||
void AddRef() { ++mCnt; }
|
||||
void Release() {
|
||||
if (--mCnt == 0) {
|
||||
delete this;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
RefPtr<Inner> mRef;
|
||||
};
|
||||
|
||||
#endif // nsTWeakRef_h__
|
@ -34,7 +34,6 @@
|
||||
#include "nsISupports.h"
|
||||
|
||||
#include "nsTArray.h"
|
||||
#include "nsTWeakRef.h"
|
||||
|
||||
#include "nsCOMPtr.h"
|
||||
#include "nsCOMArray.h"
|
||||
|
Loading…
Reference in New Issue
Block a user