| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #ifndef UI_BASE_CLIPBOARD_CLIPBOARD_H_ | |
| 6 #define UI_BASE_CLIPBOARD_CLIPBOARD_H_ | |
| 7 | |
| 8 #include <map> | |
| 9 #include <string> | |
| 10 #include <vector> | |
| 11 | |
| 12 #include "base/compiler_specific.h" | |
| 13 #include "base/gtest_prod_util.h" | |
| 14 #include "base/memory/shared_memory.h" | |
| 15 #include "base/process/process.h" | |
| 16 #include "base/strings/string16.h" | |
| 17 #include "base/threading/platform_thread.h" | |
| 18 #include "base/threading/thread_checker.h" | |
| 19 #include "ui/base/clipboard/clipboard_types.h" | |
| 20 #include "ui/base/ui_base_export.h" | |
| 21 | |
| 22 #if defined(OS_WIN) | |
| 23 #include <objidl.h> | |
| 24 #endif | |
| 25 | |
| 26 namespace base { | |
| 27 class FilePath; | |
| 28 | |
| 29 namespace win { | |
| 30 class MessageWindow; | |
| 31 } // namespace win | |
| 32 } // namespace base | |
| 33 | |
| 34 // TODO(dcheng): Temporary until the IPC layer doesn't use WriteObjects(). | |
| 35 namespace content { | |
| 36 class ClipboardMessageFilter; | |
| 37 } | |
| 38 | |
| 39 namespace gfx { | |
| 40 class Size; | |
| 41 } | |
| 42 | |
| 43 class SkBitmap; | |
| 44 | |
| 45 #ifdef __OBJC__ | |
| 46 @class NSString; | |
| 47 #else | |
| 48 class NSString; | |
| 49 #endif | |
| 50 | |
| 51 namespace ui { | |
| 52 class ClipboardTest; | |
| 53 class ScopedClipboardWriter; | |
| 54 | |
| 55 class UI_BASE_EXPORT Clipboard : NON_EXPORTED_BASE(public base::ThreadChecker) { | |
| 56 public: | |
| 57 // MIME type constants. | |
| 58 static const char kMimeTypeText[]; | |
| 59 static const char kMimeTypeURIList[]; | |
| 60 static const char kMimeTypeDownloadURL[]; | |
| 61 static const char kMimeTypeHTML[]; | |
| 62 static const char kMimeTypeRTF[]; | |
| 63 static const char kMimeTypePNG[]; | |
| 64 | |
| 65 // Platform neutral holder for native data representation of a clipboard type. | |
| 66 struct UI_BASE_EXPORT FormatType { | |
| 67 FormatType(); | |
| 68 ~FormatType(); | |
| 69 | |
| 70 // Serializes and deserializes a FormatType for use in IPC messages. | |
| 71 std::string Serialize() const; | |
| 72 static FormatType Deserialize(const std::string& serialization); | |
| 73 | |
| 74 #if defined(USE_AURA) | |
| 75 // FormatType can be used in a set on some platforms. | |
| 76 bool operator<(const FormatType& other) const; | |
| 77 #endif | |
| 78 | |
| 79 #if defined(OS_WIN) | |
| 80 const FORMATETC& ToFormatEtc() const { return data_; } | |
| 81 #elif defined(USE_AURA) || defined(OS_ANDROID) | |
| 82 const std::string& ToString() const { return data_; } | |
| 83 #elif defined(OS_MACOSX) | |
| 84 NSString* ToNSString() const { return data_; } | |
| 85 // Custom copy and assignment constructor to handle NSString. | |
| 86 FormatType(const FormatType& other); | |
| 87 FormatType& operator=(const FormatType& other); | |
| 88 #endif | |
| 89 | |
| 90 bool Equals(const FormatType& other) const; | |
| 91 | |
| 92 private: | |
| 93 friend class Clipboard; | |
| 94 | |
| 95 // Platform-specific glue used internally by the Clipboard class. Each | |
| 96 // plaform should define,at least one of each of the following: | |
| 97 // 1. A constructor that wraps that native clipboard format descriptor. | |
| 98 // 2. An accessor to retrieve the wrapped descriptor. | |
| 99 // 3. A data member to hold the wrapped descriptor. | |
| 100 // | |
| 101 // Note that in some cases, the accessor for the wrapped descriptor may be | |
| 102 // public, as these format types can be used by drag and drop code as well. | |
| 103 #if defined(OS_WIN) | |
| 104 explicit FormatType(UINT native_format); | |
| 105 FormatType(UINT native_format, LONG index); | |
| 106 FORMATETC data_; | |
| 107 #elif defined(USE_AURA) || defined(OS_ANDROID) | |
| 108 explicit FormatType(const std::string& native_format); | |
| 109 std::string data_; | |
| 110 #elif defined(OS_MACOSX) | |
| 111 explicit FormatType(NSString* native_format); | |
| 112 NSString* data_; | |
| 113 #else | |
| 114 #error No FormatType definition. | |
| 115 #endif | |
| 116 | |
| 117 // Copyable and assignable, since this is essentially an opaque value type. | |
| 118 }; | |
| 119 | |
| 120 // TODO(dcheng): Make this private once the IPC layer no longer needs to | |
| 121 // serialize this information. | |
| 122 // ObjectType designates the type of data to be stored in the clipboard. This | |
| 123 // designation is shared across all OSes. The system-specific designation | |
| 124 // is defined by FormatType. A single ObjectType might be represented by | |
| 125 // several system-specific FormatTypes. For example, on Linux the CBF_TEXT | |
| 126 // ObjectType maps to "text/plain", "STRING", and several other formats. On | |
| 127 // windows it maps to CF_UNICODETEXT. | |
| 128 enum ObjectType { | |
| 129 CBF_TEXT, | |
| 130 CBF_HTML, | |
| 131 CBF_RTF, | |
| 132 CBF_BOOKMARK, | |
| 133 CBF_WEBKIT, | |
| 134 CBF_SMBITMAP, // Bitmap from shared memory. | |
| 135 CBF_DATA, // Arbitrary block of bytes. | |
| 136 }; | |
| 137 | |
| 138 // ObjectMap is a map from ObjectType to associated data. | |
| 139 // The data is organized differently for each ObjectType. The following | |
| 140 // table summarizes what kind of data is stored for each key. | |
| 141 // * indicates an optional argument. | |
| 142 // | |
| 143 // Key Arguments Type | |
| 144 // ------------------------------------- | |
| 145 // CBF_TEXT text char array | |
| 146 // CBF_HTML html char array | |
| 147 // url* char array | |
| 148 // CBF_RTF data byte array | |
| 149 // CBF_BOOKMARK html char array | |
| 150 // url char array | |
| 151 // CBF_WEBKIT none empty vector | |
| 152 // CBF_SMBITMAP shared_mem A pointer to an unmapped base::SharedMemory | |
| 153 // object containing the bitmap data. The bitmap | |
| 154 // data should be premultiplied. | |
| 155 // size gfx::Size struct | |
| 156 // CBF_DATA format char array | |
| 157 // data byte array | |
| 158 typedef std::vector<char> ObjectMapParam; | |
| 159 typedef std::vector<ObjectMapParam> ObjectMapParams; | |
| 160 typedef std::map<int /* ObjectType */, ObjectMapParams> ObjectMap; | |
| 161 | |
| 162 static bool IsSupportedClipboardType(int32 type) { | |
| 163 switch (type) { | |
| 164 case CLIPBOARD_TYPE_COPY_PASTE: | |
| 165 return true; | |
| 166 #if !defined(OS_WIN) && !defined(OS_MACOSX) && !defined(OS_CHROMEOS) | |
| 167 case CLIPBOARD_TYPE_SELECTION: | |
| 168 return true; | |
| 169 #endif | |
| 170 } | |
| 171 return false; | |
| 172 } | |
| 173 | |
| 174 static ClipboardType FromInt(int32 type) { | |
| 175 return static_cast<ClipboardType>(type); | |
| 176 } | |
| 177 | |
| 178 // Sets the list of threads that are allowed to access the clipboard. | |
| 179 static void SetAllowedThreads( | |
| 180 const std::vector<base::PlatformThreadId>& allowed_threads); | |
| 181 | |
| 182 // Returns the clipboard object for the current thread. | |
| 183 // | |
| 184 // Most implementations will have at most one clipboard which will live on | |
| 185 // the main UI thread, but Windows has tricky semantics where there have to | |
| 186 // be two clipboards: one that lives on the UI thread and one that lives on | |
| 187 // the IO thread. | |
| 188 static Clipboard* GetForCurrentThread(); | |
| 189 | |
| 190 // Destroys the clipboard for the current thread. Usually, this will clean up | |
| 191 // all clipboards, except on Windows. (Previous code leaks the IO thread | |
| 192 // clipboard, so it shouldn't be a problem.) | |
| 193 static void DestroyClipboardForCurrentThread(); | |
| 194 | |
| 195 // Returns a sequence number which uniquely identifies clipboard state. | |
| 196 // This can be used to version the data on the clipboard and determine | |
| 197 // whether it has changed. | |
| 198 virtual uint64 GetSequenceNumber(ClipboardType type) = 0; | |
| 199 | |
| 200 // Tests whether the clipboard contains a certain format | |
| 201 virtual bool IsFormatAvailable(const FormatType& format, | |
| 202 ClipboardType type) const = 0; | |
| 203 | |
| 204 // Clear the clipboard data. | |
| 205 virtual void Clear(ClipboardType type) = 0; | |
| 206 | |
| 207 virtual void ReadAvailableTypes(ClipboardType type, | |
| 208 std::vector<base::string16>* types, | |
| 209 bool* contains_filenames) const = 0; | |
| 210 | |
| 211 // Reads UNICODE text from the clipboard, if available. | |
| 212 virtual void ReadText(ClipboardType type, base::string16* result) const = 0; | |
| 213 | |
| 214 // Reads ASCII text from the clipboard, if available. | |
| 215 virtual void ReadAsciiText(ClipboardType type, std::string* result) const = 0; | |
| 216 | |
| 217 // Reads HTML from the clipboard, if available. If the HTML fragment requires | |
| 218 // context to parse, |fragment_start| and |fragment_end| are indexes into | |
| 219 // markup indicating the beginning and end of the actual fragment. Otherwise, | |
| 220 // they will contain 0 and markup->size(). | |
| 221 virtual void ReadHTML(ClipboardType type, | |
| 222 base::string16* markup, | |
| 223 std::string* src_url, | |
| 224 uint32* fragment_start, | |
| 225 uint32* fragment_end) const = 0; | |
| 226 | |
| 227 // Reads RTF from the clipboard, if available. Stores the result as a byte | |
| 228 // vector. | |
| 229 virtual void ReadRTF(ClipboardType type, std::string* result) const = 0; | |
| 230 | |
| 231 // Reads an image from the clipboard, if available. | |
| 232 virtual SkBitmap ReadImage(ClipboardType type) const = 0; | |
| 233 | |
| 234 virtual void ReadCustomData(ClipboardType clipboard_type, | |
| 235 const base::string16& type, | |
| 236 base::string16* result) const = 0; | |
| 237 | |
| 238 // Reads a bookmark from the clipboard, if available. | |
| 239 virtual void ReadBookmark(base::string16* title, std::string* url) const = 0; | |
| 240 | |
| 241 // Reads raw data from the clipboard with the given format type. Stores result | |
| 242 // as a byte vector. | |
| 243 virtual void ReadData(const FormatType& format, | |
| 244 std::string* result) const = 0; | |
| 245 | |
| 246 // Gets the FormatType corresponding to an arbitrary format string, | |
| 247 // registering it with the system if needed. Due to Windows/Linux | |
| 248 // limitiations, |format_string| must never be controlled by the user. | |
| 249 static FormatType GetFormatType(const std::string& format_string); | |
| 250 | |
| 251 // Get format identifiers for various types. | |
| 252 static const FormatType& GetUrlFormatType(); | |
| 253 static const FormatType& GetUrlWFormatType(); | |
| 254 static const FormatType& GetMozUrlFormatType(); | |
| 255 static const FormatType& GetPlainTextFormatType(); | |
| 256 static const FormatType& GetPlainTextWFormatType(); | |
| 257 static const FormatType& GetFilenameFormatType(); | |
| 258 static const FormatType& GetFilenameWFormatType(); | |
| 259 static const FormatType& GetWebKitSmartPasteFormatType(); | |
| 260 // Win: MS HTML Format, Other: Generic HTML format | |
| 261 static const FormatType& GetHtmlFormatType(); | |
| 262 static const FormatType& GetRtfFormatType(); | |
| 263 static const FormatType& GetBitmapFormatType(); | |
| 264 // TODO(raymes): Unify web custom data and pepper custom data: | |
| 265 // crbug.com/158399. | |
| 266 static const FormatType& GetWebCustomDataFormatType(); | |
| 267 static const FormatType& GetPepperCustomDataFormatType(); | |
| 268 | |
| 269 // Embeds a pointer to a SharedMemory object pointed to by |bitmap_handle| | |
| 270 // belonging to |process| into a shared bitmap [CBF_SMBITMAP] slot in | |
| 271 // |objects|. The pointer is deleted by DispatchObjects(). | |
| 272 // | |
| 273 // On non-Windows platforms, |process| is ignored. | |
| 274 static bool ReplaceSharedMemHandle(ObjectMap* objects, | |
| 275 base::SharedMemoryHandle bitmap_handle, | |
| 276 base::ProcessHandle process) | |
| 277 WARN_UNUSED_RESULT; | |
| 278 #if defined(OS_WIN) | |
| 279 // Firefox text/html | |
| 280 static const FormatType& GetTextHtmlFormatType(); | |
| 281 static const FormatType& GetCFHDropFormatType(); | |
| 282 static const FormatType& GetFileDescriptorFormatType(); | |
| 283 static const FormatType& GetFileContentZeroFormatType(); | |
| 284 static const FormatType& GetIDListFormatType(); | |
| 285 #endif | |
| 286 | |
| 287 protected: | |
| 288 static Clipboard* Create(); | |
| 289 | |
| 290 Clipboard() {} | |
| 291 virtual ~Clipboard() {} | |
| 292 | |
| 293 // Write a bunch of objects to the system clipboard. Copies are made of the | |
| 294 // contents of |objects|. | |
| 295 virtual void WriteObjects(ClipboardType type, const ObjectMap& objects) = 0; | |
| 296 | |
| 297 void DispatchObject(ObjectType type, const ObjectMapParams& params); | |
| 298 | |
| 299 virtual void WriteText(const char* text_data, size_t text_len) = 0; | |
| 300 | |
| 301 virtual void WriteHTML(const char* markup_data, | |
| 302 size_t markup_len, | |
| 303 const char* url_data, | |
| 304 size_t url_len) = 0; | |
| 305 | |
| 306 virtual void WriteRTF(const char* rtf_data, size_t data_len) = 0; | |
| 307 | |
| 308 virtual void WriteBookmark(const char* title_data, | |
| 309 size_t title_len, | |
| 310 const char* url_data, | |
| 311 size_t url_len) = 0; | |
| 312 | |
| 313 virtual void WriteWebSmartPaste() = 0; | |
| 314 | |
| 315 virtual void WriteBitmap(const SkBitmap& bitmap) = 0; | |
| 316 | |
| 317 virtual void WriteData(const FormatType& format, | |
| 318 const char* data_data, | |
| 319 size_t data_len) = 0; | |
| 320 | |
| 321 private: | |
| 322 FRIEND_TEST_ALL_PREFIXES(ClipboardTest, SharedBitmapTest); | |
| 323 FRIEND_TEST_ALL_PREFIXES(ClipboardTest, EmptyHTMLTest); | |
| 324 friend class ClipboardTest; | |
| 325 // For access to WriteObjects(). | |
| 326 // TODO(dcheng): Remove the temporary exception for content. | |
| 327 friend class content::ClipboardMessageFilter; | |
| 328 friend class ScopedClipboardWriter; | |
| 329 | |
| 330 DISALLOW_COPY_AND_ASSIGN(Clipboard); | |
| 331 }; | |
| 332 | |
| 333 } // namespace ui | |
| 334 | |
| 335 #endif // UI_BASE_CLIPBOARD_CLIPBOARD_H_ | |
| OLD | NEW |