summaryrefslogtreecommitdiffstats
path: root/src/core/renderer/pepper/pepper_flash_clipboard_message_filter_qt.cpp
blob: 28472794d97453794f7aa3db0700d551d12f5dd1 (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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: http://www.qt.io/licensing/
**
** This file is part of the QtWebEngine module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see http://www.qt.io/terms-conditions. For further
** information use the contact form at http://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 3 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPLv3 included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 3 requirements
** will be met: https://www.gnu.org/licenses/lgpl.html.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 2.0 or later as published by the Free
** Software Foundation and appearing in the file LICENSE.GPL included in
** the packaging of this file. Please review the following information to
** ensure the GNU General Public License version 2.0 requirements will be
** met: http://www.gnu.org/licenses/gpl-2.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/

// based on chrome/browser/renderer_host/pepper/pepper_flash_clipboard_message_filter.cc
// Copyright (c) 2012 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 "pepper_flash_clipboard_message_filter_qt.h"

#include "base/pickle.h"
#include "base/strings/utf_string_conversions.h"
#include "content/public/browser/browser_thread.h"
#include "ipc/ipc_message.h"
#include "ipc/ipc_message_macros.h"
#include "ppapi/c/pp_errors.h"
#include "ppapi/c/private/ppb_flash_clipboard.h"
#include "ppapi/host/dispatch_host_message.h"
#include "ppapi/host/host_message_context.h"
#include "ppapi/host/ppapi_host.h"
#include "ppapi/proxy/ppapi_messages.h"
#include "ppapi/proxy/resource_message_params.h"
#include "ui/base/clipboard/scoped_clipboard_writer.h"

using content::BrowserThread;

namespace QtWebEngineCore {

namespace {

const size_t kMaxClipboardWriteSize = 1000000;

ui::ClipboardType ConvertClipboardType(uint32_t type)
{
    switch (type) {
    case PP_FLASH_CLIPBOARD_TYPE_STANDARD:
        return ui::CLIPBOARD_TYPE_COPY_PASTE;
    case PP_FLASH_CLIPBOARD_TYPE_SELECTION:
        return ui::CLIPBOARD_TYPE_SELECTION;
    }
    NOTREACHED();
    return ui::CLIPBOARD_TYPE_COPY_PASTE;
}

// Functions to pack/unpack custom data from a pickle. See the header file for
// more detail on custom formats in Pepper.
// TODO(raymes): Currently pepper custom formats are stored in their own
// native format type. However we should be able to store them in the same way
// as "Web Custom" formats are. This would allow clipboard data to be shared
// between pepper applications and web applications. However currently web apps
// assume all data that is placed on the clipboard is UTF16 and pepper allows
// arbitrary data so this change would require some reworking of the chrome
// clipboard interface for custom data.
bool JumpToFormatInPickle(const base::string16& format, base::PickleIterator* iter)
{
    size_t size = 0;
    if (!iter->ReadSizeT(&size))
        return false;
    for (size_t i = 0; i < size; ++i) {
        base::string16 stored_format;
        if (!iter->ReadString16(&stored_format))
            return false;
        if (stored_format == format)
            return true;
        int skip_length;
        if (!iter->ReadLength(&skip_length))
            return false;
        if (!iter->SkipBytes(skip_length))
            return false;
    }
    return false;
}

bool IsFormatAvailableInPickle(const base::string16& format, const base::Pickle& pickle)
{
    base::PickleIterator iter(pickle);
    return JumpToFormatInPickle(format, &iter);
}

std::string ReadDataFromPickle(const base::string16& format, const base::Pickle& pickle)
{
    std::string result;
    base::PickleIterator iter(pickle);
    if (!JumpToFormatInPickle(format, &iter) || !iter.ReadString(&result))
        return std::string();
    return result;
}

bool WriteDataToPickle(const std::map<base::string16, std::string>& data, base::Pickle* pickle)
{
    pickle->WriteSizeT(data.size());
    for (std::map<base::string16, std::string>::const_iterator it = data.begin(); it != data.end(); ++it) {
        if (!pickle->WriteString16(it->first))
            return false;
        if (!pickle->WriteString(it->second))
            return false;
    }
    return true;
}

}  // namespace

PepperFlashClipboardMessageFilter::PepperFlashClipboardMessageFilter() {}

PepperFlashClipboardMessageFilter::~PepperFlashClipboardMessageFilter() {}

scoped_refptr<base::TaskRunner>
PepperFlashClipboardMessageFilter::OverrideTaskRunnerForMessage(const IPC::Message& msg)
{
    // Clipboard writes should always occur on the UI thread due to the
    // restrictions of various platform APIs. In general, the clipboard is not
    // thread-safe, so all clipboard calls should be serviced from the UI thread.
    if (msg.type() == PpapiHostMsg_FlashClipboard_WriteData::ID)
        return BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI);

    // Windows needs clipboard reads to be serviced from the IO thread because
    // these are sync IPCs which can result in deadlocks with plugins if serviced
    // from the UI thread. Note that Windows clipboard calls ARE thread-safe so it
    // is ok for reads and writes to be serviced from different threads.
#if !defined(OS_WIN)
    return BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI);
#else
    return BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO);
