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

Side by Side Diff: app/os_exchange_data.cc

Issue 113321: Stubs out OSExchangeData for views on gtk. (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 11 years, 7 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
« no previous file with comments | « app/os_exchange_data.h ('k') | app/os_exchange_data_gtk.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include <shlobj.h>
6
7 #include "app/os_exchange_data.h"
8
9 #include "app/l10n_util.h"
10 #include "base/clipboard_util.h"
11 #include "base/file_util.h"
12 #include "base/logging.h"
13 #include "base/pickle.h"
14 #include "base/scoped_handle.h"
15 #include "base/stl_util-inl.h"
16 #include "base/string_util.h"
17 #include "googleurl/src/gurl.h"
18 #include "grit/generated_resources.h"
19 #include "net/base/net_util.h"
20
21 // Creates a new STGMEDIUM object to hold the specified text. The caller
22 // owns the resulting object. The "Bytes" version does not NULL terminate, the
23 // string version does.
24 static STGMEDIUM* GetStorageForBytes(const char* data, size_t bytes);
25 static STGMEDIUM* GetStorageForWString(const std::wstring& data);
26 static STGMEDIUM* GetStorageForString(const std::string& data);
27 // Creates the contents of an Internet Shortcut file for the given URL.
28 static void GetInternetShortcutFileContents(const GURL& url, std::string* data);
29 // Creates a valid file name given a suggested title and URL.
30 static void CreateValidFileNameFromTitle(const GURL& url,
31 const std::wstring& title,
32 std::wstring* validated);
33 // Creates a File Descriptor for the creation of a file to the given URL and
34 // returns a handle to it.
35 static STGMEDIUM* GetStorageForFileDescriptor(
36 const std::wstring& valid_file_name);
37
38 ///////////////////////////////////////////////////////////////////////////////
39 // FormatEtcEnumerator
40
41 //
42 // This object implements an enumeration interface. The existence of an
43 // implementation of this interface is exposed to clients through
44 // OSExchangeData's EnumFormatEtc method. Our implementation is nobody's
45 // business but our own, so it lives in this file.
46 //
47 // This Windows API is truly a gem. It wants to be an enumerator but assumes
48 // some sort of sequential data (why not just use an array?). See comments
49 // throughout.
50 //
51 class FormatEtcEnumerator : public IEnumFORMATETC {
52 public:
53 FormatEtcEnumerator(OSExchangeData::StoredData::const_iterator begin,
54 OSExchangeData::StoredData::const_iterator end);
55 ~FormatEtcEnumerator();
56
57 // IEnumFORMATETC implementation:
58 HRESULT __stdcall Next(
59 ULONG count, FORMATETC* elements_array, ULONG* elements_fetched);
60 HRESULT __stdcall Skip(ULONG skip_count);
61 HRESULT __stdcall Reset();
62 HRESULT __stdcall Clone(IEnumFORMATETC** clone);
63
64 // IUnknown implementation:
65 HRESULT __stdcall QueryInterface(const IID& iid, void** object);
66 ULONG __stdcall AddRef();
67 ULONG __stdcall Release();
68
69 private:
70 // This can only be called from |CloneFromOther|, since it initializes the
71 // contents_ from the other enumerator's contents.
72 FormatEtcEnumerator() : ref_count_(0) {
73 }
74
75 // Clone a new FormatEtc from another instance of this enumeration.
76 static FormatEtcEnumerator* CloneFromOther(const FormatEtcEnumerator* other);
77
78 private:
79 // We are _forced_ to use a vector as our internal data model as Windows'
80 // retarded IEnumFORMATETC API assumes a deterministic ordering of elements
81 // through methods like Next and Skip. This exposes the underlying data
82 // structure to the user. Bah.
83 std::vector<FORMATETC*> contents_;
84
85 // The cursor of the active enumeration - an index into |contents_|.
86 int cursor_;
87
88 LONG ref_count_;
89
90 DISALLOW_EVIL_CONSTRUCTORS(FormatEtcEnumerator);
91 };
92
93 // Safely makes a copy of all of the relevant bits of a FORMATETC object.
94 static void CloneFormatEtc(FORMATETC* source, FORMATETC* clone) {
95 *clone = *source;
96 if (source->ptd) {
97 source->ptd =
98 static_cast<DVTARGETDEVICE*>(CoTaskMemAlloc(sizeof(DVTARGETDEVICE)));
99 *(clone->ptd) = *(source->ptd);
100 }
101 }
102
103 FormatEtcEnumerator::FormatEtcEnumerator(
104 OSExchangeData::StoredData::const_iterator start,
105 OSExchangeData::StoredData::const_iterator end)
106 : ref_count_(0), cursor_(0) {
107 // Copy FORMATETC data from our source into ourselves.
108 while (start != end) {
109 FORMATETC* format_etc = new FORMATETC;
110 CloneFormatEtc(&(*start)->format_etc, format_etc);
111 contents_.push_back(format_etc);
112 ++start;
113 }
114 }
115
116 FormatEtcEnumerator::~FormatEtcEnumerator() {
117 STLDeleteContainerPointers(contents_.begin(), contents_.end());
118 }
119
120 STDMETHODIMP FormatEtcEnumerator::Next(
121 ULONG count, FORMATETC* elements_array, ULONG* elements_fetched) {
122 // MSDN says |elements_fetched| is allowed to be NULL if count is 1.
123 if (!elements_fetched)
124 DCHECK(count == 1);
125
126 // This method copies count elements into |elements_array|.
127 int index = 0;
128 while (cursor_ < static_cast<int>(contents_.size()) &&
129 static_cast<ULONG>(index) < count) {
130 CloneFormatEtc(contents_.at(cursor_), &elements_array[index]);
131 ++cursor_;
132 ++index;
133 }
134 // The out param is for how many we actually copied.
135 if (elements_fetched)
136 *elements_fetched = index;
137
138 // If the two don't agree, then we fail.
139 return index == count ? S_OK : S_FALSE;
140 }
141
142 STDMETHODIMP FormatEtcEnumerator::Skip(ULONG skip_count) {
143 cursor_ += skip_count;
144 // MSDN implies it's OK to leave the enumerator trashed.
145 // "Whatever you say, boss"
146 return cursor_ <= static_cast<int>(contents_.size()) ? S_OK : S_FALSE;
147 }
148
149 STDMETHODIMP FormatEtcEnumerator::Reset() {
150 cursor_ = 0;
151 return S_OK;
152 }
153
154 STDMETHODIMP FormatEtcEnumerator::Clone(IEnumFORMATETC** clone) {
155 // Clone the current enumerator in its exact state, including cursor.
156 FormatEtcEnumerator* e = CloneFromOther(this);
157 e->AddRef();
158 *clone = e;
159 return S_OK;
160 }
161
162 STDMETHODIMP FormatEtcEnumerator::QueryInterface(const IID& iid,
163 void** object) {
164 *object = NULL;
165 if (IsEqualIID(iid, IID_IUnknown) || IsEqualIID(iid, IID_IEnumFORMATETC)) {
166 *object = this;
167 } else {
168 return E_NOINTERFACE;
169 }
170 AddRef();
171 return S_OK;
172 }
173
174 ULONG FormatEtcEnumerator::AddRef() {
175 return InterlockedIncrement(&ref_count_);
176 }
177
178 ULONG FormatEtcEnumerator::Release() {
179 if (InterlockedDecrement(&ref_count_) == 0) {
180 ULONG copied_refcnt = ref_count_;
181 delete this;
182 return copied_refcnt;
183 }
184 return ref_count_;
185 }
186
187 // static
188 FormatEtcEnumerator* FormatEtcEnumerator::CloneFromOther(
189 const FormatEtcEnumerator* other) {
190 FormatEtcEnumerator* e = new FormatEtcEnumerator;
191 // Copy FORMATETC data from our source into ourselves.
192 std::vector<FORMATETC*>::const_iterator start = other->contents_.begin();
193 while (start != other->contents_.end()) {
194 FORMATETC* format_etc = new FORMATETC;
195 CloneFormatEtc(*start, format_etc);
196 e->contents_.push_back(format_etc);
197 ++start;
198 }
199 // Carry over
200 e->cursor_ = other->cursor_;
201 return e;
202 }
203
204 ///////////////////////////////////////////////////////////////////////////////
205 // OSExchangeData, public:
206
207 // static
208 bool OSExchangeData::HasPlainTextURL(IDataObject* source) {
209 std::wstring plain_text;
210 return (ClipboardUtil::GetPlainText(source, &plain_text) &&
211 !plain_text.empty() && GURL(plain_text).is_valid());
212 }
213
214 // static
215 bool OSExchangeData::GetPlainTextURL(IDataObject* source, GURL* url) {
216 std::wstring plain_text;
217 if (ClipboardUtil::GetPlainText(source, &plain_text) &&
218 !plain_text.empty()) {
219 GURL gurl(plain_text);
220 if (gurl.is_valid()) {
221 *url = gurl;
222 return true;
223 }
224 }
225 return false;
226 }
227
228 OSExchangeData::OSExchangeData()
229 : ref_count_(0) {
230 }
231
232 OSExchangeData::OSExchangeData(IDataObject* source)
233 : ref_count_(0) {
234 source_object_ = source;
235 }
236
237 OSExchangeData::~OSExchangeData() {
238 STLDeleteContainerPointers(contents_.begin(), contents_.end());
239 }
240
241 void OSExchangeData::SetString(const std::wstring& data) {
242 STGMEDIUM* storage = GetStorageForWString(data);
243 contents_.push_back(new StoredDataInfo(CF_UNICODETEXT, storage));
244
245 // Also add plain text.
246 storage = GetStorageForString(WideToUTF8(data));
247 contents_.push_back(new StoredDataInfo(CF_TEXT, storage));
248 }
249
250 void OSExchangeData::SetURL(const GURL& url, const std::wstring& title) {
251 // NOTE WELL:
252 // Every time you change the order of the first two CLIPFORMATS that get
253 // added here, you need to update the EnumerationViaCOM test case in
254 // the _unittest.cc file to reflect the new arrangement otherwise that test
255 // will fail! It assumes an insertion order.
256
257 // Add text/x-moz-url for drags from Firefox
258 std::wstring x_moz_url_str = UTF8ToWide(url.spec());
259 x_moz_url_str += '\n';
260 x_moz_url_str += title;
261 STGMEDIUM* storage = GetStorageForWString(x_moz_url_str);
262 contents_.push_back(new StoredDataInfo(
263 ClipboardUtil::GetMozUrlFormat()->cfFormat, storage));
264
265 // Add a .URL shortcut file for dragging to Explorer.
266 std::wstring valid_file_name;
267 CreateValidFileNameFromTitle(url, title, &valid_file_name);
268 std::string shortcut_url_file_contents;
269 GetInternetShortcutFileContents(url, &shortcut_url_file_contents);
270 SetFileContents(valid_file_name, shortcut_url_file_contents);
271
272 // Add a UniformResourceLocator link for apps like IE and Word.
273 storage = GetStorageForWString(UTF8ToWide(url.spec()));
274 contents_.push_back(new StoredDataInfo(
275 ClipboardUtil::GetUrlWFormat()->cfFormat, storage));
276 storage = GetStorageForString(url.spec());
277 contents_.push_back(new StoredDataInfo(
278 ClipboardUtil::GetUrlFormat()->cfFormat, storage));
279
280 // TODO(beng): (http://b/1085501) add CF_HTML...
281
282 // Also add text representations (these should be last since they're the
283 // least preferable).
284 storage = GetStorageForWString(UTF8ToWide(url.spec()));
285 contents_.push_back(new StoredDataInfo(CF_UNICODETEXT, storage));
286 storage = GetStorageForString(url.spec());
287 contents_.push_back(new StoredDataInfo(CF_TEXT, storage));
288 }
289
290 void OSExchangeData::SetFilename(const std::wstring& full_path) {
291 const size_t drop_size = sizeof(DROPFILES);
292 const size_t bytes = drop_size + (full_path.length() + 2) * sizeof(wchar_t);
293 HANDLE hdata = ::GlobalAlloc(GMEM_MOVEABLE, bytes);
294 if (!hdata)
295 return;
296
297 ScopedHGlobal<DROPFILES> locked_mem(hdata);
298 DROPFILES* drop_files = locked_mem.get();
299 drop_files->pFiles = sizeof(DROPFILES);
300 drop_files->fWide = TRUE;
301 wchar_t* data = reinterpret_cast<wchar_t*>((BYTE*)(drop_files) + drop_size);
302 const size_t copy_size = (full_path.length() + 1) * sizeof(wchar_t);
303 memcpy(data, full_path.c_str(), copy_size);
304 data[full_path.length() + 1] = L'\0'; // Double NULL
305
306 // Set up the STGMEDIUM
307 STGMEDIUM* storage = new STGMEDIUM;
308 storage->tymed = TYMED_HGLOBAL;
309 storage->hGlobal = drop_files;
310 storage->pUnkForRelease = NULL;
311
312 // Set up the StoredDataInfo
313 StoredDataInfo* info = new StoredDataInfo(CF_HDROP, storage);
314 contents_.push_back(info);
315 }
316
317 void OSExchangeData::SetPickledData(CLIPFORMAT format, const Pickle& data) {
318 STGMEDIUM* storage = GetStorageForString(
319 std::string(static_cast<const char *>(data.data()),
320 static_cast<size_t>(data.size())));
321 contents_.push_back(new StoredDataInfo(format, storage));
322 }
323
324 void OSExchangeData::SetFileContents(const std::wstring& filename,
325 const std::string& file_contents) {
326 // Add CFSTR_FILEDESCRIPTOR
327 STGMEDIUM* storage = GetStorageForFileDescriptor(filename);
328 contents_.push_back(new StoredDataInfo(
329 ClipboardUtil::GetFileDescriptorFormat()->cfFormat, storage));
330
331 // Add CFSTR_FILECONTENTS
332 storage = GetStorageForBytes(file_contents.data(), file_contents.length());
333 contents_.push_back(new StoredDataInfo(
334 ClipboardUtil::GetFileContentFormatZero()->cfFormat, storage));
335 }
336
337 void OSExchangeData::SetHtml(const std::wstring& html, const GURL& base_url) {
338 // Add both MS CF_HTML and text/html format. CF_HTML should be in utf-8.
339 std::string utf8_html = WideToUTF8(html);
340 std::string url = base_url.is_valid() ? base_url.spec() : std::string();
341
342 std::string cf_html = ClipboardUtil::HtmlToCFHtml(utf8_html, url);
343 STGMEDIUM* storage = GetStorageForBytes(cf_html.c_str(), cf_html.size());
344 contents_.push_back(new StoredDataInfo(
345 ClipboardUtil::GetHtmlFormat()->cfFormat, storage));
346
347 STGMEDIUM* storage_plain = GetStorageForBytes(utf8_html.c_str(),
348 utf8_html.size());
349 contents_.push_back(new StoredDataInfo(
350 ClipboardUtil::GetTextHtmlFormat()->cfFormat, storage_plain));
351 }
352
353 bool OSExchangeData::GetString(std::wstring* data) const {
354 return ClipboardUtil::GetPlainText(source_object_, data);
355 }
356
357 bool OSExchangeData::GetURLAndTitle(GURL* url, std::wstring* title) const {
358 std::wstring url_str;
359 bool success = ClipboardUtil::GetUrl(source_object_, &url_str, title);
360 if (success) {
361 GURL test_url(url_str);
362 if (test_url.is_valid()) {
363 *url = test_url;
364 return true;
365 }
366 } else if (GetPlainTextURL(source_object_, url)) {
367 title->clear();
368 return true;
369 }
370 return false;
371 }
372
373 bool OSExchangeData::GetFilename(std::wstring* full_path) const {
374 std::vector<std::wstring> filenames;
375 bool success = ClipboardUtil::GetFilenames(source_object_, &filenames);
376 if (success)
377 full_path->assign(filenames[0]);
378 return success;
379 }
380
381 bool OSExchangeData::GetPickledData(CLIPFORMAT format, Pickle* data) const {
382 DCHECK(data);
383 FORMATETC format_etc =
384 { format, NULL, DVASPECT_CONTENT, -1, TYMED_HGLOBAL };
385 bool success = false;
386 STGMEDIUM medium;
387 if (SUCCEEDED(source_object_->GetData(&format_etc, &medium))) {
388 if (medium.tymed & TYMED_HGLOBAL) {
389 ScopedHGlobal<char> c_data(medium.hGlobal);
390 DCHECK(c_data.Size() > 0);
391 // Need to subtract 1 as SetPickledData adds an extra byte to the end.
392 *data = Pickle(c_data.get(), static_cast<int>(c_data.Size() - 1));
393 success = true;
394 }
395 ReleaseStgMedium(&medium);
396 }
397 return success;
398 }
399
400 bool OSExchangeData::GetFileContents(std::wstring* filename,
401 std::string* file_contents) const {
402 return ClipboardUtil::GetFileContents(source_object_, filename,
403 file_contents);
404 }
405
406 bool OSExchangeData::GetHtml(std::wstring* html, GURL* base_url) const {
407 std::string url;
408 bool success = ClipboardUtil::GetHtml(source_object_, html, &url);
409 if (success)
410 *base_url = GURL(url);
411 return success;
412 }
413
414 bool OSExchangeData::HasString() const {
415 return ClipboardUtil::HasPlainText(source_object_);
416 }
417
418 bool OSExchangeData::HasURL() const {
419 return (ClipboardUtil::HasUrl(source_object_) ||
420 HasPlainTextURL(source_object_));
421 }
422
423 bool OSExchangeData::HasFile() const {
424 return ClipboardUtil::HasFilenames(source_object_);
425 }
426
427 bool OSExchangeData::HasFormat(CLIPFORMAT format) const {
428 FORMATETC format_etc =
429 { format, NULL, DVASPECT_CONTENT, -1, TYMED_HGLOBAL };
430 return (source_object_->QueryGetData(&format_etc) == S_OK);
431 }
432
433 ///////////////////////////////////////////////////////////////////////////////
434 // OSExchangeData, IDataObject implementation:
435
436 // The following function, DuplicateMedium, is derived from WCDataObject.cpp
437 // in the WebKit source code. This is the license information for the file:
438 /*
439 * Copyright (C) 2007 Apple Inc. All rights reserved.
440 *
441 * Redistribution and use in source and binary forms, with or without
442 * modification, are permitted provided that the following conditions
443 * are met:
444 * 1. Redistributions of source code must retain the above copyright
445 * notice, this list of conditions and the following disclaimer.
446 * 2. Redistributions in binary form must reproduce the above copyright
447 * notice, this list of conditions and the following disclaimer in the
448 * documentation and/or other materials provided with the distribution.
449 *
450 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
451 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
452 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
453 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
454 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
455 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
456 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
457 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
458 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
459 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
460 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
461 */
462 static void DuplicateMedium(CLIPFORMAT source_clipformat,
463 STGMEDIUM* source,
464 STGMEDIUM* destination) {
465 switch (source->tymed) {
466 case TYMED_HGLOBAL:
467 destination->hGlobal =
468 static_cast<HGLOBAL>(OleDuplicateData(
469 source->hGlobal, source_clipformat, 0));
470 break;
471 case TYMED_MFPICT:
472 destination->hMetaFilePict =
473 static_cast<HMETAFILEPICT>(OleDuplicateData(
474 source->hMetaFilePict, source_clipformat, 0));
475 break;
476 case TYMED_GDI:
477 destination->hBitmap =
478 static_cast<HBITMAP>(OleDuplicateData(
479 source->hBitmap, source_clipformat, 0));
480 break;
481 case TYMED_ENHMF:
482 destination->hEnhMetaFile =
483 static_cast<HENHMETAFILE>(OleDuplicateData(
484 source->hEnhMetaFile, source_clipformat, 0));
485 break;
486 case TYMED_FILE:
487 destination->lpszFileName =
488 static_cast<LPOLESTR>(OleDuplicateData(
489 source->lpszFileName, source_clipformat, 0));
490 break;
491 case TYMED_ISTREAM:
492 destination->pstm = source->pstm;
493 destination->pstm->AddRef();
494 break;
495 case TYMED_ISTORAGE:
496 destination->pstg = source->pstg;
497 destination->pstg->AddRef();
498 break;
499 }
500
501 destination->tymed = source->tymed;
502 destination->pUnkForRelease = source->pUnkForRelease;
503 if (destination->pUnkForRelease)
504 destination->pUnkForRelease->AddRef();
505 }
506
507 HRESULT OSExchangeData::GetData(FORMATETC* format_etc, STGMEDIUM* medium) {
508 StoredData::const_iterator iter = contents_.begin();
509 while (iter != contents_.end()) {
510 if ((*iter)->format_etc.cfFormat == format_etc->cfFormat) {
511 DuplicateMedium((*iter)->format_etc.cfFormat, (*iter)->medium, medium);
512 return S_OK;
513 }
514 ++iter;
515 }
516
517 return DV_E_FORMATETC;
518 }
519
520 HRESULT OSExchangeData::GetDataHere(FORMATETC* format_etc, STGMEDIUM* medium) {
521 return DATA_E_FORMATETC;
522 }
523
524 HRESULT OSExchangeData::QueryGetData(FORMATETC* format_etc) {
525 StoredData::const_iterator iter = contents_.begin();
526 while (iter != contents_.end()) {
527 if ((*iter)->format_etc.cfFormat == format_etc->cfFormat)
528 return S_OK;
529 ++iter;
530 }
531 return DV_E_FORMATETC;
532 }
533
534 HRESULT OSExchangeData::GetCanonicalFormatEtc(
535 FORMATETC* format_etc, FORMATETC* result) {
536 format_etc->ptd = NULL;
537 return E_NOTIMPL;
538 }
539
540 HRESULT OSExchangeData::SetData(
541 FORMATETC* format_etc, STGMEDIUM* medium, BOOL should_release) {
542 STGMEDIUM* local_medium = new STGMEDIUM;
543 if (should_release) {
544 *local_medium = *medium;
545 } else {
546 DuplicateMedium(format_etc->cfFormat, medium, local_medium);
547 }
548
549 StoredDataInfo* info =
550 new StoredDataInfo(format_etc->cfFormat, local_medium);
551 info->medium->tymed = format_etc->tymed;
552 info->owns_medium = !!should_release;
553 contents_.push_back(info);
554
555 return S_OK;
556 }
557
558 HRESULT OSExchangeData::EnumFormatEtc(
559 DWORD direction, IEnumFORMATETC** enumerator) {
560 if (direction == DATADIR_GET) {
561 FormatEtcEnumerator* e =
562 new FormatEtcEnumerator(contents_.begin(), contents_.end());
563 e->AddRef();
564 *enumerator = e;
565 return S_OK;
566 }
567 return E_NOTIMPL;
568 }
569
570 HRESULT OSExchangeData::DAdvise(
571 FORMATETC* format_etc, DWORD advf, IAdviseSink* sink, DWORD* connection) {
572 return OLE_E_ADVISENOTSUPPORTED;
573 }
574
575 HRESULT OSExchangeData::DUnadvise(DWORD connection) {
576 return OLE_E_ADVISENOTSUPPORTED;
577 }
578
579 HRESULT OSExchangeData::EnumDAdvise(IEnumSTATDATA** enumerator) {
580 return OLE_E_ADVISENOTSUPPORTED;
581 }
582
583 ///////////////////////////////////////////////////////////////////////////////
584 // OSExchangeData, IUnknown implementation:
585
586 HRESULT OSExchangeData::QueryInterface(const IID& iid, void** object) {
587 *object = NULL;
588 if (IsEqualIID(iid, IID_IUnknown) || IsEqualIID(iid, IID_IDataObject)) {
589 *object = this;
590 } else {
591 return E_NOINTERFACE;
592 }
593 AddRef();
594 return S_OK;
595 }
596
597 ULONG OSExchangeData::AddRef() {
598 return InterlockedIncrement(&ref_count_);
599 }
600
601 ULONG OSExchangeData::Release() {
602 if (InterlockedDecrement(&ref_count_) == 0) {
603 ULONG copied_refcnt = ref_count_;
604 delete this;
605 return copied_refcnt;
606 }
607 return ref_count_;
608 }
609
610 ///////////////////////////////////////////////////////////////////////////////
611 // OSExchangeData, private:
612
613 static STGMEDIUM* GetStorageForBytes(const char* data, size_t bytes) {
614 HANDLE handle = GlobalAlloc(GPTR, static_cast<int>(bytes));
615 ScopedHGlobal<char> scoped(handle);
616 size_t allocated = static_cast<size_t>(GlobalSize(handle));
617 memcpy(scoped.get(), data, allocated);
618
619 STGMEDIUM* storage = new STGMEDIUM;
620 storage->hGlobal = handle;
621 storage->tymed = TYMED_HGLOBAL;
622 storage->pUnkForRelease = NULL;
623 return storage;
624 }
625
626 template<class T>
627 static HGLOBAL CopyStringToGlobalHandle(const T& payload) {
628 int bytes = static_cast<int>(payload.size() + 1) * sizeof(T::value_type);
629 HANDLE handle = GlobalAlloc(GPTR, bytes);
630 void* data = GlobalLock(handle);
631 size_t allocated = static_cast<size_t>(GlobalSize(handle));
632 memcpy(data, payload.c_str(), allocated);
633 static_cast<T::value_type*>(data)[payload.size()] = '\0';
634 GlobalUnlock(handle);
635 return handle;
636 }
637
638 static STGMEDIUM* GetStorageForWString(const std::wstring& data) {
639 STGMEDIUM* storage = new STGMEDIUM;
640 storage->hGlobal = CopyStringToGlobalHandle<std::wstring>(data);
641 storage->tymed = TYMED_HGLOBAL;
642 storage->pUnkForRelease = NULL;
643 return storage;
644 }
645
646 static STGMEDIUM* GetStorageForString(const std::string& data) {
647 STGMEDIUM* storage = new STGMEDIUM;
648 storage->hGlobal = CopyStringToGlobalHandle<std::string>(data);
649 storage->tymed = TYMED_HGLOBAL;
650 storage->pUnkForRelease = NULL;
651 return storage;
652 }
653
654 static void GetInternetShortcutFileContents(const GURL& url,
655 std::string* data) {
656 DCHECK(data);
657 static const std::string kInternetShortcutFileStart =
658 "[InternetShortcut]\r\nURL=";
659 static const std::string kInternetShortcutFileEnd =
660 "\r\n";
661 *data = kInternetShortcutFileStart + url.spec() + kInternetShortcutFileEnd;
662 }
663
664 static void CreateValidFileNameFromTitle(const GURL& url,
665 const std::wstring& title,
666 std::wstring* validated) {
667 if (title.empty()) {
668 if (url.is_valid()) {
669 *validated = net::GetSuggestedFilename(
670 url, std::string(), std::string(), std::wstring());
671 } else {
672 // Nothing else can be done, just use a default.
673 *validated = l10n_util::GetString(IDS_UNTITLED_SHORTCUT_FILE_NAME);
674 }
675 } else {
676 *validated = title;
677 file_util::ReplaceIllegalCharacters(validated, '-');
678 }
679 static const wchar_t extension[] = L".url";
680 static const size_t max_length = MAX_PATH - arraysize(extension);
681 if (validated->size() > max_length)
682 validated->erase(max_length);
683 *validated += extension;
684 }
685
686 static STGMEDIUM* GetStorageForFileDescriptor(
687 const std::wstring& valid_file_name) {
688 DCHECK(!valid_file_name.empty() && valid_file_name.size() + 1 <= MAX_PATH);
689 HANDLE handle = GlobalAlloc(GPTR, sizeof(FILEGROUPDESCRIPTOR));
690 FILEGROUPDESCRIPTOR* descriptor =
691 reinterpret_cast<FILEGROUPDESCRIPTOR*>(GlobalLock(handle));
692
693 descriptor->cItems = 1;
694 wcscpy_s(descriptor->fgd[0].cFileName,
695 valid_file_name.size() + 1,
696 valid_file_name.c_str());
697 descriptor->fgd[0].dwFlags = FD_LINKUI;
698
699 GlobalUnlock(handle);
700
701 STGMEDIUM* storage = new STGMEDIUM;
702 storage->hGlobal = handle;
703 storage->tymed = TYMED_HGLOBAL;
704 storage->pUnkForRelease = NULL;
705 return storage;
706 }
OLDNEW
« no previous file with comments | « app/os_exchange_data.h ('k') | app/os_exchange_data_gtk.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698