OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 "app/clipboard/clipboard_util_win.h" | 5 #include "app/clipboard/clipboard_util_win.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 |
11 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
(...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
186 if (!HasUrl(data_object)) | 186 if (!HasUrl(data_object)) |
187 return false; | 187 return false; |
188 | 188 |
189 // Try to extract a URL from |data_object| in a variety of formats. | 189 // Try to extract a URL from |data_object| in a variety of formats. |
190 STGMEDIUM store; | 190 STGMEDIUM store; |
191 if (GetUrlFromHDrop(data_object, url, title)) | 191 if (GetUrlFromHDrop(data_object, url, title)) |
192 return true; | 192 return true; |
193 | 193 |
194 if (SUCCEEDED(data_object->GetData(GetMozUrlFormat(), &store)) || | 194 if (SUCCEEDED(data_object->GetData(GetMozUrlFormat(), &store)) || |
195 SUCCEEDED(data_object->GetData(GetUrlWFormat(), &store))) { | 195 SUCCEEDED(data_object->GetData(GetUrlWFormat(), &store))) { |
196 // Mozilla URL format or unicode URL | 196 { |
197 ScopedHGlobal<wchar_t> data(store.hGlobal); | 197 // Mozilla URL format or unicode URL |
198 SplitUrlAndTitle(data.get(), url, title); | 198 ScopedHGlobal<wchar_t> data(store.hGlobal); |
| 199 SplitUrlAndTitle(data.get(), url, title); |
| 200 } |
199 ReleaseStgMedium(&store); | 201 ReleaseStgMedium(&store); |
200 return true; | 202 return true; |
201 } | 203 } |
202 | 204 |
203 if (SUCCEEDED(data_object->GetData(GetUrlFormat(), &store))) { | 205 if (SUCCEEDED(data_object->GetData(GetUrlFormat(), &store))) { |
204 // URL using ascii | 206 { |
205 ScopedHGlobal<char> data(store.hGlobal); | 207 // URL using ascii |
206 SplitUrlAndTitle(UTF8ToWide(data.get()), url, title); | 208 ScopedHGlobal<char> data(store.hGlobal); |
| 209 SplitUrlAndTitle(UTF8ToWide(data.get()), url, title); |
| 210 } |
207 ReleaseStgMedium(&store); | 211 ReleaseStgMedium(&store); |
208 return true; | 212 return true; |
209 } | 213 } |
210 | 214 |
211 if (SUCCEEDED(data_object->GetData(GetFilenameWFormat(), &store))) { | 215 if (SUCCEEDED(data_object->GetData(GetFilenameWFormat(), &store))) { |
212 // filename using unicode | |
213 ScopedHGlobal<wchar_t> data(store.hGlobal); | |
214 bool success = false; | 216 bool success = false; |
215 if (data.get() && data.get()[0] && | 217 { |
216 (PathFileExists(data.get()) || PathIsUNC(data.get()))) { | 218 // filename using unicode |
217 wchar_t file_url[INTERNET_MAX_URL_LENGTH]; | 219 ScopedHGlobal<wchar_t> data(store.hGlobal); |
218 DWORD file_url_len = arraysize(file_url); | 220 if (data.get() && data.get()[0] && |
219 if (SUCCEEDED(::UrlCreateFromPathW(data.get(), file_url, &file_url_len, | 221 (PathFileExists(data.get()) || PathIsUNC(data.get()))) { |
220 0))) { | 222 wchar_t file_url[INTERNET_MAX_URL_LENGTH]; |
221 url->assign(file_url); | 223 DWORD file_url_len = arraysize(file_url); |
222 title->assign(file_url); | 224 if (SUCCEEDED(::UrlCreateFromPathW(data.get(), file_url, &file_url_len, |
223 success = true; | 225 0))) { |
| 226 url->assign(file_url); |
| 227 title->assign(file_url); |
| 228 success = true; |
| 229 } |
224 } | 230 } |
225 } | 231 } |
226 ReleaseStgMedium(&store); | 232 ReleaseStgMedium(&store); |
227 if (success) | 233 if (success) |
228 return true; | 234 return true; |
229 } | 235 } |
230 | 236 |
231 if (SUCCEEDED(data_object->GetData(GetFilenameFormat(), &store))) { | 237 if (SUCCEEDED(data_object->GetData(GetFilenameFormat(), &store))) { |
232 // filename using ascii | |
233 ScopedHGlobal<char> data(store.hGlobal); | |
234 bool success = false; | 238 bool success = false; |
235 if (data.get() && data.get()[0] && (PathFileExistsA(data.get()) || | 239 { |
236 PathIsUNCA(data.get()))) { | 240 // filename using ascii |
237 char file_url[INTERNET_MAX_URL_LENGTH]; | 241 ScopedHGlobal<char> data(store.hGlobal); |
238 DWORD file_url_len = arraysize(file_url); | 242 if (data.get() && data.get()[0] && (PathFileExistsA(data.get()) || |
239 if (SUCCEEDED(::UrlCreateFromPathA(data.get(), file_url, &file_url_len, | 243 PathIsUNCA(data.get()))) { |
240 0))) { | 244 char file_url[INTERNET_MAX_URL_LENGTH]; |
241 url->assign(UTF8ToWide(file_url)); | 245 DWORD file_url_len = arraysize(file_url); |
242 title->assign(*url); | 246 if (SUCCEEDED(::UrlCreateFromPathA(data.get(), file_url, &file_url_len, |
243 success = true; | 247 0))) { |
| 248 url->assign(UTF8ToWide(file_url)); |
| 249 title->assign(*url); |
| 250 success = true; |
| 251 } |
244 } | 252 } |
245 } | 253 } |
246 ReleaseStgMedium(&store); | 254 ReleaseStgMedium(&store); |
247 if (success) | 255 if (success) |
248 return true; | 256 return true; |
249 } | 257 } |
250 | 258 |
251 return false; | 259 return false; |
252 } | 260 } |
253 | 261 |
(...skipping 28 matching lines...) Expand all Loading... |
282 } | 290 } |
283 | 291 |
284 bool ClipboardUtil::GetPlainText(IDataObject* data_object, | 292 bool ClipboardUtil::GetPlainText(IDataObject* data_object, |
285 std::wstring* plain_text) { | 293 std::wstring* plain_text) { |
286 DCHECK(data_object && plain_text); | 294 DCHECK(data_object && plain_text); |
287 if (!HasPlainText(data_object)) | 295 if (!HasPlainText(data_object)) |
288 return false; | 296 return false; |
289 | 297 |
290 STGMEDIUM store; | 298 STGMEDIUM store; |
291 if (SUCCEEDED(data_object->GetData(GetPlainTextWFormat(), &store))) { | 299 if (SUCCEEDED(data_object->GetData(GetPlainTextWFormat(), &store))) { |
292 // Unicode text | 300 { |
293 ScopedHGlobal<wchar_t> data(store.hGlobal); | 301 // Unicode text |
294 plain_text->assign(data.get()); | 302 ScopedHGlobal<wchar_t> data(store.hGlobal); |
| 303 plain_text->assign(data.get()); |
| 304 } |
295 ReleaseStgMedium(&store); | 305 ReleaseStgMedium(&store); |
296 return true; | 306 return true; |
297 } | 307 } |
298 | 308 |
299 if (SUCCEEDED(data_object->GetData(GetPlainTextFormat(), &store))) { | 309 if (SUCCEEDED(data_object->GetData(GetPlainTextFormat(), &store))) { |
300 // ascii text | 310 { |
301 ScopedHGlobal<char> data(store.hGlobal); | 311 // ascii text |
302 plain_text->assign(UTF8ToWide(data.get())); | 312 ScopedHGlobal<char> data(store.hGlobal); |
| 313 plain_text->assign(UTF8ToWide(data.get())); |
| 314 } |
303 ReleaseStgMedium(&store); | 315 ReleaseStgMedium(&store); |
304 return true; | 316 return true; |
305 } | 317 } |
306 | 318 |
307 // If a file is dropped on the window, it does not provide either of the | 319 // If a file is dropped on the window, it does not provide either of the |
308 // plain text formats, so here we try to forcibly get a url. | 320 // plain text formats, so here we try to forcibly get a url. |
309 std::wstring title; | 321 std::wstring title; |
310 return GetUrl(data_object, plain_text, &title); | 322 return GetUrl(data_object, plain_text, &title); |
311 } | 323 } |
312 | 324 |
313 bool ClipboardUtil::GetHtml(IDataObject* data_object, | 325 bool ClipboardUtil::GetHtml(IDataObject* data_object, |
314 std::wstring* html, std::string* base_url) { | 326 std::wstring* html, std::string* base_url) { |
315 DCHECK(data_object && html && base_url); | 327 DCHECK(data_object && html && base_url); |
316 | 328 |
317 STGMEDIUM store; | 329 STGMEDIUM store; |
318 if (SUCCEEDED(data_object->QueryGetData(GetHtmlFormat())) && | 330 if (SUCCEEDED(data_object->QueryGetData(GetHtmlFormat())) && |
319 SUCCEEDED(data_object->GetData(GetHtmlFormat(), &store))) { | 331 SUCCEEDED(data_object->GetData(GetHtmlFormat(), &store))) { |
320 // MS CF html | 332 { |
321 ScopedHGlobal<char> data(store.hGlobal); | 333 // MS CF html |
| 334 ScopedHGlobal<char> data(store.hGlobal); |
322 | 335 |
323 std::string html_utf8; | 336 std::string html_utf8; |
324 CFHtmlToHtml(std::string(data.get(), data.Size()), &html_utf8, base_url); | 337 CFHtmlToHtml(std::string(data.get(), data.Size()), &html_utf8, base_url); |
325 html->assign(UTF8ToWide(html_utf8)); | 338 html->assign(UTF8ToWide(html_utf8)); |
326 | 339 } |
327 ReleaseStgMedium(&store); | 340 ReleaseStgMedium(&store); |
328 return true; | 341 return true; |
329 } | 342 } |
330 | 343 |
331 if (FAILED(data_object->QueryGetData(GetTextHtmlFormat()))) | 344 if (FAILED(data_object->QueryGetData(GetTextHtmlFormat()))) |
332 return false; | 345 return false; |
333 | 346 |
334 if (FAILED(data_object->GetData(GetTextHtmlFormat(), &store))) | 347 if (FAILED(data_object->GetData(GetTextHtmlFormat(), &store))) |
335 return false; | 348 return false; |
336 | 349 |
337 // text/html | 350 { |
338 ScopedHGlobal<wchar_t> data(store.hGlobal); | 351 // text/html |
339 html->assign(data.get()); | 352 ScopedHGlobal<wchar_t> data(store.hGlobal); |
| 353 html->assign(data.get()); |
| 354 } |
340 ReleaseStgMedium(&store); | 355 ReleaseStgMedium(&store); |
341 return true; | 356 return true; |
342 } | 357 } |
343 | 358 |
344 bool ClipboardUtil::GetFileContents(IDataObject* data_object, | 359 bool ClipboardUtil::GetFileContents(IDataObject* data_object, |
345 std::wstring* filename, std::string* file_contents) { | 360 std::wstring* filename, std::string* file_contents) { |
346 DCHECK(data_object && filename && file_contents); | 361 DCHECK(data_object && filename && file_contents); |
347 if (!SUCCEEDED(data_object->QueryGetData(GetFileContentFormatZero())) && | 362 if (!SUCCEEDED(data_object->QueryGetData(GetFileContentFormatZero())) && |
348 !SUCCEEDED(data_object->QueryGetData(GetFileDescriptorFormat()))) | 363 !SUCCEEDED(data_object->QueryGetData(GetFileDescriptorFormat()))) |
349 return false; | 364 return false; |
350 | 365 |
351 STGMEDIUM content; | 366 STGMEDIUM content; |
352 // The call to GetData can be very slow depending on what is in | 367 // The call to GetData can be very slow depending on what is in |
353 // |data_object|. | 368 // |data_object|. |
354 if (SUCCEEDED(data_object->GetData(GetFileContentFormatZero(), &content))) { | 369 if (SUCCEEDED(data_object->GetData(GetFileContentFormatZero(), &content))) { |
355 if (TYMED_HGLOBAL == content.tymed) { | 370 if (TYMED_HGLOBAL == content.tymed) { |
356 ScopedHGlobal<char> data(content.hGlobal); | 371 ScopedHGlobal<char> data(content.hGlobal); |
357 file_contents->assign(data.get(), data.Size()); | 372 file_contents->assign(data.get(), data.Size()); |
358 } | 373 } |
359 ReleaseStgMedium(&content); | 374 ReleaseStgMedium(&content); |
360 } | 375 } |
361 | 376 |
362 STGMEDIUM description; | 377 STGMEDIUM description; |
363 if (SUCCEEDED(data_object->GetData(GetFileDescriptorFormat(), | 378 if (SUCCEEDED(data_object->GetData(GetFileDescriptorFormat(), |
364 &description))) { | 379 &description))) { |
365 ScopedHGlobal<FILEGROUPDESCRIPTOR> fgd(description.hGlobal); | 380 { |
366 // We expect there to be at least one file in here. | 381 ScopedHGlobal<FILEGROUPDESCRIPTOR> fgd(description.hGlobal); |
367 DCHECK_GE(fgd->cItems, 1u); | 382 // We expect there to be at least one file in here. |
368 filename->assign(fgd->fgd[0].cFileName); | 383 DCHECK_GE(fgd->cItems, 1u); |
| 384 filename->assign(fgd->fgd[0].cFileName); |
| 385 } |
369 ReleaseStgMedium(&description); | 386 ReleaseStgMedium(&description); |
370 } | 387 } |
371 return true; | 388 return true; |
372 } | 389 } |
373 | 390 |
374 // HtmlToCFHtml and CFHtmlToHtml are based on similar methods in | 391 // HtmlToCFHtml and CFHtmlToHtml are based on similar methods in |
375 // WebCore/platform/win/ClipboardUtilitiesWin.cpp. | 392 // WebCore/platform/win/ClipboardUtilitiesWin.cpp. |
376 /* | 393 /* |
377 * Copyright (C) 2007, 2008 Apple Inc. All rights reserved. | 394 * Copyright (C) 2007, 2008 Apple Inc. All rights reserved. |
378 * | 395 * |
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
479 size_t fragment_start = cf_html.find('>', tag_start) + 1; | 496 size_t fragment_start = cf_html.find('>', tag_start) + 1; |
480 size_t tag_end = cf_html.rfind("EndFragment", std::string::npos); | 497 size_t tag_end = cf_html.rfind("EndFragment", std::string::npos); |
481 size_t fragment_end = cf_html.rfind('<', tag_end); | 498 size_t fragment_end = cf_html.rfind('<', tag_end); |
482 if (fragment_start != std::string::npos && | 499 if (fragment_start != std::string::npos && |
483 fragment_end != std::string::npos) { | 500 fragment_end != std::string::npos) { |
484 *html = cf_html.substr(fragment_start, fragment_end - fragment_start); | 501 *html = cf_html.substr(fragment_start, fragment_end - fragment_start); |
485 TrimWhitespace(*html, TRIM_ALL, html); | 502 TrimWhitespace(*html, TRIM_ALL, html); |
486 } | 503 } |
487 } | 504 } |
488 } | 505 } |
OLD | NEW |