summaryrefslogtreecommitdiffstats
path: root/base/clipboard_linux.cc
blob: 5906319cd5c4370c72257801b00721018b3410e5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/clipboard.h"
#include "base/string_util.h"

#include <gtk/gtk.h>
#include <map>
#include <set>
#include <string>
#include <utility>

namespace {

static const char* kMimeHtml = "text/html";
static const char* kMimeText = "text/plain";
static const char* kMimeWebkitSmartPaste = "chrome-internal/webkit-paste";

// GtkClipboardGetFunc callback.
// GTK will call this when an application wants data we copied to the clipboard.
void GetData(GtkClipboard* clipboard,
             GtkSelectionData* selection_data,
             guint info,
             gpointer user_data) {
  Clipboard::TargetMap* data_map =
      reinterpret_cast<Clipboard::TargetMap*>(user_data);

  Clipboard::TargetMap::iterator iter =
      data_map->find(std::string(gdk_atom_name(selection_data->target)));

  if (iter == data_map->end())
    return;

  gtk_selection_data_set(selection_data, selection_data->target, 8,
                         reinterpret_cast<guchar*>(iter->second.first),
                         iter->second.second);
}

// GtkClipboardClearFunc callback.
// We are guaranteed this will be called exactly once for each call to
// gtk_clipboard_set_with_data
void ClearData(GtkClipboard* clipboard,
               gpointer user_data) {
  Clipboard::TargetMap* map =
      reinterpret_cast<Clipboard::TargetMap*>(user_data);
  std::set<char*> ptrs;

  for (Clipboard::TargetMap::iterator iter = map->begin();
       iter != map->end(); ++iter)
    ptrs.insert(iter->second.first);

  for (std::set<char*>::iterator iter = ptrs.begin();
       iter != ptrs.end(); ++iter)
    delete[] *iter;

  delete map;
}

// Frees the pointers in the given map and clears the map.
// Does not double-free any pointers.
void FreeTargetMap(Clipboard::TargetMap map) {
  std::set<char*> ptrs;

  for (Clipboard::TargetMap::iterator iter = map.begin();
       iter != map.end(); ++iter)
    ptrs.insert(iter->second.first);

  for (std::set<char*>::iterator iter = ptrs.begin();
       iter != ptrs.end(); ++iter)
    delete[] *iter;

  map.clear();
}

}  // namespace

Clipboard::Clipboard() {
  clipboard_ = gtk_clipboard_get(GDK_SELECTION_CLIPBOARD);
}

Clipboard::~Clipboard() {
  // TODO(estade): do we want to save clipboard data after we exit?
  // gtk_clipboard_set_can_store and gtk_clipboard_store work
  // but have strangely awful performance.
}

void Clipboard::WriteObjects(const ObjectMap& objects) {
  clipboard_data_ = new TargetMap();

  for (ObjectMap::const_iterator iter = objects.begin();
       iter != objects.end(); ++iter) {
    DispatchObject(static_cast<ObjectType>(iter->first), iter->second);
  }

  SetGtkClipboard();
}

// Take ownership of the GTK clipboard and inform it of the targets we support.
void Clipboard::SetGtkClipboard() {
  GtkTargetEntry targets[clipboard_data_->size()];

  int i = 0;
  for (Clipboard::TargetMap::iterator iter = clipboard_data_->begin();
       iter != clipboard_data_->end(); ++iter, ++i) {
    char* target_string = new char[iter->first.size() + 1];
    strcpy(target_string, iter->first.c_str());
    targets[i].target = target_string;
    targets[i].flags = 0;
    targets[i].info = i;
  }

  gtk_clipboard_set_with_data(clipboard_, targets,
                              clipboard_data_->size(),
                              GetData, ClearData,
                              clipboard_data_);

  for (size_t i = 0; i < clipboard_data_->size(); i++)
    delete[] targets[i].target;
}

