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

Side by Side Diff: base/clipboard_linux.cc

Issue 159815: Refactor bookmark clipboard code to be cross platform. (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: fix UMR Created 11 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 | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2006-2008 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 "base/clipboard.h" 5 #include "base/clipboard.h"
6 6
7 #include <gtk/gtk.h> 7 #include <gtk/gtk.h>
8 #include <map> 8 #include <map>
9 #include <set> 9 #include <set>
10 #include <string> 10 #include <string>
11 #include <utility> 11 #include <utility>
12 12
13 #include "base/file_path.h" 13 #include "base/file_path.h"
14 #include "base/gfx/size.h" 14 #include "base/gfx/size.h"
15 #include "base/scoped_ptr.h" 15 #include "base/scoped_ptr.h"
16 #include "base/linux_util.h" 16 #include "base/linux_util.h"
17 #include "base/string_util.h" 17 #include "base/string_util.h"
18 18
19 namespace { 19 namespace {
20 20
21 const char kMimeBmp[] = "image/bmp"; 21 const char kMimeBmp[] = "image/bmp";
22 const char kMimeHtml[] = "text/html"; 22 const char kMimeHtml[] = "text/html";
23 const char kMimeText[] = "text/plain"; 23 const char kMimeText[] = "text/plain";
24 const char kMimeWebkitSmartPaste[] = "chromium-internal/webkit-paste"; 24 const char kMimeWebkitSmartPaste[] = "chromium/x-webkit-paste";
25 25
26 std::string GdkAtomToString(const GdkAtom& atom) { 26 std::string GdkAtomToString(const GdkAtom& atom) {
27 gchar* name = gdk_atom_name(atom); 27 gchar* name = gdk_atom_name(atom);
28 std::string rv(name); 28 std::string rv(name);
29 g_free(name); 29 g_free(name);
30 return rv; 30 return rv;
31 } 31 }
32 32
33 GdkAtom StringToGdkAtom(const std::string& str) { 33 GdkAtom StringToGdkAtom(const std::string& str) {
34 return gdk_atom_intern(str.c_str(), false); 34 return gdk_atom_intern(str.c_str(), false);
(...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after
186 186
187 void Clipboard::WriteHyperlink(const char* title_data, size_t title_len, 187 void Clipboard::WriteHyperlink(const char* title_data, size_t title_len,
188 const char* url_data, size_t url_len) { 188 const char* url_data, size_t url_len) {
189 NOTIMPLEMENTED(); 189 NOTIMPLEMENTED();
190 } 190 }
191 191
192 void Clipboard::WriteFiles(const char* file_data, size_t file_len) { 192 void Clipboard::WriteFiles(const char* file_data, size_t file_len) {
193 NOTIMPLEMENTED(); 193 NOTIMPLEMENTED();
194 } 194 }
195 195
196 void Clipboard::WriteData(const char* format_name, size_t format_len,
197 const char* data_data, size_t data_len) {
198 char* data = new char[data_len];
199 memcpy(data, data_data, data_len);
200 std::string format(format_name, format_len);
201 InsertMapping(format.c_str(), data, data_len);
202 }
203
196 // We do not use gtk_clipboard_wait_is_target_available because of 204 // We do not use gtk_clipboard_wait_is_target_available because of
197 // a bug with the gtk clipboard. It caches the available targets 205 // a bug with the gtk clipboard. It caches the available targets
198 // and does not always refresh the cache when it is appropriate. 206 // and does not always refresh the cache when it is appropriate.
199 bool Clipboard::IsFormatAvailable(const Clipboard::FormatType& format) const { 207 bool Clipboard::IsFormatAvailable(const Clipboard::FormatType& format) const {
200 bool format_is_plain_text = GetPlainTextFormatType() == format; 208 bool format_is_plain_text = GetPlainTextFormatType() == format;
201 if (format_is_plain_text) { 209 if (format_is_plain_text) {
202 // This tries a number of common text targets. 210 // This tries a number of common text targets.
203 if (gtk_clipboard_wait_is_text_available(clipboard_)) 211 if (gtk_clipboard_wait_is_text_available(clipboard_))
204 return true; 212 return true;
205 } 213 }
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
239 break; 247 break;
240 } 248 }
241 } 249 }
242 250
243 g_free(targets); 251 g_free(targets);
244 gtk_selection_data_free(data); 252 gtk_selection_data_free(data);
245 253
246 return retval; 254 return retval;
247 } 255 }
248 256
257 bool Clipboard::IsFormatAvailableByString(const std::string& format) const {
258 return IsFormatAvailable(format);
259 }
260
249 void Clipboard::ReadText(string16* result) const { 261 void Clipboard::ReadText(string16* result) const {
250 result->clear(); 262 result->clear();
251 gchar* text = gtk_clipboard_wait_for_text(clipboard_); 263 gchar* text = gtk_clipboard_wait_for_text(clipboard_);
252 264
253 if (text == NULL) 265 if (text == NULL)
254 return; 266 return;
255 267
256 // TODO(estade): do we want to handle the possible error here? 268 // TODO(estade): do we want to handle the possible error here?
257 UTF8ToUTF16(text, strlen(text), result); 269 UTF8ToUTF16(text, strlen(text), result);
258 g_free(text); 270 g_free(text);
(...skipping 18 matching lines...) Expand all
277 // TODO(port): set *src_url. 289 // TODO(port): set *src_url.
278 void Clipboard::ReadHTML(string16* markup, std::string* src_url) const { 290 void Clipboard::ReadHTML(string16* markup, std::string* src_url) const {
279 markup->clear(); 291 markup->clear();
280 292
281 GtkSelectionData* data = gtk_clipboard_wait_for_contents(clipboard_, 293 GtkSelectionData* data = gtk_clipboard_wait_for_contents(clipboard_,
282 StringToGdkAtom(GetHtmlFormatType())); 294 StringToGdkAtom(GetHtmlFormatType()));
283 295
284 if (!data) 296 if (!data)
285 return; 297 return;
286 298
287 UTF8ToUTF16(reinterpret_cast<char*>(data->data), 299 UTF8ToUTF16(reinterpret_cast<char*>(data->data), data->length, markup);
288 strlen(reinterpret_cast<char*>(data->data)), 300 gtk_selection_data_free(data);
289 markup); 301 }
302
303 void Clipboard::ReadBookmark(string16* title, std::string* url) const {
304 // TODO(estade): implement this.
305 }
306
307 void Clipboard::ReadData(const std::string& format, std::string* result) {
308 GtkSelectionData* data =
309 gtk_clipboard_wait_for_contents(clipboard_, StringToGdkAtom(format));
310 if (!data)
311 return;
312 result->assign(reinterpret_cast<char*>(data->data), data->length);
290 gtk_selection_data_free(data); 313 gtk_selection_data_free(data);
291 } 314 }
292 315
293 // static 316 // static
294 Clipboard::FormatType Clipboard::GetPlainTextFormatType() { 317 Clipboard::FormatType Clipboard::GetPlainTextFormatType() {
295 return GdkAtomToString(GDK_TARGET_STRING); 318 return GdkAtomToString(GDK_TARGET_STRING);
296 } 319 }
297 320
298 // static 321 // static
299 Clipboard::FormatType Clipboard::GetPlainTextWFormatType() { 322 Clipboard::FormatType Clipboard::GetPlainTextWFormatType() {
(...skipping 22 matching lines...) Expand all
322 345
323 if (iter != clipboard_data_->end()) { 346 if (iter != clipboard_data_->end()) {
324 if (strcmp(kMimeBmp, key) == 0) 347 if (strcmp(kMimeBmp, key) == 0)
325 g_object_unref(reinterpret_cast<GdkPixbuf*>(iter->second.first)); 348 g_object_unref(reinterpret_cast<GdkPixbuf*>(iter->second.first));
326 else 349 else
327 delete[] iter->second.first; 350 delete[] iter->second.first;
328 } 351 }
329 352
330 (*clipboard_data_)[key] = std::make_pair(data, data_len); 353 (*clipboard_data_)[key] = std::make_pair(data, data_len);
331 } 354 }
OLDNEW
« no previous file with comments | « base/clipboard.cc ('k') | base/clipboard_unittest.cc » ('j') | base/clipboard_unittest.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698