summaryrefslogtreecommitdiffstats
path: root/chrome/browser/extensions/extension_clipboard_api.cc
blob: 0c7ce279fa3ba7070046aba2a38e8792ba3cbcd2 (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
257
258
259
260
261
262
263
264
265
266
267
268
// Copyright (c) 2011 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 "chrome/browser/extensions/extension_clipboard_api.h"

#include <string>

#include "base/string16.h"
#include "base/values.h"
#include "chrome/browser/browser_process.h"
#include "googleurl/src/gurl.h"
#include "ui/base/clipboard/clipboard.h"
#include "ui/base/clipboard/scoped_clipboard_writer.h"

namespace {

enum SupportedMimeType {
  MIME_TYPE_TEXT,
  MIME_TYPE_HTML,
  // Not really a type. Used when iterating through enum values. Should allways
  // be the last value in the enum.
  MIME_TYPE_COUNT
};

const char kDataProperty[] = "data";
const char kUrlProperty[] = "url";

const char kMimeTypePlainText[] = "text/plain";
const char kMimeTypeHtml[] = "text/html";

const char kBufferStandard[] = "standard";
const char kBufferSelection[] = "selection";

const char kMimeTypeNotSupportedError[] = "MIME type is not supported.";
const char kBufferReadNotSupportedError[] =
    "Reading from the buffer is not supported.";
const char kBufferWriteNotSupportedError[] =
    "Writing to the buffer is not supported.";
const char kBufferNotSupportedError[] = "Buffer is not supported.";

// Converts data MIME type to data format known to ui::Clipboard. MIME type is
// given as a string. Returns false iff the string could not be converted.
bool ConvertMimeTypeStringToClipboardType(const std::string& mime_type,
                           ui::Clipboard::FormatType* format_type) {
  if (mime_type == kMimeTypePlainText) {
    *format_type = ui::Clipboard::GetPlainTextFormatType();
    return true;
  }
  if (mime_type == kMimeTypeHtml) {
    *format_type = ui::Clipboard::GetHtmlFormatType();
    return true;
  }
  return false;
}

// Converts data MIME type to data format known to ui::Clipboard. MIME type is
// given as a enum value. Returns false iff the enum value could not be
// converted.
bool ConvertMimeTypeEnumToClipboardType(SupportedMimeType mime_type,
                         ui::Clipboard::FormatType* format_type) {
  switch (mime_type) {
    case MIME_TYPE_TEXT:
      *format_type = ui::Clipboard::GetPlainTextFormatType();
      return true;
    case MIME_TYPE_HTML:
      *format_type = ui::Clipboard::GetHtmlFormatType();
      return true;
    default:
      return false;
  }
}

// Converts MIME type enum value to string. Returns false iff the enum could not
// be converted.
bool MimeTypeEnumToString(SupportedMimeType mime_type_enum,
                          std::string* mime_type_str) {
  switch (mime_type_enum) {
    case MIME_TYPE_TEXT:
      *mime_type_str = kMimeTypePlainText;
      return true;
    case MIME_TYPE_HTML:
      *mime_type_str = kMimeTypeHtml;
      return true;
    default:
      return false;
  }
}

// Converts a buffer type given as a string to buffer type known to
// ui::Clipboard. Returns false iff the conversion is not possible.
bool ConvertBufferTypeToClipboardType(const std::string& buffer,
                       ui::Clipboard::Buffer* buffer_type) {
  if (buffer == kBufferStandard) {
    *buffer_type = ui::Clipboard::BUFFER_STANDARD;
    return true;
  }
  if (buffer == kBufferSelection) {
    *buffer_type = ui::Clipboard::BUFFER_SELECTION;
    return true;
  }
  return false;
}

}  // namespace

bool WriteDataClipboardFunction::RunImpl() {
  if (args_->GetSize() != 3 && args_->GetSize() != 4) {
    return false;
  }

  std::string mime_type;
  args_->GetString(0, &mime_type);

  std::string buffer;
  args_->GetString(1, &buffer);

  // At the moment only writing to standard cllipboard buffer is possible.
  if (buffer != kBufferStandard) {
    error_ = kBufferWriteNotSupportedError;
    return false;
  }

  if (mime_type == kMimeTypePlainText)
    return WritePlainText(buffer);

  if (mime_type == kMimeTypeHtml)
    return WriteHtml(buffer);

  error_ = kMimeTypeNotSupportedError;
  return false;
}

bool WriteDataClipboardFunction::WritePlainText(const std::string& buffer) {
  string16 data;
  args_->GetString(2, &data);

  ui::ScopedClipboardWriter scw(g_browser_process->clipboard());

  scw.WriteText(data);
  return true;
}

bool WriteDataClipboardFunction::WriteHtml(const std::string& buffer) {
  string16 data;
  args_->GetString(2, &data);

  std::string url_str;
  // |url| parameter is optional.
  if (args_->GetSize() == 4)
    args_->GetString(3, &url_str);

  // We do this to verify passed url is well-formed.
  GURL url(url_str);

  ui::ScopedClipboardWriter scw(g_browser_process->clipboard());

  scw.WriteHTML(data, url.spec());
  return true;
}

bool ReadDataClipboardFunction::RunImpl() {
  if (args_->GetSize() != 2) {
    return false;
  }
  std::string mime_type;
  args_->GetString(0, &mime_type);

  std::string buffer;
  args_->GetString(1, &buffer);

// Windows and Mac don't support selection clipboard buffer.
#if (defined(OS_WIN) || defined(OS_MACOSX))
  if (buffer != kBufferStandard) {
    error_ = kBufferReadNotSupportedError;
    return false;
  }
#endif

  if (mime_type == kMimeTypePlainText)
    return ReadPlainText(buffer);

  if (mime_type == kMimeTypeHtml)
    return ReadHtml(buffer);

  error_ = kMimeTypeNotSupportedError;
  return false;
}

bool ReadDataClipboardFunction::ReadPlainText(const std::string& buffer) {
  string16 data;
  ui::Clipboard* clipboard = g_browser_process->clipboard();

  ui::Clipboard::Buffer clipboard_buffer;
  if (ConvertBufferTypeToClipboardType(buffer, &clipboard_buffer)) {
    clipboard->ReadText(clipboard_buffer, &data);
  } else {
    error_ = kBufferNotSupportedError;
    return false;
  }

  DictionaryValue* callbackData = new DictionaryValue();
  callbackData->SetString(kDataProperty, data);
  result_.reset(callbackData);
  return true;
}

bool ReadDataClipboardFunction::ReadHtml(const std::string& buffer) {
  ui::Clipboard* clipboard = g_browser_process->clipboard();

  string16 data;
  std::string url;
  uint32 fragment_start = 0;
  uint32 fragment_end = 0;
  ui::Clipboard::Buffer clipboard_buffer;

  if (ConvertBufferTypeToClipboardType(buffer, &clipboard_buffer)) {
    clipboard->ReadHTML(clipboard_buffer, &data, &url, &fragment_start,
        &fragment_end);
  } else {
    error_ = kBufferNotSupportedError;
    return false;
  }

  DictionaryValue* html_data = new base::DictionaryValue();
  size_t fragment_len = static_cast<size_t>(fragment_end - fragment_start);
  html_data->SetString(kDataProperty,
                       data.substr(static_cast<size_t>(fragment_start),
                                   fragment_len));
  html_data->SetString(kUrlProperty, url);
  result_.reset(html_data);

  return true;
}

bool GetAvailableMimeTypesClipboardFunction::RunImpl() {
  if (args_->GetSize() != 1) {
    return false;
  }

  std::string buffer_arg;
  args_->GetString(0, &buffer_arg);

  ui::Clipboard::Buffer buffer;
  if (!ConvertBufferTypeToClipboardType(buffer_arg, &buffer)) {
    error_ = kBufferNotSupportedError;
    return false;
  }

  ui::Clipboard* clipboard = g_browser_process->clipboard();

  ListValue* availableMimeTypes = new ListValue();

  for (int i = 0; i < MIME_TYPE_COUNT; i++) {
    ui::Clipboard::FormatType format;
    SupportedMimeType mime_type = static_cast<SupportedMimeType>(i);
    if (ConvertMimeTypeEnumToClipboardType(mime_type, &format) &&
        clipboard->IsFormatAvailable(format, buffer)) {
      std::string mime_type_str;
      if (!MimeTypeEnumToString(mime_type, &mime_type_str))
        continue;
      availableMimeTypes->Append(new StringValue(mime_type_str));
    }
  }

  result_.reset(availableMimeTypes);
  return true;
}