OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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/base/dragdrop/os_exchange_data_provider_aurax11.h" | 5 #include "ui/base/dragdrop/os_exchange_data_provider_aurax11.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/message_pump_aurax11.h" | 8 #include "base/message_pump_aurax11.h" |
9 #include "base/strings/string_util.h" | 9 #include "base/strings/string_util.h" |
10 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
11 #include "net/base/net_util.h" | 11 #include "net/base/net_util.h" |
12 #include "ui/base/clipboard/clipboard.h" | 12 #include "ui/base/clipboard/clipboard.h" |
13 #include "ui/base/clipboard/scoped_clipboard_writer.h" | 13 #include "ui/base/clipboard/scoped_clipboard_writer.h" |
14 #include "ui/base/dragdrop/desktop_selection_provider_aurax11.h" | |
15 #include "ui/base/x/selection_utils.h" | 14 #include "ui/base/x/selection_utils.h" |
16 #include "ui/base/x/x11_util.h" | 15 #include "ui/base/x/x11_util.h" |
17 | 16 |
18 // Note: the GetBlah() methods are used immediately by the | 17 // Note: the GetBlah() methods are used immediately by the |
19 // web_contents_view_aura.cc:PrepareWebDropData(), while the omnibox is a | 18 // web_contents_view_aura.cc:PrepareWebDropData(), while the omnibox is a |
20 // little more discriminating and calls HasBlah() before trying to get the | 19 // little more discriminating and calls HasBlah() before trying to get the |
21 // information. | 20 // information. |
22 | 21 |
23 namespace ui { | 22 namespace ui { |
24 | 23 |
25 namespace { | 24 namespace { |
26 | 25 |
27 const char kDndSelection[] = "XdndSelection"; | 26 const char kDndSelection[] = "XdndSelection"; |
28 | 27 |
29 const char* kAtomsToCache[] = { | 28 const char* kAtomsToCache[] = { |
30 kString, | 29 kString, |
31 kText, | 30 kText, |
32 kUtf8String, | 31 kUtf8String, |
33 kDndSelection, | 32 kDndSelection, |
34 Clipboard::kMimeTypeURIList, | 33 Clipboard::kMimeTypeURIList, |
35 kMimeTypeMozillaURL, | 34 kMimeTypeMozillaURL, |
35 Clipboard::kMimeTypeText, | |
36 NULL | 36 NULL |
37 }; | 37 }; |
38 | 38 |
39 } // namespace | 39 } // namespace |
40 | 40 |
41 OSExchangeDataProviderAuraX11::OSExchangeDataProviderAuraX11( | 41 OSExchangeDataProviderAuraX11::OSExchangeDataProviderAuraX11( |
42 ui::DesktopSelectionProviderAuraX11* provider, | |
43 ::Window x_window, | 42 ::Window x_window, |
44 const std::vector< ::Atom> targets) | 43 SelectionFormatMap* selection) |
45 : x_display_(GetXDisplay()), | 44 : x_display_(GetXDisplay()), |
46 x_root_window_(DefaultRootWindow(x_display_)), | 45 x_root_window_(DefaultRootWindow(x_display_)), |
47 own_window_(false), | 46 own_window_(false), |
48 selection_event_provider_(provider), | |
49 x_window_(x_window), | 47 x_window_(x_window), |
50 atom_cache_(x_display_, kAtomsToCache), | 48 atom_cache_(x_display_, kAtomsToCache), |
51 selection_requestor_(x_display_, x_window_, | 49 format_map_(selection), |
52 atom_cache_.GetAtom(kDndSelection)), | |
53 selection_owner_(x_display_, x_window_, | 50 selection_owner_(x_display_, x_window_, |
54 atom_cache_.GetAtom(kDndSelection)), | 51 atom_cache_.GetAtom(kDndSelection)) { |
55 targets_(targets) { | |
56 // We don't know all possible MIME types at compile time. | 52 // We don't know all possible MIME types at compile time. |
57 atom_cache_.allow_uncached_atoms(); | 53 atom_cache_.allow_uncached_atoms(); |
58 | |
59 selection_event_provider_->SetDropHandler(this); | |
60 } | 54 } |
61 | 55 |
62 OSExchangeDataProviderAuraX11::OSExchangeDataProviderAuraX11() | 56 OSExchangeDataProviderAuraX11::OSExchangeDataProviderAuraX11() |
63 : x_display_(GetXDisplay()), | 57 : x_display_(GetXDisplay()), |
64 x_root_window_(DefaultRootWindow(x_display_)), | 58 x_root_window_(DefaultRootWindow(x_display_)), |
65 own_window_(true), | 59 own_window_(true), |
66 selection_event_provider_(NULL), | |
67 x_window_(XCreateWindow( | 60 x_window_(XCreateWindow( |
68 x_display_, | 61 x_display_, |
69 x_root_window_, | 62 x_root_window_, |
70 -100, -100, 10, 10, // x, y, width, height | 63 -100, -100, 10, 10, // x, y, width, height |
71 0, // border width | 64 0, // border width |
72 CopyFromParent, // depth | 65 CopyFromParent, // depth |
73 InputOnly, | 66 InputOnly, |
74 CopyFromParent, // visual | 67 CopyFromParent, // visual |
75 0, | 68 0, |
76 NULL)), | 69 NULL)), |
77 atom_cache_(x_display_, kAtomsToCache), | 70 atom_cache_(x_display_, kAtomsToCache), |
78 selection_requestor_(x_display_, x_window_, | 71 format_map_(new SelectionFormatMap), |
79 atom_cache_.GetAtom(kDndSelection)), | |
80 selection_owner_(x_display_, x_window_, | 72 selection_owner_(x_display_, x_window_, |
81 atom_cache_.GetAtom(kDndSelection)) { | 73 atom_cache_.GetAtom(kDndSelection)) { |
82 // We don't know all possible MIME types at compile time. | 74 // We don't know all possible MIME types at compile time. |
83 atom_cache_.allow_uncached_atoms(); | 75 atom_cache_.allow_uncached_atoms(); |
84 | 76 |
85 XStoreName(x_display_, x_window_, "Chromium Drag & Drop Window"); | 77 XStoreName(x_display_, x_window_, "Chromium Drag & Drop Window"); |
86 | 78 |
87 base::MessagePumpAuraX11::Current()->AddDispatcherForWindow(this, x_window_); | 79 base::MessagePumpAuraX11::Current()->AddDispatcherForWindow(this, x_window_); |
88 } | 80 } |
89 | 81 |
90 OSExchangeDataProviderAuraX11::~OSExchangeDataProviderAuraX11() { | 82 OSExchangeDataProviderAuraX11::~OSExchangeDataProviderAuraX11() { |
91 if (own_window_) { | 83 if (own_window_) { |
92 base::MessagePumpAuraX11::Current()->RemoveDispatcherForWindow(x_window_); | 84 base::MessagePumpAuraX11::Current()->RemoveDispatcherForWindow(x_window_); |
93 XDestroyWindow(x_display_, x_window_); | 85 XDestroyWindow(x_display_, x_window_); |
94 } else { | |
95 selection_event_provider_->SetDropHandler(NULL); | |
96 } | 86 } |
97 } | 87 } |
98 | 88 |
99 void OSExchangeDataProviderAuraX11::OnSelectionNotify( | 89 void OSExchangeDataProviderAuraX11::TakeOwnershipOfSelection() const { |
100 const XSelectionEvent& event) { | 90 selection_owner_.TakeOwnershipOfSelection( |
101 selection_requestor_.OnSelectionNotify(event); | 91 scoped_ptr<SelectionFormatMap>(format_map_->Clone())); |
102 } | 92 } |
103 | 93 |
104 void OSExchangeDataProviderAuraX11::SetString(const string16& data) { | 94 void OSExchangeDataProviderAuraX11::RetrieveTargets( |
105 NOTIMPLEMENTED(); | 95 std::vector<Atom>* targets) const { |
96 selection_owner_.RetrieveTargets(targets); | |
97 } | |
98 | |
99 SelectionFormatMap* OSExchangeDataProviderAuraX11::CloneFormatMap() const { | |
100 // We clone the |selection_owner_|'s format map instead of our own in case | |
101 // ours has been modified since TakeOwnershipOfSelection() was called. | |
102 return selection_owner_.selection_format_map()->Clone(); | |
103 } | |
104 | |
105 void OSExchangeDataProviderAuraX11::SetString(const string16& text_data) { | |
106 std::string utf8 = UTF16ToUTF8(text_data); | |
107 | |
108 size_t text_len = utf8.size(); | |
109 char* data = new char[text_len]; | |
110 memcpy(data, utf8.c_str(), text_len); | |
111 | |
112 format_map_->Insert( | |
113 atom_cache_.GetAtom(Clipboard::kMimeTypeText), data, text_len); | |
114 format_map_->Insert( | |
115 atom_cache_.GetAtom(kText), data, text_len); | |
116 format_map_->Insert( | |
117 atom_cache_.GetAtom(kString), data, text_len); | |
118 format_map_->Insert( | |
119 atom_cache_.GetAtom(kUtf8String), data, text_len); | |
106 } | 120 } |
107 | 121 |
108 void OSExchangeDataProviderAuraX11::SetURL(const GURL& url, | 122 void OSExchangeDataProviderAuraX11::SetURL(const GURL& url, |
109 const string16& title) { | 123 const string16& title) { |
110 NOTIMPLEMENTED(); | 124 NOTIMPLEMENTED(); |
111 } | 125 } |
112 | 126 |
113 void OSExchangeDataProviderAuraX11::SetFilename(const base::FilePath& path) { | 127 void OSExchangeDataProviderAuraX11::SetFilename(const base::FilePath& path) { |
114 NOTIMPLEMENTED(); | 128 NOTIMPLEMENTED(); |
115 } | 129 } |
116 | 130 |
117 void OSExchangeDataProviderAuraX11::SetFilenames( | 131 void OSExchangeDataProviderAuraX11::SetFilenames( |
118 const std::vector<OSExchangeData::FileInfo>& filenames) { | 132 const std::vector<OSExchangeData::FileInfo>& filenames) { |
119 NOTIMPLEMENTED(); | 133 NOTIMPLEMENTED(); |
120 } | 134 } |
121 | 135 |
122 void OSExchangeDataProviderAuraX11::SetPickledData( | 136 void OSExchangeDataProviderAuraX11::SetPickledData( |
123 const OSExchangeData::CustomFormat& format, | 137 const OSExchangeData::CustomFormat& format, |
124 const Pickle& data) { | 138 const Pickle& data) { |
125 NOTIMPLEMENTED(); | 139 NOTIMPLEMENTED(); |
126 } | 140 } |
127 | 141 |
128 bool OSExchangeDataProviderAuraX11::GetString(string16* result) const { | 142 bool OSExchangeDataProviderAuraX11::GetString(string16* result) const { |
129 std::vector< ::Atom> text_atoms = ui::GetTextAtomsFrom(&atom_cache_); | 143 std::vector< ::Atom> text_atoms = ui::GetTextAtomsFrom(&atom_cache_); |
130 std::vector< ::Atom> requested_types; | 144 std::vector< ::Atom> requested_types; |
131 ui::GetAtomIntersection(text_atoms, targets_, &requested_types); | 145 ui::GetAtomIntersection(text_atoms, GetTargets(), &requested_types); |
132 | 146 |
133 scoped_ptr<ui::SelectionData> data( | 147 scoped_ptr<ui::SelectionData> data(format_map_->GetFirstOf(requested_types)); |
134 selection_requestor_.RequestAndWaitForTypes(requested_types)); | |
135 if (data) { | 148 if (data) { |
136 std::string text = data->GetText(); | 149 std::string text = data->GetText(); |
137 *result = UTF8ToUTF16(text); | 150 *result = UTF8ToUTF16(text); |
138 return true; | 151 return true; |
139 } | 152 } |
140 | 153 |
141 return false; | 154 return false; |
142 } | 155 } |
143 | 156 |
144 bool OSExchangeDataProviderAuraX11::GetURLAndTitle(GURL* url, | 157 bool OSExchangeDataProviderAuraX11::GetURLAndTitle(GURL* url, |
145 string16* title) const { | 158 string16* title) const { |
146 std::vector< ::Atom> url_atoms = ui::GetURLAtomsFrom(&atom_cache_); | 159 std::vector< ::Atom> url_atoms = ui::GetURLAtomsFrom(&atom_cache_); |
147 std::vector< ::Atom> requested_types; | 160 std::vector< ::Atom> requested_types; |
148 ui::GetAtomIntersection(url_atoms, targets_, &requested_types); | 161 ui::GetAtomIntersection(url_atoms, GetTargets(), &requested_types); |
149 | 162 |
150 scoped_ptr<ui::SelectionData> data( | 163 scoped_ptr<ui::SelectionData> data(format_map_->GetFirstOf(requested_types)); |
151 selection_requestor_.RequestAndWaitForTypes(requested_types)); | |
152 if (data) { | 164 if (data) { |
153 // TODO(erg): Technically, both of these forms can accept multiple URLs, | 165 // TODO(erg): Technically, both of these forms can accept multiple URLs, |
154 // but that doesn't match the assumptions of the rest of the system which | 166 // but that doesn't match the assumptions of the rest of the system which |
155 // expect single types. | 167 // expect single types. |
156 | 168 |
157 if (data->type() == atom_cache_.GetAtom(kMimeTypeMozillaURL)) { | 169 if (data->type() == atom_cache_.GetAtom(kMimeTypeMozillaURL)) { |
158 // Mozilla URLs are (UTF16: URL, newline, title). | 170 // Mozilla URLs are (UTF16: URL, newline, title). |
159 string16 unparsed; | 171 string16 unparsed; |
160 data->AssignTo(&unparsed); | 172 data->AssignTo(&unparsed); |
161 | 173 |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
206 bool OSExchangeDataProviderAuraX11::GetPickledData( | 218 bool OSExchangeDataProviderAuraX11::GetPickledData( |
207 const OSExchangeData::CustomFormat& format, | 219 const OSExchangeData::CustomFormat& format, |
208 Pickle* data) const { | 220 Pickle* data) const { |
209 NOTIMPLEMENTED(); | 221 NOTIMPLEMENTED(); |
210 return false; | 222 return false; |
211 } | 223 } |
212 | 224 |
213 bool OSExchangeDataProviderAuraX11::HasString() const { | 225 bool OSExchangeDataProviderAuraX11::HasString() const { |
214 std::vector< ::Atom> text_atoms = ui::GetTextAtomsFrom(&atom_cache_); | 226 std::vector< ::Atom> text_atoms = ui::GetTextAtomsFrom(&atom_cache_); |
215 std::vector< ::Atom> requested_types; | 227 std::vector< ::Atom> requested_types; |
216 ui::GetAtomIntersection(text_atoms, targets_, &requested_types); | 228 ui::GetAtomIntersection(text_atoms, GetTargets(), &requested_types); |
217 return !requested_types.empty(); | 229 return !requested_types.empty(); |
218 } | 230 } |
219 | 231 |
220 bool OSExchangeDataProviderAuraX11::HasURL() const { | 232 bool OSExchangeDataProviderAuraX11::HasURL() const { |
221 std::vector< ::Atom> url_atoms = ui::GetURLAtomsFrom(&atom_cache_); | 233 std::vector< ::Atom> url_atoms = ui::GetURLAtomsFrom(&atom_cache_); |
222 std::vector< ::Atom> requested_types; | 234 std::vector< ::Atom> requested_types; |
223 ui::GetAtomIntersection(url_atoms, targets_, &requested_types); | 235 ui::GetAtomIntersection(url_atoms, GetTargets(), &requested_types); |
224 return !requested_types.empty(); | 236 return !requested_types.empty(); |
225 } | 237 } |
226 | 238 |
227 bool OSExchangeDataProviderAuraX11::HasFile() const { | 239 bool OSExchangeDataProviderAuraX11::HasFile() const { |
228 // On X11, files are passed by URL and aren't separate. | 240 // On X11, files are passed by URL and aren't separate. |
229 return false; | 241 return false; |
230 } | 242 } |
231 | 243 |
232 bool OSExchangeDataProviderAuraX11::HasCustomFormat( | 244 bool OSExchangeDataProviderAuraX11::HasCustomFormat( |
233 const OSExchangeData::CustomFormat& format) const { | 245 const OSExchangeData::CustomFormat& format) const { |
234 std::vector< ::Atom> url_atoms; | 246 std::vector< ::Atom> url_atoms; |
235 url_atoms.push_back(atom_cache_.GetAtom(format.ToString().c_str())); | 247 url_atoms.push_back(atom_cache_.GetAtom(format.ToString().c_str())); |
236 std::vector< ::Atom> requested_types; | 248 std::vector< ::Atom> requested_types; |
237 ui::GetAtomIntersection(url_atoms, targets_, &requested_types); | 249 ui::GetAtomIntersection(url_atoms, GetTargets(), &requested_types); |
238 | 250 |
239 return !requested_types.empty(); | 251 return !requested_types.empty(); |
240 } | 252 } |
241 | 253 |
242 void OSExchangeDataProviderAuraX11::SetHtml(const string16& html, | 254 void OSExchangeDataProviderAuraX11::SetHtml(const string16& html, |
243 const GURL& base_url) { | 255 const GURL& base_url) { |
244 NOTIMPLEMENTED(); | 256 NOTIMPLEMENTED(); |
245 } | 257 } |
246 | 258 |
247 bool OSExchangeDataProviderAuraX11::GetHtml(string16* html, | 259 bool OSExchangeDataProviderAuraX11::GetHtml(string16* html, |
248 GURL* base_url) const { | 260 GURL* base_url) const { |
249 std::vector< ::Atom> url_atoms; | 261 std::vector< ::Atom> url_atoms; |
250 url_atoms.push_back(atom_cache_.GetAtom(Clipboard::kMimeTypeHTML)); | 262 url_atoms.push_back(atom_cache_.GetAtom(Clipboard::kMimeTypeHTML)); |
251 std::vector< ::Atom> requested_types; | 263 std::vector< ::Atom> requested_types; |
252 ui::GetAtomIntersection(url_atoms, targets_, &requested_types); | 264 ui::GetAtomIntersection(url_atoms, GetTargets(), &requested_types); |
253 | 265 |
254 scoped_ptr<ui::SelectionData> data( | 266 scoped_ptr<ui::SelectionData> data(format_map_->GetFirstOf(requested_types)); |
255 selection_requestor_.RequestAndWaitForTypes(requested_types)); | |
256 if (data) { | 267 if (data) { |
257 *html = data->GetHtml(); | 268 *html = data->GetHtml(); |
258 *base_url = GURL(); | 269 *base_url = GURL(); |
259 return true; | 270 return true; |
260 } | 271 } |
261 | 272 |
262 return false; | 273 return false; |
263 } | 274 } |
264 | 275 |
265 bool OSExchangeDataProviderAuraX11::HasHtml() const { | 276 bool OSExchangeDataProviderAuraX11::HasHtml() const { |
266 std::vector< ::Atom> url_atoms; | 277 std::vector< ::Atom> url_atoms; |
267 url_atoms.push_back(atom_cache_.GetAtom(Clipboard::kMimeTypeHTML)); | 278 url_atoms.push_back(atom_cache_.GetAtom(Clipboard::kMimeTypeHTML)); |
268 std::vector< ::Atom> requested_types; | 279 std::vector< ::Atom> requested_types; |
269 ui::GetAtomIntersection(url_atoms, targets_, &requested_types); | 280 ui::GetAtomIntersection(url_atoms, GetTargets(), &requested_types); |
270 | 281 |
271 return !requested_types.empty(); | 282 return !requested_types.empty(); |
272 } | 283 } |
273 | 284 |
274 void OSExchangeDataProviderAuraX11::SetDragImage( | 285 void OSExchangeDataProviderAuraX11::SetDragImage( |
275 const gfx::ImageSkia& image, | 286 const gfx::ImageSkia& image, |
276 const gfx::Vector2d& cursor_offset) { | 287 const gfx::Vector2d& cursor_offset) { |
277 NOTIMPLEMENTED(); | 288 drag_image_ = image; |
289 drag_image_offset_ = cursor_offset; | |
278 } | 290 } |
279 | 291 |
280 const gfx::ImageSkia& OSExchangeDataProviderAuraX11::GetDragImage() const { | 292 const gfx::ImageSkia& OSExchangeDataProviderAuraX11::GetDragImage() const { |
281 NOTIMPLEMENTED(); | |
282 return drag_image_; | 293 return drag_image_; |
283 } | 294 } |
284 | 295 |
285 const gfx::Vector2d& OSExchangeDataProviderAuraX11::GetDragImageOffset() const { | 296 const gfx::Vector2d& OSExchangeDataProviderAuraX11::GetDragImageOffset() const { |
286 NOTIMPLEMENTED(); | |
287 return drag_image_offset_; | 297 return drag_image_offset_; |
288 } | 298 } |
289 | 299 |
290 bool OSExchangeDataProviderAuraX11::Dispatch(const base::NativeEvent& event) { | 300 bool OSExchangeDataProviderAuraX11::Dispatch(const base::NativeEvent& event) { |
291 // TODO(erg): Implement this side when we implement sending data. | 301 XEvent* xev = event; |
292 return false; | 302 switch (xev->type) { |
303 case SelectionRequest: { | |
Daniel Erat
2013/06/17 22:09:57
nit: don't need curly brackets here
| |
304 selection_owner_.OnSelectionRequest(xev->xselectionrequest); | |
305 break; | |
306 } | |
307 default: { | |
Daniel Erat
2013/06/17 22:09:57
or here
| |
308 NOTIMPLEMENTED(); | |
309 } | |
310 } | |
311 | |
312 return true; | |
293 } | 313 } |
294 | 314 |
295 bool OSExchangeDataProviderAuraX11::GetPlainTextURL(GURL* url) const { | 315 bool OSExchangeDataProviderAuraX11::GetPlainTextURL(GURL* url) const { |
296 NOTIMPLEMENTED(); | 316 NOTIMPLEMENTED(); |
297 return false; | 317 return false; |
298 } | 318 } |
299 | 319 |
320 std::vector< ::Atom> OSExchangeDataProviderAuraX11::GetTargets() const { | |
321 return format_map_->GetTypes(); | |
322 } | |
323 | |
300 /////////////////////////////////////////////////////////////////////////////// | 324 /////////////////////////////////////////////////////////////////////////////// |
301 // OSExchangeData, public: | 325 // OSExchangeData, public: |
302 | 326 |
303 // static | 327 // static |
304 OSExchangeData::Provider* OSExchangeData::CreateProvider() { | 328 OSExchangeData::Provider* OSExchangeData::CreateProvider() { |
305 return new OSExchangeDataProviderAuraX11(); | 329 return new OSExchangeDataProviderAuraX11(); |
306 } | 330 } |
307 | 331 |
308 } // namespace ui | 332 } // namespace ui |
OLD | NEW |