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