Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / ui / base / clipboard / clipboard.h
index 2a8ed7b..be8e730 100644 (file)
 
 #include "base/compiler_specific.h"
 #include "base/gtest_prod_util.h"
-#include "base/memory/scoped_ptr.h"
 #include "base/memory/shared_memory.h"
 #include "base/process/process.h"
 #include "base/strings/string16.h"
 #include "base/threading/platform_thread.h"
 #include "base/threading/thread_checker.h"
 #include "ui/base/clipboard/clipboard_types.h"
-#include "ui/base/ui_export.h"
-
-#if defined(TOOLKIT_GTK)
-#include <gdk/gdk.h>
-#endif
+#include "ui/base/ui_base_export.h"
 
 #if defined(OS_WIN)
 #include <objidl.h>
-#elif defined(OS_ANDROID)
-#include <jni.h>
-
-#include "base/android/jni_android.h"
-#include "base/android/scoped_java_ref.h"
-#endif
-
-#if defined(USE_AURA) && defined(USE_X11)
-#include "base/memory/scoped_ptr.h"
 #endif
 
 namespace base {
@@ -45,16 +31,17 @@ class MessageWindow;
 }  // namespace win
 }  // namespace base
 
+// TODO(dcheng): Temporary until the IPC layer doesn't use WriteObjects().
+namespace content {
+class ClipboardMessageFilter;
+}
+
 namespace gfx {
 class Size;
 }
 
 class SkBitmap;
 
-#if defined(TOOLKIT_GTK)
-typedef struct _GtkClipboard GtkClipboard;
-#endif
-
 #ifdef __OBJC__
 @class NSString;
 #else
