summaryrefslogtreecommitdiffstats
path: root/components/html_viewer/web_clipboard_impl.cc
blob: 0641a936696b990515242b6522ca1b06a801d016 (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
// Copyright 2014 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 "components/html_viewer/web_clipboard_impl.h"

#include "base/bind.h"
#include "components/html_viewer/blink_basic_type_converters.h"

using mojo::Array;
using mojo::Clipboard;
using mojo::Map;
using mojo::String;

namespace html_viewer {
namespace {

void CopyUint64(uint64_t* output, uint64_t input) {
  *output = input;
}

void CopyWebString(blink::WebString* output, const Array<uint8_t>& input) {
  // blink does not differentiate between the requested data type not existing
  // and the empty string.
  if (input.is_null()) {
    output->reset();
  } else {
    *output = blink::WebString::fromUTF8(
        reinterpret_cast<const char*>(&input.front()),
        input.size());
  }
}

void CopyURL(blink::WebURL* pageURL, const Array<uint8_t>& input) {
  if (input.is_null()) {
    *pageURL = blink::WebURL();
  } else {
    *pageURL = GURL(std::string(reinterpret_cast<const char*>(&input.front()),
                                input.size()));
  }
}

void CopyVectorString(std::vector<std::string>* output,
                      const Array<String>& input) {
  *output = input.To<std::vector<std::string> >();
}

template <typename T, typename U>
bool Contains(const std::vector<T>& v, const U& item) {
  return std::find(v.begin(), v.end(), item) != v.end();
}

const char kMimeTypeWebkitSmartPaste[] = "chromium/x-webkit-paste";

}  // namespace

WebClipboardImpl::WebClipboardImpl(mojo::ClipboardPtr clipboard)
    : clipboard_(clipboard.Pass()) {
}

WebClipboardImpl::~WebClipboardImpl() {
}

uint64_t WebClipboardImpl::sequenceNumber(Buffer buffer) {
  mojo::Clipboard::Type clipboard_type = ConvertBufferType(buffer);

  uint64_t number = 0;
  clipboard_->GetSequenceNumber(clipboard_type,
                                base::Bind(&CopyUint64, &number));

  // Force this to be synchronous.
  clipboard_.WaitForIncomingResponse();
  return number;
}

bool WebClipboardImpl::isFormatAvailable(Format format, Buffer buffer) {
  Clipboard::Type clipboard_type = ConvertBufferType(buffer);

  std::vector<std::string> types;
  clipboard_->GetAvailableMimeTypes(
      clipboard_type, base::Bind(&CopyVectorString, &types));

  // Force this to be synchronous.
  clipboard_.WaitForIncomingResponse();

  switch (format) {
    case FormatPlainText:
      return Contains(types, Clipboard::MIME_TYPE_TEXT);
    case FormatHTML:
      return Contains(types, Clipboard::MIME_TYPE_HTML);
    case FormatSmartPaste:
      return Contains(types, kMimeTypeWebkitSmartPaste);
    case FormatBookmark:
      // This might be difficult.
      return false;
  }

  return false;
}

blink::WebVector<blink::WebString> WebClipboardImpl::readAvailableTypes(
    Buffer buffer,
    bool* contains_filenames) {
  Clipboard::Type clipboard_type = ConvertBufferType(buffer);

  std::vector<std::string> types;
  clipboard_->GetAvailableMimeTypes(
      clipboard_type, base::Bind(&CopyVectorString, &types));

  // Force this to be synchronous.
  clipboard_.WaitForIncomingResponse();

  // AFAICT, every instance of setting contains_filenames is false.
  *contains_filenames = false;

  blink::WebVector<blink::WebString> output(types.size());
  for (size_t i = 0; i < types.size(); ++i) {
    output[i] = blink::WebString::fromUTF8(types[i]);
  }

  return output;
}

blink::WebString WebClipboardImpl::readPlainText(Buffer buffer) {
  Clipboard::Type type = ConvertBufferType(buffer);

  blink::WebString text;
  clipboard_->ReadMimeType(type, Clipboard::MIME_TYPE_TEXT,
                           base::Bind(&CopyWebString, &text));

  // Force this to be synchronous.
  clipboard_.WaitForIncomingResponse();

  return text;
}

blink::WebString WebClipboardImpl::readHTML(Buffer buffer,
                                            blink::WebURL* page_url,
                                            unsigned* fragment_start,
                                            unsigned* fragment_end) {
  Clipboard::Type type = ConvertBufferType(buffer);

  blink::WebString html;
  clipboard_->ReadMimeType(type, Clipboard::MIME_TYPE_HTML,
                           base::Bind(&CopyWebString, &html));
  clipboard_.WaitForIncomingResponse();

  *fragment_start = 0;
  *fragment_end = static_cast<unsigned>(html.length());

  clipboard_->ReadMimeType(type, Clipboard::MIME_TYPE_URL,
                           base::Bind(&CopyURL, page_url));
  clipboard_.WaitForIncomingResponse();

  return html;
}

blink::WebString WebClipboardImpl::readCustomData(
    Buffer buffer,
    const blink::WebString& mime_type) {
  Clipboard::Type clipboard_type = ConvertBufferType(buffer);

  blink::WebString data;
  clipboard_->ReadMimeType(
      clipboard_type, mime_type.utf8(), base::Bind(&CopyWebString, &data));

  // Force this to be synchronous.
  clipboard_.WaitForIncomingResponse();

  return data;
}

void WebClipboardImpl::writePlainText(const blink::WebString& plain_text) {
  Map<String, Array<uint8_t>> data;
  data[Clipboard::MIME_TYPE_TEXT] = Array<uint8_t>::From(plain_text);

  clipboard_->WriteClipboardData(Clipboard::TYPE_COPY_PASTE, data.Pass());
}

void WebClipboardImpl::writeHTML(const blink::WebString& html_text,
                                 const blink::WebURL& source_url,
                                 const blink::WebString& plain_text,
                                 bool writeSmartPaste) {
  Map<String, Array<uint8_t>> data;
  data[Clipboard::MIME_TYPE_TEXT] = Array<uint8_t>::From(plain_text);
  data[Clipboard::MIME_TYPE_HTML] = Array<uint8_t>::From(html_text);
  data[Clipboard::MIME_TYPE_URL] = Array<uint8_t>::From(source_url.string());

  if (writeSmartPaste)
    data[kMimeTypeWebkitSmartPaste] = Array<uint8_t>::From(blink::WebString());

  clipboard_->WriteClipboardData(Clipboard::TYPE_COPY_PASTE, data.Pass());
}

Clipboard::Type WebClipboardImpl::ConvertBufferType(Buffer buffer) {
  switch (buffer) {
    case BufferStandard:
      return Clipboard::TYPE_COPY_PASTE;
    case BufferSelection:
      return Clipboard::TYPE_SELECTION;
  }

  NOTREACHED();
  return Clipboard::TYPE_COPY_PASTE;
}

}  // namespace html_viewer