void Clipboard::WriteText(const char* text_data, size_t text_len) {
  char* data = new char[text_len];
  memcpy(data, text_data, text_len);

  InsertMapping(kMimeText, data, text_len);
  InsertMapping("TEXT", data, text_len);
  InsertMapping("STRING", data, text_len);
  InsertMapping("UTF8_STRING", data, text_len);
  InsertMapping("COMPOUND_TEXT", data, text_len);
}

void Clipboard::WriteHTML(const char* markup_data,
                          size_t markup_len,
                          const char* url_data,
                          size_t url_len) {
  // TODO(estade): might not want to ignore |url_data|
  char* data = new char[markup_len];
  memcpy(data, markup_data, markup_len);

  InsertMapping(kMimeHtml, data, markup_len);
}

// Write an extra flavor that signifies WebKit was the last to modify the
// pasteboard. This flavor has no data.
void Clipboard::WriteWebSmartPaste() {
  InsertMapping(kMimeWebkitSmartPaste, NULL, 0);
}

// We do not use gtk_clipboard_wait_is_target_available because of
// a bug with the gtk clipboard. It caches the available targets
// and does not always refresh the cache when it is appropriate.
// TODO(estade): When gnome bug 557315 is resolved, change this function
// to use gtk_clipboard_wait_is_target_available. Also, catch requests
// for plain text and change them to gtk_clipboard_wait_is_text_available
// (which checks for several standard text targets).
bool Clipboard::IsFormatAvailable(Clipboard::FormatType format) const {
  bool retval = false;
  GdkAtom* targets = NULL;
  GtkSelectionData* data =
      gtk_clipboard_wait_for_contents(clipboard_,
                                      gdk_atom_intern("TARGETS", false));

  if (!data)
    return false;

  int num = 0;
  gtk_selection_data_get_targets(data, &targets, &num);

  for (int i = 0; i < num; i++) {
    if (targets[i] == format) {
      retval = true;
      break;
    }
  }

  gtk_selection_data_free(data);
  g_free(targets);

  return retval;
}

void Clipboard::ReadText(std::wstring* result) const {
  result->clear();
  gchar* text = gtk_clipboard_wait_for_text(clipboard_);

  if (text == NULL)
   return;

  // TODO(estade): do we want to handle the possible error here?
  UTF8ToWide(text, strlen(text), result);
  g_free(text);
}

void Clipboard::ReadAsciiText(std::string* result) const {
  result->clear();
  gchar* text = gtk_clipboard_wait_for_text(clipboard_);

  if (text == NULL)
   return;

  result->assign(text);
  g_free(text);
}

// TODO(estade): handle different charsets.
void Clipboard::ReadHTML(std::wstring* markup, std::string* src_url) const {
  markup->clear();

  GtkSelectionData* data = gtk_clipboard_wait_for_contents(clipboard_,
                                                           GetHtmlFormatType());

  if (!data)
    return;

  UTF8ToWide(reinterpret_cast<char*>(data->data),
             strlen(reinterpret_cast<char*>(data->data)),
             markup);
  gtk_selection_data_free(data);
}

// static
Clipboard::FormatType Clipboard::GetPlainTextFormatType() {
  return GDK_TARGET_STRING;
}

// static
Clipboard::FormatType Clipboard::GetPlainTextWFormatType() {
  return GetPlainTextFormatType();
}

// static
Clipboard::FormatType Clipboard::GetHtmlFormatType() {
  return gdk_atom_intern(kMimeHtml, false);
}

// static
Clipboard::FormatType Clipboard::GetWebKitSmartPasteFormatType() {
  return gdk_atom_intern(kMimeWebkitSmartPaste, false);
}

// Insert the key/value pair in the clipboard_data structure. If
// the mapping already exists, it frees the associated data. Don't worry
// about double freeing because if the same key is inserted into the
// map twice, it must have come from different Write* functions and the
// data pointer cannot be the same.
void Clipboard::InsertMapping(const char* key,
                              char* data,
                              size_t data_len) {
  TargetMap::iterator iter = clipboard_data_->find(key);

  if (iter != clipboard_data_->end())
    delete[] iter->second.first;

  (*clipboard_data_)[key] = std::make_pair(data, data_len);
}