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

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

Issue 720373003: Add FakeClipboard implementation for unit tests. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: One more fix... Created 6 years, 1 month 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
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/lazy_instance.h"
13 #include "base/memory/shared_memory.h" 14 #include "base/memory/shared_memory.h"
14 #include "base/process/process.h" 15 #include "base/process/process.h"
15 #include "base/strings/string16.h" 16 #include "base/strings/string16.h"
17 #include "base/synchronization/lock.h"
16 #include "base/threading/platform_thread.h" 18 #include "base/threading/platform_thread.h"
17 #include "base/threading/thread_checker.h" 19 #include "base/threading/thread_checker.h"
18 #include "ui/base/clipboard/clipboard_types.h" 20 #include "ui/base/clipboard/clipboard_types.h"
19 #include "ui/base/ui_base_export.h" 21 #include "ui/base/ui_base_export.h"
20 22
21 #if defined(OS_WIN) 23 #if defined(OS_WIN)
22 #include <objidl.h> 24 #include <objidl.h>
23 #endif 25 #endif
24 26
25 namespace base { 27 namespace base {
(...skipping 17 matching lines...) Expand all
43 45
44 #ifdef __OBJC__ 46 #ifdef __OBJC__
45 @class NSString; 47 @class NSString;
46 #else 48 #else
47 class NSString; 49 class NSString;
48 #endif 50 #endif
49 51
50 namespace ui { 52 namespace ui {
51 template <typename T> 53 template <typename T>
52 class ClipboardTest; 54 class ClipboardTest;
55 class FakeClipboard;
53 class ScopedClipboardWriter; 56 class ScopedClipboardWriter;
54 57
55 class UI_BASE_EXPORT Clipboard : NON_EXPORTED_BASE(public base::ThreadChecker) { 58 class UI_BASE_EXPORT Clipboard : NON_EXPORTED_BASE(public base::ThreadChecker) {
56 public: 59 public:
57 // MIME type constants. 60 // MIME type constants.
58 static const char kMimeTypeText[]; 61 static const char kMimeTypeText[];
59 static const char kMimeTypeURIList[]; 62 static const char kMimeTypeURIList[];
60 static const char kMimeTypeDownloadURL[]; 63 static const char kMimeTypeDownloadURL[];
61 static const char kMimeTypeHTML[]; 64 static const char kMimeTypeHTML[];
62 static const char kMimeTypeRTF[]; 65 static const char kMimeTypeRTF[];
63 static const char kMimeTypePNG[]; 66 static const char kMimeTypePNG[];
64 67
65 // Platform neutral holder for native data representation of a clipboard type. 68 // Platform neutral holder for native data representation of a clipboard type.
66 struct UI_BASE_EXPORT FormatType { 69 struct UI_BASE_EXPORT FormatType {
67 FormatType(); 70 FormatType();
68 ~FormatType(); 71 ~FormatType();
69 72
70 // Serializes and deserializes a FormatType for use in IPC messages. 73 // Serializes and deserializes a FormatType for use in IPC messages.
71 std::string Serialize() const; 74 std::string Serialize() const;
72 static FormatType Deserialize(const std::string& serialization); 75 static FormatType Deserialize(const std::string& serialization);
73 76
74 #if !defined(OS_ANDROID)
75 // FormatType can be used in a set on some platforms. 77 // FormatType can be used in a set on some platforms.
76 bool operator<(const FormatType& other) const; 78 bool operator<(const FormatType& other) const;
77 #endif
78 79
79 #if defined(OS_WIN) 80 #if defined(OS_WIN)
80 const FORMATETC& ToFormatEtc() const { return data_; } 81 const FORMATETC& ToFormatEtc() const { return data_; }
81 #elif defined(USE_AURA) || defined(OS_ANDROID) 82 #elif defined(USE_AURA) || defined(OS_ANDROID)
82 const std::string& ToString() const { return data_; } 83 const std::string& ToString() const { return data_; }
83 #elif defined(OS_MACOSX) 84 #elif defined(OS_MACOSX)
84 NSString* ToNSString() const { return data_; } 85 NSString* ToNSString() const { return data_; }
85 // Custom copy and assignment constructor to handle NSString. 86 // Custom copy and assignment constructor to handle NSString.
86 FormatType(const FormatType& other); 87 FormatType(const FormatType& other);
87 FormatType& operator=(const FormatType& other); 88 FormatType& operator=(const FormatType& other);
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after
180 const std::vector<base::PlatformThreadId>& allowed_threads); 181 const std::vector<base::PlatformThreadId>& allowed_threads);
181 182
182 // Returns the clipboard object for the current thread. 183 // Returns the clipboard object for the current thread.
183 // 184 //
184 // Most implementations will have at most one clipboard which will live on 185 // 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 // 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 // be two clipboards: one that lives on the UI thread and one that lives on
187 // the IO thread. 188 // the IO thread.
188 static Clipboard* GetForCurrentThread(); 189 static Clipboard* GetForCurrentThread();
189 190
191 // Testing helper to Replace any clipboard for the current thread with a fake
sky 2014/11/14 02:10:31 Why the caps for Replace?
dcheng 2014/11/14 03:30:28 Done.
192 // clipboard. Useful for unit tests, since those generally run in parallel.
193 static void UseFakeForCurrentThreadForTest();
sky 2014/11/14 02:10:31 How about UseTestClipboardForCurrentThread? Or Moc
dcheng 2014/11/14 03:30:28 Done.
194
190 // Destroys the clipboard for the current thread. Usually, this will clean up 195 // Destroys the clipboard for the current thread. Usually, this will clean up
191 // all clipboards, except on Windows. (Previous code leaks the IO thread 196 // all clipboards, except on Windows. (Previous code leaks the IO thread
192 // clipboard, so it shouldn't be a problem.) 197 // clipboard, so it shouldn't be a problem.)
193 static void DestroyClipboardForCurrentThread(); 198 static void DestroyClipboardForCurrentThread();
194 199
195 // Returns a sequence number which uniquely identifies clipboard state. 200 // Returns a sequence number which uniquely identifies clipboard state.
196 // This can be used to version the data on the clipboard and determine 201 // This can be used to version the data on the clipboard and determine
197 // whether it has changed. 202 // whether it has changed.
198 virtual uint64 GetSequenceNumber(ClipboardType type) = 0; 203 virtual uint64 GetSequenceNumber(ClipboardType type) const = 0;
199 204
200 // Tests whether the clipboard contains a certain format 205 // Tests whether the clipboard contains a certain format
201 virtual bool IsFormatAvailable(const FormatType& format, 206 virtual bool IsFormatAvailable(const FormatType& format,
202 ClipboardType type) const = 0; 207 ClipboardType type) const = 0;
203 208
204 // Clear the clipboard data. 209 // Clear the clipboard data.
205 virtual void Clear(ClipboardType type) = 0; 210 virtual void Clear(ClipboardType type) = 0;
206 211
207 virtual void ReadAvailableTypes(ClipboardType type, 212 virtual void ReadAvailableTypes(ClipboardType type,
208 std::vector<base::string16>* types, 213 std::vector<base::string16>* types,
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after
319 size_t data_len) = 0; 324 size_t data_len) = 0;
320 325
321 private: 326 private:
322 template <typename T> 327 template <typename T>
323 friend class ClipboardTest; 328 friend class ClipboardTest;
324 // For access to WriteObjects(). 329 // For access to WriteObjects().
325 // TODO(dcheng): Remove the temporary exception for content. 330 // TODO(dcheng): Remove the temporary exception for content.
326 friend class content::ClipboardMessageFilter; 331 friend class content::ClipboardMessageFilter;
327 friend class ScopedClipboardWriter; 332 friend class ScopedClipboardWriter;
328 333
334 // A list of allowed threads. By default, this is empty and no thread checking
335 // is done (in the unit test case), but a user (like content) can set which
336 // threads are allowed to call this method.
337 typedef std::vector<base::PlatformThreadId> AllowedThreadsVector;
338 static base::LazyInstance<AllowedThreadsVector> allowed_threads_;
339
340 // Mapping from threads to clipboard objects.
341 typedef std::map<base::PlatformThreadId, Clipboard*> ClipboardMap;
342 static base::LazyInstance<ClipboardMap> clipboard_map_;
343
344 // Mutex that controls access to |g_clipboard_map|.
345 static base::LazyInstance<base::Lock>::Leaky clipboard_map_lock_;
346
329 DISALLOW_COPY_AND_ASSIGN(Clipboard); 347 DISALLOW_COPY_AND_ASSIGN(Clipboard);
330 }; 348 };
331 349
332 } // namespace ui 350 } // namespace ui
333 351
334 #endif // UI_BASE_CLIPBOARD_CLIPBOARD_H_ 352 #endif // UI_BASE_CLIPBOARD_CLIPBOARD_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698