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 "content/browser/tab_contents/web_drag_dest_gtk.h" | 5 #include "content/browser/tab_contents/web_drag_dest_gtk.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/file_path.h" | 10 #include "base/file_path.h" |
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
138 // We might get the data from an old get_data() request that we no longer | 138 // We might get the data from an old get_data() request that we no longer |
139 // care about. | 139 // care about. |
140 if (context != context_) | 140 if (context != context_) |
141 return; | 141 return; |
142 | 142 |
143 data_requests_--; | 143 data_requests_--; |
144 | 144 |
145 // Decode the data. | 145 // Decode the data. |
146 gint data_length = gtk_selection_data_get_length(data); | 146 gint data_length = gtk_selection_data_get_length(data); |
147 const guchar* raw_data = gtk_selection_data_get_data(data); | 147 const guchar* raw_data = gtk_selection_data_get_data(data); |
| 148 GdkAtom target = gtk_selection_data_get_target(data); |
148 if (raw_data && data_length > 0) { | 149 if (raw_data && data_length > 0) { |
149 // If the source can't provide us with valid data for a requested target, | 150 // If the source can't provide us with valid data for a requested target, |
150 // raw_data will be NULL. | 151 // raw_data will be NULL. |
151 if (data->target == ui::GetAtomForTarget(ui::TEXT_PLAIN)) { | 152 if (target == ui::GetAtomForTarget(ui::TEXT_PLAIN)) { |
152 guchar* text = gtk_selection_data_get_text(data); | 153 guchar* text = gtk_selection_data_get_text(data); |
153 if (text) { | 154 if (text) { |
154 drop_data_->plain_text = | 155 drop_data_->plain_text = |
155 UTF8ToUTF16(std::string(reinterpret_cast<const char*>(text))); | 156 UTF8ToUTF16(std::string(reinterpret_cast<const char*>(text))); |
156 g_free(text); | 157 g_free(text); |
157 } | 158 } |
158 } else if (data->target == ui::GetAtomForTarget(ui::TEXT_URI_LIST)) { | 159 } else if (target == ui::GetAtomForTarget(ui::TEXT_URI_LIST)) { |
159 gchar** uris = gtk_selection_data_get_uris(data); | 160 gchar** uris = gtk_selection_data_get_uris(data); |
160 if (uris) { | 161 if (uris) { |
161 drop_data_->url = GURL(); | 162 drop_data_->url = GURL(); |
162 for (gchar** uri_iter = uris; *uri_iter; uri_iter++) { | 163 for (gchar** uri_iter = uris; *uri_iter; uri_iter++) { |
163 // Most file managers populate text/uri-list with file URLs when | 164 // Most file managers populate text/uri-list with file URLs when |
164 // dragging files. To avoid exposing file system paths to web content, | 165 // dragging files. To avoid exposing file system paths to web content, |
165 // file URLs are never set as the URL content for the drop. | 166 // file URLs are never set as the URL content for the drop. |
166 // TODO(estade): Can the filenames have a non-UTF8 encoding? | 167 // TODO(estade): Can the filenames have a non-UTF8 encoding? |
167 GURL url(*uri_iter); | 168 GURL url(*uri_iter); |
168 FilePath file_path; | 169 FilePath file_path; |
169 if (url.SchemeIs(chrome::kFileScheme) && | 170 if (url.SchemeIs(chrome::kFileScheme) && |
170 net::FileURLToFilePath(url, &file_path)) { | 171 net::FileURLToFilePath(url, &file_path)) { |
171 drop_data_->filenames.push_back(UTF8ToUTF16(file_path.value())); | 172 drop_data_->filenames.push_back(UTF8ToUTF16(file_path.value())); |
172 // This is a hack. Some file managers also populate text/plain with | 173 // This is a hack. Some file managers also populate text/plain with |
173 // a file URL when dragging files, so we clear it to avoid exposing | 174 // a file URL when dragging files, so we clear it to avoid exposing |
174 // it to the web content. | 175 // it to the web content. |
175 drop_data_->plain_text.clear(); | 176 drop_data_->plain_text.clear(); |
176 } else if (!drop_data_->url.is_valid()) { | 177 } else if (!drop_data_->url.is_valid()) { |
177 // Also set the first non-file URL as the URL content for the drop. | 178 // Also set the first non-file URL as the URL content for the drop. |
178 drop_data_->url = url; | 179 drop_data_->url = url; |
179 } | 180 } |
180 } | 181 } |
181 g_strfreev(uris); | 182 g_strfreev(uris); |
182 } | 183 } |
183 } else if (data->target == ui::GetAtomForTarget(ui::TEXT_HTML)) { | 184 } else if (target == ui::GetAtomForTarget(ui::TEXT_HTML)) { |
184 // TODO(estade): Can the html have a non-UTF8 encoding? | 185 // TODO(estade): Can the html have a non-UTF8 encoding? |
185 drop_data_->text_html = | 186 drop_data_->text_html = |
186 UTF8ToUTF16(std::string(reinterpret_cast<const char*>(raw_data), | 187 UTF8ToUTF16(std::string(reinterpret_cast<const char*>(raw_data), |
187 data_length)); | 188 data_length)); |
188 // We leave the base URL empty. | 189 // We leave the base URL empty. |
189 } else if (data->target == ui::GetAtomForTarget(ui::NETSCAPE_URL)) { | 190 } else if (target == ui::GetAtomForTarget(ui::NETSCAPE_URL)) { |
190 std::string netscape_url(reinterpret_cast<const char*>(raw_data), | 191 std::string netscape_url(reinterpret_cast<const char*>(raw_data), |
191 data_length); | 192 data_length); |
192 size_t split = netscape_url.find_first_of('\n'); | 193 size_t split = netscape_url.find_first_of('\n'); |
193 if (split != std::string::npos) { | 194 if (split != std::string::npos) { |
194 drop_data_->url = GURL(netscape_url.substr(0, split)); | 195 drop_data_->url = GURL(netscape_url.substr(0, split)); |
195 if (split < netscape_url.size() - 1) | 196 if (split < netscape_url.size() - 1) |
196 drop_data_->url_title = UTF8ToUTF16(netscape_url.substr(split + 1)); | 197 drop_data_->url_title = UTF8ToUTF16(netscape_url.substr(split + 1)); |
197 } | 198 } |
198 } else if (data->target == ui::GetAtomForTarget(ui::CHROME_NAMED_URL)) { | 199 } else if (target == ui::GetAtomForTarget(ui::CHROME_NAMED_URL)) { |
199 ui::ExtractNamedURL(data, &drop_data_->url, &drop_data_->url_title); | 200 ui::ExtractNamedURL(data, &drop_data_->url, &drop_data_->url_title); |
200 } else if (data->target == ui::GetAtomForTarget(ui::CUSTOM_DATA)) { | 201 } else if (target == ui::GetAtomForTarget(ui::CUSTOM_DATA)) { |
201 ui::ReadCustomDataIntoMap( | 202 ui::ReadCustomDataIntoMap( |
202 raw_data, data_length, &drop_data_->custom_data); | 203 raw_data, data_length, &drop_data_->custom_data); |
203 } | 204 } |
204 } | 205 } |
205 | 206 |
206 // For CHROME_BOOKMARK_ITEM, we have to handle the case where the drag source | 207 // For CHROME_BOOKMARK_ITEM, we have to handle the case where the drag source |
207 // doesn't have any data available for us. In this case we try to synthesize a | 208 // doesn't have any data available for us. In this case we try to synthesize a |
208 // URL bookmark. | 209 // URL bookmark. |
209 // Note that bookmark drag data is encoded in the same format for both | 210 // Note that bookmark drag data is encoded in the same format for both |
210 // GTK and Views, hence we can share the same logic here. | 211 // GTK and Views, hence we can share the same logic here. |
211 if (delegate() && data->target == delegate()->GetBookmarkTargetAtom()) { | 212 if (delegate() && target == delegate()->GetBookmarkTargetAtom()) { |
212 if (raw_data && data_length > 0) { | 213 if (raw_data && data_length > 0) { |
213 delegate()->OnReceiveDataFromGtk(data); | 214 delegate()->OnReceiveDataFromGtk(data); |
214 } else { | 215 } else { |
215 delegate()->OnReceiveProcessedData(drop_data_->url, | 216 delegate()->OnReceiveProcessedData(drop_data_->url, |
216 drop_data_->url_title); | 217 drop_data_->url_title); |
217 } | 218 } |
218 } | 219 } |
219 | 220 |
220 if (data_requests_ == 0) { | 221 if (data_requests_ == 0) { |
221 // Tell the renderer about the drag. | 222 // Tell the renderer about the drag. |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
262 | 263 |
263 // The second parameter is just an educated guess as to whether or not the | 264 // The second parameter is just an educated guess as to whether or not the |
264 // drag succeeded, but at least we will get the drag-end animation right | 265 // drag succeeded, but at least we will get the drag-end animation right |
265 // sometimes. | 266 // sometimes. |
266 gtk_drag_finish(context, is_drop_target_, FALSE, time); | 267 gtk_drag_finish(context, is_drop_target_, FALSE, time); |
267 | 268 |
268 return TRUE; | 269 return TRUE; |
269 } | 270 } |
270 | 271 |
271 } // namespace content | 272 } // namespace content |
OLD | NEW |