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 "webkit/plugins/ppapi/ppb_flash_clipboard_impl.h" | |
6 | |
7 #include <algorithm> | |
8 #include <string> | |
9 | |
10 #include "base/logging.h" | |
11 #include "base/memory/ref_counted.h" | |
12 #include "base/utf_string_conversions.h" | |
13 #include "ppapi/c/pp_errors.h" | |
14 #include "ppapi/c/private/ppb_flash_clipboard.h" | |
15 #include "ppapi/shared_impl/ppapi_globals.h" | |
16 #include "ppapi/shared_impl/var.h" | |
17 #include "ppapi/shared_impl/var_tracker.h" | |
18 #include "webkit/glue/clipboard_client.h" | |
19 #include "webkit/glue/scoped_clipboard_writer_glue.h" | |
20 #include "webkit/plugins/ppapi/common.h" | |
21 #include "webkit/plugins/ppapi/host_globals.h" | |
22 #include "webkit/plugins/ppapi/ppapi_plugin_instance.h" | |
23 | |
24 using ppapi::StringVar; | |
25 | |
26 namespace webkit { | |
27 namespace ppapi { | |
28 | |
29 namespace { | |
30 | |
31 const size_t kMaxClipboardWriteSize = 1000000; | |
32 | |
33 ui::Clipboard::Buffer ConvertClipboardType( | |
34 PP_Flash_Clipboard_Type type) { | |
35 switch (type) { | |
36 case PP_FLASH_CLIPBOARD_TYPE_STANDARD: | |
37 return ui::Clipboard::BUFFER_STANDARD; | |
38 case PP_FLASH_CLIPBOARD_TYPE_SELECTION: | |
39 return ui::Clipboard::BUFFER_SELECTION; | |
40 } | |
41 NOTREACHED(); | |
42 return ui::Clipboard::BUFFER_STANDARD; | |
43 } | |
44 | |
45 } // namespace | |
46 | |
47 PPB_Flash_Clipboard_Impl::PPB_Flash_Clipboard_Impl(PluginInstance* instance) | |
48 : instance_(instance), | |
49 client_() { | |
50 } | |
51 | |
52 bool PPB_Flash_Clipboard_Impl::Init() { | |
53 // Initialize the ClipboardClient for writing to the clipboard. | |
54 if (!client_.get()) { | |
55 if (!instance_) | |
56 return false; | |
57 PluginDelegate* plugin_delegate = instance_->delegate(); | |
58 if (!plugin_delegate) | |
59 return false; | |
60 client_.reset(plugin_delegate->CreateClipboardClient()); | |
61 } | |
62 return true; | |
63 } | |
64 | |
65 PPB_Flash_Clipboard_Impl::~PPB_Flash_Clipboard_Impl() { | |
66 } | |
67 | |
68 ::ppapi::thunk::PPB_Flash_Clipboard_FunctionAPI* | |
69 PPB_Flash_Clipboard_Impl::AsPPB_Flash_Clipboard_FunctionAPI() { | |
70 return this; | |
71 } | |
72 | |
73 PP_Bool PPB_Flash_Clipboard_Impl::IsFormatAvailable( | |
74 PP_Instance instance, | |
75 PP_Flash_Clipboard_Type clipboard_type, | |
76 PP_Flash_Clipboard_Format format) { | |
77 if (!Init()) | |
78 return PP_FALSE; | |
79 | |
80 if (clipboard_type != PP_FLASH_CLIPBOARD_TYPE_STANDARD) { | |
81 NOTIMPLEMENTED(); | |
82 return PP_FALSE; | |
83 } | |
84 | |
85 ui::Clipboard::Buffer buffer_type = ConvertClipboardType(clipboard_type); | |
86 switch (format) { | |
87 case PP_FLASH_CLIPBOARD_FORMAT_PLAINTEXT: { | |
88 bool plain = client_->IsFormatAvailable( | |
89 ui::Clipboard::GetPlainTextFormatType(), buffer_type); | |
90 bool plainw = client_->IsFormatAvailable( | |
91 ui::Clipboard::GetPlainTextWFormatType(), buffer_type); | |
92 return BoolToPPBool(plain || plainw); | |
93 } | |
94 case PP_FLASH_CLIPBOARD_FORMAT_HTML: | |
95 return BoolToPPBool(client_->IsFormatAvailable( | |
96 ui::Clipboard::GetHtmlFormatType(), buffer_type)); | |
97 case PP_FLASH_CLIPBOARD_FORMAT_RTF: | |
98 return BoolToPPBool(client_->IsFormatAvailable( | |
99 ui::Clipboard::GetRtfFormatType(), buffer_type)); | |
100 case PP_FLASH_CLIPBOARD_FORMAT_INVALID: | |
101 break; | |
102 } | |
103 | |
104 return PP_FALSE; | |
105 } | |
106 | |
107 PP_Var PPB_Flash_Clipboard_Impl::ReadData( | |
108 PP_Instance instance, | |
109 PP_Flash_Clipboard_Type clipboard_type, | |
110 PP_Flash_Clipboard_Format format) { | |
111 if (!Init()) | |
112 return PP_MakeUndefined(); | |
113 | |
114 if (clipboard_type != PP_FLASH_CLIPBOARD_TYPE_STANDARD) { | |
115 NOTIMPLEMENTED(); | |
116 return PP_MakeUndefined(); | |
117 } | |
118 | |
119 if (!IsFormatAvailable(instance, clipboard_type, format)) { | |
120 return PP_MakeNull(); | |
121 } | |
122 | |
123 ui::Clipboard::Buffer buffer_type = ConvertClipboardType(clipboard_type); | |
124 | |
125 switch (format) { | |
126 case PP_FLASH_CLIPBOARD_FORMAT_PLAINTEXT: { | |
127 if (client_->IsFormatAvailable(ui::Clipboard::GetPlainTextWFormatType(), | |
128 buffer_type)) { | |
129 string16 text; | |
130 client_->ReadText(buffer_type, &text); | |
131 if (!text.empty()) | |
132 return StringVar::StringToPPVar(UTF16ToUTF8(text)); | |
133 } | |
134 | |
135 if (client_->IsFormatAvailable(ui::Clipboard::GetPlainTextFormatType(), | |
136 buffer_type)) { | |
137 std::string text; | |
138 client_->ReadAsciiText(buffer_type, &text); | |
139 if (!text.empty()) | |
140 return StringVar::StringToPPVar(text); | |
141 } | |
142 | |
143 return PP_MakeNull(); | |
144 } | |
145 case PP_FLASH_CLIPBOARD_FORMAT_HTML: { | |
146 string16 html_stdstr; | |
147 GURL gurl; | |
148 uint32 fragment_start; | |
149 uint32 fragment_end; | |
150 client_->ReadHTML(buffer_type, | |
151 &html_stdstr, | |
152 &gurl, | |
153 &fragment_start, | |
154 &fragment_end); | |
155 return StringVar::StringToPPVar(UTF16ToUTF8(html_stdstr)); | |
156 } | |
157 case PP_FLASH_CLIPBOARD_FORMAT_RTF: { | |
158 std::string result; | |
159 client_->ReadRTF(buffer_type, &result); | |
160 return ::ppapi::PpapiGlobals::Get()->GetVarTracker()-> | |
161 MakeArrayBufferPPVar(result.size(), result.data()); | |
162 } | |
163 case PP_FLASH_CLIPBOARD_FORMAT_INVALID: | |
164 break; | |
165 } | |
166 | |
167 return PP_MakeUndefined(); | |
168 } | |
169 | |
170 int32_t PPB_Flash_Clipboard_Impl::WriteDataItem( | |
171 const PP_Flash_Clipboard_Format format, | |
172 const PP_Var& data, | |
173 ScopedClipboardWriterGlue* scw) { | |
174 switch (format) { | |
175 case PP_FLASH_CLIPBOARD_FORMAT_PLAINTEXT: { | |
176 StringVar* text_string = StringVar::FromPPVar(data); | |
177 if (!text_string) | |
178 return PP_ERROR_BADARGUMENT; | |
179 | |
180 if (text_string->value().length() > kMaxClipboardWriteSize) | |
181 return PP_ERROR_NOSPACE; | |
182 | |
183 scw->WriteText(UTF8ToUTF16(text_string->value())); | |
184 return PP_OK; | |
185 } | |
186 case PP_FLASH_CLIPBOARD_FORMAT_HTML: { | |
187 StringVar* text_string = StringVar::FromPPVar(data); | |
188 if (!text_string) | |
189 return PP_ERROR_BADARGUMENT; | |
190 | |
191 if (text_string->value().length() > kMaxClipboardWriteSize) | |
192 return PP_ERROR_NOSPACE; | |
193 | |
194 scw->WriteHTML(UTF8ToUTF16(text_string->value()), ""); | |
195 return PP_OK; | |
196 } | |
197 case PP_FLASH_CLIPBOARD_FORMAT_RTF: { | |
198 ::ppapi::ArrayBufferVar* rtf_data = | |
199 ::ppapi::ArrayBufferVar::FromPPVar(data); | |
200 if (!rtf_data) | |
201 return PP_ERROR_BADARGUMENT; | |
202 | |
203 if (rtf_data->ByteLength() > kMaxClipboardWriteSize) | |
204 return PP_ERROR_NOSPACE; | |
205 | |
206 scw->WriteRTF(std::string(static_cast<char*>(rtf_data->Map()), | |
207 rtf_data->ByteLength())); | |
208 return PP_OK; | |
209 } | |
210 case PP_FLASH_CLIPBOARD_FORMAT_INVALID: | |
211 break; | |
212 } | |
213 | |
214 return PP_ERROR_BADARGUMENT; | |
215 } | |
216 | |
217 int32_t PPB_Flash_Clipboard_Impl::WriteData( | |
218 PP_Instance instance, | |
219 PP_Flash_Clipboard_Type clipboard_type, | |
220 uint32_t data_item_count, | |
221 const PP_Flash_Clipboard_Format formats[], | |
222 const PP_Var data_items[]) { | |
223 if (!Init()) | |
224 return PP_ERROR_FAILED; | |
225 | |
226 if (clipboard_type != PP_FLASH_CLIPBOARD_TYPE_STANDARD) { | |
227 NOTIMPLEMENTED(); | |
228 return PP_ERROR_FAILED; | |
229 } | |
230 | |
231 if (data_item_count == 0) { | |
232 client_->Clear(ConvertClipboardType(clipboard_type)); | |
233 return PP_OK; | |
234 } | |
235 ScopedClipboardWriterGlue scw(client_.get()); | |
236 for (uint32_t i = 0; i < data_item_count; ++i) { | |
237 int32_t res = WriteDataItem(formats[i], data_items[i], &scw); | |
238 if (res != PP_OK) { | |
239 // Need to clear the objects so nothing is written. | |
240 scw.Reset(); | |
241 return res; | |
242 } | |
243 } | |
244 | |
245 return PP_OK; | |
246 } | |
247 | |
248 } // namespace ppapi | |
249 } // namespace webkit | |
OLD | NEW |