#endif
}

int32_t PepperFlashClipboardMessageFilter::OnResourceMessageReceived(const IPC::Message& msg,
                                                                     ppapi::host::HostMessageContext* context)
{
    PPAPI_BEGIN_MESSAGE_MAP(PepperFlashClipboardMessageFilter, msg)
        PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_FlashClipboard_RegisterCustomFormat,
                                          OnMsgRegisterCustomFormat)
        PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_FlashClipboard_IsFormatAvailable,
                                          OnMsgIsFormatAvailable)
        PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_FlashClipboard_ReadData,
                                          OnMsgReadData)
        PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_FlashClipboard_WriteData,
                                          OnMsgWriteData)
        PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_FlashClipboard_GetSequenceNumber,
                                          OnMsgGetSequenceNumber)
    PPAPI_END_MESSAGE_MAP()
    return PP_ERROR_FAILED;
}

int32_t PepperFlashClipboardMessageFilter::OnMsgRegisterCustomFormat(
    ppapi::host::HostMessageContext* host_context,
    const std::string& format_name)
{
    uint32_t format = m_custom_formats.RegisterFormat(format_name);
    if (format == PP_FLASH_CLIPBOARD_FORMAT_INVALID)
        return PP_ERROR_FAILED;
    host_context->reply_msg = PpapiPluginMsg_FlashClipboard_RegisterCustomFormatReply(format);
    return PP_OK;
}

int32_t PepperFlashClipboardMessageFilter::OnMsgIsFormatAvailable(
    ppapi::host::HostMessageContext* host_context,
    uint32_t clipboard_type,
    uint32_t format)
{
    if (clipboard_type != PP_FLASH_CLIPBOARD_TYPE_STANDARD) {
        NOTIMPLEMENTED();
        return PP_ERROR_FAILED;
    }

    ui::Clipboard* clipboard = ui::Clipboard::GetForCurrentThread();
    ui::ClipboardType type = ConvertClipboardType(clipboard_type);
    bool available = false;
    switch (format) {
    case PP_FLASH_CLIPBOARD_FORMAT_PLAINTEXT: {
        bool plain = clipboard->IsFormatAvailable(ui::Clipboard::GetPlainTextFormatType(), type);
        bool plainw = clipboard->IsFormatAvailable(ui::Clipboard::GetPlainTextWFormatType(), type);
        available = plain || plainw;
        break;
    }
    case PP_FLASH_CLIPBOARD_FORMAT_HTML:
        available = clipboard->IsFormatAvailable(ui::Clipboard::GetHtmlFormatType(), type);
        break;
    case PP_FLASH_CLIPBOARD_FORMAT_RTF:
        available = clipboard->IsFormatAvailable(ui::Clipboard::GetRtfFormatType(), type);
        break;
    case PP_FLASH_CLIPBOARD_FORMAT_INVALID:
        break;
    default:
        if (m_custom_formats.IsFormatRegistered(format)) {
            std::string format_name = m_custom_formats.GetFormatName(format);
            std::string clipboard_data;
            clipboard->ReadData(ui::Clipboard::GetPepperCustomDataFormatType(), &clipboard_data);
            base::Pickle pickle(clipboard_data.data(), clipboard_data.size());
            available = IsFormatAvailableInPickle(base::UTF8ToUTF16(format_name), pickle);
        }
        break;
    }

    return available ? PP_OK : PP_ERROR_FAILED;
}

