Bug 1261299 - Add sSelectionTransferable and to get the current selection (chrome/content) needed for the OSX service menu. r=mstange
authorJimmy Wang <jimmyw22@gmail.com>
Fri, 08 Jul 2016 11:21:43 -0400
changeset 309900 fb83488b8880a9d8a4c97089fbf5f3c5a3b0301c
parent 309899 fc0cbef7e3e9af63035b38ebfec88806a8ce0dc6
child 309901 6888ab7ba2f3467c3f89941034d5d0b5538cdf84
push id20342
push userkwierso@gmail.com
push dateThu, 18 Aug 2016 23:33:24 +0000
treeherderfx-team@abac53d4d247 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmstange
bugs1261299
milestone51.0a1
Bug 1261299 - Add sSelectionTransferable and to get the current selection (chrome/content) needed for the OSX service menu. r=mstange 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
widget/cocoa/nsClipboard.h
widget/cocoa/nsClipboard.mm
widget/nsBaseClipboard.cpp
widget/nsBaseClipboard.h
--- 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;
 
 };