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

Side by Side Diff: base/clipboard_win.cc

Issue 159815: Refactor bookmark clipboard code to be cross platform. (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: fix UMR Created 11 years, 4 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 | Annotate | Revision Log
OLDNEW
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 // Many of these functions are based on those found in 5 // Many of these functions are based on those found in
6 // webkit/port/platform/PasteboardWin.cpp 6 // webkit/port/platform/PasteboardWin.cpp
7 7
8 #include "base/clipboard.h" 8 #include "base/clipboard.h"
9 9
10 #include <shlobj.h> 10 #include <shlobj.h>
(...skipping 355 matching lines...) Expand 10 before | Expand all | Expand 10 after
366 DROPFILES* drop_files = reinterpret_cast<DROPFILES*>(data); 366 DROPFILES* drop_files = reinterpret_cast<DROPFILES*>(data);
367 drop_files->pFiles = sizeof(DROPFILES); 367 drop_files->pFiles = sizeof(DROPFILES);
368 drop_files->fWide = TRUE; 368 drop_files->fWide = TRUE;
369 369
370 memcpy(data + sizeof(DROPFILES), file_data, file_len); 370 memcpy(data + sizeof(DROPFILES), file_data, file_len);
371 371
372 ::GlobalUnlock(hdata); 372 ::GlobalUnlock(hdata);
373 WriteToClipboard(CF_HDROP, hdata); 373 WriteToClipboard(CF_HDROP, hdata);
374 } 374 }
375 375
376 void Clipboard::WriteData(const char* format_name, size_t format_len,
377 const char* data_data, size_t data_len) {
378 std::string format(format_name, format_len);
379 CLIPFORMAT clip_format =
380 ::RegisterClipboardFormat(ASCIIToWide(format).c_str());
381
382 HGLOBAL hdata = ::GlobalAlloc(GMEM_MOVEABLE, data_len);
383 if (!hdata)
384 return;
385
386 char* data = static_cast<char*>(::GlobalLock(hdata));
387 memcpy(data, data_data, data_len);
388 ::GlobalUnlock(data);
389 }
390
376 void Clipboard::WriteToClipboard(unsigned int format, HANDLE handle) { 391 void Clipboard::WriteToClipboard(unsigned int format, HANDLE handle) {
377 DCHECK(clipboard_owner_); 392 DCHECK(clipboard_owner_);
378 if (handle && !::SetClipboardData(format, handle)) { 393 if (handle && !::SetClipboardData(format, handle)) {
379 DCHECK(ERROR_CLIPBOARD_NOT_OPEN != GetLastError()); 394 DCHECK(ERROR_CLIPBOARD_NOT_OPEN != GetLastError());
380 FreeData(format, handle); 395 FreeData(format, handle);
381 } 396 }
382 } 397 }
383 398
384 bool Clipboard::IsFormatAvailable(const Clipboard::FormatType& format) const { 399 bool Clipboard::IsFormatAvailable(const Clipboard::FormatType& format) const {
385 return ::IsClipboardFormatAvailable(StringToInt(format)) != FALSE; 400 return ::IsClipboardFormatAvailable(StringToInt(format)) != FALSE;
386 } 401 }
387 402
403 bool Clipboard::IsFormatAvailableByString(const std::string& ascii_format) const {
sky 2009/08/04 16:14:29 nit: > 80
404 std::wstring wide_format = ASCIIToWide(ascii_format);
405 CLIPFORMAT format = ::RegisterClipboardFormat(wide_format.c_str());
406 return ::IsClipboardFormatAvailable(format) != FALSE;
407 }
408
388 void Clipboard::ReadText(string16* result) const { 409 void Clipboard::ReadText(string16* result) const {
389 if (!result) { 410 if (!result) {
390 NOTREACHED(); 411 NOTREACHED();
391 return; 412 return;
392 } 413 }
393 414
394 result->clear(); 415 result->clear();
395 416
396 // Acquire the clipboard. 417 // Acquire the clipboard.
397 ScopedClipboard clipboard; 418 ScopedClipboard clipboard;
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after
514 if (count) { 535 if (count) {
515 for (int i = 0; i < count; ++i) { 536 for (int i = 0; i < count; ++i) {
516 int size = ::DragQueryFile(drop, i, NULL, 0) + 1; 537 int size = ::DragQueryFile(drop, i, NULL, 0) + 1;
517 std::wstring file; 538 std::wstring file;
518 ::DragQueryFile(drop, i, WriteInto(&file, size), size); 539 ::DragQueryFile(drop, i, WriteInto(&file, size), size);
519 files->push_back(FilePath(file)); 540 files->push_back(FilePath(file));
520 } 541 }
521 } 542 }
522 } 543 }
523 544
545 void Clipboard::ReadData(const std::string& format, std::string* result) {
546 if (!result) {
547 NOTREACHED();
548 return;
549 }
550
551 CLIPFORMAT clip_format =
552 ::RegisterClipboardFormat(ASCIIToWide(format).c_str());
553
554 ScopedClipboard clipboard;
555 if (!clipboard.Acquire(GetClipboardWindow()))
556 return;
557
558 HANDLE data = ::GetClipboardData(clip_format);
559 if (!data)
560 return;
561
562 result->assign(static_cast<const char*>(::GlobalLock(data)),
563 ::GlobalSize(data));
564 ::GlobalUnlock(data);
565 }
566
524 // static 567 // static
525 void Clipboard::ParseBookmarkClipboardFormat(const string16& bookmark, 568 void Clipboard::ParseBookmarkClipboardFormat(const string16& bookmark,
526 string16* title, 569 string16* title,
527 std::string* url) { 570 std::string* url) {
528 const string16 kDelim = ASCIIToUTF16("\r\n"); 571 const string16 kDelim = ASCIIToUTF16("\r\n");
529 572
530 const size_t title_end = bookmark.find_first_of(kDelim); 573 const size_t title_end = bookmark.find_first_of(kDelim);
531 if (title) 574 if (title)
532 title->assign(bookmark.substr(0, title_end)); 575 title->assign(bookmark.substr(0, title_end));
533 576
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after
645 HWND Clipboard::GetClipboardWindow() const { 688 HWND Clipboard::GetClipboardWindow() const {
646 if (!clipboard_owner_ && create_window_) { 689 if (!clipboard_owner_ && create_window_) {
647 clipboard_owner_ = ::CreateWindow(L"ClipboardOwnerWindowClass", 690 clipboard_owner_ = ::CreateWindow(L"ClipboardOwnerWindowClass",
648 L"ClipboardOwnerWindow", 691 L"ClipboardOwnerWindow",
649 0, 0, 0, 0, 0, 692 0, 0, 0, 0, 0,
650 HWND_MESSAGE, 693 HWND_MESSAGE,
651 0, 0, 0); 694 0, 0, 0);
652 } 695 }
653 return clipboard_owner_; 696 return clipboard_owner_;
654 } 697 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698