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

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

Issue 12041078: Clear the clipboard closing Incognito window (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Selection clipboard support Created 7 years, 10 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
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/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 138 matching lines...) Expand 10 before | Expand all | Expand 10 after
162 // size gfx::Size struct 163 // size gfx::Size struct
163 // CBF_SMBITMAP shared_mem A pointer to an unmapped base::SharedMemory 164 // CBF_SMBITMAP shared_mem A pointer to an unmapped base::SharedMemory
164 // object containing the bitmap data. 165 // object containing the bitmap data.
165 // size gfx::Size struct 166 // size gfx::Size struct
166 // CBF_DATA format char array 167 // CBF_DATA format char array
167 // data byte array 168 // data byte array
168 typedef std::vector<char> ObjectMapParam; 169 typedef std::vector<char> ObjectMapParam;
169 typedef std::vector<ObjectMapParam> ObjectMapParams; 170 typedef std::vector<ObjectMapParam> ObjectMapParams;
170 typedef std::map<int /* ObjectType */, ObjectMapParams> ObjectMap; 171 typedef std::map<int /* ObjectType */, ObjectMapParams> ObjectMap;
171 172
173 // WriteObject() caller can use the SourceTag that will be stored in the
174 // clipboard. NULL value means "no tag".
175 typedef void* SourceTag;
176 static ObjectMapParam SourceTag2Binary(SourceTag tag);
177 static SourceTag Binary2SourceTag(const std::string& serialization);
178
172 // Buffer designates which clipboard the action should be applied to. 179 // Buffer designates which clipboard the action should be applied to.
173 // Only platforms that use the X Window System support the selection 180 // Only platforms that use the X Window System support the selection
174 // buffer. 181 // buffer.
175 enum Buffer { 182 enum Buffer {
176 BUFFER_STANDARD, 183 BUFFER_STANDARD,
177 BUFFER_SELECTION, 184 BUFFER_SELECTION,
178 }; 185 };
179 186
187 // This observer does track only calls to Clipboard::WriteObjects.
188 // Don't use it for notification about changed OS clipboard.
189 class ClipboardObserverForTesting {
jam 2013/02/11 21:28:53 observer interfaces for one callback is too heavyw
vasilii 2013/02/13 16:57:26 Done.
190 public:
191 // Called at the end of Clipboard::WriteObjects().
192 virtual void OnWriteObjects(Buffer buffer) = 0;
193 protected:
194 virtual ~ClipboardObserverForTesting() {}
195 };
196
197 void AddObserver(ClipboardObserverForTesting* obs) {
198 observer_list_.AddObserver(obs);
199 }
200
201 void RemoveObserver(ClipboardObserverForTesting* obs) {
202 observer_list_.RemoveObserver(obs);
203 }
204
180 static bool IsValidBuffer(int32 buffer) { 205 static bool IsValidBuffer(int32 buffer) {
181 switch (buffer) { 206 switch (buffer) {
182 case BUFFER_STANDARD: 207 case BUFFER_STANDARD:
183 return true; 208 return true;
184 #if defined(USE_X11) && !defined(OS_CHROMEOS) 209 #if defined(USE_X11) && !defined(OS_CHROMEOS)
185 case BUFFER_SELECTION: 210 case BUFFER_SELECTION:
186 return true; 211 return true;
187 #endif 212 #endif
188 } 213 }
189 return false; 214 return false;
(...skipping 17 matching lines...) Expand all
207 232
208 // Destroys the clipboard for the current thread. Usually, this will clean up 233 // Destroys the clipboard for the current thread. Usually, this will clean up
209 // all clipboards, except on Windows. (Previous code leaks the IO thread 234 // all clipboards, except on Windows. (Previous code leaks the IO thread
210 // clipboard, so it shouldn't be a problem.) 235 // clipboard, so it shouldn't be a problem.)
211 static void DestroyClipboardForCurrentThread(); 236 static void DestroyClipboardForCurrentThread();
212 237
213 // Write a bunch of objects to the system clipboard. Copies are made of the 238 // 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. 239 // 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 240 // On linux they are copied into a structure owned by the Clipboard object and
216 // kept until the system clipboard is set again. 241 // kept until the system clipboard is set again.
217 void WriteObjects(Buffer buffer, const ObjectMap& objects); 242 // SourceTag is optional value to be stored in the clipboard, NULL won't be
243 // stored.
244 void WriteObjects(Buffer buffer, const ObjectMap& objects, SourceTag tag);
218 245
219 // Returns a sequence number which uniquely identifies clipboard state. 246 // Returns a sequence number which uniquely identifies clipboard state.
220 // This can be used to version the data on the clipboard and determine 247 // This can be used to version the data on the clipboard and determine
221 // whether it has changed. 248 // whether it has changed.
222 uint64 GetSequenceNumber(Buffer buffer); 249 uint64 GetSequenceNumber(Buffer buffer);
223 250
224 // Tests whether the clipboard contains a certain format 251 // Tests whether the clipboard contains a certain format
225 bool IsFormatAvailable(const FormatType& format, Buffer buffer) const; 252 bool IsFormatAvailable(const FormatType& format, Buffer buffer) const;
226 253
227 // Clear the clipboard data. 254 // Clear the clipboard data.
(...skipping 26 matching lines...) Expand all
254 const string16& type, 281 const string16& type,
255 string16* result) const; 282 string16* result) const;
256 283
257 // Reads a bookmark from the clipboard, if available. 284 // Reads a bookmark from the clipboard, if available.
258 void ReadBookmark(string16* title, std::string* url) const; 285 void ReadBookmark(string16* title, std::string* url) const;
259 286
260 // Reads raw data from the clipboard with the given format type. Stores result 287 // Reads raw data from the clipboard with the given format type. Stores result
261 // as a byte vector. 288 // as a byte vector.
262 void ReadData(const FormatType& format, std::string* result) const; 289 void ReadData(const FormatType& format, std::string* result) const;
263 290
291 // Reads Source tag from the clipboard, if available.
292 SourceTag ReadSourceTag(Buffer buffer) const;
293
264 // Gets the FormatType corresponding to an arbitrary format string, 294 // Gets the FormatType corresponding to an arbitrary format string,
265 // registering it with the system if needed. Due to Windows/Linux 295 // registering it with the system if needed. Due to Windows/Linux
266 // limitiations, |format_string| must never be controlled by the user. 296 // limitiations, |format_string| must never be controlled by the user.
267 static FormatType GetFormatType(const std::string& format_string); 297 static FormatType GetFormatType(const std::string& format_string);
268 298
269 // Get format identifiers for various types. 299 // Get format identifiers for various types.
270 static const FormatType& GetUrlFormatType(); 300 static const FormatType& GetUrlFormatType();
271 static const FormatType& GetUrlWFormatType(); 301 static const FormatType& GetUrlWFormatType();
272 static const FormatType& GetMozUrlFormatType(); 302 static const FormatType& GetMozUrlFormatType();
273 static const FormatType& GetPlainTextFormatType(); 303 static const FormatType& GetPlainTextFormatType();
274 static const FormatType& GetPlainTextWFormatType(); 304 static const FormatType& GetPlainTextWFormatType();
275 static const FormatType& GetFilenameFormatType(); 305 static const FormatType& GetFilenameFormatType();
276 static const FormatType& GetFilenameWFormatType(); 306 static const FormatType& GetFilenameWFormatType();
277 static const FormatType& GetWebKitSmartPasteFormatType(); 307 static const FormatType& GetWebKitSmartPasteFormatType();
278 // Win: MS HTML Format, Other: Generic HTML format 308 // Win: MS HTML Format, Other: Generic HTML format
279 static const FormatType& GetHtmlFormatType(); 309 static const FormatType& GetHtmlFormatType();
280 static const FormatType& GetRtfFormatType(); 310 static const FormatType& GetRtfFormatType();
281 static const FormatType& GetBitmapFormatType(); 311 static const FormatType& GetBitmapFormatType();
282 // TODO(raymes): Unify web custom data and pepper custom data: 312 // TODO(raymes): Unify web custom data and pepper custom data:
283 // crbug.com/158399. 313 // crbug.com/158399.
284 static const FormatType& GetWebCustomDataFormatType(); 314 static const FormatType& GetWebCustomDataFormatType();
285 static const FormatType& GetPepperCustomDataFormatType(); 315 static const FormatType& GetPepperCustomDataFormatType();
316 static const FormatType& GetSourceTagFormatType();
286 317
287 // Embeds a pointer to a SharedMemory object pointed to by |bitmap_handle| 318 // Embeds a pointer to a SharedMemory object pointed to by |bitmap_handle|
288 // belonging to |process| into a shared bitmap [CBF_SMBITMAP] slot in 319 // belonging to |process| into a shared bitmap [CBF_SMBITMAP] slot in
289 // |objects|. The pointer is deleted by DispatchObjects(). 320 // |objects|. The pointer is deleted by DispatchObjects().
290 // 321 //
291 // On non-Windows platforms, |process| is ignored. 322 // On non-Windows platforms, |process| is ignored.
292 static void ReplaceSharedMemHandle(ObjectMap* objects, 323 static void ReplaceSharedMemHandle(ObjectMap* objects,
293 base::SharedMemoryHandle bitmap_handle, 324 base::SharedMemoryHandle bitmap_handle,
294 base::ProcessHandle process); 325 base::ProcessHandle process);
295 #if defined(OS_WIN) 326 #if defined(OS_WIN)
296 // Firefox text/html 327 // Firefox text/html
297 static const FormatType& GetTextHtmlFormatType(); 328 static const FormatType& GetTextHtmlFormatType();
298 static const FormatType& GetCFHDropFormatType(); 329 static const FormatType& GetCFHDropFormatType();
299 static const FormatType& GetFileDescriptorFormatType(); 330 static const FormatType& GetFileDescriptorFormatType();
300 static const FormatType& GetFileContentFormatZeroType(); 331 static const FormatType& GetFileContentFormatZeroType();
301 #endif 332 #endif
302 333
303 private: 334 private:
304 FRIEND_TEST_ALL_PREFIXES(ClipboardTest, SharedBitmapTest); 335 FRIEND_TEST_ALL_PREFIXES(ClipboardTest, SharedBitmapTest);
305 FRIEND_TEST_ALL_PREFIXES(ClipboardTest, EmptyHTMLTest); 336 FRIEND_TEST_ALL_PREFIXES(ClipboardTest, EmptyHTMLTest);
306 friend class ClipboardTest; 337 friend class ClipboardTest;
307 338
308 Clipboard(); 339 Clipboard();
309 ~Clipboard(); 340 ~Clipboard();
310 341
311 void DispatchObject(ObjectType type, const ObjectMapParams& params); 342 void DispatchObject(ObjectType type, const ObjectMapParams& params);
312 343
344 void WriteObjectsImpl(Buffer buffer, const ObjectMap& objects, SourceTag tag);
345
313 void WriteText(const char* text_data, size_t text_len); 346 void WriteText(const char* text_data, size_t text_len);
314 347
315 void WriteHTML(const char* markup_data, 348 void WriteHTML(const char* markup_data,
316 size_t markup_len, 349 size_t markup_len,
317 const char* url_data, 350 const char* url_data,
318 size_t url_len); 351 size_t url_len);
319 352
320 void WriteRTF(const char* rtf_data, size_t data_len); 353 void WriteRTF(const char* rtf_data, size_t data_len);
321 354
322 void WriteBookmark(const char* title_data, 355 void WriteBookmark(const char* title_data,
323 size_t title_len, 356 size_t title_len,
324 const char* url_data, 357 const char* url_data,
325 size_t url_len); 358 size_t url_len);
326 359
327 void WriteWebSmartPaste(); 360 void WriteWebSmartPaste();
328 361
329 void WriteBitmap(const char* pixel_data, const char* size_data); 362 void WriteBitmap(const char* pixel_data, const char* size_data);
330 363
331 void WriteData(const FormatType& format, 364 void WriteData(const FormatType& format,
332 const char* data_data, 365 const char* data_data,
333 size_t data_len); 366 size_t data_len);
367
368 void WriteSourceTag(SourceTag tag);
334 #if defined(OS_WIN) 369 #if defined(OS_WIN)
335 void WriteBitmapFromHandle(HBITMAP source_hbitmap, 370 void WriteBitmapFromHandle(HBITMAP source_hbitmap,
336 const gfx::Size& size); 371 const gfx::Size& size);
337 372
338 // Safely write to system clipboard. Free |handle| on failure. 373 // Safely write to system clipboard. Free |handle| on failure.
339 void WriteToClipboard(unsigned int format, HANDLE handle); 374 void WriteToClipboard(unsigned int format, HANDLE handle);
340 375
341 static void ParseBookmarkClipboardFormat(const string16& bookmark, 376 static void ParseBookmarkClipboardFormat(const string16& bookmark,
342 string16* title, 377 string16* title,
343 std::string* url); 378 std::string* url);
(...skipping 17 matching lines...) Expand all
361 // of data we intend to put on the clipboard on clipboard_data_ as 396 // of data we intend to put on the clipboard on clipboard_data_ as
362 // WriteObjects is running, and then at the end call SetGtkClipboard 397 // WriteObjects is running, and then at the end call SetGtkClipboard
363 // which replaces whatever is on the system clipboard with the 398 // which replaces whatever is on the system clipboard with the
364 // contents of clipboard_data_. 399 // contents of clipboard_data_.
365 400
366 public: 401 public:
367 typedef std::map<std::string, std::pair<char*, size_t> > TargetMap; 402 typedef std::map<std::string, std::pair<char*, size_t> > TargetMap;
368 403
369 private: 404 private:
370 // Write changes to gtk clipboard. 405 // Write changes to gtk clipboard.
371 void SetGtkClipboard(Buffer buffer); 406 void SetGtkClipboard(Buffer buffer, SourceTag tag);
372 // Insert a mapping into clipboard_data_. 407 // Insert a mapping into clipboard_data_.
373 void InsertMapping(const char* key, char* data, size_t data_len); 408 void InsertMapping(const char* key, char* data, size_t data_len);
374 409
375 // Find the gtk clipboard for the passed buffer enum. 410 // Find the gtk clipboard for the passed buffer enum.
376 GtkClipboard* LookupBackingClipboard(Buffer clipboard) const; 411 GtkClipboard* LookupBackingClipboard(Buffer clipboard) const;
412 // Reads raw data from the specified clipboard with the given format type.
dcheng 2013/02/11 22:58:21 I think in the long run, I'd prefer to see ReadDat
vasilii 2013/02/12 15:58:32 Yes, I agree to modify ReadData(). But note that R
413 void ReadDataImpl(Buffer buffer,
414 const FormatType& format,
415 std::string* result) const;
377 416
378 TargetMap* clipboard_data_; 417 TargetMap* clipboard_data_;
379 GtkClipboard* clipboard_; 418 GtkClipboard* clipboard_;
380 GtkClipboard* primary_selection_; 419 GtkClipboard* primary_selection_;
381 #elif defined(USE_AURA) && defined(USE_X11) && !defined(OS_CHROMEOS) 420 #elif defined(USE_AURA) && defined(USE_X11) && !defined(OS_CHROMEOS)
382 private: 421 private:
422 // Reads raw data from the specified clipboard with the given format type.
423 void ReadDataImpl(Buffer buffer,
424 const FormatType& format,
425 std::string* result) const;
383 // We keep our implementation details private because otherwise we bring in 426 // We keep our implementation details private because otherwise we bring in
384 // the X11 headers and break chrome compile. 427 // the X11 headers and break chrome compile.
385 class AuraX11Details; 428 class AuraX11Details;
386 scoped_ptr<AuraX11Details> aurax11_details_; 429 scoped_ptr<AuraX11Details> aurax11_details_;
387 #endif 430 #endif
431 ObserverList<ClipboardObserverForTesting> observer_list_;
388 432
389 DISALLOW_COPY_AND_ASSIGN(Clipboard); 433 DISALLOW_COPY_AND_ASSIGN(Clipboard);
390 }; 434 };
391 435
392 } // namespace ui 436 } // namespace ui
393 437
394 #endif // UI_BASE_CLIPBOARD_CLIPBOARD_H_ 438 #endif // UI_BASE_CLIPBOARD_CLIPBOARD_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698