Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(84)

Side by Side Diff: ui/base/clipboard/clipboard.h

Issue 658963003: Change Clipboard to use virtual methods for testing purposes. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: One more typo... Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « ui/base/android/ui_base_jni_registrar.cc ('k') | ui/base/clipboard/clipboard.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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/memory/scoped_ptr.h"
15 #include "base/memory/shared_memory.h" 14 #include "base/memory/shared_memory.h"
16 #include "base/process/process.h" 15 #include "base/process/process.h"
17 #include "base/strings/string16.h" 16 #include "base/strings/string16.h"
18 #include "base/threading/platform_thread.h" 17 #include "base/threading/platform_thread.h"
19 #include "base/threading/thread_checker.h" 18 #include "base/threading/thread_checker.h"
20 #include "ui/base/clipboard/clipboard_types.h" 19 #include "ui/base/clipboard/clipboard_types.h"
21 #include "ui/base/ui_base_export.h" 20 #include "ui/base/ui_base_export.h"
22 21
23 #if defined(OS_WIN) 22 #if defined(OS_WIN)
24 #include <objidl.h> 23 #include <objidl.h>
25 #elif defined(OS_ANDROID)
26 #include <jni.h>
27
28 #include "base/android/jni_android.h"
29 #include "base/android/scoped_java_ref.h"
30 #endif
31
32 #if defined(USE_AURA) && defined(USE_X11)
33 #include "base/memory/scoped_ptr.h"
34 #endif 24 #endif
35 25
36 namespace base { 26 namespace base {
37 class FilePath; 27 class FilePath;
38 28
39 namespace win { 29 namespace win {
40 class MessageWindow; 30 class MessageWindow;
41 } // namespace win 31 } // namespace win
42 } // namespace base 32 } // namespace base
43 33
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
81 std::string Serialize() const; 71 std::string Serialize() const;
82 static FormatType Deserialize(const std::string& serialization); 72 static FormatType Deserialize(const std::string& serialization);
83 73
84 #if defined(USE_AURA) 74 #if defined(USE_AURA)
85 // FormatType can be used in a set on some platforms. 75 // FormatType can be used in a set on some platforms.
86 bool operator<(const FormatType& other) const; 76 bool operator<(const FormatType& other) const;
87 #endif 77 #endif
88 78
89 #if defined(OS_WIN) 79 #if defined(OS_WIN)
90 const FORMATETC& ToFormatEtc() const { return data_; } 80 const FORMATETC& ToFormatEtc() const { return data_; }
91 #elif defined(USE_AURA) 81 #elif defined(USE_AURA) || defined(OS_ANDROID)
92 const std::string& ToString() const { return data_; } 82 const std::string& ToString() const { return data_; }
93 #elif defined(OS_MACOSX) 83 #elif defined(OS_MACOSX)
94 NSString* ToNSString() const { return data_; } 84 NSString* ToNSString() const { return data_; }
95 // Custom copy and assignment constructor to handle NSString. 85 // Custom copy and assignment constructor to handle NSString.
96 FormatType(const FormatType& other); 86 FormatType(const FormatType& other);
97 FormatType& operator=(const FormatType& other); 87 FormatType& operator=(const FormatType& other);
98 #endif 88 #endif
99 89
100 bool Equals(const FormatType& other) const; 90 bool Equals(const FormatType& other) const;
101 91
102 private: 92 private:
103 friend class Clipboard; 93 friend class Clipboard;
104 94
105 // Platform-specific glue used internally by the Clipboard class. Each 95 // Platform-specific glue used internally by the Clipboard class. Each
106 // plaform should define,at least one of each of the following: 96 // plaform should define,at least one of each of the following:
107 // 1. A constructor that wraps that native clipboard format descriptor. 97 // 1. A constructor that wraps that native clipboard format descriptor.
108 // 2. An accessor to retrieve the wrapped descriptor. 98 // 2. An accessor to retrieve the wrapped descriptor.
109 // 3. A data member to hold the wrapped descriptor. 99 // 3. A data member to hold the wrapped descriptor.
110 // 100 //
111 // Note that in some cases, the accessor for the wrapped descriptor may be 101 // Note that in some cases, the accessor for the wrapped descriptor may be
112 // public, as these format types can be used by drag and drop code as well. 102 // public, as these format types can be used by drag and drop code as well.
113 #if defined(OS_WIN) 103 #if defined(OS_WIN)
114 explicit FormatType(UINT native_format); 104 explicit FormatType(UINT native_format);
115 FormatType(UINT native_format, LONG index); 105 FormatType(UINT native_format, LONG index);
116 UINT ToUINT() const { return data_.cfFormat; }
dcheng 2014/10/16 09:38:05 I ended up removing this because it was awkward to
117 FORMATETC data_; 106 FORMATETC data_;
118 #elif defined(USE_AURA) 107 #elif defined(USE_AURA) || defined(OS_ANDROID)
119 explicit FormatType(const std::string& native_format); 108 explicit FormatType(const std::string& native_format);
120 const std::string& data() const { return data_; }
121 std::string data_; 109 std::string data_;
122 #elif defined(OS_MACOSX) 110 #elif defined(OS_MACOSX)
123 explicit FormatType(NSString* native_format); 111 explicit FormatType(NSString* native_format);
124 NSString* data_; 112 NSString* data_;
125 #elif defined(OS_ANDROID)
126 explicit FormatType(const std::string& native_format);
127 const std::string& data() const { return data_; }
128 std::string data_;
129 #else 113 #else
130 #error No FormatType definition. 114 #error No FormatType definition.
131 #endif 115 #endif
132 116
133 // Copyable and assignable, since this is essentially an opaque value type. 117 // Copyable and assignable, since this is essentially an opaque value type.
134 }; 118 };
135 119
136 // TODO(dcheng): Make this private once the IPC layer no longer needs to 120 // TODO(dcheng): Make this private once the IPC layer no longer needs to
137 // serialize this information. 121 // serialize this information.
138 // ObjectType designates the type of data to be stored in the clipboard. This 122 // ObjectType designates the type of data to be stored in the clipboard. This
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
204 static Clipboard* GetForCurrentThread(); 188 static Clipboard* GetForCurrentThread();
205 189
206 // Destroys the clipboard for the current thread. Usually, this will clean up 190 // Destroys the clipboard for the current thread. Usually, this will clean up
207 // all clipboards, except on Windows. (Previous code leaks the IO thread 191 // all clipboards, except on Windows. (Previous code leaks the IO thread
208 // clipboard, so it shouldn't be a problem.) 192 // clipboard, so it shouldn't be a problem.)
209 static void DestroyClipboardForCurrentThread(); 193 static void DestroyClipboardForCurrentThread();
210 194
211 // Returns a sequence number which uniquely identifies clipboard state. 195 // Returns a sequence number which uniquely identifies clipboard state.
212 // This can be used to version the data on the clipboard and determine 196 // This can be used to version the data on the clipboard and determine
213 // whether it has changed. 197 // whether it has changed.
214 uint64 GetSequenceNumber(ClipboardType type); 198 virtual uint64 GetSequenceNumber(ClipboardType type) = 0;
215 199
216 // Tests whether the clipboard contains a certain format 200 // Tests whether the clipboard contains a certain format
217 bool IsFormatAvailable(const FormatType& format, ClipboardType type) const; 201 virtual bool IsFormatAvailable(const FormatType& format,
202 ClipboardType type) const = 0;
218 203
219 // Clear the clipboard data. 204 // Clear the clipboard data.
220 void Clear(ClipboardType type); 205 virtual void Clear(ClipboardType type) = 0;
221 206
222 void ReadAvailableTypes(ClipboardType type, 207 virtual void ReadAvailableTypes(ClipboardType type,
223 std::vector<base::string16>* types, 208 std::vector<base::string16>* types,
224 bool* contains_filenames) const; 209 bool* contains_filenames) const = 0;
225 210
226 // Reads UNICODE text from the clipboard, if available. 211 // Reads UNICODE text from the clipboard, if available.
227 void ReadText(ClipboardType type, base::string16* result) const; 212 virtual void ReadText(ClipboardType type, base::string16* result) const = 0;
228 213
229 // Reads ASCII text from the clipboard, if available. 214 // Reads ASCII text from the clipboard, if available.
230 void ReadAsciiText(ClipboardType type, std::string* result) const; 215 virtual void ReadAsciiText(ClipboardType type, std::string* result) const = 0;
231 216
232 // Reads HTML from the clipboard, if available. If the HTML fragment requires 217 // Reads HTML from the clipboard, if available. If the HTML fragment requires
233 // context to parse, |fragment_start| and |fragment_end| are indexes into 218 // context to parse, |fragment_start| and |fragment_end| are indexes into
234 // markup indicating the beginning and end of the actual fragment. Otherwise, 219 // markup indicating the beginning and end of the actual fragment. Otherwise,
235 // they will contain 0 and markup->size(). 220 // they will contain 0 and markup->size().
236 void ReadHTML(ClipboardType type, 221 virtual void ReadHTML(ClipboardType type,
237 base::string16* markup, 222 base::string16* markup,
238 std::string* src_url, 223 std::string* src_url,
239 uint32* fragment_start, 224 uint32* fragment_start,
240 uint32* fragment_end) const; 225 uint32* fragment_end) const = 0;
241 226
242 // Reads RTF from the clipboard, if available. Stores the result as a byte 227 // Reads RTF from the clipboard, if available. Stores the result as a byte
243 // vector. 228 // vector.
244 void ReadRTF(ClipboardType type, std::string* result) const; 229 virtual void ReadRTF(ClipboardType type, std::string* result) const = 0;
245 230
246 // Reads an image from the clipboard, if available. 231 // Reads an image from the clipboard, if available.
247 SkBitmap ReadImage(ClipboardType type) const; 232 virtual SkBitmap ReadImage(ClipboardType type) const = 0;
248 233
249 void ReadCustomData(ClipboardType clipboard_type, 234 virtual void ReadCustomData(ClipboardType clipboard_type,
250 const base::string16& type, 235 const base::string16& type,
251 base::string16* result) const; 236 base::string16* result) const = 0;
252 237
253 // Reads a bookmark from the clipboard, if available. 238 // Reads a bookmark from the clipboard, if available.
254 void ReadBookmark(base::string16* title, std::string* url) const; 239 virtual void ReadBookmark(base::string16* title, std::string* url) const = 0;
255 240
256 // Reads raw data from the clipboard with the given format type. Stores result 241 // Reads raw data from the clipboard with the given format type. Stores result
257 // as a byte vector. 242 // as a byte vector.
258 void ReadData(const FormatType& format, std::string* result) const; 243 virtual void ReadData(const FormatType& format,
244 std::string* result) const = 0;
259 245
260 // Gets the FormatType corresponding to an arbitrary format string, 246 // Gets the FormatType corresponding to an arbitrary format string,
261 // registering it with the system if needed. Due to Windows/Linux 247 // registering it with the system if needed. Due to Windows/Linux
262 // limitiations, |format_string| must never be controlled by the user. 248 // limitiations, |format_string| must never be controlled by the user.
263 static FormatType GetFormatType(const std::string& format_string); 249 static FormatType GetFormatType(const std::string& format_string);
264 250
265 // Get format identifiers for various types. 251 // Get format identifiers for various types.
266 static const FormatType& GetUrlFormatType(); 252 static const FormatType& GetUrlFormatType();
267 static const FormatType& GetUrlWFormatType(); 253 static const FormatType& GetUrlWFormatType();
268 static const FormatType& GetMozUrlFormatType(); 254 static const FormatType& GetMozUrlFormatType();
(...skipping 22 matching lines...) Expand all
291 WARN_UNUSED_RESULT; 277 WARN_UNUSED_RESULT;
292 #if defined(OS_WIN) 278 #if defined(OS_WIN)
293 // Firefox text/html 279 // Firefox text/html
294 static const FormatType& GetTextHtmlFormatType(); 280 static const FormatType& GetTextHtmlFormatType();
295 static const FormatType& GetCFHDropFormatType(); 281 static const FormatType& GetCFHDropFormatType();
296 static const FormatType& GetFileDescriptorFormatType(); 282 static const FormatType& GetFileDescriptorFormatType();
297 static const FormatType& GetFileContentZeroFormatType(); 283 static const FormatType& GetFileContentZeroFormatType();
298 static const FormatType& GetIDListFormatType(); 284 static const FormatType& GetIDListFormatType();
299 #endif 285 #endif
300 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
301 private: 321 private:
302 FRIEND_TEST_ALL_PREFIXES(ClipboardTest, SharedBitmapTest); 322 FRIEND_TEST_ALL_PREFIXES(ClipboardTest, SharedBitmapTest);
303 FRIEND_TEST_ALL_PREFIXES(ClipboardTest, EmptyHTMLTest); 323 FRIEND_TEST_ALL_PREFIXES(ClipboardTest, EmptyHTMLTest);
304 friend class ClipboardTest; 324 friend class ClipboardTest;
305 // For access to WriteObjects(). 325 // For access to WriteObjects().
306 // TODO(dcheng): Remove the temporary exception for content. 326 // TODO(dcheng): Remove the temporary exception for content.
307 friend class content::ClipboardMessageFilter; 327 friend class content::ClipboardMessageFilter;
308 friend class ScopedClipboardWriter; 328 friend class ScopedClipboardWriter;
309 329
310 Clipboard();
311 ~Clipboard();
312
313 // Write a bunch of objects to the system clipboard. Copies are made of the
314 // contents of |objects|.
315 void WriteObjects(ClipboardType type, const ObjectMap& objects);
316
317 void DispatchObject(ObjectType type, const ObjectMapParams& params);
318
319 void WriteText(const char* text_data, size_t text_len);
320
321 void WriteHTML(const char* markup_data,
322 size_t markup_len,
323 const char* url_data,
324 size_t url_len);
325
326 void WriteRTF(const char* rtf_data, size_t data_len);
327
328 void WriteBookmark(const char* title_data,
329 size_t title_len,
330 const char* url_data,
331 size_t url_len);
332
333 void WriteWebSmartPaste();
334
335 void WriteBitmap(const SkBitmap& bitmap);
336
337 void WriteData(const FormatType& format,
338 const char* data_data,
339 size_t data_len);
340 #if defined(OS_WIN)
341 void WriteBitmapFromHandle(HBITMAP source_hbitmap,
342 const gfx::Size& size);
343
344 // Safely write to system clipboard. Free |handle| on failure.
345 void WriteToClipboard(unsigned int format, HANDLE handle);
346
347 static void ParseBookmarkClipboardFormat(const base::string16& bookmark,
348 base::string16* title,
349 std::string* url);
350
351 // Free a handle depending on its type (as intuited from format)
352 static void FreeData(unsigned int format, HANDLE data);
353
354 // Return the window that should be the clipboard owner, creating it
355 // if neccessary. Marked const for lazily initialization by const methods.
356 HWND GetClipboardWindow() const;
357
358 // Mark this as mutable so const methods can still do lazy initialization.
359 mutable scoped_ptr<base::win::MessageWindow> clipboard_owner_;
360
361 #elif defined(USE_CLIPBOARD_AURAX11)
362 private:
363 // We keep our implementation details private because otherwise we bring in
364 // the X11 headers and break chrome compile.
365 class AuraX11Details;
366 scoped_ptr<AuraX11Details> aurax11_details_;
367 #endif
368
369 DISALLOW_COPY_AND_ASSIGN(Clipboard); 330 DISALLOW_COPY_AND_ASSIGN(Clipboard);
370 }; 331 };
371 332
372 } // namespace ui 333 } // namespace ui
373 334
374 #endif // UI_BASE_CLIPBOARD_CLIPBOARD_H_ 335 #endif // UI_BASE_CLIPBOARD_CLIPBOARD_H_
OLDNEW
« no previous file with comments | « ui/base/android/ui_base_jni_registrar.cc ('k') | ui/base/clipboard/clipboard.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698