OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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 BASE_CLIPBOARD_H_ | 5 #ifndef BASE_CLIPBOARD_H_ |
6 #define BASE_CLIPBOARD_H_ | 6 #define BASE_CLIPBOARD_H_ |
7 | 7 |
8 #include <map> | 8 #include <map> |
9 #include <string> | 9 #include <string> |
10 #include <utility> | 10 #include <utility> |
(...skipping 13 matching lines...) Expand all Loading... |
24 | 24 |
25 class Clipboard { | 25 class Clipboard { |
26 public: | 26 public: |
27 #if defined(OS_WIN) | 27 #if defined(OS_WIN) |
28 typedef unsigned int FormatType; | 28 typedef unsigned int FormatType; |
29 #elif defined(OS_MACOSX) | 29 #elif defined(OS_MACOSX) |
30 typedef NSString *FormatType; | 30 typedef NSString *FormatType; |
31 #elif defined(OS_LINUX) | 31 #elif defined(OS_LINUX) |
32 typedef struct _GdkAtom* FormatType; | 32 typedef struct _GdkAtom* FormatType; |
33 typedef struct _GtkClipboard GtkClipboard; | 33 typedef struct _GtkClipboard GtkClipboard; |
34 // A mapping from target (format) string to the relevant data (usually a | 34 typedef std::map<std::string, std::pair<char*, size_t> > TargetMap; |
35 // string, but potentially arbitrary data). | |
36 typedef std::map<std::string, std::pair<uint8*, size_t> > TargetMap; | |
37 #endif | 35 #endif |
38 | 36 |
| 37 // ObjectType designates the type of data to be stored in the clipboard. This |
| 38 // designation is shared across all OSes. The system-specific designation |
| 39 // is defined by FormatType. A single ObjectType might be represented by |
| 40 // several system-specific FormatTypes. For example, on Linux the CBF_TEXT |
| 41 // ObjectType maps to "text/plain", "STRING", and several other formats. On |
| 42 // windows it maps to CF_UNICODETEXT. |
| 43 enum ObjectType { |
| 44 CBF_TEXT, |
| 45 CBF_HTML, |
| 46 CBF_BOOKMARK, |
| 47 CBF_LINK, |
| 48 CBF_FILES, |
| 49 CBF_WEBKIT, |
| 50 CBF_BITMAP, |
| 51 CBF_SMBITMAP // bitmap from shared memory |
| 52 }; |
| 53 |
| 54 // ObjectMap is a map from ObjectType to associated data. |
| 55 // The data is organized differently for each ObjectType. The following |
| 56 // table summarizes what kind of data is stored for each key. |
| 57 // * indicates an optional argument. |
| 58 // |
| 59 // Key Arguments Type |
| 60 // ------------------------------------- |
| 61 // CBF_TEXT text char array |
| 62 // CBF_HTML html char array |
| 63 // url* char array |
| 64 // CBF_BOOKMARK html char array |
| 65 // url char array |
| 66 // CBF_LINK html char array |
| 67 // url char array |
| 68 // CBF_FILES files char array representing multiple files. |
| 69 // Filenames are separated by null characters and |
| 70 // the final filename is double null terminated. |
| 71 // CBF_WEBKIT none empty vector |
| 72 // CBF_BITMAP pixels byte array |
| 73 // size gfx::Size struct |
| 74 // CBF_SMBITMAP shared_mem shared memory handle |
| 75 // size gfx::Size struct |
| 76 typedef std::vector<char> ObjectMapParam; |
| 77 typedef std::vector<ObjectMapParam> ObjectMapParams; |
| 78 typedef std::map<int /* ObjectType */, ObjectMapParams> ObjectMap; |
| 79 |
39 Clipboard(); | 80 Clipboard(); |
40 ~Clipboard(); | 81 ~Clipboard(); |
41 | 82 |
42 // Clears the clipboard. It is usually a good idea to clear the clipboard | 83 // Write a bunch of objects to the system clipboard. Copies are made of the |
43 // before writing content to the clipboard. | 84 // contents of |objects|. On Windows they are copied to the system clipboard. |
44 void Clear(); | 85 // On linux they are copied into a structure owned by the Clipboard object and |
| 86 // kept until the system clipboard is set again. |
| 87 void WriteObjects(const ObjectMap& objects); |
45 | 88 |
46 // Adds UNICODE and ASCII text to the clipboard. | 89 // Behaves as above. If there is some shared memory handle passed as one of |
47 void WriteText(const std::wstring& text); | 90 // the objects, it came from the process designated by |process|. This will |
48 | 91 // assist in turning it into a shared memory region that the current process |
49 // Adds HTML to the clipboard. The url parameter is optional, but especially | 92 // can use. |
50 // useful if the HTML fragment contains relative links | 93 void WriteObjects(const ObjectMap& objects, ProcessHandle process); |
51 void WriteHTML(const std::wstring& markup, const std::string& src_url); | |
52 | |
53 // Adds a bookmark to the clipboard | |
54 void WriteBookmark(const std::wstring& title, const std::string& url); | |
55 | |
56 // Adds both a bookmark and an HTML hyperlink to the clipboard. It is a | |
57 // convenience wrapper around WriteBookmark and WriteHTML. | |
58 void WriteHyperlink(const std::wstring& title, const std::string& url); | |
59 | |
60 #if defined(OS_WIN) | |
61 // Adds a bitmap to the clipboard | |
62 // This is the slowest way to copy a bitmap to the clipboard as we must first | |
63 // memcpy the pixels into GDI and the blit the bitmap to the clipboard. | |
64 // Pixel format is assumed to be 32-bit BI_RGB. | |
65 void WriteBitmap(const void* pixels, const gfx::Size& size); | |
66 | |
67 // Adds a bitmap to the clipboard | |
68 // This function requires read and write access to the bitmap, but does not | |
69 // actually modify the shared memory region. | |
70 // Pixel format is assumed to be 32-bit BI_RGB. | |
71 void WriteBitmapFromSharedMemory(const SharedMemory& bitmap, | |
72 const gfx::Size& size); | |
73 | |
74 // Adds a bitmap to the clipboard | |
75 // This is the fastest way to copy a bitmap to the clipboard. The HBITMAP | |
76 // may either be device-dependent or device-independent. | |
77 void WriteBitmapFromHandle(HBITMAP hbitmap, const gfx::Size& size); | |
78 | |
79 // Used by WebKit to determine whether WebKit wrote the clipboard last | |
80 void WriteWebSmartPaste(); | |
81 #endif | |
82 | |
83 // Adds a file or group of files to the clipboard. | |
84 void WriteFile(const std::wstring& file); | |
85 void WriteFiles(const std::vector<std::wstring>& files); | |
86 | 94 |
87 // Tests whether the clipboard contains a certain format | 95 // Tests whether the clipboard contains a certain format |
88 bool IsFormatAvailable(FormatType format) const; | 96 bool IsFormatAvailable(FormatType format) const; |
89 | 97 |
90 // Reads UNICODE text from the clipboard, if available. | 98 // Reads UNICODE text from the clipboard, if available. |
91 void ReadText(std::wstring* result) const; | 99 void ReadText(std::wstring* result) const; |
92 | 100 |
93 // Reads ASCII text from the clipboard, if available. | 101 // Reads ASCII text from the clipboard, if available. |
94 void ReadAsciiText(std::string* result) const; | 102 void ReadAsciiText(std::string* result) const; |
95 | 103 |
(...skipping 22 matching lines...) Expand all Loading... |
118 static FormatType GetBitmapFormatType(); | 126 static FormatType GetBitmapFormatType(); |
119 // Firefox text/html | 127 // Firefox text/html |
120 static FormatType GetTextHtmlFormatType(); | 128 static FormatType GetTextHtmlFormatType(); |
121 static FormatType GetCFHDropFormatType(); | 129 static FormatType GetCFHDropFormatType(); |
122 static FormatType GetFileDescriptorFormatType(); | 130 static FormatType GetFileDescriptorFormatType(); |
123 static FormatType GetFileContentFormatZeroType(); | 131 static FormatType GetFileContentFormatZeroType(); |
124 static FormatType GetWebKitSmartPasteFormatType(); | 132 static FormatType GetWebKitSmartPasteFormatType(); |
125 #endif | 133 #endif |
126 | 134 |
127 private: | 135 private: |
| 136 void WriteText(const char* text_data, size_t text_len); |
| 137 |
| 138 void WriteHTML(const char* markup_data, |
| 139 size_t markup_len, |
| 140 const char* url_data, |
| 141 size_t url_len); |
| 142 |
| 143 void WriteBookmark(const char* title_data, |
| 144 size_t title_len, |
| 145 const char* url_data, |
| 146 size_t url_len); |
| 147 |
| 148 void WriteHyperlink(const char* title_data, |
| 149 size_t title_len, |
| 150 const char* url_data, |
| 151 size_t url_len); |
| 152 |
| 153 void WriteWebSmartPaste(); |
| 154 |
| 155 void WriteFiles(const char* file_data, size_t file_len); |
| 156 |
| 157 void DispatchObject(ObjectType type, const ObjectMapParams& params); |
128 #if defined(OS_WIN) | 158 #if defined(OS_WIN) |
129 static void MarkupToHTMLClipboardFormat(const std::wstring& markup, | 159 void WriteBitmap(const char* pixel_data, const char* size_data); |
| 160 |
| 161 void WriteBitmapFromSharedMemory(const char* bitmap_data, |
| 162 const char* size_data, |
| 163 ProcessHandle handle); |
| 164 |
| 165 void WriteBitmapFromHandle(HBITMAP source_hbitmap, |
| 166 const gfx::Size& size); |
| 167 |
| 168 // Safely write to system clipboard. Free |handle| on failure. |
| 169 void WriteToClipboard(FormatType format, HANDLE handle); |
| 170 |
| 171 static void MarkupToHTMLClipboardFormat(const std::string& markup, |
130 const std::string& src_url, | 172 const std::string& src_url, |
131 std::string* html_fragment); | 173 std::string* html_fragment); |
132 | 174 |
133 static void ParseHTMLClipboardFormat(const std::string& html_fragment, | 175 static void ParseHTMLClipboardFormat(const std::string& html_fragment, |
134 std::wstring* markup, | 176 std::wstring* markup, |
135 std::string* src_url); | 177 std::string* src_url); |
136 | 178 |
137 static void ParseBookmarkClipboardFormat(const std::wstring& bookmark, | 179 static void ParseBookmarkClipboardFormat(const std::wstring& bookmark, |
138 std::wstring* title, | 180 std::wstring* title, |
139 std::string* url); | 181 std::string* url); |
140 | 182 |
| 183 // Free a handle depending on its type (as intuited from format) |
| 184 static void FreeData(FormatType format, HANDLE data); |
| 185 |
141 HWND clipboard_owner_; | 186 HWND clipboard_owner_; |
142 #elif defined(OS_LINUX) | 187 #elif defined(OS_LINUX) |
| 188 // Data is stored in the |clipboard_data_| map until it is saved to the system |
| 189 // clipboard. The Store* functions save data to the |clipboard_data_| map. The |
| 190 // SetGtkClipboard function replaces whatever is on the system clipboard with |
| 191 // the contents of |clipboard_data_|. |
| 192 // The Write* functions make a deep copy of the data passed to them an store |
| 193 // it in |clipboard_data_|. |
| 194 |
143 // Write changes to gtk clipboard. | 195 // Write changes to gtk clipboard. |
144 void SetGtkClipboard(); | 196 void SetGtkClipboard(); |
145 // Free pointers in clipboard_data_ and clear() the map. | 197 // Free pointers in clipboard_data_ and clear() the map. |
146 void FreeTargetMap(); | 198 void FreeTargetMap(); |
147 // Insert a mapping into clipboard_data_, or change an existing mapping. | 199 // Insert a mapping into clipboard_data_. |
148 uint8* InsertOrOverwrite(std::string target, uint8* data, size_t data_len); | 200 void InsertMapping(const char* key, char* data, size_t data_len); |
149 | 201 |
150 // We have to be able to store multiple formats on the clipboard | 202 TargetMap* clipboard_data_; |
151 // simultaneously. The Chrome Clipboard class accomplishes this by | |
152 // expecting that consecutive calls to Write* (WriteHTML, WriteText, etc.) | |
153 // The GTK clipboard interface does not naturally support consecutive calls | |
154 // building on one another. So we keep all data in a map, and always pass the | |
155 // map when we are setting the gtk clipboard. Consecutive calls to Write* | |
156 // will write to the map and set the GTK clipboard, then add to the same | |
157 // map and set the GTK clipboard again. GTK thinks it is wiping out the | |
158 // clipboard but we are actually keeping the previous data. | |
159 TargetMap clipboard_data_; | |
160 GtkClipboard* clipboard_; | 203 GtkClipboard* clipboard_; |
161 #endif | 204 #endif |
162 | 205 |
163 DISALLOW_EVIL_CONSTRUCTORS(Clipboard); | 206 DISALLOW_EVIL_CONSTRUCTORS(Clipboard); |
164 }; | 207 }; |
165 | 208 |
166 #endif // BASE_CLIPBOARD_H_ | 209 #endif // BASE_CLIPBOARD_H_ |
167 | 210 |
OLD | NEW |