OLD | NEW |
| (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 #ifndef CHROME_COMMON_OS_EXCHANGE_DATA_H__ | |
6 #define CHROME_COMMON_OS_EXCHANGE_DATA_H__ | |
7 | |
8 #include <atlbase.h> | |
9 #include <objidl.h> | |
10 #include <vector> | |
11 | |
12 #include "base/basictypes.h" | |
13 | |
14 class GURL; | |
15 class Pickle; | |
16 | |
17 /////////////////////////////////////////////////////////////////////////////// | |
18 // | |
19 // OSExchangeData | |
20 // An object that holds interchange data to be sent out to OS services like | |
21 // clipboard, drag and drop, etc. This object exposes an API that clients can | |
22 // use to specify raw data and its high level type. This object takes care of | |
23 // translating that into something the OS can understand. | |
24 // | |
25 /////////////////////////////////////////////////////////////////////////////// | |
26 class OSExchangeData : public IDataObject { | |
27 public: | |
28 // Returns true if source has plain text that is a valid url. | |
29 static bool HasPlainTextURL(IDataObject* source); | |
30 | |
31 // Returns true if source has plain text that is a valid URL and sets url to | |
32 // that url. | |
33 static bool GetPlainTextURL(IDataObject* source, GURL* url); | |
34 | |
35 OSExchangeData(); | |
36 OSExchangeData(IDataObject* source); | |
37 virtual ~OSExchangeData(); | |
38 | |
39 // These functions add data to the OSExchangeData object of various Chrome | |
40 // types. The OSExchangeData object takes care of translating the data into | |
41 // a format suitable for exchange with the OS. | |
42 // NOTE WELL: Typically, a data object like this will contain only one of the | |
43 // following types of data. In cases where more data is held, the | |
44 // order in which these functions are called is _important_! | |
45 // ---> The order types are added to an OSExchangeData object controls | |
46 // the order of enumeration in our IEnumFORMATETC implementation! | |
47 // This comes into play when selecting the best (most preferable) | |
48 // data type for insertion into a DropTarget. | |
49 void SetString(const std::wstring& data); | |
50 // A URL can have an optional title in some exchange formats. | |
51 void SetURL(const GURL& url, const std::wstring& title); | |
52 // A full path to a file | |
53 void SetFilename(const std::wstring& full_path); | |
54 // Adds pickled data of the specified format. | |
55 void SetPickledData(CLIPFORMAT format, const Pickle& data); | |
56 // Adds the bytes of a file (CFSTR_FILECONTENTS and CFSTR_FILEDESCRIPTOR). | |
57 void SetFileContents(const std::wstring& filename, | |
58 const std::string& file_contents); | |
59 // Adds a snippet of HTML. |html| is just raw html but this sets both | |
60 // text/html and CF_HTML. | |
61 void SetHtml(const std::wstring& html, const GURL& base_url); | |
62 | |
63 // These functions retrieve data of the specified type. If data exists, the | |
64 // functions return and the result is in the out parameter. If the data does | |
65 // not exist, the out parameter is not touched. The out parameter cannot be | |
66 // NULL. | |
67 bool GetString(std::wstring* data) const; | |
68 bool GetURLAndTitle(GURL* url, std::wstring* title) const; | |
69 // Return the path of a file, if available. | |
70 bool GetFilename(std::wstring* full_path) const; | |
71 bool GetPickledData(CLIPFORMAT format, Pickle* data) const; | |
72 bool GetFileContents(std::wstring* filename, | |
73 std::string* file_contents) const; | |
74 bool GetHtml(std::wstring* html, GURL* base_url) const; | |
75 | |
76 // Test whether or not data of certain types is present, without actually | |
77 // returning anything. | |
78 bool HasString() const; | |
79 bool HasURL() const; | |
80 bool HasURLTitle() const; | |
81 bool HasFile() const; | |
82 bool HasFormat(CLIPFORMAT format) const; | |
83 | |
84 // IDataObject implementation: | |
85 HRESULT __stdcall GetData(FORMATETC* format_etc, STGMEDIUM* medium); | |
86 HRESULT __stdcall GetDataHere(FORMATETC* format_etc, STGMEDIUM* medium); | |
87 HRESULT __stdcall QueryGetData(FORMATETC* format_etc); | |
88 HRESULT __stdcall GetCanonicalFormatEtc( | |
89 FORMATETC* format_etc, FORMATETC* result); | |
90 HRESULT __stdcall SetData( | |
91 FORMATETC* format_etc, STGMEDIUM* medium, BOOL should_release); | |
92 HRESULT __stdcall EnumFormatEtc( | |
93 DWORD direction, IEnumFORMATETC** enumerator); | |
94 HRESULT __stdcall DAdvise( | |
95 FORMATETC* format_etc, DWORD advf, IAdviseSink* sink, DWORD* connection); | |
96 HRESULT __stdcall DUnadvise(DWORD connection); | |
97 HRESULT __stdcall EnumDAdvise(IEnumSTATDATA** enumerator); | |
98 | |
99 // IUnknown implementation: | |
100 HRESULT __stdcall QueryInterface(const IID& iid, void** object); | |
101 ULONG __stdcall AddRef(); | |
102 ULONG __stdcall Release(); | |
103 | |
104 private: | |
105 // FormatEtcEnumerator only likes us for our StoredDataMap typedef. | |
106 friend class FormatEtcEnumerator; | |
107 | |
108 // Our internal representation of stored data & type info. | |
109 struct StoredDataInfo { | |
110 FORMATETC format_etc; | |
111 STGMEDIUM* medium; | |
112 bool owns_medium; | |
113 | |
114 StoredDataInfo(CLIPFORMAT cf, STGMEDIUM* a_medium) { | |
115 format_etc.cfFormat = cf; | |
116 format_etc.dwAspect = DVASPECT_CONTENT; | |
117 format_etc.lindex = -1; | |
118 format_etc.ptd = NULL; | |
119 format_etc.tymed = a_medium->tymed; | |
120 | |
121 owns_medium = true; | |
122 | |
123 medium = a_medium; | |
124 } | |
125 | |
126 ~StoredDataInfo() { | |
127 if (owns_medium) { | |
128 ReleaseStgMedium(medium); | |
129 delete medium; | |
130 } | |
131 } | |
132 }; | |
133 | |
134 typedef std::vector<StoredDataInfo*> StoredData; | |
135 StoredData contents_; | |
136 | |
137 CComPtr<IDataObject> source_object_; | |
138 | |
139 LONG ref_count_; | |
140 | |
141 DISALLOW_EVIL_CONSTRUCTORS(OSExchangeData); | |
142 }; | |
143 | |
144 #endif // #ifndef CHROME_COMMON_OS_EXCHANGE_DATA_H__ | |
OLD | NEW |