| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2009 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 BASE_CLIPBOARD_H_ | |
| 6 #define BASE_CLIPBOARD_H_ | |
| 7 | |
| 8 #include <map> | |
| 9 #include <string> | |
| 10 #include <vector> | |
| 11 | |
| 12 #include "base/process.h" | |
| 13 #include "base/string16.h" | |
| 14 | |
| 15 namespace gfx { | |
| 16 class Size; | |
| 17 } | |
| 18 | |
| 19 class FilePath; | |
| 20 | |
| 21 class Clipboard { | |
| 22 public: | |
| 23 typedef std::string FormatType; | |
| 24 #if defined(USE_X11) | |
| 25 typedef struct _GtkClipboard GtkClipboard; | |
| 26 typedef std::map<FormatType, std::pair<char*, size_t> > TargetMap; | |
| 27 #endif | |
| 28 | |
| 29 // ObjectType designates the type of data to be stored in the clipboard. This | |
| 30 // designation is shared across all OSes. The system-specific designation | |
| 31 // is defined by FormatType. A single ObjectType might be represented by | |
| 32 // several system-specific FormatTypes. For example, on Linux the CBF_TEXT | |
| 33 // ObjectType maps to "text/plain", "STRING", and several other formats. On | |
| 34 // windows it maps to CF_UNICODETEXT. | |
| 35 enum ObjectType { | |
| 36 CBF_TEXT, | |
| 37 CBF_HTML, | |
| 38 CBF_BOOKMARK, | |
| 39 CBF_FILES, | |
| 40 CBF_WEBKIT, | |
| 41 CBF_BITMAP, | |
| 42 CBF_SMBITMAP, // Bitmap from shared memory. | |
| 43 CBF_DATA, // Arbitrary block of bytes. | |
| 44 }; | |
| 45 | |
| 46 // ObjectMap is a map from ObjectType to associated data. | |
| 47 // The data is organized differently for each ObjectType. The following | |
| 48 // table summarizes what kind of data is stored for each key. | |
| 49 // * indicates an optional argument. | |
| 50 // | |
| 51 // Key Arguments Type | |
| 52 // ------------------------------------- | |
| 53 // CBF_TEXT text char array | |
| 54 // CBF_HTML html char array | |
| 55 // url* char array | |
| 56 // CBF_BOOKMARK html char array | |
| 57 // url char array | |
| 58 // CBF_LINK html char array | |
| 59 // url char array | |
| 60 // CBF_FILES files char array representing multiple files. | |
| 61 // Filenames are separated by null characters and | |
| 62 // the final filename is double null terminated. | |
| 63 // The filenames are encoded in platform-specific | |
| 64 // encoding. | |
| 65 // CBF_WEBKIT none empty vector | |
| 66 // CBF_BITMAP pixels byte array | |
| 67 // size gfx::Size struct | |
| 68 // CBF_SMBITMAP shared_mem shared memory handle | |
| 69 // size gfx::Size struct | |
| 70 // CBF_DATA format char array | |
| 71 // data byte array | |
| 72 typedef std::vector<char> ObjectMapParam; | |
| 73 typedef std::vector<ObjectMapParam> ObjectMapParams; | |
| 74 typedef std::map<int /* ObjectType */, ObjectMapParams> ObjectMap; | |
| 75 | |
| 76 // Buffer designates which clipboard the action should be applied to. | |
| 77 // Only platforms that use the X Window System support the selection | |
| 78 // buffer. Furthermore we currently only use a buffer other than the | |
| 79 // standard buffer when reading from the clipboard so only those | |
| 80 // functions accept a buffer parameter. | |
| 81 enum Buffer { | |
| 82 BUFFER_STANDARD, | |
| 83 #if defined(OS_LINUX) | |
| 84 BUFFER_SELECTION, | |
| 85 #endif | |
| 86 }; | |
| 87 | |
| 88 static bool IsValidBuffer(int32 buffer) { | |
| 89 switch (buffer) { | |
| 90 case BUFFER_STANDARD: | |
| 91 return true; | |
| 92 #if defined(OS_LINUX) | |
| 93 case BUFFER_SELECTION: | |
| 94 return true; | |
| 95 #endif | |
| 96 } | |
| 97 return false; | |
| 98 } | |
| 99 | |
| 100 static Buffer FromInt(int32 buffer) { | |
| 101 return static_cast<Buffer>(buffer); | |
| 102 } | |
| 103 | |
| 104 Clipboard(); | |
| 105 ~Clipboard(); | |
| 106 | |
| 107 // Write a bunch of objects to the system clipboard. Copies are made of the | |
| 108 // contents of |objects|. On Windows they are copied to the system clipboard. | |
| 109 // On linux they are copied into a structure owned by the Clipboard object and | |
| 110 // kept until the system clipboard is set again. | |
| 111 void WriteObjects(const ObjectMap& objects); | |
| 112 | |
| 113 // Behaves as above. If there is some shared memory handle passed as one of | |
| 114 // the objects, it came from the process designated by |process|. This will | |
| 115 // assist in turning it into a shared memory region that the current process | |
| 116 // can use. | |
| 117 void WriteObjects(const ObjectMap& objects, base::ProcessHandle process); | |
| 118 | |
| 119 // On Linux, we need to know when the clipboard is set to a URL. Most | |
| 120 // platforms don't care. | |
| 121 #if !defined(OS_LINUX) | |
| 122 void DidWriteURL(const std::string& utf8_text) {} | |
| 123 #else // !defined(OS_LINUX) | |
| 124 void DidWriteURL(const std::string& utf8_text); | |
| 125 #endif | |
| 126 | |
| 127 // Tests whether the clipboard contains a certain format | |
| 128 bool IsFormatAvailable(const FormatType& format, Buffer buffer) const; | |
| 129 | |
| 130 // As above, but instead of interpreting |format| by some platform-specific | |
| 131 // definition, interpret it as a literal MIME type. | |
| 132 bool IsFormatAvailableByString(const std::string& format, | |
| 133 Buffer buffer) const; | |
| 134 | |
| 135 // Reads UNICODE text from the clipboard, if available. | |
| 136 void ReadText(Buffer buffer, string16* result) const; | |
| 137 | |
| 138 // Reads ASCII text from the clipboard, if available. | |
| 139 void ReadAsciiText(Buffer buffer, std::string* result) const; | |
| 140 | |
| 141 // Reads HTML from the clipboard, if available. | |
| 142 void ReadHTML(Buffer buffer, string16* markup, std::string* src_url) const; | |
| 143 | |
| 144 // Reads a bookmark from the clipboard, if available. | |
| 145 void ReadBookmark(string16* title, std::string* url) const; | |
| 146 | |
| 147 // Reads a file or group of files from the clipboard, if available, into the | |
| 148 // out parameter. | |
| 149 void ReadFile(FilePath* file) const; | |
| 150 void ReadFiles(std::vector<FilePath>* files) const; | |
| 151 | |
| 152 // Reads raw data from the clipboard with the given format type. Stores result | |
| 153 // as a byte vector. | |
| 154 void ReadData(const std::string& format, std::string* result); | |
| 155 | |
| 156 // Get format Identifiers for various types. | |
| 157 static FormatType GetUrlFormatType(); | |
| 158 static FormatType GetUrlWFormatType(); | |
| 159 static FormatType GetMozUrlFormatType(); | |
| 160 static FormatType GetPlainTextFormatType(); | |
| 161 static FormatType GetPlainTextWFormatType(); | |
| 162 static FormatType GetFilenameFormatType(); | |
| 163 static FormatType GetFilenameWFormatType(); | |
| 164 static FormatType GetWebKitSmartPasteFormatType(); | |
| 165 // Win: MS HTML Format, Other: Generic HTML format | |
| 166 static FormatType GetHtmlFormatType(); | |
| 167 #if defined(OS_WIN) | |
| 168 static FormatType GetBitmapFormatType(); | |
| 169 // Firefox text/html | |
| 170 static FormatType GetTextHtmlFormatType(); | |
| 171 static FormatType GetCFHDropFormatType(); | |
| 172 static FormatType GetFileDescriptorFormatType(); | |
| 173 static FormatType GetFileContentFormatZeroType(); | |
| 174 | |
| 175 // Duplicates any remote shared memory handle embedded inside |objects| that | |
| 176 // was created by |process| so that it can be used by this process. | |
| 177 static void DuplicateRemoteHandles(base::ProcessHandle process, | |
| 178 ObjectMap* objects); | |
| 179 #endif | |
| 180 | |
| 181 private: | |
| 182 void DispatchObject(ObjectType type, const ObjectMapParams& params); | |
| 183 | |
| 184 void WriteText(const char* text_data, size_t text_len); | |
| 185 | |
| 186 void WriteHTML(const char* markup_data, | |
| 187 size_t markup_len, | |
| 188 const char* url_data, | |
| 189 size_t url_len); | |
| 190 | |
| 191 void WriteBookmark(const char* title_data, | |
| 192 size_t title_len, | |
| 193 const char* url_data, | |
| 194 size_t url_len); | |
| 195 | |
| 196 void WriteWebSmartPaste(); | |
| 197 | |
| 198 void WriteFiles(const char* file_data, size_t file_len); | |
| 199 | |
| 200 void WriteBitmap(const char* pixel_data, const char* size_data); | |
| 201 | |
| 202 #if defined(OS_WIN) || defined(OS_LINUX) || defined(OS_FREEBSD) | |
| 203 // |format_name| is an ASCII string and should be NULL-terminated. | |
| 204 // TODO(estade): port to mac. | |
| 205 void WriteData(const char* format_name, size_t format_len, | |
| 206 const char* data_data, size_t data_len); | |
| 207 #endif | |
| 208 #if defined(OS_WIN) | |
| 209 void WriteBitmapFromSharedMemory(const char* bitmap_data, | |
| 210 const char* size_data, | |
| 211 base::ProcessHandle handle); | |
| 212 | |
| 213 void WriteBitmapFromHandle(HBITMAP source_hbitmap, | |
| 214 const gfx::Size& size); | |
| 215 | |
| 216 // Safely write to system clipboard. Free |handle| on failure. | |
| 217 void WriteToClipboard(unsigned int format, HANDLE handle); | |
| 218 | |
| 219 static void ParseBookmarkClipboardFormat(const string16& bookmark, | |
| 220 string16* title, | |
| 221 std::string* url); | |
| 222 | |
| 223 // Free a handle depending on its type (as intuited from format) | |
| 224 static void FreeData(unsigned int format, HANDLE data); | |
| 225 | |
| 226 // Return the window that should be the clipboard owner, creating it | |
| 227 // if neccessary. Marked const for lazily initialization by const methods. | |
| 228 HWND GetClipboardWindow() const; | |
| 229 | |
| 230 // Mark this as mutable so const methods can still do lazy initialization. | |
| 231 mutable HWND clipboard_owner_; | |
| 232 | |
| 233 // True if we can create a window. | |
| 234 bool create_window_; | |
| 235 #elif defined(USE_X11) | |
| 236 // Data is stored in the |clipboard_data_| map until it is saved to the system | |
| 237 // clipboard. The Store* functions save data to the |clipboard_data_| map. The | |
| 238 // SetGtkClipboard function replaces whatever is on the system clipboard with | |
| 239 // the contents of |clipboard_data_|. | |
| 240 // The Write* functions make a deep copy of the data passed to them an store | |
| 241 // it in |clipboard_data_|. | |
| 242 | |
| 243 // Write changes to gtk clipboard. | |
| 244 void SetGtkClipboard(); | |
| 245 // Insert a mapping into clipboard_data_. | |
| 246 void InsertMapping(const char* key, char* data, size_t data_len); | |
| 247 | |
| 248 // find the gtk clipboard for the passed buffer enum | |
| 249 GtkClipboard* LookupBackingClipboard(Buffer clipboard) const; | |
| 250 | |
| 251 TargetMap* clipboard_data_; | |
| 252 GtkClipboard* clipboard_; | |
| 253 GtkClipboard* primary_selection_; | |
| 254 #endif | |
| 255 | |
| 256 DISALLOW_COPY_AND_ASSIGN(Clipboard); | |
| 257 }; | |
| 258 | |
| 259 #endif // BASE_CLIPBOARD_H_ | |
| OLD | NEW |