Bug 1261299 - Add sSelectionTransferable and to get the current selection (chrome/content) needed for the OSX service menu.
MozReview-Commit-ID: 4n5clge2tr8
***
Bug 1261299 - Add sSelectionTransferable and use in e10s to get the content selection needed for the OSX service menu.
MozReview-Commit-ID: HbZ7S4HfFtn
--- a/widget/cocoa/nsClipboard.h
+++ b/widget/cocoa/nsClipboard.h
@@ -3,16 +3,17 @@
* 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 nsClipboard_h_
#define nsClipboard_h_
#include "nsBaseClipboard.h"
#include "nsXPIDLString.h"
+#include "mozilla/StaticPtr.h"
#import <Cocoa/Cocoa.h>
class nsITransferable;
class nsClipboard : public nsBaseClipboard
{
@@ -20,26 +21,32 @@ public:
nsClipboard();
virtual ~nsClipboard();
// nsIClipboard
NS_IMETHOD HasDataMatchingFlavors(const char** aFlavorList, uint32_t aLength,
int32_t aWhichClipboard, bool *_retval);
NS_IMETHOD SupportsFindClipboard(bool *_retval);
+ // On macOS, cache the transferable of the current selection (chrome/content)
+ // in the parent process. This is needed for the services menu which
+ // requires synchronous access to the current selection.
+ static mozilla::StaticRefPtr<nsITransferable> sSelectionCache;
+
// Helper methods, used also by nsDragService
static NSDictionary* PasteboardDictFromTransferable(nsITransferable *aTransferable);
static bool IsStringType(const nsCString& aMIMEType, NSString** aPasteboardType);
static NSString* WrapHtmlForSystemPasteboard(NSString* aString);
static nsresult TransferableFromPasteboard(nsITransferable *aTransferable, NSPasteboard *pboard);
protected:
// impelement the native clipboard behavior
NS_IMETHOD SetNativeClipboardData(int32_t aWhichClipboard);
NS_IMETHOD GetNativeClipboardData(nsITransferable * aTransferable, int32_t aWhichClipboard);
+ void SetSelectionCache(nsITransferable* aTransferable);
private:
int32_t mCachedClipboard;
int32_t mChangeCount; // Set to the native change count after any modification of the clipboard.
};
#endif // nsClipboard_h_
--- a/widget/cocoa/nsClipboard.mm
+++ b/widget/cocoa/nsClipboard.mm
@@ -30,26 +30,29 @@ using mozilla::LogLevel;
// Screenshots use the (undocumented) png pasteboard type.
#define IMAGE_PASTEBOARD_TYPES NSTIFFPboardType, @"Apple PNG pasteboard type", nil
extern PRLogModuleInfo* sCocoaLog;
extern void EnsureLogInitialized();
+mozilla::StaticRefPtr<nsITransferable> nsClipboard::sSelectionCache;
+
nsClipboard::nsClipboard() : nsBaseClipboard()
{
mCachedClipboard = -1;
mChangeCount = 0;
EnsureLogInitialized();
}
nsClipboard::~nsClipboard()
{
+ sSelectionCache = nullptr;
}
// We separate this into its own function because after an @try, all local
// variables within that function get marked as volatile, and our C++ type
// system doesn't like volatile things.
static NSData*
GetDataFromPasteboard(NSPasteboard* aPasteboard, NSString* aType)
{
@@ -59,16 +62,22 @@ GetDataFromPasteboard(NSPasteboard* aPas
} @catch (NSException* e) {
NS_WARNING(nsPrintfCString("Exception raised while getting data from the pasteboard: \"%s - %s\"",
[[e name] UTF8String], [[e reason] UTF8String]).get());
mozilla::Unused << e;
}
return data;
}
+void
+nsClipboard::SetSelectionCache(nsITransferable *aTransferable)
+{
+ sSelectionCache = aTransferable;
+}
+
NS_IMETHODIMP
nsClipboard::SetNativeClipboardData(int32_t aWhichClipboard)
{
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
if ((aWhichClipboard != kGlobalClipboard && aWhichClipboard != kFindClipboard) || !mTransferable)
return NS_ERROR_FAILURE;
--- a/widget/nsBaseClipboard.cpp
+++ b/widget/nsBaseClipboard.cpp
@@ -29,16 +29,24 @@ NS_IMPL_ISUPPORTS(nsBaseClipboard, nsICl
* Sets the transferable object
*
*/
NS_IMETHODIMP nsBaseClipboard::SetData(nsITransferable * aTransferable, nsIClipboardOwner * anOwner,
int32_t aWhichClipboard)
{
NS_ASSERTION ( aTransferable, "clipboard given a null transferable" );
+ if (aWhichClipboard == kSelectionCache) {
+ if (aTransferable) {
+ SetSelectionCache(aTransferable);
+ return NS_OK;
+ }
+ return NS_ERROR_FAILURE;
+ }
+
if (aTransferable == mTransferable && anOwner == mClipboardOwner)
return NS_OK;
bool selectClipPresent;
SupportsSelectionClipboard(&selectClipPresent);
bool findClipPresent;
SupportsFindClipboard(&findClipPresent);
if ( !selectClipPresent && !findClipPresent && aWhichClipboard != kGlobalClipboard )
return NS_ERROR_FAILURE;
--- a/widget/nsBaseClipboard.h
+++ b/widget/nsBaseClipboard.h
@@ -29,16 +29,17 @@ public:
// nsIClipboard
NS_DECL_NSICLIPBOARD
protected:
virtual ~nsBaseClipboard();
NS_IMETHOD SetNativeClipboardData ( int32_t aWhichClipboard ) = 0;
NS_IMETHOD GetNativeClipboardData ( nsITransferable * aTransferable, int32_t aWhichClipboard ) = 0;
+ virtual void SetSelectionCache (nsITransferable* aTransferable) = 0;
bool mEmptyingForSetData;
bool mIgnoreEmptyNotification;
nsCOMPtr<nsIClipboardOwner> mClipboardOwner;
nsCOMPtr<nsITransferable> mTransferable;
};