| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 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 | 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 "app/clipboard/clipboard.h" | 8 #include "app/clipboard/clipboard.h" |
| 9 | 9 |
| 10 #include <shlobj.h> | 10 #include <shlobj.h> |
| 11 #include <shellapi.h> | 11 #include <shellapi.h> |
| 12 | 12 |
| 13 #include "app/clipboard/clipboard_util_win.h" | 13 #include "app/clipboard/clipboard_util_win.h" |
| 14 #include "base/file_path.h" | 14 #include "base/file_path.h" |
| 15 #include "base/lock.h" | 15 #include "base/lock.h" |
| 16 #include "base/logging.h" | 16 #include "base/logging.h" |
| 17 #include "base/message_loop.h" | 17 #include "base/message_loop.h" |
| 18 #include "base/shared_memory.h" | 18 #include "base/shared_memory.h" |
| 19 #include "base/string_util.h" | 19 #include "base/string_util.h" // Remove when ASCIIToWide is in utf_string_... |
| 20 #include "base/string_number_conversions.h" |
| 20 #include "base/utf_string_conversions.h" | 21 #include "base/utf_string_conversions.h" |
| 21 #include "gfx/size.h" | 22 #include "gfx/size.h" |
| 22 | 23 |
| 23 namespace { | 24 namespace { |
| 24 | 25 |
| 25 // A scoper to manage acquiring and automatically releasing the clipboard. | 26 // A scoper to manage acquiring and automatically releasing the clipboard. |
| 26 class ScopedClipboard { | 27 class ScopedClipboard { |
| 27 public: | 28 public: |
| 28 ScopedClipboard() : opened_(false) { } | 29 ScopedClipboard() : opened_(false) { } |
| 29 | 30 |
| (...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 181 size_t url_len) { | 182 size_t url_len) { |
| 182 std::string markup(markup_data, markup_len); | 183 std::string markup(markup_data, markup_len); |
| 183 std::string url; | 184 std::string url; |
| 184 | 185 |
| 185 if (url_len > 0) | 186 if (url_len > 0) |
| 186 url.assign(url_data, url_len); | 187 url.assign(url_data, url_len); |
| 187 | 188 |
| 188 std::string html_fragment = ClipboardUtil::HtmlToCFHtml(markup, url); | 189 std::string html_fragment = ClipboardUtil::HtmlToCFHtml(markup, url); |
| 189 HGLOBAL glob = CreateGlobalData(html_fragment); | 190 HGLOBAL glob = CreateGlobalData(html_fragment); |
| 190 | 191 |
| 191 WriteToClipboard(StringToInt(GetHtmlFormatType()), glob); | 192 WriteToClipboard(ClipboardUtil::GetHtmlFormat()->cfFormat, glob); |
| 192 } | 193 } |
| 193 | 194 |
| 194 void Clipboard::WriteBookmark(const char* title_data, | 195 void Clipboard::WriteBookmark(const char* title_data, |
| 195 size_t title_len, | 196 size_t title_len, |
| 196 const char* url_data, | 197 const char* url_data, |
| 197 size_t url_len) { | 198 size_t url_len) { |
| 198 std::string bookmark(title_data, title_len); | 199 std::string bookmark(title_data, title_len); |
| 199 bookmark.append(1, L'\n'); | 200 bookmark.append(1, L'\n'); |
| 200 bookmark.append(url_data, url_len); | 201 bookmark.append(url_data, url_len); |
| 201 | 202 |
| 202 string16 wide_bookmark = UTF8ToWide(bookmark); | 203 string16 wide_bookmark = UTF8ToWide(bookmark); |
| 203 HGLOBAL glob = CreateGlobalData(wide_bookmark); | 204 HGLOBAL glob = CreateGlobalData(wide_bookmark); |
| 204 | 205 |
| 205 WriteToClipboard(StringToInt(GetUrlWFormatType()), glob); | 206 WriteToClipboard(ClipboardUtil::GetUrlWFormat()->cfFormat, glob); |
| 206 } | 207 } |
| 207 | 208 |
| 208 void Clipboard::WriteWebSmartPaste() { | 209 void Clipboard::WriteWebSmartPaste() { |
| 209 DCHECK(clipboard_owner_); | 210 DCHECK(clipboard_owner_); |
| 210 ::SetClipboardData(StringToInt(GetWebKitSmartPasteFormatType()), NULL); | 211 ::SetClipboardData(ClipboardUtil::GetWebKitSmartPasteFormat()->cfFormat, |
| 212 NULL); |
| 211 } | 213 } |
| 212 | 214 |
| 213 void Clipboard::WriteBitmap(const char* pixel_data, const char* size_data) { | 215 void Clipboard::WriteBitmap(const char* pixel_data, const char* size_data) { |
| 214 const gfx::Size* size = reinterpret_cast<const gfx::Size*>(size_data); | 216 const gfx::Size* size = reinterpret_cast<const gfx::Size*>(size_data); |
| 215 HDC dc = ::GetDC(NULL); | 217 HDC dc = ::GetDC(NULL); |
| 216 | 218 |
| 217 // This doesn't actually cost us a memcpy when the bitmap comes from the | 219 // This doesn't actually cost us a memcpy when the bitmap comes from the |
| 218 // renderer as we load it into the bitmap using setPixels which just sets a | 220 // renderer as we load it into the bitmap using setPixels which just sets a |
| 219 // pointer. Someone has to memcpy it into GDI, it might as well be us here. | 221 // pointer. Someone has to memcpy it into GDI, it might as well be us here. |
| 220 | 222 |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 307 DCHECK(clipboard_owner_); | 309 DCHECK(clipboard_owner_); |
| 308 if (handle && !::SetClipboardData(format, handle)) { | 310 if (handle && !::SetClipboardData(format, handle)) { |
| 309 DCHECK(ERROR_CLIPBOARD_NOT_OPEN != GetLastError()); | 311 DCHECK(ERROR_CLIPBOARD_NOT_OPEN != GetLastError()); |
| 310 FreeData(format, handle); | 312 FreeData(format, handle); |
| 311 } | 313 } |
| 312 } | 314 } |
| 313 | 315 |
| 314 bool Clipboard::IsFormatAvailable(const Clipboard::FormatType& format, | 316 bool Clipboard::IsFormatAvailable(const Clipboard::FormatType& format, |
| 315 Clipboard::Buffer buffer) const { | 317 Clipboard::Buffer buffer) const { |
| 316 DCHECK_EQ(buffer, BUFFER_STANDARD); | 318 DCHECK_EQ(buffer, BUFFER_STANDARD); |
| 317 return ::IsClipboardFormatAvailable(StringToInt(format)) != FALSE; | 319 int f; |
| 320 if (!base::StringToInt(format, &f)) |
| 321 return false; |
| 322 return ::IsClipboardFormatAvailable(f) != FALSE; |
| 318 } | 323 } |
| 319 | 324 |
| 320 bool Clipboard::IsFormatAvailableByString( | 325 bool Clipboard::IsFormatAvailableByString( |
| 321 const std::string& ascii_format, Clipboard::Buffer buffer) const { | 326 const std::string& ascii_format, Clipboard::Buffer buffer) const { |
| 322 DCHECK_EQ(buffer, BUFFER_STANDARD); | 327 DCHECK_EQ(buffer, BUFFER_STANDARD); |
| 323 std::wstring wide_format = ASCIIToWide(ascii_format); | 328 std::wstring wide_format = ASCIIToWide(ascii_format); |
| 324 CLIPFORMAT format = ::RegisterClipboardFormat(wide_format.c_str()); | 329 CLIPFORMAT format = ::RegisterClipboardFormat(wide_format.c_str()); |
| 325 return ::IsClipboardFormatAvailable(format) != FALSE; | 330 return ::IsClipboardFormatAvailable(format) != FALSE; |
| 326 } | 331 } |
| 327 | 332 |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 377 markup->clear(); | 382 markup->clear(); |
| 378 | 383 |
| 379 if (src_url) | 384 if (src_url) |
| 380 src_url->clear(); | 385 src_url->clear(); |
| 381 | 386 |
| 382 // Acquire the clipboard. | 387 // Acquire the clipboard. |
| 383 ScopedClipboard clipboard; | 388 ScopedClipboard clipboard; |
| 384 if (!clipboard.Acquire(GetClipboardWindow())) | 389 if (!clipboard.Acquire(GetClipboardWindow())) |
| 385 return; | 390 return; |
| 386 | 391 |
| 387 HANDLE data = ::GetClipboardData(StringToInt(GetHtmlFormatType())); | 392 HANDLE data = ::GetClipboardData(ClipboardUtil::GetHtmlFormat()->cfFormat); |
| 388 if (!data) | 393 if (!data) |
| 389 return; | 394 return; |
| 390 | 395 |
| 391 std::string html_fragment(static_cast<const char*>(::GlobalLock(data))); | 396 std::string html_fragment(static_cast<const char*>(::GlobalLock(data))); |
| 392 ::GlobalUnlock(data); | 397 ::GlobalUnlock(data); |
| 393 | 398 |
| 394 std::string markup_utf8; | 399 std::string markup_utf8; |
| 395 ClipboardUtil::CFHtmlToHtml(html_fragment, markup ? &markup_utf8 : NULL, | 400 ClipboardUtil::CFHtmlToHtml(html_fragment, markup ? &markup_utf8 : NULL, |
| 396 src_url); | 401 src_url); |
| 397 if (markup) | 402 if (markup) |
| 398 markup->assign(UTF8ToWide(markup_utf8)); | 403 markup->assign(UTF8ToWide(markup_utf8)); |
| 399 } | 404 } |
| 400 | 405 |
| 401 void Clipboard::ReadBookmark(string16* title, std::string* url) const { | 406 void Clipboard::ReadBookmark(string16* title, std::string* url) const { |
| 402 if (title) | 407 if (title) |
| 403 title->clear(); | 408 title->clear(); |
| 404 | 409 |
| 405 if (url) | 410 if (url) |
| 406 url->clear(); | 411 url->clear(); |
| 407 | 412 |
| 408 // Acquire the clipboard. | 413 // Acquire the clipboard. |
| 409 ScopedClipboard clipboard; | 414 ScopedClipboard clipboard; |
| 410 if (!clipboard.Acquire(GetClipboardWindow())) | 415 if (!clipboard.Acquire(GetClipboardWindow())) |
| 411 return; | 416 return; |
| 412 | 417 |
| 413 HANDLE data = ::GetClipboardData(StringToInt(GetUrlWFormatType())); | 418 HANDLE data = ::GetClipboardData(ClipboardUtil::GetUrlWFormat()->cfFormat); |
| 414 if (!data) | 419 if (!data) |
| 415 return; | 420 return; |
| 416 | 421 |
| 417 string16 bookmark(static_cast<const char16*>(::GlobalLock(data))); | 422 string16 bookmark(static_cast<const char16*>(::GlobalLock(data))); |
| 418 ::GlobalUnlock(data); | 423 ::GlobalUnlock(data); |
| 419 | 424 |
| 420 ParseBookmarkClipboardFormat(bookmark, title, url); | 425 ParseBookmarkClipboardFormat(bookmark, title, url); |
| 421 } | 426 } |
| 422 | 427 |
| 423 // Read a file in HDROP format from the clipboard. | 428 // Read a file in HDROP format from the clipboard. |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 500 | 505 |
| 501 if (url) { | 506 if (url) { |
| 502 const size_t url_start = bookmark.find_first_not_of(kDelim, title_end); | 507 const size_t url_start = bookmark.find_first_not_of(kDelim, title_end); |
| 503 if (url_start != string16::npos) | 508 if (url_start != string16::npos) |
| 504 *url = UTF16ToUTF8(bookmark.substr(url_start, string16::npos)); | 509 *url = UTF16ToUTF8(bookmark.substr(url_start, string16::npos)); |
| 505 } | 510 } |
| 506 } | 511 } |
| 507 | 512 |
| 508 // static | 513 // static |
| 509 Clipboard::FormatType Clipboard::GetUrlFormatType() { | 514 Clipboard::FormatType Clipboard::GetUrlFormatType() { |
| 510 return IntToString(ClipboardUtil::GetUrlFormat()->cfFormat); | 515 return base::IntToString(ClipboardUtil::GetUrlFormat()->cfFormat); |
| 511 } | 516 } |
| 512 | 517 |
| 513 // static | 518 // static |
| 514 Clipboard::FormatType Clipboard::GetUrlWFormatType() { | 519 Clipboard::FormatType Clipboard::GetUrlWFormatType() { |
| 515 return IntToString(ClipboardUtil::GetUrlWFormat()->cfFormat); | 520 return base::IntToString(ClipboardUtil::GetUrlWFormat()->cfFormat); |
| 516 } | 521 } |
| 517 | 522 |
| 518 // static | 523 // static |
| 519 Clipboard::FormatType Clipboard::GetMozUrlFormatType() { | 524 Clipboard::FormatType Clipboard::GetMozUrlFormatType() { |
| 520 return IntToString(ClipboardUtil::GetMozUrlFormat()->cfFormat); | 525 return base::IntToString(ClipboardUtil::GetMozUrlFormat()->cfFormat); |
| 521 } | 526 } |
| 522 | 527 |
| 523 // static | 528 // static |
| 524 Clipboard::FormatType Clipboard::GetPlainTextFormatType() { | 529 Clipboard::FormatType Clipboard::GetPlainTextFormatType() { |
| 525 return IntToString(ClipboardUtil::GetPlainTextFormat()->cfFormat); | 530 return base::IntToString(ClipboardUtil::GetPlainTextFormat()->cfFormat); |
| 526 } | 531 } |
| 527 | 532 |
| 528 // static | 533 // static |
| 529 Clipboard::FormatType Clipboard::GetPlainTextWFormatType() { | 534 Clipboard::FormatType Clipboard::GetPlainTextWFormatType() { |
| 530 return IntToString(ClipboardUtil::GetPlainTextWFormat()->cfFormat); | 535 return base::IntToString(ClipboardUtil::GetPlainTextWFormat()->cfFormat); |
| 531 } | 536 } |
| 532 | 537 |
| 533 // static | 538 // static |
| 534 Clipboard::FormatType Clipboard::GetFilenameFormatType() { | 539 Clipboard::FormatType Clipboard::GetFilenameFormatType() { |
| 535 return IntToString(ClipboardUtil::GetFilenameFormat()->cfFormat); | 540 return base::IntToString(ClipboardUtil::GetFilenameFormat()->cfFormat); |
| 536 } | 541 } |
| 537 | 542 |
| 538 // static | 543 // static |
| 539 Clipboard::FormatType Clipboard::GetFilenameWFormatType() { | 544 Clipboard::FormatType Clipboard::GetFilenameWFormatType() { |
| 540 return IntToString(ClipboardUtil::GetFilenameWFormat()->cfFormat); | 545 return base::IntToString(ClipboardUtil::GetFilenameWFormat()->cfFormat); |
| 541 } | 546 } |
| 542 | 547 |
| 543 // MS HTML Format | 548 // MS HTML Format |
| 544 // static | 549 // static |
| 545 Clipboard::FormatType Clipboard::GetHtmlFormatType() { | 550 Clipboard::FormatType Clipboard::GetHtmlFormatType() { |
| 546 return IntToString(ClipboardUtil::GetHtmlFormat()->cfFormat); | 551 return base::IntToString(ClipboardUtil::GetHtmlFormat()->cfFormat); |
| 547 } | 552 } |
| 548 | 553 |
| 549 // static | 554 // static |
| 550 Clipboard::FormatType Clipboard::GetBitmapFormatType() { | 555 Clipboard::FormatType Clipboard::GetBitmapFormatType() { |
| 551 return IntToString(CF_BITMAP); | 556 return base::IntToString(CF_BITMAP); |
| 552 } | 557 } |
| 553 | 558 |
| 554 // Firefox text/html | 559 // Firefox text/html |
| 555 // static | 560 // static |
| 556 Clipboard::FormatType Clipboard::GetTextHtmlFormatType() { | 561 Clipboard::FormatType Clipboard::GetTextHtmlFormatType() { |
| 557 return IntToString(ClipboardUtil::GetTextHtmlFormat()->cfFormat); | 562 return base::IntToString(ClipboardUtil::GetTextHtmlFormat()->cfFormat); |
| 558 } | 563 } |
| 559 | 564 |
| 560 // static | 565 // static |
| 561 Clipboard::FormatType Clipboard::GetCFHDropFormatType() { | 566 Clipboard::FormatType Clipboard::GetCFHDropFormatType() { |
| 562 return IntToString(ClipboardUtil::GetCFHDropFormat()->cfFormat); | 567 return base::IntToString(ClipboardUtil::GetCFHDropFormat()->cfFormat); |
| 563 } | 568 } |
| 564 | 569 |
| 565 // static | 570 // static |
| 566 Clipboard::FormatType Clipboard::GetFileDescriptorFormatType() { | 571 Clipboard::FormatType Clipboard::GetFileDescriptorFormatType() { |
| 567 return IntToString(ClipboardUtil::GetFileDescriptorFormat()->cfFormat); | 572 return base::IntToString(ClipboardUtil::GetFileDescriptorFormat()->cfFormat); |
| 568 } | 573 } |
| 569 | 574 |
| 570 // static | 575 // static |
| 571 Clipboard::FormatType Clipboard::GetFileContentFormatZeroType() { | 576 Clipboard::FormatType Clipboard::GetFileContentFormatZeroType() { |
| 572 return IntToString(ClipboardUtil::GetFileContentFormatZero()->cfFormat); | 577 return base::IntToString(ClipboardUtil::GetFileContentFormatZero()->cfFormat); |
| 573 } | 578 } |
| 574 | 579 |
| 575 // static | 580 // static |
| 576 Clipboard::FormatType Clipboard::GetWebKitSmartPasteFormatType() { | 581 Clipboard::FormatType Clipboard::GetWebKitSmartPasteFormatType() { |
| 577 return IntToString(ClipboardUtil::GetWebKitSmartPasteFormat()->cfFormat); | 582 return base::IntToString( |
| 583 ClipboardUtil::GetWebKitSmartPasteFormat()->cfFormat); |
| 578 } | 584 } |
| 579 | 585 |
| 580 // static | 586 // static |
| 581 void Clipboard::FreeData(unsigned int format, HANDLE data) { | 587 void Clipboard::FreeData(unsigned int format, HANDLE data) { |
| 582 if (format == CF_BITMAP) | 588 if (format == CF_BITMAP) |
| 583 ::DeleteObject(static_cast<HBITMAP>(data)); | 589 ::DeleteObject(static_cast<HBITMAP>(data)); |
| 584 else | 590 else |
| 585 ::GlobalFree(data); | 591 ::GlobalFree(data); |
| 586 } | 592 } |
| 587 | 593 |
| 588 HWND Clipboard::GetClipboardWindow() const { | 594 HWND Clipboard::GetClipboardWindow() const { |
| 589 if (!clipboard_owner_ && create_window_) { | 595 if (!clipboard_owner_ && create_window_) { |
| 590 clipboard_owner_ = ::CreateWindow(L"ClipboardOwnerWindowClass", | 596 clipboard_owner_ = ::CreateWindow(L"ClipboardOwnerWindowClass", |
| 591 L"ClipboardOwnerWindow", | 597 L"ClipboardOwnerWindow", |
| 592 0, 0, 0, 0, 0, | 598 0, 0, 0, 0, 0, |
| 593 HWND_MESSAGE, | 599 HWND_MESSAGE, |
| 594 0, 0, 0); | 600 0, 0, 0); |
| 595 } | 601 } |
| 596 return clipboard_owner_; | 602 return clipboard_owner_; |
| 597 } | 603 } |
| OLD | NEW |