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

Side by Side Diff: ui/views/mus/os_exchange_data_provider_mus.cc

Issue 2240323003: mus: Implement OSExchangeDataProviderMus. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: No, sky wanted a dcheck. I misunderstood. Created 4 years, 4 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
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 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 "ui/views/mus/os_exchange_data_provider_mus.h" 5 #include "ui/views/mus/os_exchange_data_provider_mus.h"
6 6
7 #include "base/stl_util.h"
8 #include "base/strings/string_split.h"
9 #include "base/strings/utf_string_conversions.h"
10 #include "mojo/common/common_type_converters.h"
11 #include "net/base/filename_util.h"
12 #include "services/ui/public/interfaces/clipboard.mojom.h"
13 #include "ui/base/dragdrop/file_info.h"
14 #include "url/gurl.h"
15
7 namespace views { 16 namespace views {
8 17
18 namespace {
19
20 std::vector<uint8_t> FromString(const std::string& str) {
21 return std::vector<uint8_t>(str.begin(), str.end());
22 }
23
24 std::string ToString(const std::vector<uint8_t>& v) {
25 return std::string(v.begin(), v.end());
26 }
27
28 base::string16 ToString16(const std::vector<uint8_t>& v) {
29 DCHECK_EQ(0u, v.size() % 2);
30 return base::string16(
31 reinterpret_cast<const base::char16*>(v.data()),
32 v.size() / 2);
33 }
34
35 std::vector<base::StringPiece> ParseURIList(const std::vector<uint8_t>& data) {
36 return base::SplitStringPiece(
37 base::StringPiece(
38 reinterpret_cast<const char*>(&data.front()), data.size()),
39 "\n",
40 base::KEEP_WHITESPACE, base::SPLIT_WANT_NONEMPTY);
41 }
42
43 void AddString16ToVector(const base::string16& str,
44 std::vector<uint8_t>* bytes) {
45 const unsigned char* front =
46 reinterpret_cast<const uint8_t*>(str.data());
47 bytes->insert(bytes->end(), front, front + (str.size() * 2));
48 }
49
50 } // namespace
51
9 OSExchangeDataProviderMus::OSExchangeDataProviderMus() {} 52 OSExchangeDataProviderMus::OSExchangeDataProviderMus() {}
10 53
11 OSExchangeDataProviderMus::~OSExchangeDataProviderMus() {} 54 OSExchangeDataProviderMus::~OSExchangeDataProviderMus() {}
12 55
56 OSExchangeDataProviderMus::Data OSExchangeDataProviderMus::GetData() const {
57 return mime_data_;
58 }
59
13 std::unique_ptr<ui::OSExchangeData::Provider> 60 std::unique_ptr<ui::OSExchangeData::Provider>
14 OSExchangeDataProviderMus::Clone() const { 61 OSExchangeDataProviderMus::Clone() const {
15 return std::unique_ptr<ui::OSExchangeData::Provider>(); 62 std::unique_ptr<OSExchangeDataProviderMus> r =
63 base::MakeUnique<OSExchangeDataProviderMus>();
64 r->drag_image_ = drag_image_;
65 r->drag_image_offset_ = drag_image_offset_;
66 r->mime_data_ = mime_data_;
67 return base::WrapUnique<ui::OSExchangeData::Provider>(r.release());
16 } 68 }
17 69
18 void OSExchangeDataProviderMus::MarkOriginatedFromRenderer() { 70 void OSExchangeDataProviderMus::MarkOriginatedFromRenderer() {
71 // Currently unimplemented because ChromeOS doesn't need this.
72 //
73 // TODO(erg): Implement this when we start porting mus to other platforms.
19 } 74 }
20 75
21 bool OSExchangeDataProviderMus::DidOriginateFromRenderer() const { 76 bool OSExchangeDataProviderMus::DidOriginateFromRenderer() const {
22 return false; 77 return false;
23 } 78 }
24 79
25 void OSExchangeDataProviderMus::SetString(const base::string16& data) { 80 void OSExchangeDataProviderMus::SetString(const base::string16& data) {
81 if (HasString())
82 return;
83
84 mime_data_[ui::mojom::kMimeTypeText] = FromString(base::UTF16ToUTF8(data));
26 } 85 }
27 86
28 void OSExchangeDataProviderMus::SetURL(const GURL& url, 87 void OSExchangeDataProviderMus::SetURL(const GURL& url,
29 const base::string16& title) { 88 const base::string16& title) {
89 base::string16 spec = base::UTF8ToUTF16(url.spec());
90 std::vector<unsigned char> data;
91 AddString16ToVector(spec, &data);
92 AddString16ToVector(base::ASCIIToUTF16("\n"), &data);
93 AddString16ToVector(title, &data);
94 mime_data_[ui::mojom::kMimeTypeMozillaURL] = std::move(data);
95
96 if (!base::ContainsKey(mime_data_, ui::mojom::kMimeTypeText))
97 mime_data_[ui::mojom::kMimeTypeText] = FromString(url.spec());
30 } 98 }
31 99
32 void OSExchangeDataProviderMus::SetFilename(const base::FilePath& path) { 100 void OSExchangeDataProviderMus::SetFilename(const base::FilePath& path) {
101 std::vector<ui::FileInfo> data;
102 data.push_back(ui::FileInfo(path, base::FilePath()));
103 SetFilenames(data);
33 } 104 }
34 105
35 void OSExchangeDataProviderMus::SetFilenames( 106 void OSExchangeDataProviderMus::SetFilenames(
36 const std::vector<ui::FileInfo>& file_names) { 107 const std::vector<ui::FileInfo>& file_names) {
108 std::vector<std::string> paths;
109 for (std::vector<ui::FileInfo>::const_iterator it = file_names.begin();
110 it != file_names.end();
111 ++it) {
112 std::string url_spec = net::FilePathToFileURL(it->path).spec();
113 if (!url_spec.empty())
114 paths.push_back(url_spec);
115 }
116
117 std::string joined_data = base::JoinString(paths, "\n");
118 mime_data_[ui::mojom::kMimeTypeURIList] = FromString(joined_data);
37 } 119 }
38 120
39 void OSExchangeDataProviderMus::SetPickledData( 121 void OSExchangeDataProviderMus::SetPickledData(
40 const ui::Clipboard::FormatType& format, 122 const ui::Clipboard::FormatType& format,
41 const base::Pickle& data) { 123 const base::Pickle& pickle) {
124 const unsigned char* bytes =
125 reinterpret_cast<const unsigned char*>(pickle.data());
126
127 mime_data_[format.Serialize()] = mojo::Array<uint8_t>(
128 std::vector<uint8_t>(bytes, bytes + pickle.size()));
42 } 129 }
43 130
44 bool OSExchangeDataProviderMus::GetString(base::string16* data) const { 131 bool OSExchangeDataProviderMus::GetString(base::string16* data) const {
45 return false; 132 auto it = mime_data_.find(ui::mojom::kMimeTypeText);
133 if (it != mime_data_.end())
134 *data = base::UTF8ToUTF16(ToString(it->second));
135 return it != mime_data_.end();
46 } 136 }
47 137
48 bool OSExchangeDataProviderMus::GetURLAndTitle( 138 bool OSExchangeDataProviderMus::GetURLAndTitle(
49 ui::OSExchangeData::FilenameToURLPolicy policy, 139 ui::OSExchangeData::FilenameToURLPolicy policy,
50 GURL* url, 140 GURL* url,
51 base::string16* title) const { 141 base::string16* title) const {
52 return false; 142 auto it = mime_data_.find(ui::mojom::kMimeTypeMozillaURL);
143 if (it == mime_data_.end()) {
144 title->clear();
145 return GetPlainTextURL(url) ||
146 (policy == ui::OSExchangeData::CONVERT_FILENAMES && GetFileURL(url));
147 }
148
149 base::string16 data = ToString16(it->second);
150 base::string16::size_type newline = data.find('\n');
151 if (newline == std::string::npos)
152 return false;
153
154 GURL unparsed_url(data.substr(0, newline));
155 if (!unparsed_url.is_valid())
156 return false;
157
158 *url = unparsed_url;
159 *title = data.substr(newline + 1);
160 return true;
53 } 161 }
54 162
55 bool OSExchangeDataProviderMus::GetFilename(base::FilePath* path) const { 163 bool OSExchangeDataProviderMus::GetFilename(base::FilePath* path) const {
164 std::vector<ui::FileInfo> filenames;
165 if (GetFilenames(&filenames)) {
166 *path = filenames.front().path;
167 return true;
168 }
169
56 return false; 170 return false;
57 } 171 }
58 172
59 bool OSExchangeDataProviderMus::GetFilenames( 173 bool OSExchangeDataProviderMus::GetFilenames(
60 std::vector<ui::FileInfo>* file_names) const { 174 std::vector<ui::FileInfo>* file_names) const {
61 return false; 175 auto it = mime_data_.find(ui::mojom::kMimeTypeURIList);
176 if (it == mime_data_.end())
177 return false;
178
179 file_names->clear();
180 for (const base::StringPiece& piece : ParseURIList(it->second)) {
181 GURL url(piece);
182 base::FilePath file_path;
183 if (url.SchemeIsFile() && net::FileURLToFilePath(url, &file_path))
184 file_names->push_back(ui::FileInfo(file_path, base::FilePath()));
185 }
186
187 return true;
62 } 188 }
63 189
64 bool OSExchangeDataProviderMus::GetPickledData( 190 bool OSExchangeDataProviderMus::GetPickledData(
65 const ui::Clipboard::FormatType& format, 191 const ui::Clipboard::FormatType& format,
66 base::Pickle* data) const { 192 base::Pickle* data) const {
67 return false; 193 auto it = mime_data_.find(format.Serialize());
194 if (it == mime_data_.end())
195 return false;
196
197 // Note that the pickle object on the right hand side of the assignment
198 // only refers to the bytes in |data|. The assignment copies the data.
199 *data = base::Pickle(reinterpret_cast<const char*>(it->second.data()),
200 static_cast<int>(it->second.size()));
201 return true;
68 } 202 }
69 203
70 bool OSExchangeDataProviderMus::HasString() const { 204 bool OSExchangeDataProviderMus::HasString() const {
71 return false; 205 return base::ContainsKey(mime_data_, ui::mojom::kMimeTypeText);
72 } 206 }
73 207
74 bool OSExchangeDataProviderMus::HasURL( 208 bool OSExchangeDataProviderMus::HasURL(
75 ui::OSExchangeData::FilenameToURLPolicy policy) const { 209 ui::OSExchangeData::FilenameToURLPolicy policy) const {
210 if (base::ContainsKey(mime_data_, ui::mojom::kMimeTypeMozillaURL))
211 return true;
212
213 auto it = mime_data_.find(ui::mojom::kMimeTypeURIList);
214 if (it == mime_data_.end())
215 return false;
216
217 for (const base::StringPiece& piece : ParseURIList(it->second)) {
218 if (!GURL(piece).SchemeIsFile() ||
219 policy == ui::OSExchangeData::CONVERT_FILENAMES) {
220 return true;
221 }
222 }
223
76 return false; 224 return false;
77 } 225 }
78 226
79 bool OSExchangeDataProviderMus::HasFile() const { 227 bool OSExchangeDataProviderMus::HasFile() const {
228 auto it = mime_data_.find(ui::mojom::kMimeTypeURIList);
229 if (it == mime_data_.end())
230 return false;
231
232 for (const base::StringPiece& piece : ParseURIList(it->second)) {
233 GURL url(piece);
234 base::FilePath file_path;
235 if (url.SchemeIsFile() && net::FileURLToFilePath(url, &file_path))
236 return true;
237 }
238
80 return false; 239 return false;
81 } 240 }
82 241
83 bool OSExchangeDataProviderMus::HasCustomFormat( 242 bool OSExchangeDataProviderMus::HasCustomFormat(
84 const ui::Clipboard::FormatType& format) const { 243 const ui::Clipboard::FormatType& format) const {
85 return false; 244 return base::ContainsKey(mime_data_, format.Serialize());
86 } 245 }
87 246
88 // These methods were added in an ad-hoc way to different operating 247 // These methods were added in an ad-hoc way to different operating
89 // systems. We need to support them until they get cleaned up. 248 // systems. We need to support them until they get cleaned up.
90 #if (!defined(OS_CHROMEOS) && defined(USE_X11)) || defined(OS_WIN) 249 #if (!defined(OS_CHROMEOS) && defined(USE_X11)) || defined(OS_WIN)
91 void OSExchangeDataProviderMus::SetFileContents( 250 void OSExchangeDataProviderMus::SetFileContents(
92 const base::FilePath& filename, 251 const base::FilePath& filename,
93 const std::string& file_contents) { 252 const std::string& file_contents) {
94 253
95 } 254 }
(...skipping 11 matching lines...) Expand all
107 } 266 }
108 267
109 void OSExchangeDataProviderMus::SetDownloadFileInfo( 268 void OSExchangeDataProviderMus::SetDownloadFileInfo(
110 const ui::OSExchangeData::DownloadFileInfo& download) { 269 const ui::OSExchangeData::DownloadFileInfo& download) {
111 } 270 }
112 #endif 271 #endif
113 272
114 #if defined(USE_AURA) 273 #if defined(USE_AURA)
115 void OSExchangeDataProviderMus::SetHtml(const base::string16& html, 274 void OSExchangeDataProviderMus::SetHtml(const base::string16& html,
116 const GURL& base_url) { 275 const GURL& base_url) {
117 276 std::vector<unsigned char> bytes;
277 // Manually jam a UTF16 BOM into bytes because otherwise, other programs will
278 // assume UTF-8.
279 bytes.push_back(0xFF);
280 bytes.push_back(0xFE);
281 AddString16ToVector(html, &bytes);
282 mime_data_[ui::mojom::kMimeTypeHTML] = bytes;
118 } 283 }
119 284
120 bool OSExchangeDataProviderMus::GetHtml(base::string16* html, 285 bool OSExchangeDataProviderMus::GetHtml(base::string16* html,
121 GURL* base_url) const { 286 GURL* base_url) const {
122 return false; 287 auto it = mime_data_.find(ui::mojom::kMimeTypeHTML);
288 if (it == mime_data_.end())
289 return false;
290
291 const unsigned char* data = it->second.data();
292 size_t size = it->second.size();
293 base::string16 markup;
294
295 // If the data starts with 0xFEFF, i.e., Byte Order Mark, assume it is
296 // UTF-16, otherwise assume UTF-8.
297 if (size >= 2 &&
298 reinterpret_cast<const uint16_t*>(data)[0] == 0xFEFF) {
299 markup.assign(reinterpret_cast<const base::char16*>(data) + 1,
300 (size / 2) - 1);
301 } else {
302 base::UTF8ToUTF16(reinterpret_cast<const char*>(data), size, &markup);
303 }
304
305 // If there is a terminating NULL, drop it.
306 if (!markup.empty() && markup.at(markup.length() - 1) == '\0')
307 markup.resize(markup.length() - 1);
308
309 *html = markup;
310 *base_url = GURL();
311 return true;
123 } 312 }
124 313
125 bool OSExchangeDataProviderMus::HasHtml() const { 314 bool OSExchangeDataProviderMus::HasHtml() const {
126 return false; 315 return base::ContainsKey(mime_data_, ui::mojom::kMimeTypeHTML);
127 } 316 }
128 #endif 317 #endif
129 318
130 #if defined(USE_AURA) || defined(OS_MACOSX) 319 #if defined(USE_AURA) || defined(OS_MACOSX)
131 void OSExchangeDataProviderMus::SetDragImage( 320 void OSExchangeDataProviderMus::SetDragImage(
132 const gfx::ImageSkia& image, 321 const gfx::ImageSkia& image,
133 const gfx::Vector2d& cursor_offset) { 322 const gfx::Vector2d& cursor_offset) {
134 drag_image_ = image; 323 drag_image_ = image;
135 drag_image_offset_ = cursor_offset; 324 drag_image_offset_ = cursor_offset;
136 } 325 }
137 326
138 const gfx::ImageSkia& OSExchangeDataProviderMus::GetDragImage() const { 327 const gfx::ImageSkia& OSExchangeDataProviderMus::GetDragImage() const {
139 return drag_image_; 328 return drag_image_;
140 } 329 }
141 330
142 const gfx::Vector2d& OSExchangeDataProviderMus::GetDragImageOffset() const { 331 const gfx::Vector2d& OSExchangeDataProviderMus::GetDragImageOffset() const {
143 return drag_image_offset_; 332 return drag_image_offset_;
144 } 333 }
145 #endif 334 #endif
146 335
336 bool OSExchangeDataProviderMus::GetFileURL(GURL* url) const {
337 base::FilePath file_path;
338 if (!GetFilename(&file_path))
339 return false;
340
341 GURL test_url = net::FilePathToFileURL(file_path);
342 if (!test_url.is_valid())
343 return false;
344
345 if (url)
346 *url = test_url;
347 return true;
348 }
349
350 bool OSExchangeDataProviderMus::GetPlainTextURL(GURL* url) const {
351 base::string16 str;
352 if (!GetString(&str))
353 return false;
354
355 GURL test_url(str);
356 if (!test_url.is_valid())
357 return false;
358
359 if (url)
360 *url = test_url;
361 return true;
362 }
363
147 } // namespace views 364 } // namespace views
OLDNEW
« no previous file with comments | « ui/views/mus/os_exchange_data_provider_mus.h ('k') | ui/views/mus/os_exchange_data_provider_mus_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698