1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "ppapi/proxy/flash_clipboard_resource.h"
7 #include "base/numerics/safe_conversions.h"
8 #include "ipc/ipc_message.h"
9 #include "ppapi/c/pp_errors.h"
10 #include "ppapi/proxy/ppapi_messages.h"
11 #include "ppapi/shared_impl/ppapi_globals.h"
12 #include "ppapi/shared_impl/var.h"
13 #include "ppapi/shared_impl/var_tracker.h"
20 // Returns whether the given clipboard type is valid.
21 bool IsValidClipboardType(PP_Flash_Clipboard_Type type
) {
22 return type
== PP_FLASH_CLIPBOARD_TYPE_STANDARD
||
23 type
== PP_FLASH_CLIPBOARD_TYPE_SELECTION
;
26 // Convert a PP_Var to/from a string which is transmitted to the pepper host.
27 // These functions assume the format is valid.
28 bool PPVarToClipboardString(int32_t format
,
30 std::string
* string_out
) {
31 if (format
== PP_FLASH_CLIPBOARD_FORMAT_PLAINTEXT
||
32 format
== PP_FLASH_CLIPBOARD_FORMAT_HTML
) {
33 StringVar
* string_var
= StringVar::FromPPVar(var
);
36 *string_out
= string_var
->value();
39 // All other formats are expected to be array buffers.
40 ArrayBufferVar
* array_buffer_var
= ArrayBufferVar::FromPPVar(var
);
41 if (!array_buffer_var
)
43 *string_out
= std::string(static_cast<const char*>(array_buffer_var
->Map()),
44 array_buffer_var
->ByteLength());
49 PP_Var
ClipboardStringToPPVar(int32_t format
,
50 const std::string
& string
) {
51 if (format
== PP_FLASH_CLIPBOARD_FORMAT_PLAINTEXT
||
52 format
== PP_FLASH_CLIPBOARD_FORMAT_HTML
) {
53 return StringVar::StringToPPVar(string
);
55 // All other formats are expected to be array buffers.
56 return PpapiGlobals::Get()->GetVarTracker()->MakeArrayBufferPPVar(
57 base::checked_cast
<uint32_t>(string
.size()), string
.data());
62 FlashClipboardResource::FlashClipboardResource(
63 Connection connection
, PP_Instance instance
)
64 : PluginResource(connection
, instance
) {
65 SendCreate(BROWSER
, PpapiHostMsg_FlashClipboard_Create());
68 FlashClipboardResource::~FlashClipboardResource() {
71 thunk::PPB_Flash_Clipboard_API
*
72 FlashClipboardResource::AsPPB_Flash_Clipboard_API() {
76 uint32_t FlashClipboardResource::RegisterCustomFormat(
78 const char* format_name
) {
79 // Check to see if the format has already been registered.
80 uint32_t format
= clipboard_formats_
.GetFormatID(format_name
);
81 if (format
!= PP_FLASH_CLIPBOARD_FORMAT_INVALID
)
84 SyncCall
<PpapiPluginMsg_FlashClipboard_RegisterCustomFormatReply
>(
86 PpapiHostMsg_FlashClipboard_RegisterCustomFormat(format_name
),
88 if (result
!= PP_OK
|| format
== PP_FLASH_CLIPBOARD_FORMAT_INVALID
)
89 return PP_FLASH_CLIPBOARD_FORMAT_INVALID
;
90 clipboard_formats_
.SetRegisteredFormat(format_name
, format
);
94 PP_Bool
FlashClipboardResource::IsFormatAvailable(
96 PP_Flash_Clipboard_Type clipboard_type
,
98 if (IsValidClipboardType(clipboard_type
) &&
99 (FlashClipboardFormatRegistry::IsValidPredefinedFormat(format
) ||
100 clipboard_formats_
.IsFormatRegistered(format
))) {
101 int32_t result
= SyncCall
<IPC::Message
>(BROWSER
,
102 PpapiHostMsg_FlashClipboard_IsFormatAvailable(clipboard_type
, format
));
103 return result
== PP_OK
? PP_TRUE
: PP_FALSE
;
108 PP_Var
FlashClipboardResource::ReadData(
109 PP_Instance instance
,
110 PP_Flash_Clipboard_Type clipboard_type
,
114 SyncCall
<PpapiPluginMsg_FlashClipboard_ReadDataReply
>(
116 PpapiHostMsg_FlashClipboard_ReadData(clipboard_type
, format
),
119 return PP_MakeUndefined();
121 return ClipboardStringToPPVar(format
, value
);
124 int32_t FlashClipboardResource::WriteData(
125 PP_Instance instance
,
126 PP_Flash_Clipboard_Type clipboard_type
,
127 uint32_t data_item_count
,
128 const uint32_t formats
[],
129 const PP_Var data_items
[]) {
130 if (!IsValidClipboardType(clipboard_type
))
131 return PP_ERROR_BADARGUMENT
;
132 std::vector
<uint32_t> formats_vector
;
133 std::vector
<std::string
> data_items_vector
;
134 for (size_t i
= 0; i
< data_item_count
; ++i
) {
135 if (!clipboard_formats_
.IsFormatRegistered(formats
[i
]) &&
136 !FlashClipboardFormatRegistry::IsValidPredefinedFormat(formats
[i
])) {
137 return PP_ERROR_BADARGUMENT
;
139 formats_vector
.push_back(formats
[i
]);
141 if (!PPVarToClipboardString(formats
[i
], data_items
[i
], &string
))
142 return PP_ERROR_BADARGUMENT
;
143 data_items_vector
.push_back(string
);
147 PpapiHostMsg_FlashClipboard_WriteData(
148 static_cast<uint32_t>(clipboard_type
),
152 // Assume success, since it allows us to avoid a sync IPC.
156 PP_Bool
FlashClipboardResource::GetSequenceNumber(
157 PP_Instance instance
,
158 PP_Flash_Clipboard_Type clipboard_type
,
159 uint64_t* sequence_number
) {
161 SyncCall
<PpapiPluginMsg_FlashClipboard_GetSequenceNumberReply
>(
163 PpapiHostMsg_FlashClipboard_GetSequenceNumber(clipboard_type
),
165 return PP_FromBool(result
== PP_OK
);