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 "ipc/ipc_message.h"
8 #include "ppapi/c/pp_errors.h"
9 #include "ppapi/proxy/ppapi_messages.h"
10 #include "ppapi/shared_impl/ppapi_globals.h"
11 #include "ppapi/shared_impl/var.h"
12 #include "ppapi/shared_impl/var_tracker.h"
19 // Returns whether the given clipboard type is valid.
20 bool IsValidClipboardType(PP_Flash_Clipboard_Type type
) {
21 return type
== PP_FLASH_CLIPBOARD_TYPE_STANDARD
||
22 type
== PP_FLASH_CLIPBOARD_TYPE_SELECTION
;
25 // Convert a PP_Var to/from a string which is transmitted to the pepper host.
26 // These functions assume the format is valid.
27 bool PPVarToClipboardString(int32_t format
,
29 std::string
* string_out
) {
30 if (format
== PP_FLASH_CLIPBOARD_FORMAT_PLAINTEXT
||
31 format
== PP_FLASH_CLIPBOARD_FORMAT_HTML
) {
32 StringVar
* string_var
= StringVar::FromPPVar(var
);
35 *string_out
= string_var
->value();
38 // All other formats are expected to be array buffers.
39 ArrayBufferVar
* array_buffer_var
= ArrayBufferVar::FromPPVar(var
);
40 if (!array_buffer_var
)
42 *string_out
= std::string(static_cast<const char*>(array_buffer_var
->Map()),
43 array_buffer_var
->ByteLength());
48 PP_Var
ClipboardStringToPPVar(int32_t format
,
49 const std::string
& string
) {
50 if (format
== PP_FLASH_CLIPBOARD_FORMAT_PLAINTEXT
||
51 format
== PP_FLASH_CLIPBOARD_FORMAT_HTML
) {
52 return StringVar::StringToPPVar(string
);
54 // All other formats are expected to be array buffers.
55 return PpapiGlobals::Get()->GetVarTracker()->MakeArrayBufferPPVar(
56 string
.size(), string
.data());
61 FlashClipboardResource::FlashClipboardResource(
62 Connection connection
, PP_Instance instance
)
63 : PluginResource(connection
, instance
) {
64 SendCreate(RENDERER
, PpapiHostMsg_FlashClipboard_Create());
67 FlashClipboardResource::~FlashClipboardResource() {
70 thunk::PPB_Flash_Clipboard_API
*
71 FlashClipboardResource::AsPPB_Flash_Clipboard_API() {
75 uint32_t FlashClipboardResource::RegisterCustomFormat(
77 const char* format_name
) {
78 // Check to see if the format has already been registered.
79 uint32_t format
= clipboard_formats_
.GetFormatID(format_name
);
80 if (format
!= PP_FLASH_CLIPBOARD_FORMAT_INVALID
)
83 SyncCall
<PpapiPluginMsg_FlashClipboard_RegisterCustomFormatReply
>(
85 PpapiHostMsg_FlashClipboard_RegisterCustomFormat(format_name
),
87 if (result
!= PP_OK
|| format
== PP_FLASH_CLIPBOARD_FORMAT_INVALID
)
88 return PP_FLASH_CLIPBOARD_FORMAT_INVALID
;
89 clipboard_formats_
.SetRegisteredFormat(format_name
, format
);
93 PP_Bool
FlashClipboardResource::IsFormatAvailable(
95 PP_Flash_Clipboard_Type clipboard_type
,
97 if (IsValidClipboardType(clipboard_type
) &&
98 (FlashClipboardFormatRegistry::IsValidPredefinedFormat(format
) ||
99 clipboard_formats_
.IsFormatRegistered(format
))) {
100 int32_t result
= SyncCall
<IPC::Message
>(RENDERER
,
101 PpapiHostMsg_FlashClipboard_IsFormatAvailable(clipboard_type
, format
));
102 return result
== PP_OK
? PP_TRUE
: PP_FALSE
;
107 PP_Var
FlashClipboardResource::ReadData(
108 PP_Instance instance
,
109 PP_Flash_Clipboard_Type clipboard_type
,
113 SyncCall
<PpapiPluginMsg_FlashClipboard_ReadDataReply
>(
115 PpapiHostMsg_FlashClipboard_ReadData(clipboard_type
, format
),
118 return PP_MakeUndefined();
120 return ClipboardStringToPPVar(format
, value
);
123 int32_t FlashClipboardResource::WriteData(
124 PP_Instance instance
,
125 PP_Flash_Clipboard_Type clipboard_type
,
126 uint32_t data_item_count
,
127 const uint32_t formats
[],
128 const PP_Var data_items
[]) {
129 if (!IsValidClipboardType(clipboard_type
))
130 return PP_ERROR_BADARGUMENT
;
131 std::vector
<uint32_t> formats_vector
;
132 std::vector
<std::string
> data_items_vector
;
133 for (size_t i
= 0; i
< data_item_count
; ++i
) {
134 if (!clipboard_formats_
.IsFormatRegistered(formats
[i
]) &&
135 !FlashClipboardFormatRegistry::IsValidPredefinedFormat(formats
[i
])) {
136 return PP_ERROR_BADARGUMENT
;
138 formats_vector
.push_back(formats
[i
]);
140 if (!PPVarToClipboardString(formats
[i
], data_items
[i
], &string
))
141 return PP_ERROR_BADARGUMENT
;
142 data_items_vector
.push_back(string
);
146 PpapiHostMsg_FlashClipboard_WriteData(
147 static_cast<uint32_t>(clipboard_type
),
151 // Assume success, since it allows us to avoid a sync IPC.