| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef UI_BASE_CLIPBOARD_CLIPBOARD_H_ | 5 #ifndef UI_BASE_CLIPBOARD_CLIPBOARD_H_ |
| 6 #define UI_BASE_CLIPBOARD_CLIPBOARD_H_ | 6 #define UI_BASE_CLIPBOARD_CLIPBOARD_H_ |
| 7 | 7 |
| 8 #include <map> | 8 #include <map> |
| 9 #include <string> | 9 #include <string> |
| 10 #include <vector> | 10 #include <vector> |
| 11 | 11 |
| 12 #include "base/compiler_specific.h" | 12 #include "base/compiler_specific.h" |
| 13 #include "base/gtest_prod_util.h" | 13 #include "base/gtest_prod_util.h" |
| 14 #include "base/observer_list.h" |
| 14 #include "base/process.h" | 15 #include "base/process.h" |
| 15 #include "base/shared_memory.h" | 16 #include "base/shared_memory.h" |
| 16 #include "base/string16.h" | 17 #include "base/string16.h" |
| 17 #include "base/threading/thread_checker.h" | 18 #include "base/threading/thread_checker.h" |
| 18 #include "base/threading/platform_thread.h" | 19 #include "base/threading/platform_thread.h" |
| 19 #include "ui/base/ui_export.h" | 20 #include "ui/base/ui_export.h" |
| 20 | 21 |
| 21 #if defined(TOOLKIT_GTK) | 22 #if defined(TOOLKIT_GTK) |
| 22 #include <gdk/gdk.h> | 23 #include <gdk/gdk.h> |
| 23 #endif | 24 #endif |
| (...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 128 enum ObjectType { | 129 enum ObjectType { |
| 129 CBF_TEXT, | 130 CBF_TEXT, |
| 130 CBF_HTML, | 131 CBF_HTML, |
| 131 CBF_RTF, | 132 CBF_RTF, |
| 132 CBF_BOOKMARK, | 133 CBF_BOOKMARK, |
| 133 CBF_FILES, | 134 CBF_FILES, |
| 134 CBF_WEBKIT, | 135 CBF_WEBKIT, |
| 135 CBF_BITMAP, | 136 CBF_BITMAP, |
| 136 CBF_SMBITMAP, // Bitmap from shared memory. | 137 CBF_SMBITMAP, // Bitmap from shared memory. |
| 137 CBF_DATA, // Arbitrary block of bytes. | 138 CBF_DATA, // Arbitrary block of bytes. |
| 139 CBF_SOURCE_TAG, // Signifies the source of data. |
| 138 }; | 140 }; |
| 139 | 141 |
| 140 // ObjectMap is a map from ObjectType to associated data. | 142 // ObjectMap is a map from ObjectType to associated data. |
| 141 // The data is organized differently for each ObjectType. The following | 143 // The data is organized differently for each ObjectType. The following |
| 142 // table summarizes what kind of data is stored for each key. | 144 // table summarizes what kind of data is stored for each key. |
| 143 // * indicates an optional argument. | 145 // * indicates an optional argument. |
| 144 // | 146 // |
| 145 // Key Arguments Type | 147 // Key Arguments Type |
| 146 // ------------------------------------- | 148 // ------------------------------------- |
| 147 // CBF_TEXT text char array | 149 // CBF_TEXT text char array |
| (...skipping 14 matching lines...) Expand all Loading... |
| 162 // size gfx::Size struct | 164 // size gfx::Size struct |
| 163 // CBF_SMBITMAP shared_mem A pointer to an unmapped base::SharedMemory | 165 // CBF_SMBITMAP shared_mem A pointer to an unmapped base::SharedMemory |
| 164 // object containing the bitmap data. | 166 // object containing the bitmap data. |
| 165 // size gfx::Size struct | 167 // size gfx::Size struct |
| 166 // CBF_DATA format char array | 168 // CBF_DATA format char array |
| 167 // data byte array | 169 // data byte array |
| 168 typedef std::vector<char> ObjectMapParam; | 170 typedef std::vector<char> ObjectMapParam; |
| 169 typedef std::vector<ObjectMapParam> ObjectMapParams; | 171 typedef std::vector<ObjectMapParam> ObjectMapParams; |
| 170 typedef std::map<int /* ObjectType */, ObjectMapParams> ObjectMap; | 172 typedef std::map<int /* ObjectType */, ObjectMapParams> ObjectMap; |
| 171 | 173 |
| 174 // WriteObject() caller can use the SourceTag that will be stored in the |
| 175 // clipboard. NULL value means "no tag". |
| 176 typedef void* SourceTag; |
| 177 static ObjectMapParam SourceTag2Binary(SourceTag tag); |
| 178 static SourceTag Binary2SourceTag(const std::string& serialization); |
| 179 |
| 172 // Buffer designates which clipboard the action should be applied to. | 180 // Buffer designates which clipboard the action should be applied to. |
| 173 // Only platforms that use the X Window System support the selection | 181 // Only platforms that use the X Window System support the selection |
| 174 // buffer. | 182 // buffer. |
| 175 enum Buffer { | 183 enum Buffer { |
| 176 BUFFER_STANDARD, | 184 BUFFER_STANDARD, |
| 177 BUFFER_SELECTION, | 185 BUFFER_SELECTION, |
| 178 }; | 186 }; |
| 179 | 187 |
| 188 // This observer does track only calls to Clipboard::WriteObjects. |
| 189 // Don't use it for notification about changed OS clipboard. |
| 190 class ClipboardObserverForTesting { |
| 191 public: |
| 192 // Called at the end of Clipboard::WriteObjects(). |
| 193 virtual void OnWriteObjects(Buffer buffer) = 0; |
| 194 protected: |
| 195 virtual ~ClipboardObserverForTesting() {} |
| 196 }; |
| 197 |
| 198 void AddObserver(ClipboardObserverForTesting* obs) { |
| 199 observer_list_.AddObserver(obs); |
| 200 } |
| 201 |
| 202 void RemoveObserver(ClipboardObserverForTesting* obs) { |
| 203 observer_list_.RemoveObserver(obs); |
| 204 } |
| 205 |
| 180 static bool IsValidBuffer(int32 buffer) { | 206 static bool IsValidBuffer(int32 buffer) { |
| 181 switch (buffer) { | 207 switch (buffer) { |
| 182 case BUFFER_STANDARD: | 208 case BUFFER_STANDARD: |
| 183 return true; | 209 return true; |
| 184 #if defined(USE_X11) && !defined(OS_CHROMEOS) | 210 #if defined(USE_X11) && !defined(OS_CHROMEOS) |
| 185 case BUFFER_SELECTION: | 211 case BUFFER_SELECTION: |
| 186 return true; | 212 return true; |
| 187 #endif | 213 #endif |
| 188 } | 214 } |
| 189 return false; | 215 return false; |
| (...skipping 17 matching lines...) Expand all Loading... |
| 207 | 233 |
| 208 // Destroys the clipboard for the current thread. Usually, this will clean up | 234 // Destroys the clipboard for the current thread. Usually, this will clean up |
| 209 // all clipboards, except on Windows. (Previous code leaks the IO thread | 235 // all clipboards, except on Windows. (Previous code leaks the IO thread |
| 210 // clipboard, so it shouldn't be a problem.) | 236 // clipboard, so it shouldn't be a problem.) |
| 211 static void DestroyClipboardForCurrentThread(); | 237 static void DestroyClipboardForCurrentThread(); |
| 212 | 238 |
| 213 // Write a bunch of objects to the system clipboard. Copies are made of the | 239 // Write a bunch of objects to the system clipboard. Copies are made of the |
| 214 // contents of |objects|. On Windows they are copied to the system clipboard. | 240 // contents of |objects|. On Windows they are copied to the system clipboard. |
| 215 // On linux they are copied into a structure owned by the Clipboard object and | 241 // On linux they are copied into a structure owned by the Clipboard object and |
| 216 // kept until the system clipboard is set again. | 242 // kept until the system clipboard is set again. |
| 217 void WriteObjects(Buffer buffer, const ObjectMap& objects); | 243 // SourceTag is optional value to be stored in the clipboard, NULL won't be |
| 244 // stored. |
| 245 void WriteObjects(Buffer buffer, const ObjectMap& objects, SourceTag tag); |
| 218 | 246 |
| 219 // Returns a sequence number which uniquely identifies clipboard state. | 247 // Returns a sequence number which uniquely identifies clipboard state. |
| 220 // This can be used to version the data on the clipboard and determine | 248 // This can be used to version the data on the clipboard and determine |
| 221 // whether it has changed. | 249 // whether it has changed. |
| 222 uint64 GetSequenceNumber(Buffer buffer); | 250 uint64 GetSequenceNumber(Buffer buffer); |
| 223 | 251 |
| 224 // Tests whether the clipboard contains a certain format | 252 // Tests whether the clipboard contains a certain format |
| 225 bool IsFormatAvailable(const FormatType& format, Buffer buffer) const; | 253 bool IsFormatAvailable(const FormatType& format, Buffer buffer) const; |
| 226 | 254 |
| 227 // Clear the clipboard data. | 255 // Clear the clipboard data. |
| (...skipping 26 matching lines...) Expand all Loading... |
| 254 const string16& type, | 282 const string16& type, |
| 255 string16* result) const; | 283 string16* result) const; |
| 256 | 284 |
| 257 // Reads a bookmark from the clipboard, if available. | 285 // Reads a bookmark from the clipboard, if available. |
| 258 void ReadBookmark(string16* title, std::string* url) const; | 286 void ReadBookmark(string16* title, std::string* url) const; |
| 259 | 287 |
| 260 // Reads raw data from the clipboard with the given format type. Stores result | 288 // Reads raw data from the clipboard with the given format type. Stores result |
| 261 // as a byte vector. | 289 // as a byte vector. |
| 262 void ReadData(const FormatType& format, std::string* result) const; | 290 void ReadData(const FormatType& format, std::string* result) const; |
| 263 | 291 |
| 292 // Reads Source tag from the clipboard, if available. |
| 293 SourceTag ReadSourceTag() const; |
| 294 |
| 264 // Gets the FormatType corresponding to an arbitrary format string, | 295 // Gets the FormatType corresponding to an arbitrary format string, |
| 265 // registering it with the system if needed. Due to Windows/Linux | 296 // registering it with the system if needed. Due to Windows/Linux |
| 266 // limitiations, |format_string| must never be controlled by the user. | 297 // limitiations, |format_string| must never be controlled by the user. |
| 267 static FormatType GetFormatType(const std::string& format_string); | 298 static FormatType GetFormatType(const std::string& format_string); |
| 268 | 299 |
| 269 // Get format identifiers for various types. | 300 // Get format identifiers for various types. |
| 270 static const FormatType& GetUrlFormatType(); | 301 static const FormatType& GetUrlFormatType(); |
| 271 static const FormatType& GetUrlWFormatType(); | 302 static const FormatType& GetUrlWFormatType(); |
| 272 static const FormatType& GetMozUrlFormatType(); | 303 static const FormatType& GetMozUrlFormatType(); |
| 273 static const FormatType& GetPlainTextFormatType(); | 304 static const FormatType& GetPlainTextFormatType(); |
| 274 static const FormatType& GetPlainTextWFormatType(); | 305 static const FormatType& GetPlainTextWFormatType(); |
| 275 static const FormatType& GetFilenameFormatType(); | 306 static const FormatType& GetFilenameFormatType(); |
| 276 static const FormatType& GetFilenameWFormatType(); | 307 static const FormatType& GetFilenameWFormatType(); |
| 277 static const FormatType& GetWebKitSmartPasteFormatType(); | 308 static const FormatType& GetWebKitSmartPasteFormatType(); |
| 278 // Win: MS HTML Format, Other: Generic HTML format | 309 // Win: MS HTML Format, Other: Generic HTML format |
| 279 static const FormatType& GetHtmlFormatType(); | 310 static const FormatType& GetHtmlFormatType(); |
| 280 static const FormatType& GetRtfFormatType(); | 311 static const FormatType& GetRtfFormatType(); |
| 281 static const FormatType& GetBitmapFormatType(); | 312 static const FormatType& GetBitmapFormatType(); |
| 282 // TODO(raymes): Unify web custom data and pepper custom data: | 313 // TODO(raymes): Unify web custom data and pepper custom data: |
| 283 // crbug.com/158399. | 314 // crbug.com/158399. |
| 284 static const FormatType& GetWebCustomDataFormatType(); | 315 static const FormatType& GetWebCustomDataFormatType(); |
| 285 static const FormatType& GetPepperCustomDataFormatType(); | 316 static const FormatType& GetPepperCustomDataFormatType(); |
| 317 static const FormatType& GetSourceTagFormatType(); |
| 286 | 318 |
| 287 // Embeds a pointer to a SharedMemory object pointed to by |bitmap_handle| | 319 // Embeds a pointer to a SharedMemory object pointed to by |bitmap_handle| |
| 288 // belonging to |process| into a shared bitmap [CBF_SMBITMAP] slot in | 320 // belonging to |process| into a shared bitmap [CBF_SMBITMAP] slot in |
| 289 // |objects|. The pointer is deleted by DispatchObjects(). | 321 // |objects|. The pointer is deleted by DispatchObjects(). |
| 290 // | 322 // |
| 291 // On non-Windows platforms, |process| is ignored. | 323 // On non-Windows platforms, |process| is ignored. |
| 292 static void ReplaceSharedMemHandle(ObjectMap* objects, | 324 static void ReplaceSharedMemHandle(ObjectMap* objects, |
| 293 base::SharedMemoryHandle bitmap_handle, | 325 base::SharedMemoryHandle bitmap_handle, |
| 294 base::ProcessHandle process); | 326 base::ProcessHandle process); |
| 295 #if defined(OS_WIN) | 327 #if defined(OS_WIN) |
| 296 // Firefox text/html | 328 // Firefox text/html |
| 297 static const FormatType& GetTextHtmlFormatType(); | 329 static const FormatType& GetTextHtmlFormatType(); |
| 298 static const FormatType& GetCFHDropFormatType(); | 330 static const FormatType& GetCFHDropFormatType(); |
| 299 static const FormatType& GetFileDescriptorFormatType(); | 331 static const FormatType& GetFileDescriptorFormatType(); |
| 300 static const FormatType& GetFileContentFormatZeroType(); | 332 static const FormatType& GetFileContentFormatZeroType(); |
| 301 #endif | 333 #endif |
| 302 | 334 |
| 303 private: | 335 private: |
| 304 FRIEND_TEST_ALL_PREFIXES(ClipboardTest, SharedBitmapTest); | 336 FRIEND_TEST_ALL_PREFIXES(ClipboardTest, SharedBitmapTest); |
| 305 FRIEND_TEST_ALL_PREFIXES(ClipboardTest, EmptyHTMLTest); | 337 FRIEND_TEST_ALL_PREFIXES(ClipboardTest, EmptyHTMLTest); |
| 306 friend class ClipboardTest; | 338 friend class ClipboardTest; |
| 307 | 339 |
| 308 Clipboard(); | 340 Clipboard(); |
| 309 ~Clipboard(); | 341 ~Clipboard(); |
| 310 | 342 |
| 311 void DispatchObject(ObjectType type, const ObjectMapParams& params); | 343 void DispatchObject(ObjectType type, const ObjectMapParams& params); |
| 312 | 344 |
| 345 void WriteObjectsImpl(Buffer buffer, const ObjectMap& objects, SourceTag tag); |
| 346 |
| 313 void WriteText(const char* text_data, size_t text_len); | 347 void WriteText(const char* text_data, size_t text_len); |
| 314 | 348 |
| 315 void WriteHTML(const char* markup_data, | 349 void WriteHTML(const char* markup_data, |
| 316 size_t markup_len, | 350 size_t markup_len, |
| 317 const char* url_data, | 351 const char* url_data, |
| 318 size_t url_len); | 352 size_t url_len); |
| 319 | 353 |
| 320 void WriteRTF(const char* rtf_data, size_t data_len); | 354 void WriteRTF(const char* rtf_data, size_t data_len); |
| 321 | 355 |
| 322 void WriteBookmark(const char* title_data, | 356 void WriteBookmark(const char* title_data, |
| 323 size_t title_len, | 357 size_t title_len, |
| 324 const char* url_data, | 358 const char* url_data, |
| 325 size_t url_len); | 359 size_t url_len); |
| 326 | 360 |
| 327 void WriteWebSmartPaste(); | 361 void WriteWebSmartPaste(); |
| 328 | 362 |
| 329 void WriteBitmap(const char* pixel_data, const char* size_data); | 363 void WriteBitmap(const char* pixel_data, const char* size_data); |
| 330 | 364 |
| 331 void WriteData(const FormatType& format, | 365 void WriteData(const FormatType& format, |
| 332 const char* data_data, | 366 const char* data_data, |
| 333 size_t data_len); | 367 size_t data_len); |
| 368 |
| 369 void WriteSourceTag(SourceTag tag); |
| 334 #if defined(OS_WIN) | 370 #if defined(OS_WIN) |
| 335 void WriteBitmapFromHandle(HBITMAP source_hbitmap, | 371 void WriteBitmapFromHandle(HBITMAP source_hbitmap, |
| 336 const gfx::Size& size); | 372 const gfx::Size& size); |
| 337 | 373 |
| 338 // Safely write to system clipboard. Free |handle| on failure. | 374 // Safely write to system clipboard. Free |handle| on failure. |
| 339 void WriteToClipboard(unsigned int format, HANDLE handle); | 375 void WriteToClipboard(unsigned int format, HANDLE handle); |
| 340 | 376 |
| 341 static void ParseBookmarkClipboardFormat(const string16& bookmark, | 377 static void ParseBookmarkClipboardFormat(const string16& bookmark, |
| 342 string16* title, | 378 string16* title, |
| 343 std::string* url); | 379 std::string* url); |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 378 TargetMap* clipboard_data_; | 414 TargetMap* clipboard_data_; |
| 379 GtkClipboard* clipboard_; | 415 GtkClipboard* clipboard_; |
| 380 GtkClipboard* primary_selection_; | 416 GtkClipboard* primary_selection_; |
| 381 #elif defined(USE_AURA) && defined(USE_X11) && !defined(OS_CHROMEOS) | 417 #elif defined(USE_AURA) && defined(USE_X11) && !defined(OS_CHROMEOS) |
| 382 private: | 418 private: |
| 383 // We keep our implementation details private because otherwise we bring in | 419 // We keep our implementation details private because otherwise we bring in |
| 384 // the X11 headers and break chrome compile. | 420 // the X11 headers and break chrome compile. |
| 385 class AuraX11Details; | 421 class AuraX11Details; |
| 386 scoped_ptr<AuraX11Details> aurax11_details_; | 422 scoped_ptr<AuraX11Details> aurax11_details_; |
| 387 #endif | 423 #endif |
| 424 ObserverList<ClipboardObserverForTesting> observer_list_; |
| 388 | 425 |
| 389 DISALLOW_COPY_AND_ASSIGN(Clipboard); | 426 DISALLOW_COPY_AND_ASSIGN(Clipboard); |
| 390 }; | 427 }; |
| 391 | 428 |
| 392 } // namespace ui | 429 } // namespace ui |
| 393 | 430 |
| 394 #endif // UI_BASE_CLIPBOARD_CLIPBOARD_H_ | 431 #endif // UI_BASE_CLIPBOARD_CLIPBOARD_H_ |
| OLD | NEW |