cc: Prevent TransformTree::CombineTransformsBetween from scaling by 1/0
[chromium-blink-merge.git] / ppapi / proxy / flash_clipboard_resource.cc
blob28df94cddf89437c003fee3c24675493fdc96ae3
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"
15 namespace ppapi {
16 namespace proxy {
18 namespace {
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,
29 const PP_Var& var,
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);
34 if (!string_var)
35 return false;
36 *string_out = string_var->value();
37 return true;
38 } else {
39 // All other formats are expected to be array buffers.
40 ArrayBufferVar* array_buffer_var = ArrayBufferVar::FromPPVar(var);
41 if (!array_buffer_var)
42 return false;
43 *string_out = std::string(static_cast<const char*>(array_buffer_var->Map()),
44 array_buffer_var->ByteLength());
45 return true;
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);
54 } else {
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());
60 } // namespace
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() {
73 return this;
76 uint32_t FlashClipboardResource::RegisterCustomFormat(
77 PP_Instance instance,
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)
82 return format;
83 int32_t result =
84 SyncCall<PpapiPluginMsg_FlashClipboard_RegisterCustomFormatReply>(
85 BROWSER,
86 PpapiHostMsg_FlashClipboard_RegisterCustomFormat(format_name),
87 &format);
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);
91 return format;
94 PP_Bool FlashClipboardResource::IsFormatAvailable(
95 PP_Instance instance,
96 PP_Flash_Clipboard_Type clipboard_type,
97 uint32_t format) {
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;
105 return PP_FALSE;
108 PP_Var FlashClipboardResource::ReadData(
109 PP_Instance instance,
110 PP_Flash_Clipboard_Type clipboard_type,
111 uint32_t format) {
112 std::string value;
113 int32_t result =
114 SyncCall<PpapiPluginMsg_FlashClipboard_ReadDataReply>(
115 BROWSER,
116 PpapiHostMsg_FlashClipboard_ReadData(clipboard_type, format),
117 &value);
118 if (result != PP_OK)
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]);
140 std::string string;
141 if (!PPVarToClipboardString(formats[i], data_items[i], &string))
142 return PP_ERROR_BADARGUMENT;
143 data_items_vector.push_back(string);
146 Post(BROWSER,
147 PpapiHostMsg_FlashClipboard_WriteData(
148 static_cast<uint32_t>(clipboard_type),
149 formats_vector,
150 data_items_vector));
152 // Assume success, since it allows us to avoid a sync IPC.
153 return PP_OK;
156 PP_Bool FlashClipboardResource::GetSequenceNumber(
157 PP_Instance instance,
158 PP_Flash_Clipboard_Type clipboard_type,
159 uint64_t* sequence_number) {
160 int32_t result =
161 SyncCall<PpapiPluginMsg_FlashClipboard_GetSequenceNumberReply>(
162 BROWSER,
163 PpapiHostMsg_FlashClipboard_GetSequenceNumber(clipboard_type),
164 sequence_number);
165 return PP_FromBool(result == PP_OK);
168 } // namespace proxy
169 } // namespace ppapi