int32_t PepperFlashClipboardMessageFilter::OnMsgReadData(
    ppapi::host::HostMessageContext* host_context,
    uint32_t clipboard_type,
    uint32_t format)
{
    if (clipboard_type != PP_FLASH_CLIPBOARD_TYPE_STANDARD) {
        NOTIMPLEMENTED();
        return PP_ERROR_FAILED;
    }

    ui::Clipboard* clipboard = ui::Clipboard::GetForCurrentThread();
    ui::ClipboardType type = ConvertClipboardType(clipboard_type);
    std::string clipboard_string;
    int32_t result = PP_ERROR_FAILED;
    switch (format) {
    case PP_FLASH_CLIPBOARD_FORMAT_PLAINTEXT: {
        if (clipboard->IsFormatAvailable(ui::Clipboard::GetPlainTextWFormatType(), type)) {
            base::string16 text;
            clipboard->ReadText(type, &text);
            if (!text.empty()) {
                result = PP_OK;
                clipboard_string = base::UTF16ToUTF8(text);
                break;
            }
        }
        // If the PlainTextW format isn't available or is empty, take the
        // ASCII text format.
        if (clipboard->IsFormatAvailable(ui::Clipboard::GetPlainTextFormatType(), type)) {
            result = PP_OK;
            clipboard->ReadAsciiText(type, &clipboard_string);
        }
        break;
    }
    case PP_FLASH_CLIPBOARD_FORMAT_HTML: {
        if (!clipboard->IsFormatAvailable(ui::Clipboard::GetHtmlFormatType(), type)) {
            break;
        }

        base::string16 html;
        std::string url;
        uint32 fragment_start;
        uint32 fragment_end;
        clipboard->ReadHTML(type, &html, &url, &fragment_start, &fragment_end);
        result = PP_OK;
        clipboard_string = base::UTF16ToUTF8(html.substr(fragment_start, fragment_end - fragment_start));
        break;
    }
    case PP_FLASH_CLIPBOARD_FORMAT_RTF: {
        if (!clipboard->IsFormatAvailable(ui::Clipboard::GetRtfFormatType(), type)) {
            break;
        }
        result = PP_OK;
        clipboard->ReadRTF(type, &clipboard_string);
        break;
    }
    case PP_FLASH_CLIPBOARD_FORMAT_INVALID:
        break;
    default:
        if (m_custom_formats.IsFormatRegistered(format)) {
            base::string16 format_name =
                base::UTF8ToUTF16(m_custom_formats.GetFormatName(format));
            std::string clipboard_data;
            clipboard->ReadData(ui::Clipboard::GetPepperCustomDataFormatType(),
                                &clipboard_data);
            base::Pickle pickle(clipboard_data.data(), clipboard_data.size());
            if (IsFormatAvailableInPickle(format_name, pickle)) {
                result = PP_OK;
                clipboard_string = ReadDataFromPickle(format_name, pickle);
            }
        }
        break;
    }

    if (result == PP_OK) {
        host_context->reply_msg = PpapiPluginMsg_FlashClipboard_ReadDataReply(clipboard_string);
    }
    return result;
}

int32_t PepperFlashClipboardMessageFilter::OnMsgWriteData(
    ppapi::host::HostMessageContext* host_context,
    uint32_t clipboard_type,
    const std::vector<uint32_t>& formats,
    const std::vector<std::string>& data)
{
    if (clipboard_type != PP_FLASH_CLIPBOARD_TYPE_STANDARD) {
        NOTIMPLEMENTED();
        return PP_ERROR_FAILED;
    }
    if (formats.size() != data.size())
        return PP_ERROR_FAILED;

    ui::Clipboard* clipboard = ui::Clipboard::GetForCurrentThread();
    ui::ClipboardType type = ConvertClipboardType(clipboard_type);
    // If no formats are passed in clear the clipboard.
    if (formats.size() == 0) {
        clipboard->Clear(type);
        return PP_OK;
    }

    ui::ScopedClipboardWriter scw(type);
    std::map<base::string16, std::string> custom_data_map;
    int32_t res = PP_OK;
    for (uint32_t i = 0; i < formats.size(); ++i) {
        if (data[i].length() > kMaxClipboardWriteSize) {
            res = PP_ERROR_NOSPACE;
            break;
        }

        switch (formats[i]) {
        case PP_FLASH_CLIPBOARD_FORMAT_PLAINTEXT:
            scw.WriteText(base::UTF8ToUTF16(data[i]));
            break;
        case PP_FLASH_CLIPBOARD_FORMAT_HTML:
            scw.WriteHTML(base::UTF8ToUTF16(data[i]), std::string());
            break;
        case PP_FLASH_CLIPBOARD_FORMAT_RTF:
            scw.WriteRTF(data[i]);
            break;
        case PP_FLASH_CLIPBOARD_FORMAT_INVALID:
            res = PP_ERROR_BADARGUMENT;
            break;
        default:
            if (m_custom_formats.IsFormatRegistered(formats[i])) {
                std::string format_name = m_custom_formats.GetFormatName(formats[i]);
                custom_data_map[base::UTF8ToUTF16(format_name)] = data[i];
            } else {
                // Invalid format.
                res = PP_ERROR_BADARGUMENT;
                break;
            }
        }

        if (res != PP_OK)
            break;
    }

    if (custom_data_map.size() > 0) {
        base::Pickle pickle;
        if (WriteDataToPickle(custom_data_map, &pickle)) {
            scw.WritePickledData(pickle, ui::Clipboard::GetPepperCustomDataFormatType());
        } else {
            res = PP_ERROR_BADARGUMENT;
        }
    }

    if (res != PP_OK) {
        // Need to clear the objects so nothing is written.
        scw.Reset();
    }

    return res;
}

int32_t PepperFlashClipboardMessageFilter::OnMsgGetSequenceNumber(
    ppapi::host::HostMessageContext* host_context,
    uint32_t clipboard_type)
{
    if (clipboard_type != PP_FLASH_CLIPBOARD_TYPE_STANDARD) {
        NOTIMPLEMENTED();
        return PP_ERROR_FAILED;
    }

    ui::Clipboard* clipboard = ui::Clipboard::GetForCurrentThread();
    ui::ClipboardType type = ConvertClipboardType(clipboard_type);
    int64_t sequence_number = clipboard->GetSequenceNumber(type);
    host_context->reply_msg = PpapiPluginMsg_FlashClipboard_GetSequenceNumberReply(sequence_number);
    return PP_OK;
}

}  // namespace QtWebEngineCore