Bug 1387170 - Use custom clipboard constructor instead of singleton. r=jrmuizel

This allows instances of the clipboard to be created (like it
was pre-headless).
This commit is contained in:
Brendan Dahl 2017-08-03 15:08:16 -07:00
parent d1dd2ea172
commit 02dc1567cd
3 changed files with 24 additions and 35 deletions

@ -9,7 +9,6 @@
#include "nsArrayUtils.h"
#include "nsClipboard.h"
#include "HeadlessClipboard.h"
#include "nsSupportsPrimitives.h"
#include "nsString.h"
#include "nsReadableUtils.h"
@ -21,7 +20,6 @@
#include "nsIObserverService.h"
#include "mozilla/Services.h"
#include "mozilla/RefPtr.h"
#include "mozilla/ClearOnShutdown.h"
#include "mozilla/TimeStamp.h"
#include "imgIContainer.h"
@ -39,7 +37,6 @@
#include "mozilla/Encoding.h"
#include "gfxPlatform.h"
using namespace mozilla;
@ -81,34 +78,6 @@ selection_request_filter (GdkXEvent *gdk_xevent,
GdkEvent *event,
gpointer data);
namespace mozilla {
namespace clipboard {
StaticRefPtr<nsIClipboard> sInstance;
}
}
/* static */ already_AddRefed<nsIClipboard>
nsClipboard::GetInstance()
{
using namespace mozilla::clipboard;
if (!sInstance) {
if (gfxPlatform::IsHeadless()) {
sInstance = new widget::HeadlessClipboard();
} else {
RefPtr<nsClipboard> clipboard = new nsClipboard();
nsresult rv = clipboard->Init();
if (NS_FAILED(rv)) {
return nullptr;
}
sInstance = clipboard.forget();
}
ClearOnShutdown(&sInstance);
}
RefPtr<nsIClipboard> service = sInstance.get();
return service.forget();
}
nsClipboard::nsClipboard()
{
}

@ -23,8 +23,6 @@ public:
NS_DECL_NSICLIPBOARD
NS_DECL_NSIOBSERVER
static already_AddRefed<nsIClipboard> GetInstance();
// Make sure we are initialized, called from the factory
// constructor
nsresult Init (void);

@ -17,6 +17,7 @@
#include "nsWindow.h"
#include "nsTransferable.h"
#include "nsHTMLFormatConverter.h"
#include "HeadlessClipboard.h"
#ifdef MOZ_X11
#include "nsClipboardHelper.h"
#include "nsClipboard.h"
@ -72,7 +73,6 @@ NS_GENERIC_FACTORY_CONSTRUCTOR(nsHTMLFormatConverter)
#ifdef MOZ_X11
NS_GENERIC_FACTORY_SINGLETON_CONSTRUCTOR(nsIdleServiceGTK, nsIdleServiceGTK::GetInstance)
NS_GENERIC_FACTORY_CONSTRUCTOR(nsClipboardHelper)
NS_GENERIC_FACTORY_SINGLETON_CONSTRUCTOR(nsIClipboard, nsClipboard::GetInstance)
NS_GENERIC_FACTORY_SINGLETON_CONSTRUCTOR(nsDragService, nsDragService::GetInstance)
#endif
NS_GENERIC_FACTORY_SINGLETON_CONSTRUCTOR(nsISound, nsSound::GetInstance)
@ -194,6 +194,28 @@ nsColorPickerConstructor(nsISupports *aOuter, REFNSIID aIID,
return picker->QueryInterface(aIID, aResult);
}
static nsresult
nsClipboardConstructor(nsISupports *aOuter, REFNSIID aIID,
void **aResult)
{
*aResult = nullptr;
if (aOuter != nullptr) {
return NS_ERROR_NO_AGGREGATION;
}
nsCOMPtr<nsIClipboard> inst;
if (gfxPlatform::IsHeadless()) {
inst = new HeadlessClipboard();
} else {
RefPtr<nsClipboard> clipboard = new nsClipboard();
nsresult rv = clipboard->Init();
NS_ENSURE_SUCCESS(rv, rv);
inst = clipboard;
}
return inst->QueryInterface(aIID, aResult);
}
NS_DEFINE_NAMED_CID(NS_WINDOW_CID);
NS_DEFINE_NAMED_CID(NS_CHILD_CID);
NS_DEFINE_NAMED_CID(NS_APPSHELL_CID);
@ -239,7 +261,7 @@ static const mozilla::Module::CIDEntry kWidgetCIDs[] = {
{ &kNS_SOUND_CID, false, nullptr, nsISoundConstructor, Module::MAIN_PROCESS_ONLY },
{ &kNS_TRANSFERABLE_CID, false, nullptr, nsTransferableConstructor },
#ifdef MOZ_X11
{ &kNS_CLIPBOARD_CID, false, nullptr, nsIClipboardConstructor, Module::MAIN_PROCESS_ONLY },
{ &kNS_CLIPBOARD_CID, false, nullptr, nsClipboardConstructor, Module::MAIN_PROCESS_ONLY },
{ &kNS_CLIPBOARDHELPER_CID, false, nullptr, nsClipboardHelperConstructor },
{ &kNS_DRAGSERVICE_CID, false, nullptr, nsDragServiceConstructor, Module::MAIN_PROCESS_ONLY },
#endif