OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 #include "ppapi/proxy/flash_clipboard_resource.h" |
| 6 |
| 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" |
| 13 |
| 14 namespace ppapi { |
| 15 namespace proxy { |
| 16 |
| 17 namespace { |
| 18 // Convert a PP_Var to/from a string which is transmitted to the pepper host. |
| 19 // These functions assume the format is valid. |
| 20 bool PPVarToClipboardString(int32_t format, |
| 21 const PP_Var& var, |
| 22 std::string* string_out) { |
| 23 if (format == PP_FLASH_CLIPBOARD_FORMAT_PLAINTEXT || |
| 24 format == PP_FLASH_CLIPBOARD_FORMAT_HTML) { |
| 25 StringVar* string_var = StringVar::FromPPVar(var); |
| 26 if (!string_var) |
| 27 return false; |
| 28 *string_out = string_var->value(); |
| 29 return true; |
| 30 } else { |
| 31 // All other formats are expected to be array buffers. |
| 32 ArrayBufferVar* array_buffer_var = ArrayBufferVar::FromPPVar(var); |
| 33 if (!array_buffer_var) |
| 34 return false; |
| 35 *string_out = std::string(static_cast<const char*>(array_buffer_var->Map()), |
| 36 array_buffer_var->ByteLength()); |
| 37 return true; |
| 38 } |
| 39 } |
| 40 |
| 41 PP_Var ClipboardStringToPPVar(int32_t format, |
| 42 const std::string& string) { |
| 43 if (format == PP_FLASH_CLIPBOARD_FORMAT_PLAINTEXT || |
| 44 format == PP_FLASH_CLIPBOARD_FORMAT_HTML) { |
| 45 return StringVar::StringToPPVar(string); |
| 46 } else { |
| 47 // All other formats are expected to be array buffers. |
| 48 return PpapiGlobals::Get()->GetVarTracker()->MakeArrayBufferPPVar( |
| 49 string.size(), string.data()); |
| 50 } |
| 51 } |
| 52 } // namespace |
| 53 |
| 54 FlashClipboardResource::FlashClipboardResource( |
| 55 Connection connection, PP_Instance instance) |
| 56 : PluginResource(connection, instance) { |
| 57 SendCreate(RENDERER, PpapiHostMsg_FlashClipboard_Create()); |
| 58 } |
| 59 |
| 60 FlashClipboardResource::~FlashClipboardResource() { |
| 61 } |
| 62 |
| 63 thunk::PPB_Flash_Clipboard_API* |
| 64 FlashClipboardResource::AsPPB_Flash_Clipboard_API() { |
| 65 return this; |
| 66 } |
| 67 |
| 68 uint32_t FlashClipboardResource::RegisterCustomFormat( |
| 69 PP_Instance instance, |
| 70 const char* format_name) { |
| 71 // Check to see if the format has already been registered. |
| 72 uint32_t format = clipboard_formats_.GetFormatID(format_name); |
| 73 if (format != PP_FLASH_CLIPBOARD_FORMAT_INVALID) |
| 74 return format; |
| 75 int32_t result = |
| 76 SyncCall<PpapiPluginMsg_FlashClipboard_RegisterCustomFormatReply>( |
| 77 RENDERER, |
| 78 PpapiHostMsg_FlashClipboard_RegisterCustomFormat(format_name), |
| 79 &format); |
| 80 if (result != PP_OK || format == PP_FLASH_CLIPBOARD_FORMAT_INVALID) |
| 81 return PP_FLASH_CLIPBOARD_FORMAT_INVALID; |
| 82 clipboard_formats_.SetRegisteredFormat(format_name, format); |
| 83 return format; |
| 84 } |
| 85 |
| 86 PP_Bool FlashClipboardResource::IsFormatAvailable( |
| 87 PP_Instance instance, |
| 88 PP_Flash_Clipboard_Type clipboard_type, |
| 89 uint32_t format) { |
| 90 if (FlashClipboardFormatRegistry::IsValidClipboardType(clipboard_type) && |
| 91 (FlashClipboardFormatRegistry::IsValidPredefinedFormat(format) || |
| 92 clipboard_formats_.IsFormatRegistered(format))) { |
| 93 int32_t result = SyncCall<IPC::Message>(RENDERER, |
| 94 PpapiHostMsg_FlashClipboard_IsFormatAvailable(clipboard_type, format)); |
| 95 return result == PP_OK ? PP_TRUE : PP_FALSE; |
| 96 } |
| 97 return PP_FALSE; |
| 98 } |
| 99 |
| 100 PP_Var FlashClipboardResource::ReadData( |
| 101 PP_Instance instance, |
| 102 PP_Flash_Clipboard_Type clipboard_type, |
| 103 uint32_t format) { |
| 104 std::string value; |
| 105 int32_t result = |
| 106 SyncCall<PpapiPluginMsg_FlashClipboard_ReadDataReply>( |
| 107 RENDERER, |
| 108 PpapiHostMsg_FlashClipboard_ReadData(clipboard_type, format), |
| 109 &value); |
| 110 if (result != PP_OK) |
| 111 return PP_MakeUndefined(); |
| 112 |
| 113 return ClipboardStringToPPVar(format, value); |
| 114 } |
| 115 |
| 116 int32_t FlashClipboardResource::WriteData( |
| 117 PP_Instance instance, |
| 118 PP_Flash_Clipboard_Type clipboard_type, |
| 119 uint32_t data_item_count, |
| 120 const uint32_t formats[], |
| 121 const PP_Var data_items[]) { |
| 122 if (!FlashClipboardFormatRegistry::IsValidClipboardType(clipboard_type)) |
| 123 return PP_ERROR_BADARGUMENT; |
| 124 std::vector<uint32_t> formats_vector; |
| 125 std::vector<std::string> data_items_vector; |
| 126 for (size_t i = 0; i < data_item_count; ++i) { |
| 127 if (!clipboard_formats_.IsFormatRegistered(formats[i]) && |
| 128 !FlashClipboardFormatRegistry::IsValidPredefinedFormat(formats[i])) { |
| 129 return PP_ERROR_BADARGUMENT; |
| 130 } |
| 131 formats_vector.push_back(formats[i]); |
| 132 std::string string; |
| 133 if (!PPVarToClipboardString(formats[i], data_items[i], &string)) |
| 134 return PP_ERROR_BADARGUMENT; |
| 135 data_items_vector.push_back(string); |
| 136 } |
| 137 |
| 138 Post(RENDERER, |
| 139 PpapiHostMsg_FlashClipboard_WriteData( |
| 140 static_cast<uint32_t>(clipboard_type), |
| 141 formats_vector, |
| 142 data_items_vector)); |
| 143 |
| 144 // Assume success, since it allows us to avoid a sync IPC. |
| 145 return PP_OK; |
| 146 } |
| 147 |
| 148 } // namespace proxy |
| 149 } // namespace ppapi |
OLD | NEW |