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.
5 #include "ui/base/dragdrop/os_exchange_data_provider_aura.h"
7 #include "base/logging.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "net/base/net_util.h"
10 #include "ui/base/clipboard/clipboard.h"
11 #include "ui/base/clipboard/scoped_clipboard_writer.h"
12 #include "ui/base/dragdrop/file_info.h"
16 OSExchangeDataProviderAura::OSExchangeDataProviderAura()
20 OSExchangeDataProviderAura::~OSExchangeDataProviderAura() {}
22 OSExchangeData::Provider
* OSExchangeDataProviderAura::Clone() const {
23 OSExchangeDataProviderAura
* ret
= new OSExchangeDataProviderAura();
24 ret
->formats_
= formats_
;
25 ret
->string_
= string_
;
28 ret
->filenames_
= filenames_
;
29 ret
->pickle_data_
= pickle_data_
;
30 // We skip copying the drag images.
32 ret
->base_url_
= base_url_
;
37 void OSExchangeDataProviderAura::MarkOriginatedFromRenderer() {
38 // TODO(dcheng): Currently unneeded because ChromeOS Aura correctly separates
39 // URL and filename metadata, and does not implement the DownloadURL protocol.
42 bool OSExchangeDataProviderAura::DidOriginateFromRenderer() const {
46 void OSExchangeDataProviderAura::SetString(const base::string16
& data
) {
51 formats_
|= OSExchangeData::STRING
;
54 void OSExchangeDataProviderAura::SetURL(const GURL
& url
,
55 const base::string16
& title
) {
58 formats_
|= OSExchangeData::URL
;
60 SetString(base::UTF8ToUTF16(url
.spec()));
63 void OSExchangeDataProviderAura::SetFilename(const base::FilePath
& path
) {
65 filenames_
.push_back(FileInfo(path
, base::FilePath()));
66 formats_
|= OSExchangeData::FILE_NAME
;
69 void OSExchangeDataProviderAura::SetFilenames(
70 const std::vector
<FileInfo
>& filenames
) {
71 filenames_
= filenames
;
72 formats_
|= OSExchangeData::FILE_NAME
;
75 void OSExchangeDataProviderAura::SetPickledData(
76 const OSExchangeData::CustomFormat
& format
,
78 pickle_data_
[format
] = data
;
79 formats_
|= OSExchangeData::PICKLED_DATA
;
82 bool OSExchangeDataProviderAura::GetString(base::string16
* data
) const {
83 if ((formats_
& OSExchangeData::STRING
) == 0)
89 bool OSExchangeDataProviderAura::GetURLAndTitle(
90 OSExchangeData::FilenameToURLPolicy policy
,
92 base::string16
* title
) const {
93 // TODO(dcheng): implement filename conversion.
94 if ((formats_
& OSExchangeData::URL
) == 0) {
96 return GetPlainTextURL(url
);
107 bool OSExchangeDataProviderAura::GetFilename(base::FilePath
* path
) const {
108 if ((formats_
& OSExchangeData::FILE_NAME
) == 0)
110 DCHECK(!filenames_
.empty());
111 *path
= filenames_
[0].path
;
115 bool OSExchangeDataProviderAura::GetFilenames(
116 std::vector
<FileInfo
>* filenames
) const {
117 if ((formats_
& OSExchangeData::FILE_NAME
) == 0)
119 *filenames
= filenames_
;
123 bool OSExchangeDataProviderAura::GetPickledData(
124 const OSExchangeData::CustomFormat
& format
,
125 Pickle
* data
) const {
126 PickleData::const_iterator i
= pickle_data_
.find(format
);
127 if (i
== pickle_data_
.end())
134 bool OSExchangeDataProviderAura::HasString() const {
135 return (formats_
& OSExchangeData::STRING
) != 0;
138 bool OSExchangeDataProviderAura::HasURL(
139 OSExchangeData::FilenameToURLPolicy policy
) const {
140 // TODO(dcheng): implement filename conversion.
141 if ((formats_
& OSExchangeData::URL
) != 0) {
144 // No URL, see if we have plain text that can be parsed as a URL.
145 return GetPlainTextURL(NULL
);
148 bool OSExchangeDataProviderAura::HasFile() const {
149 return (formats_
& OSExchangeData::FILE_NAME
) != 0;
152 bool OSExchangeDataProviderAura::HasCustomFormat(
153 const OSExchangeData::CustomFormat
& format
) const {
154 return pickle_data_
.find(format
) != pickle_data_
.end();
157 void OSExchangeDataProviderAura::SetHtml(const base::string16
& html
,
158 const GURL
& base_url
) {
159 formats_
|= OSExchangeData::HTML
;
161 base_url_
= base_url
;
164 bool OSExchangeDataProviderAura::GetHtml(base::string16
* html
,
165 GURL
* base_url
) const {
166 if ((formats_
& OSExchangeData::HTML
) == 0)
169 *base_url
= base_url_
;
173 bool OSExchangeDataProviderAura::HasHtml() const {
174 return ((formats_
& OSExchangeData::HTML
) != 0);
177 void OSExchangeDataProviderAura::SetDragImage(
178 const gfx::ImageSkia
& image
,
179 const gfx::Vector2d
& cursor_offset
) {
181 drag_image_offset_
= cursor_offset
;
184 const gfx::ImageSkia
& OSExchangeDataProviderAura::GetDragImage() const {
189 OSExchangeDataProviderAura::GetDragImageOffset() const {
190 return drag_image_offset_
;
193 bool OSExchangeDataProviderAura::GetPlainTextURL(GURL
* url
) const {
194 if ((formats_
& OSExchangeData::STRING
) == 0)
197 GURL
test_url(string_
);
198 if (!test_url
.is_valid())
206 ///////////////////////////////////////////////////////////////////////////////
207 // OSExchangeData, public:
210 OSExchangeData::Provider
* OSExchangeData::CreateProvider() {
211 return new OSExchangeDataProviderAura();