Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / ui / base / dragdrop / os_exchange_data_provider_win.h
1 // Copyright (c) 2012 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 UI_BASE_DRAGDROP_OS_EXCHANGE_DATA_PROVIDER_WIN_H_
6 #define UI_BASE_DRAGDROP_OS_EXCHANGE_DATA_PROVIDER_WIN_H_
7
8 #include <objidl.h>
9 #include <shlobj.h>
10 #include <string>
11 #include <vector>
12
13 // Win8 SDK compatibility, see http://goo.gl/fufvl for more information.
14 // "Note: This interface has been renamed IDataObjectAsyncCapability."
15 // If we're building on pre-8 we define it to its old name. It's documented as
16 // being binary compatible.
17 #ifndef __IDataObjectAsyncCapability_FWD_DEFINED__
18 #define IDataObjectAsyncCapability IAsyncOperation
19 #endif
20
21 #include "base/memory/scoped_vector.h"
22 #include "base/win/scoped_comptr.h"
23 #include "ui/base/dragdrop/os_exchange_data.h"
24 #include "ui/base/ui_base_export.h"
25 #include "ui/gfx/image/image_skia.h"
26 #include "ui/gfx/vector2d.h"
27
28 namespace ui {
29
30 class DataObjectImpl : public DownloadFileObserver,
31                        public IDataObject,
32                        public IDataObjectAsyncCapability {
33  public:
34   class Observer {
35    public:
36     virtual void OnWaitForData() = 0;
37     virtual void OnDataObjectDisposed() = 0;
38    protected:
39     virtual ~Observer() { }
40   };
41
42   DataObjectImpl();
43
44   // Accessors.
45   void set_observer(Observer* observer) { observer_ = observer; }
46   void set_in_drag_loop(bool in_drag_loop) { in_drag_loop_ = in_drag_loop; }
47
48   // Number of known formats.
49   size_t size() const { return contents_.size(); }
50
51   // DownloadFileObserver implementation:
52   virtual void OnDownloadCompleted(const base::FilePath& file_path);
53   virtual void OnDownloadAborted();
54
55   // IDataObject implementation:
56   HRESULT __stdcall GetData(FORMATETC* format_etc, STGMEDIUM* medium);
57   HRESULT __stdcall GetDataHere(FORMATETC* format_etc, STGMEDIUM* medium);
58   HRESULT __stdcall QueryGetData(FORMATETC* format_etc);
59   HRESULT __stdcall GetCanonicalFormatEtc(
60       FORMATETC* format_etc, FORMATETC* result);
61   HRESULT __stdcall SetData(
62       FORMATETC* format_etc, STGMEDIUM* medium, BOOL should_release);
63   HRESULT __stdcall EnumFormatEtc(
64       DWORD direction, IEnumFORMATETC** enumerator);
65   HRESULT __stdcall DAdvise(FORMATETC* format_etc, DWORD advf,
66                             IAdviseSink* sink, DWORD* connection);
67   HRESULT __stdcall DUnadvise(DWORD connection);
68   HRESULT __stdcall EnumDAdvise(IEnumSTATDATA** enumerator);
69
70   // IDataObjectAsyncCapability implementation:
71   HRESULT __stdcall EndOperation(
72       HRESULT result, IBindCtx* reserved, DWORD effects);
73   HRESULT __stdcall GetAsyncMode(BOOL* is_op_async);
74   HRESULT __stdcall InOperation(BOOL* in_async_op);
75   HRESULT __stdcall SetAsyncMode(BOOL do_op_async);
76   HRESULT __stdcall StartOperation(IBindCtx* reserved);
77
78   // IUnknown implementation:
79   HRESULT __stdcall QueryInterface(const IID& iid, void** object);
80   ULONG __stdcall AddRef();
81   ULONG __stdcall Release();
82
83  private:
84   // FormatEtcEnumerator only likes us for our StoredDataMap typedef.
85   friend class FormatEtcEnumerator;
86   friend class OSExchangeDataProviderWin;
87
88   virtual ~DataObjectImpl();
89
90   void StopDownloads();
91
92   // Removes from contents_ the first data that matches |format|.
93   void RemoveData(const FORMATETC& format);
94
95   // Our internal representation of stored data & type info.
96   struct StoredDataInfo {
97     FORMATETC format_etc;
98     STGMEDIUM* medium;
99     bool owns_medium;
100     scoped_refptr<DownloadFileProvider> downloader;
101
102     StoredDataInfo(const FORMATETC& format_etc, STGMEDIUM* medium)
103         : format_etc(format_etc), medium(medium), owns_medium(true) {}
104
105     ~StoredDataInfo() {
106       if (owns_medium) {
107         ReleaseStgMedium(medium);
108         delete medium;
109       }
110       if (downloader.get())
111         downloader->Stop();
112     }
113   };
114
115   typedef ScopedVector<StoredDataInfo> StoredData;
116   StoredData contents_;
117
118   base::win::ScopedComPtr<IDataObject> source_object_;
119
120   bool is_aborting_;
121   bool in_drag_loop_;
122   bool in_async_mode_;
123   bool async_operation_started_;
124   Observer* observer_;
125 };
126
127 class UI_BASE_EXPORT OSExchangeDataProviderWin
128     : public OSExchangeData::Provider {
129  public:
130   // Returns true if source has plain text that is a valid url.
131   static bool HasPlainTextURL(IDataObject* source);
132
133   // Returns true if source has plain text that is a valid URL and sets url to
134   // that url.
135   static bool GetPlainTextURL(IDataObject* source, GURL* url);
136
137   static DataObjectImpl* GetDataObjectImpl(const OSExchangeData& data);
138   static IDataObject* GetIDataObject(const OSExchangeData& data);
139   static IDataObjectAsyncCapability* GetIAsyncOperation(
140       const OSExchangeData& data);
141
142   explicit OSExchangeDataProviderWin(IDataObject* source);
143   OSExchangeDataProviderWin();
144
145   virtual ~OSExchangeDataProviderWin();
146
147   IDataObject* data_object() const { return data_.get(); }
148   IDataObjectAsyncCapability* async_operation() const { return data_.get(); }
149
150   // OSExchangeData::Provider methods.
151   virtual Provider* Clone() const;
152   virtual void MarkOriginatedFromRenderer();
153   virtual bool DidOriginateFromRenderer() const;
154   virtual void SetString(const base::string16& data);
155   virtual void SetURL(const GURL& url, const base::string16& title);
156   virtual void SetFilename(const base::FilePath& path);
157   virtual void SetFilenames(const std::vector<FileInfo>& filenames);
158   virtual void SetPickledData(const OSExchangeData::CustomFormat& format,
159                               const Pickle& data);
160   virtual void SetFileContents(const base::FilePath& filename,
161                                const std::string& file_contents);
162   virtual void SetHtml(const base::string16& html, const GURL& base_url);
163
164   virtual bool GetString(base::string16* data) const;
165   virtual bool GetURLAndTitle(OSExchangeData::FilenameToURLPolicy policy,
166                               GURL* url,
167                               base::string16* title) const;
168   virtual bool GetFilename(base::FilePath* path) const;
169   virtual bool GetFilenames(std::vector<FileInfo>* filenames) const;
170   virtual bool GetPickledData(const OSExchangeData::CustomFormat& format,
171                               Pickle* data) const;
172   virtual bool GetFileContents(base::FilePath* filename,
173                                std::string* file_contents) const;
174   virtual bool GetHtml(base::string16* html, GURL* base_url) const;
175   virtual bool HasString() const;
176   virtual bool HasURL(OSExchangeData::FilenameToURLPolicy policy) const;
177   virtual bool HasFile() const;
178   virtual bool HasFileContents() const;
179   virtual bool HasHtml() const;
180   virtual bool HasCustomFormat(
181       const OSExchangeData::CustomFormat& format) const;
182   virtual void SetDownloadFileInfo(
183       const OSExchangeData::DownloadFileInfo& download_info);
184   virtual void SetDragImage(const gfx::ImageSkia& image,
185                             const gfx::Vector2d& cursor_offset) OVERRIDE;
186   virtual const gfx::ImageSkia& GetDragImage() const OVERRIDE;
187   virtual const gfx::Vector2d& GetDragImageOffset() const OVERRIDE;
188
189  private:
190   scoped_refptr<DataObjectImpl> data_;
191   base::win::ScopedComPtr<IDataObject> source_object_;
192
193   // Drag image and offset data. Only used for Ash.
194   gfx::ImageSkia drag_image_;
195   gfx::Vector2d drag_image_offset_;
196
197   DISALLOW_COPY_AND_ASSIGN(OSExchangeDataProviderWin);
198 };
199
200 }  // namespace ui
201
202 #endif  // UI_BASE_DRAGDROP_OS_EXCHANGE_DATA_PROVIDER_WIN_H_