OLD | NEW |
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 #include "base/clipboard_util.h" | 5 #include "base/clipboard_util.h" |
6 | 6 |
7 #include <shellapi.h> | 7 #include <shellapi.h> |
8 #include <shlwapi.h> | 8 #include <shlwapi.h> |
9 #include <wininet.h> | 9 #include <wininet.h> |
10 | 10 |
(...skipping 288 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
299 } else { | 299 } else { |
300 //If a file is dropped on the window, it does not provide either of the | 300 //If a file is dropped on the window, it does not provide either of the |
301 //plain text formats, so here we try to forcibly get a url. | 301 //plain text formats, so here we try to forcibly get a url. |
302 std::wstring title; | 302 std::wstring title; |
303 success = GetUrl(data_object, plain_text, &title); | 303 success = GetUrl(data_object, plain_text, &title); |
304 } | 304 } |
305 | 305 |
306 return success; | 306 return success; |
307 } | 307 } |
308 | 308 |
309 bool ClipboardUtil::GetCFHtml(IDataObject* data_object, | 309 bool ClipboardUtil::GetHtml(IDataObject* data_object, |
310 std::wstring* cf_html) { | 310 std::wstring* html, std::string* base_url) { |
311 DCHECK(data_object && cf_html); | 311 DCHECK(data_object && html && base_url); |
312 if (FAILED(data_object->QueryGetData(GetHtmlFormat()))) | |
313 return false; | |
314 | 312 |
315 STGMEDIUM store; | 313 if (SUCCEEDED(data_object->QueryGetData(GetHtmlFormat()))) { |
316 if (FAILED(data_object->GetData(GetHtmlFormat(), &store))) | 314 STGMEDIUM store; |
317 return false; | 315 if (SUCCEEDED(data_object->GetData(GetHtmlFormat(), &store))) { |
| 316 // MS CF html |
| 317 ScopedHGlobal<char> data(store.hGlobal); |
318 | 318 |
319 // MS CF html | 319 std::string html_utf8; |
320 ScopedHGlobal<char> data(store.hGlobal); | 320 CFHtmlToHtml(std::string(data.get(), data.Size()), &html_utf8, base_url); |
321 cf_html->assign(UTF8ToWide(std::string(data.get(), data.Size()))); | 321 html->assign(UTF8ToWide(html_utf8)); |
322 ReleaseStgMedium(&store); | |
323 return true; | |
324 } | |
325 | 322 |
326 bool ClipboardUtil::GetTextHtml(IDataObject* data_object, | 323 ReleaseStgMedium(&store); |
327 std::wstring* text_html) { | 324 return true; |
328 DCHECK(data_object && text_html); | 325 } |
| 326 } |
| 327 |
329 if (FAILED(data_object->QueryGetData(GetTextHtmlFormat()))) | 328 if (FAILED(data_object->QueryGetData(GetTextHtmlFormat()))) |
330 return false; | 329 return false; |
331 | 330 |
332 STGMEDIUM store; | 331 STGMEDIUM store; |
333 if (FAILED(data_object->GetData(GetTextHtmlFormat(), &store))) | 332 if (FAILED(data_object->GetData(GetTextHtmlFormat(), &store))) |
334 return false; | 333 return false; |
335 | 334 |
336 // raw html | 335 // text/html |
337 ScopedHGlobal<wchar_t> data(store.hGlobal); | 336 ScopedHGlobal<wchar_t> data(store.hGlobal); |
338 text_html->assign(data.get()); | 337 html->assign(data.get()); |
339 ReleaseStgMedium(&store); | 338 ReleaseStgMedium(&store); |
340 return true; | 339 return true; |
341 } | 340 } |
342 | 341 |
343 bool ClipboardUtil::GetFileContents(IDataObject* data_object, | 342 bool ClipboardUtil::GetFileContents(IDataObject* data_object, |
344 std::wstring* filename, std::string* file_contents) { | 343 std::wstring* filename, std::string* file_contents) { |
345 DCHECK(data_object && filename && file_contents); | 344 DCHECK(data_object && filename && file_contents); |
346 bool has_data = | 345 bool has_data = |
347 SUCCEEDED(data_object->QueryGetData(GetFileContentFormatZero())) || | 346 SUCCEEDED(data_object->QueryGetData(GetFileContentFormatZero())) || |
348 SUCCEEDED(data_object->QueryGetData(GetFileDescriptorFormat())); | 347 SUCCEEDED(data_object->QueryGetData(GetFileDescriptorFormat())); |
(...skipping 17 matching lines...) Expand all Loading... |
366 &description))) { | 365 &description))) { |
367 ScopedHGlobal<FILEGROUPDESCRIPTOR> fgd(description.hGlobal); | 366 ScopedHGlobal<FILEGROUPDESCRIPTOR> fgd(description.hGlobal); |
368 // We expect there to be at least one file in here. | 367 // We expect there to be at least one file in here. |
369 DCHECK(fgd->cItems >= 1); | 368 DCHECK(fgd->cItems >= 1); |
370 filename->assign(fgd->fgd[0].cFileName); | 369 filename->assign(fgd->fgd[0].cFileName); |
371 ReleaseStgMedium(&description); | 370 ReleaseStgMedium(&description); |
372 } | 371 } |
373 return true; | 372 return true; |
374 } | 373 } |
375 | 374 |
| 375 // HtmlToCFHtml and CFHtmlToHtml are based on similar methods in |
| 376 // WebCore/platform/win/ClipboardUtilitiesWin.cpp. |
| 377 /* |
| 378 * Copyright (C) 2007, 2008 Apple Inc. All rights reserved. |
| 379 * |
| 380 * Redistribution and use in source and binary forms, with or without |
| 381 * modification, are permitted provided that the following conditions |
| 382 * are met: |
| 383 * 1. Redistributions of source code must retain the above copyright |
| 384 * notice, this list of conditions and the following disclaimer. |
| 385 * 2. Redistributions in binary form must reproduce the above copyright |
| 386 * notice, this list of conditions and the following disclaimer in the |
| 387 * documentation and/or other materials provided with the distribution. |
| 388 * |
| 389 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY |
| 390 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 391 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 392 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR |
| 393 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 394 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 395 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 396 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
| 397 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 398 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 399 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 400 */ |
| 401 |
| 402 // Helper method for converting from text/html to MS CF_HTML. |
| 403 // Documentation for the CF_HTML format is available at |
| 404 // http://msdn.microsoft.com/en-us/library/aa767917(VS.85).aspx |
| 405 std::string ClipboardUtil::HtmlToCFHtml(const std::string& html, |
| 406 const std::string& base_url) { |
| 407 if (html.empty()) |
| 408 return std::string(); |
| 409 |
| 410 #define MAX_DIGITS 10 |
| 411 #define MAKE_NUMBER_FORMAT_1(digits) MAKE_NUMBER_FORMAT_2(digits) |
| 412 #define MAKE_NUMBER_FORMAT_2(digits) "%0" #digits "u" |
| 413 #define NUMBER_FORMAT MAKE_NUMBER_FORMAT_1(MAX_DIGITS) |
| 414 |
| 415 static const char* header = "Version:0.9\r\n" |
| 416 "StartHTML:" NUMBER_FORMAT "\r\n" |
| 417 "EndHTML:" NUMBER_FORMAT "\r\n" |
| 418 "StartFragment:" NUMBER_FORMAT "\r\n" |
| 419 "EndFragment:" NUMBER_FORMAT "\r\n"; |
| 420 static const char* source_url_prefix = "SourceURL:"; |
| 421 |
| 422 static const char* start_markup = |
| 423 "<html>\r\n<body>\r\n<!--StartFragment-->\r\n"; |
| 424 static const char* end_markup = |
| 425 "\r\n<!--EndFragment-->\r\n</body>\r\n</html>"; |
| 426 |
| 427 // Calculate offsets |
| 428 size_t start_html_offset = strlen(header) - strlen(NUMBER_FORMAT) * 4 + |
| 429 MAX_DIGITS * 4; |
| 430 if (!base_url.empty()) { |
| 431 start_html_offset += strlen(source_url_prefix) + |
| 432 base_url.length() + 1; |
| 433 } |
| 434 size_t start_fragment_offset = start_html_offset + strlen(start_markup); |
| 435 size_t end_fragment_offset = start_fragment_offset + html.length(); |
| 436 size_t end_html_offset = end_fragment_offset + strlen(end_markup); |
| 437 |
| 438 std::string result = StringPrintf(header, start_html_offset, |
| 439 end_html_offset, start_fragment_offset, end_fragment_offset); |
| 440 if (!base_url.empty()) { |
| 441 result.append(source_url_prefix); |
| 442 result.append(base_url); |
| 443 result.append("\r\n"); |
| 444 } |
| 445 result.append(start_markup); |
| 446 result.append(html); |
| 447 result.append(end_markup); |
| 448 |
| 449 #undef MAX_DIGITS |
| 450 #undef MAKE_NUMBER_FORMAT_1 |
| 451 #undef MAKE_NUMBER_FORMAT_2 |
| 452 #undef NUMBER_FORMAT |
| 453 |
| 454 return result; |
| 455 } |
| 456 |
| 457 // Helper method for converting from MS CF_HTML to text/html. |
| 458 void ClipboardUtil::CFHtmlToHtml(const std::string& cf_html, |
| 459 std::string* html, |
| 460 std::string* base_url) { |
| 461 // Obtain base_url if present. |
| 462 static std::string src_url_str("SourceURL:"); |
| 463 size_t line_start = cf_html.find(src_url_str); |
| 464 if (line_start != std::string::npos) { |
| 465 size_t src_end = cf_html.find("\n", line_start); |
| 466 size_t src_start = line_start + src_url_str.length(); |
| 467 if (src_end != std::string::npos && src_start != std::string::npos) { |
| 468 *base_url = cf_html.substr(src_start, src_end - src_start); |
| 469 } |
| 470 } |
| 471 |
| 472 // Find the markup between "<!--StartFragment -->" and "<!--EndFragment-->". |
| 473 std::string cf_html_lower = StringToLowerASCII(cf_html); |
| 474 size_t markup_start = cf_html_lower.find("<html", 0); |
| 475 size_t tag_start = cf_html.find("StartFragment", markup_start); |
| 476 size_t fragment_start = cf_html.find('>', tag_start) + 1; |
| 477 size_t tag_end = cf_html.find("EndFragment", fragment_start); |
| 478 size_t fragment_end = cf_html.rfind('<', tag_end); |
| 479 if (fragment_start != std::string::npos && |
| 480 fragment_end != std::string::npos) { |
| 481 *html = cf_html.substr(fragment_start, fragment_end - fragment_start); |
| 482 TrimWhitespace(*html, TRIM_ALL, html); |
| 483 } |
| 484 } |
OLD | NEW |