@@ -63,8 +50,9 @@ class NSString;
 
 namespace ui {
 class ClipboardTest;
+class ScopedClipboardWriter;
 
-class UI_EXPORT Clipboard : NON_EXPORTED_BASE(public base::ThreadChecker) {
+class UI_BASE_EXPORT Clipboard : NON_EXPORTED_BASE(public base::ThreadChecker) {
  public:
   // MIME type constants.
   static const char kMimeTypeText[];
@@ -75,7 +63,7 @@ class UI_EXPORT Clipboard : NON_EXPORTED_BASE(public base::ThreadChecker) {
   static const char kMimeTypePNG[];
 
   // Platform neutral holder for native data representation of a clipboard type.
-  struct UI_EXPORT FormatType {
+  struct UI_BASE_EXPORT FormatType {
     FormatType();
     ~FormatType();
 
@@ -83,26 +71,27 @@ class UI_EXPORT Clipboard : NON_EXPORTED_BASE(public base::ThreadChecker) {
     std::string Serialize() const;
     static FormatType Deserialize(const std::string& serialization);
 
-#if defined(OS_WIN) || defined(USE_AURA)
+#if !defined(OS_ANDROID)
     // FormatType can be used in a set on some platforms.
     bool operator<(const FormatType& other) const;
 #endif
 
 #if defined(OS_WIN)
     const FORMATETC& ToFormatEtc() const { return data_; }
+#elif defined(USE_AURA) || defined(OS_ANDROID)
+    const std::string& ToString() const { return data_; }
 #elif defined(OS_MACOSX)
+    NSString* ToNSString() const { return data_; }
     // Custom copy and assignment constructor to handle NSString.
     FormatType(const FormatType& other);
     FormatType& operator=(const FormatType& other);
-#elif defined(USE_AURA)
-    const std::string& ToString() const { return data_; }
 #endif
 
+    bool Equals(const FormatType& other) const;
+
    private:
     friend class Clipboard;
 
-    bool Equals(const FormatType& other) const;
-
     // Platform-specific glue used internally by the Clipboard class. Each
     // plaform should define,at least one of each of the following:
     // 1. A constructor that wraps that native clipboard format descriptor.
@@ -114,24 +103,13 @@ class UI_EXPORT Clipboard : NON_EXPORTED_BASE(public base::ThreadChecker) {
 #if defined(OS_WIN)
     explicit FormatType(UINT native_format);
     FormatType(UINT native_format, LONG index);
-    UINT ToUINT() const { return data_.cfFormat; }
     FORMATETC data_;
+#elif defined(USE_AURA) || defined(OS_ANDROID)
+    explicit FormatType(const std::string& native_format);
+    std::string data_;
 #elif defined(OS_MACOSX)
     explicit FormatType(NSString* native_format);
-    NSString* ToNSString() const { return data_; }
     NSString* data_;
-#elif defined(USE_AURA)
-    explicit FormatType(const std::string& native_format);
-    std::string data_;
-#elif defined(TOOLKIT_GTK)
-    explicit FormatType(const std::string& native_format);
-    explicit FormatType(const GdkAtom& native_format);
-    const GdkAtom& ToGdkAtom() const { return data_; }
-    GdkAtom data_;
-#elif defined(OS_ANDROID)
-    explicit FormatType(const std::string& native_format);
-    const std::string& data() const { return data_; }
-    std::string data_;
 #else
 #error No FormatType definition.
 #endif
@@ -139,6 +117,8 @@ class UI_EXPORT Clipboard : NON_EXPORTED_BASE(public base::ThreadChecker) {
     // Copyable and assignable, since this is essentially an opaque value type.
   };
 
+  // TODO(dcheng): Make this private once the IPC layer no longer needs to
+  // serialize this information.
   // ObjectType designates the type of data to be stored in the clipboard. This
   // designation is shared across all OSes. The system-specific designation
   // is defined by FormatType. A single ObjectType might be represented by
@@ -150,9 +130,7 @@ class UI_EXPORT Clipboard : NON_EXPORTED_BASE(public base::ThreadChecker) {
     CBF_HTML,
     CBF_RTF,
     CBF_BOOKMARK,
-    CBF_FILES,
     CBF_WEBKIT,
-    CBF_BITMAP,
     CBF_SMBITMAP,  // Bitmap from shared memory.
     CBF_DATA,  // Arbitrary block of bytes.
   };
@@ -170,18 +148,10 @@ class UI_EXPORT Clipboard : NON_EXPORTED_BASE(public base::ThreadChecker) {
   // CBF_RTF       data         byte array
   // CBF_BOOKMARK  html         char array
   //               url          char array
-  // CBF_LINK      html         char array
-  //               url          char array
-  // CBF_FILES     files        char array representing multiple files.
-  //                            Filenames are separated by null characters and
-  //                            the final filename is double null terminated.
-  //                            The filenames are encoded in platform-specific
-  //                            encoding.
   // CBF_WEBKIT    none         empty vector
-  // CBF_BITMAP    pixels       byte array
-  //               size         gfx::Size struct
   // CBF_SMBITMAP  shared_mem   A pointer to an unmapped base::SharedMemory
-  //                            object containing the bitmap data.
+  //                            object containing the bitmap data. The bitmap
+  //                            data should be premultiplied.
   //               size         gfx::Size struct
   // CBF_DATA      format       char array
   //               data         byte array
@@ -193,7 +163,7 @@ class UI_EXPORT Clipboard : NON_EXPORTED_BASE(public base::ThreadChecker) {
     switch (type) {
       case CLIPBOARD_TYPE_COPY_PASTE:
         return true;
-#if defined(USE_X11) && !defined(OS_CHROMEOS)
+#if !defined(OS_WIN) && !defined(OS_MACOSX) && !defined(OS_CHROMEOS)
       case CLIPBOARD_TYPE_SELECTION:
         return true;
 #endif
@@ -222,60 +192,56 @@ class UI_EXPORT Clipboard : NON_EXPORTED_BASE(public base::ThreadChecker) {
   // clipboard, so it shouldn't be a problem.)
   static void DestroyClipboardForCurrentThread();
 
-  // Write a bunch of objects to the system clipboard. Copies are made of the
-  // contents of |objects|. On Windows they are copied to the system clipboard.
-  // On linux they are copied into a structure owned by the Clipboard object and
-  // kept until the system clipboard is set again.
-  void WriteObjects(ClipboardType type, const ObjectMap& objects);
-
   // Returns a sequence number which uniquely identifies clipboard state.
   // This can be used to version the data on the clipboard and determine
   // whether it has changed.
-  uint64 GetSequenceNumber(ClipboardType type);
+  virtual uint64 GetSequenceNumber(ClipboardType type) = 0;
 
   // Tests whether the clipboard contains a certain format
-  bool IsFormatAvailable(const FormatType& format, ClipboardType type) const;
+  virtual bool IsFormatAvailable(const FormatType& format,
+                                 ClipboardType type) const = 0;
 
   // Clear the clipboard data.
-  void Clear(ClipboardType type);
+  virtual void Clear(ClipboardType type) = 0;
 
-  void ReadAvailableTypes(ClipboardType type,
-                          std::vector<string16>* types,
-                          bool* contains_filenames) const;
+  virtual void ReadAvailableTypes(ClipboardType type,
+                                  std::vector<base::string16>* types,
+                                  bool* contains_filenames) const = 0;
 
   // Reads UNICODE text from the clipboard, if available.
-  void ReadText(ClipboardType type, string16* result) const;
+  virtual void ReadText(ClipboardType type, base::string16* result) const = 0;
 
   // Reads ASCII text from the clipboard, if available.
-  void ReadAsciiText(ClipboardType type, std::string* result) const;
+  virtual void ReadAsciiText(ClipboardType type, std::string* result) const = 0;
 
   // Reads HTML from the clipboard, if available. If the HTML fragment requires
   // context to parse, |fragment_start| and |fragment_end| are indexes into
   // markup indicating the beginning and end of the actual fragment. Otherwise,
   // they will contain 0 and markup->size().
-  void ReadHTML(ClipboardType type,
-                string16* markup,
-                std::string* src_url,
-                uint32* fragment_start,
-                uint32* fragment_end) const;
+  virtual void ReadHTML(ClipboardType type,
+                        base::string16* markup,
+                        std::string* src_url,
+                        uint32* fragment_start,
+                        uint32* fragment_end) const = 0;
 
   // Reads RTF from the clipboard, if available. Stores the result as a byte
   // vector.
-  void ReadRTF(ClipboardType type, std::string* result) const;
+  virtual void ReadRTF(ClipboardType type, std::string* result) const = 0;
 
   // Reads an image from the clipboard, if available.
-  SkBitmap ReadImage(ClipboardType type) const;
+  virtual SkBitmap ReadImage(ClipboardType type) const = 0;
 
-  void ReadCustomData(ClipboardType clipboard_type,
-                      const string16& type,
-                      string16* result) const;
+  virtual void ReadCustomData(ClipboardType clipboard_type,
+                              const base::string16& type,
+                              base::string16* result) const = 0;
 
   // Reads a bookmark from the clipboard, if available.
-  void ReadBookmark(string16* title, std::string* url) const;
+  virtual void ReadBookmark(base::string16* title, std::string* url) const = 0;
 
   // Reads raw data from the clipboard with the given format type. Stores result
   // as a byte vector.
-  void ReadData(const FormatType& format, std::string* result) const;
+  virtual void ReadData(const FormatType& format,
+                        std::string* result) const = 0;
 
   // Gets the FormatType corresponding to an arbitrary format string,
   // registering it with the system if needed. Due to Windows/Linux
@@ -305,9 +271,10 @@ class UI_EXPORT Clipboard : NON_EXPORTED_BASE(public base::ThreadChecker) {
   // |objects|.  The pointer is deleted by DispatchObjects().
   //
   // On non-Windows platforms, |process| is ignored.
-  static void ReplaceSharedMemHandle(ObjectMap* objects,
+  static bool ReplaceSharedMemHandle(ObjectMap* objects,
                                      base::SharedMemoryHandle bitmap_handle,
-                                     base::ProcessHandle process);
+                                     base::ProcessHandle process)
+      WARN_UNUSED_RESULT;
 #if defined(OS_WIN)
   // Firefox text/html
   static const FormatType& GetTextHtmlFormatType();
@@ -317,89 +284,48 @@ class UI_EXPORT Clipboard : NON_EXPORTED_BASE(public base::ThreadChecker) {
   static const FormatType& GetIDListFormatType();
 #endif
 
- private:
-  FRIEND_TEST_ALL_PREFIXES(ClipboardTest, SharedBitmapTest);
-  FRIEND_TEST_ALL_PREFIXES(ClipboardTest, EmptyHTMLTest);
-  friend class ClipboardTest;
-
-  Clipboard();
-  ~Clipboard();
-
-  void DispatchObject(ObjectType type, const ObjectMapParams& params);
-
-  void WriteText(const char* text_data, size_t text_len);
+ protected:
+  static Clipboard* Create();
 
-  void WriteHTML(const char* markup_data,
-                 size_t markup_len,
-                 const char* url_data,
-                 size_t url_len);
+  Clipboard() {}
+  virtual ~Clipboard() {}
 
-  void WriteRTF(const char* rtf_data, size_t data_len);
-
-  void WriteBookmark(const char* title_data,
-                     size_t title_len,
-                     const char* url_data,
-                     size_t url_len);
-
-  void WriteWebSmartPaste();
-
-  void WriteBitmap(const char* pixel_data, const char* size_data);
+  // Write a bunch of objects to the system clipboard. Copies are made of the
+  // contents of |objects|.
+  virtual void WriteObjects(ClipboardType type, const ObjectMap& objects) = 0;
 
-  void WriteData(const FormatType& format,
-                 const char* data_data,
-                 size_t data_len);
-#if defined(OS_WIN)
-  void WriteBitmapFromHandle(HBITMAP source_hbitmap,
-                             const gfx::Size& size);
+  void DispatchObject(ObjectType type, const ObjectMapParams& params);
 
-  // Safely write to system clipboard. Free |handle| on failure.
-  void WriteToClipboard(unsigned int format, HANDLE handle);
+  virtual void WriteText(const char* text_data, size_t text_len) = 0;
 
-  static void ParseBookmarkClipboardFormat(const string16& bookmark,
-                                           string16* title,
-                                           std::string* url);
+  virtual void WriteHTML(const char* markup_data,
+                         size_t markup_len,
+                         const char* url_data,
+                         size_t url_len) = 0;
 
-  // Free a handle depending on its type (as intuited from format)
-  static void FreeData(unsigned int format, HANDLE data);
+  virtual void WriteRTF(const char* rtf_data, size_t data_len) = 0;
 
-  // Return the window that should be the clipboard owner, creating it
-  // if neccessary.  Marked const for lazily initialization by const methods.
-  HWND GetClipboardWindow() const;
+  virtual void WriteBookmark(const char* title_data,
+                             size_t title_len,
+                             const char* url_data,
+                             size_t url_len) = 0;
 
-  // Mark this as mutable so const methods can still do lazy initialization.
-  mutable scoped_ptr<base::win::MessageWindow> clipboard_owner_;
+  virtual void WriteWebSmartPaste() = 0;
 
-#elif defined(TOOLKIT_GTK)
-  // The public API is via WriteObjects() which dispatches to multiple
-  // Write*() calls, but on GTK we must write all the clipboard types
-  // in a single GTK call.  To support this we store the current set
-  // of data we intend to put on the clipboard on clipboard_data_ as
-  // WriteObjects is running, and then at the end call SetGtkClipboard
-  // which replaces whatever is on the system clipboard with the
-  // contents of clipboard_data_.
+  virtual void WriteBitmap(const SkBitmap& bitmap) = 0;
 
- public:
-  typedef std::map<std::string, std::pair<char*, size_t> > TargetMap;
+  virtual void WriteData(const FormatType& format,
+                         const char* data_data,
+                         size_t data_len) = 0;
 
  private:
-  // Write changes to gtk clipboard.
-  void SetGtkClipboard(ClipboardType type);
-  // Insert a mapping into clipboard_data_.
-  void InsertMapping(const char* key, char* data, size_t data_len);
-
-  // Find the gtk clipboard for the passed type enum.
-  GtkClipboard* LookupBackingClipboard(ClipboardType type) const;
-
-  TargetMap* clipboard_data_;
-  GtkClipboard* clipboard_;
-  GtkClipboard* primary_selection_;
-#elif defined(USE_AURA) && defined(USE_X11) && !defined(OS_CHROMEOS)
- private:
-  // We keep our implementation details private because otherwise we bring in
-  // the X11 headers and break chrome compile.
-  class AuraX11Details;
-  scoped_ptr<AuraX11Details> aurax11_details_;
-#endif
+  FRIEND_TEST_ALL_PREFIXES(ClipboardTest, SharedBitmapTest);
+  FRIEND_TEST_ALL_PREFIXES(ClipboardTest, EmptyHTMLTest);
+  friend class ClipboardTest;
+  // For access to WriteObjects().
+  // TODO(dcheng): Remove the temporary exception for content.
+  friend class content::ClipboardMessageFilter;
+  friend class ScopedClipboardWriter;
 
   DISALLOW_COPY_AND_ASSIGN(Clipboard);
 };