OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "webkit/plugins/ppapi/ppb_flash_impl.h" | 5 #include "webkit/plugins/ppapi/ppb_flash_impl.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/message_loop.h" | 10 #include "base/message_loop.h" |
11 #include "base/time.h" | 11 #include "base/time.h" |
| 12 #include "base/utf_string_conversions.h" |
12 #include "googleurl/src/gurl.h" | 13 #include "googleurl/src/gurl.h" |
13 #include "ppapi/c/dev/ppb_font_dev.h" | 14 #include "ppapi/c/dev/ppb_font_dev.h" |
14 #include "ppapi/c/private/ppb_flash.h" | 15 #include "ppapi/c/private/ppb_flash.h" |
15 #include "ppapi/shared_impl/time_conversion.h" | 16 #include "ppapi/shared_impl/time_conversion.h" |
16 #include "ppapi/shared_impl/var.h" | 17 #include "ppapi/shared_impl/var.h" |
17 #include "ppapi/thunk/enter.h" | 18 #include "ppapi/thunk/enter.h" |
18 #include "ppapi/thunk/ppb_image_data_api.h" | 19 #include "ppapi/thunk/ppb_image_data_api.h" |
19 #include "skia/ext/platform_canvas.h" | 20 #include "skia/ext/platform_canvas.h" |
20 #include "third_party/skia/include/core/SkCanvas.h" | 21 #include "third_party/skia/include/core/SkCanvas.h" |
21 #include "third_party/skia/include/core/SkMatrix.h" | 22 #include "third_party/skia/include/core/SkMatrix.h" |
22 #include "third_party/skia/include/core/SkPaint.h" | 23 #include "third_party/skia/include/core/SkPaint.h" |
23 #include "third_party/skia/include/core/SkPoint.h" | 24 #include "third_party/skia/include/core/SkPoint.h" |
24 #include "third_party/skia/include/core/SkTemplates.h" | 25 #include "third_party/skia/include/core/SkTemplates.h" |
25 #include "third_party/skia/include/core/SkTypeface.h" | 26 #include "third_party/skia/include/core/SkTypeface.h" |
26 #include "ui/gfx/rect.h" | 27 #include "ui/gfx/rect.h" |
| 28 #include "webkit/glue/clipboard_client.h" |
| 29 #include "webkit/glue/scoped_clipboard_writer_glue.h" |
27 #include "webkit/plugins/ppapi/common.h" | 30 #include "webkit/plugins/ppapi/common.h" |
28 #include "webkit/plugins/ppapi/host_globals.h" | 31 #include "webkit/plugins/ppapi/host_globals.h" |
29 #include "webkit/plugins/ppapi/plugin_delegate.h" | 32 #include "webkit/plugins/ppapi/plugin_delegate.h" |
30 #include "webkit/plugins/ppapi/plugin_module.h" | 33 #include "webkit/plugins/ppapi/plugin_module.h" |
31 #include "webkit/plugins/ppapi/ppapi_plugin_instance.h" | 34 #include "webkit/plugins/ppapi/ppapi_plugin_instance.h" |
32 #include "webkit/plugins/ppapi/ppb_url_request_info_impl.h" | 35 #include "webkit/plugins/ppapi/ppb_url_request_info_impl.h" |
33 #include "webkit/plugins/ppapi/resource_helper.h" | 36 #include "webkit/plugins/ppapi/resource_helper.h" |
34 #include "webkit/plugins/ppapi/ppb_image_data_impl.h" | 37 #include "webkit/plugins/ppapi/ppb_image_data_impl.h" |
35 | 38 |
36 using ppapi::PPTimeToTime; | 39 using ppapi::PPTimeToTime; |
37 using ppapi::StringVar; | 40 using ppapi::StringVar; |
38 using ppapi::thunk::EnterResourceNoLock; | 41 using ppapi::thunk::EnterResourceNoLock; |
39 using ppapi::thunk::PPB_ImageData_API; | 42 using ppapi::thunk::PPB_ImageData_API; |
40 using ppapi::thunk::PPB_URLRequestInfo_API; | 43 using ppapi::thunk::PPB_URLRequestInfo_API; |
41 | 44 |
42 namespace webkit { | 45 namespace webkit { |
43 namespace ppapi { | 46 namespace ppapi { |
44 | 47 |
| 48 namespace { |
| 49 |
| 50 const size_t kMaxClipboardWriteSize = 1000000; |
| 51 |
| 52 ui::Clipboard::Buffer ConvertClipboardType( |
| 53 PP_Flash_Clipboard_Type type) { |
| 54 switch (type) { |
| 55 case PP_FLASH_CLIPBOARD_TYPE_STANDARD: |
| 56 return ui::Clipboard::BUFFER_STANDARD; |
| 57 case PP_FLASH_CLIPBOARD_TYPE_SELECTION: |
| 58 return ui::Clipboard::BUFFER_SELECTION; |
| 59 } |
| 60 NOTREACHED(); |
| 61 return ui::Clipboard::BUFFER_STANDARD; |
| 62 } |
| 63 |
| 64 } // namespace |
| 65 |
45 PPB_Flash_Impl::PPB_Flash_Impl(PluginInstance* instance) | 66 PPB_Flash_Impl::PPB_Flash_Impl(PluginInstance* instance) |
46 : instance_(instance) { | 67 : instance_(instance) { |
47 } | 68 } |
48 | 69 |
49 PPB_Flash_Impl::~PPB_Flash_Impl() { | 70 PPB_Flash_Impl::~PPB_Flash_Impl() { |
50 } | 71 } |
51 | 72 |
52 void PPB_Flash_Impl::SetInstanceAlwaysOnTop(PP_Instance instance, | 73 void PPB_Flash_Impl::SetInstanceAlwaysOnTop(PP_Instance instance, |
53 PP_Bool on_top) { | 74 PP_Bool on_top) { |
54 instance_->set_always_on_top(PP_ToBool(on_top)); | 75 instance_->set_always_on_top(PP_ToBool(on_top)); |
(...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
211 | 232 |
212 void PPB_Flash_Impl::UpdateActivity(PP_Instance pp_instance) { | 233 void PPB_Flash_Impl::UpdateActivity(PP_Instance pp_instance) { |
213 // Not supported in-process. | 234 // Not supported in-process. |
214 } | 235 } |
215 | 236 |
216 PP_Var PPB_Flash_Impl::GetDeviceID(PP_Instance pp_instance) { | 237 PP_Var PPB_Flash_Impl::GetDeviceID(PP_Instance pp_instance) { |
217 // Not supported in-process. | 238 // Not supported in-process. |
218 return PP_MakeUndefined(); | 239 return PP_MakeUndefined(); |
219 } | 240 } |
220 | 241 |
| 242 PP_Bool PPB_Flash_Impl::IsClipboardFormatAvailable( |
| 243 PP_Instance instance, |
| 244 PP_Flash_Clipboard_Type clipboard_type, |
| 245 PP_Flash_Clipboard_Format format) { |
| 246 if (!InitClipboard()) |
| 247 return PP_FALSE; |
| 248 |
| 249 if (clipboard_type != PP_FLASH_CLIPBOARD_TYPE_STANDARD) { |
| 250 NOTIMPLEMENTED(); |
| 251 return PP_FALSE; |
| 252 } |
| 253 |
| 254 ui::Clipboard::Buffer buffer_type = ConvertClipboardType(clipboard_type); |
| 255 switch (format) { |
| 256 case PP_FLASH_CLIPBOARD_FORMAT_PLAINTEXT: { |
| 257 bool plain = clipboard_client_->IsFormatAvailable( |
| 258 ui::Clipboard::GetPlainTextFormatType(), buffer_type); |
| 259 bool plainw = clipboard_client_->IsFormatAvailable( |
| 260 ui::Clipboard::GetPlainTextWFormatType(), buffer_type); |
| 261 return BoolToPPBool(plain || plainw); |
| 262 } |
| 263 case PP_FLASH_CLIPBOARD_FORMAT_HTML: |
| 264 return BoolToPPBool(clipboard_client_->IsFormatAvailable( |
| 265 ui::Clipboard::GetHtmlFormatType(), buffer_type)); |
| 266 case PP_FLASH_CLIPBOARD_FORMAT_RTF: |
| 267 return BoolToPPBool(clipboard_client_->IsFormatAvailable( |
| 268 ui::Clipboard::GetRtfFormatType(), buffer_type)); |
| 269 case PP_FLASH_CLIPBOARD_FORMAT_INVALID: |
| 270 break; |
| 271 } |
| 272 |
| 273 return PP_FALSE; |
| 274 } |
| 275 |
| 276 PP_Var PPB_Flash_Impl::ReadClipboardData( |
| 277 PP_Instance instance, |
| 278 PP_Flash_Clipboard_Type clipboard_type, |
| 279 PP_Flash_Clipboard_Format format) { |
| 280 if (!InitClipboard()) |
| 281 return PP_MakeUndefined(); |
| 282 |
| 283 if (clipboard_type != PP_FLASH_CLIPBOARD_TYPE_STANDARD) { |
| 284 NOTIMPLEMENTED(); |
| 285 return PP_MakeUndefined(); |
| 286 } |
| 287 |
| 288 if (!IsClipboardFormatAvailable(instance, clipboard_type, format)) |
| 289 return PP_MakeNull(); |
| 290 |
| 291 ui::Clipboard::Buffer buffer_type = ConvertClipboardType(clipboard_type); |
| 292 |
| 293 switch (format) { |
| 294 case PP_FLASH_CLIPBOARD_FORMAT_PLAINTEXT: { |
| 295 if (clipboard_client_->IsFormatAvailable( |
| 296 ui::Clipboard::GetPlainTextWFormatType(), buffer_type)) { |
| 297 string16 text; |
| 298 clipboard_client_->ReadText(buffer_type, &text); |
| 299 if (!text.empty()) |
| 300 return StringVar::StringToPPVar(UTF16ToUTF8(text)); |
| 301 } |
| 302 |
| 303 if (clipboard_client_->IsFormatAvailable( |
| 304 ui::Clipboard::GetPlainTextFormatType(), buffer_type)) { |
| 305 std::string text; |
| 306 clipboard_client_->ReadAsciiText(buffer_type, &text); |
| 307 if (!text.empty()) |
| 308 return StringVar::StringToPPVar(text); |
| 309 } |
| 310 |
| 311 return PP_MakeNull(); |
| 312 } |
| 313 case PP_FLASH_CLIPBOARD_FORMAT_HTML: { |
| 314 string16 html_stdstr; |
| 315 GURL gurl; |
| 316 uint32 fragment_start; |
| 317 uint32 fragment_end; |
| 318 clipboard_client_->ReadHTML(buffer_type, |
| 319 &html_stdstr, |
| 320 &gurl, |
| 321 &fragment_start, |
| 322 &fragment_end); |
| 323 return StringVar::StringToPPVar(UTF16ToUTF8(html_stdstr)); |
| 324 } |
| 325 case PP_FLASH_CLIPBOARD_FORMAT_RTF: { |
| 326 std::string result; |
| 327 clipboard_client_->ReadRTF(buffer_type, &result); |
| 328 return ::ppapi::PpapiGlobals::Get()->GetVarTracker()-> |
| 329 MakeArrayBufferPPVar(result.size(), result.data()); |
| 330 } |
| 331 case PP_FLASH_CLIPBOARD_FORMAT_INVALID: |
| 332 break; |
| 333 } |
| 334 |
| 335 return PP_MakeUndefined(); |
| 336 } |
| 337 |
| 338 int32_t PPB_Flash_Impl::WriteClipboardData( |
| 339 PP_Instance instance, |
| 340 PP_Flash_Clipboard_Type clipboard_type, |
| 341 uint32_t data_item_count, |
| 342 const PP_Flash_Clipboard_Format formats[], |
| 343 const PP_Var data_items[]) { |
| 344 if (!InitClipboard()) |
| 345 return PP_ERROR_FAILED; |
| 346 |
| 347 if (clipboard_type != PP_FLASH_CLIPBOARD_TYPE_STANDARD) { |
| 348 NOTIMPLEMENTED(); |
| 349 return PP_ERROR_FAILED; |
| 350 } |
| 351 |
| 352 if (data_item_count == 0) { |
| 353 clipboard_client_->Clear(ConvertClipboardType(clipboard_type)); |
| 354 return PP_OK; |
| 355 } |
| 356 ScopedClipboardWriterGlue scw(clipboard_client_.get()); |
| 357 for (uint32_t i = 0; i < data_item_count; ++i) { |
| 358 int32_t res = WriteClipboardDataItem(formats[i], data_items[i], &scw); |
| 359 if (res != PP_OK) { |
| 360 // Need to clear the objects so nothing is written. |
| 361 scw.Reset(); |
| 362 return res; |
| 363 } |
| 364 } |
| 365 |
| 366 return PP_OK; |
| 367 } |
| 368 |
221 PP_Bool PPB_Flash_Impl::FlashIsFullscreen(PP_Instance instance) { | 369 PP_Bool PPB_Flash_Impl::FlashIsFullscreen(PP_Instance instance) { |
222 return PP_FromBool(instance_->flash_fullscreen()); | 370 return PP_FromBool(instance_->flash_fullscreen()); |
223 } | 371 } |
224 | 372 |
225 PP_Bool PPB_Flash_Impl::FlashSetFullscreen(PP_Instance instance, | 373 PP_Bool PPB_Flash_Impl::FlashSetFullscreen(PP_Instance instance, |
226 PP_Bool fullscreen) { | 374 PP_Bool fullscreen) { |
227 instance_->FlashSetFullscreen(PP_ToBool(fullscreen), true); | 375 instance_->FlashSetFullscreen(PP_ToBool(fullscreen), true); |
228 return PP_TRUE; | 376 return PP_TRUE; |
229 } | 377 } |
230 | 378 |
231 PP_Bool PPB_Flash_Impl::FlashGetScreenSize(PP_Instance instance, | 379 PP_Bool PPB_Flash_Impl::FlashGetScreenSize(PP_Instance instance, |
232 PP_Size* size) { | 380 PP_Size* size) { |
233 return instance_->GetScreenSize(instance, size); | 381 return instance_->GetScreenSize(instance, size); |
234 } | 382 } |
235 | 383 |
| 384 bool PPB_Flash_Impl::InitClipboard() { |
| 385 // Initialize the ClipboardClient for writing to the clipboard. |
| 386 if (!clipboard_client_.get()) { |
| 387 if (!instance_) |
| 388 return false; |
| 389 PluginDelegate* plugin_delegate = instance_->delegate(); |
| 390 if (!plugin_delegate) |
| 391 return false; |
| 392 clipboard_client_.reset(plugin_delegate->CreateClipboardClient()); |
| 393 } |
| 394 return true; |
| 395 } |
| 396 |
| 397 int32_t PPB_Flash_Impl::WriteClipboardDataItem( |
| 398 const PP_Flash_Clipboard_Format format, |
| 399 const PP_Var& data, |
| 400 ScopedClipboardWriterGlue* scw) { |
| 401 switch (format) { |
| 402 case PP_FLASH_CLIPBOARD_FORMAT_PLAINTEXT: { |
| 403 StringVar* text_string = StringVar::FromPPVar(data); |
| 404 if (!text_string) |
| 405 return PP_ERROR_BADARGUMENT; |
| 406 |
| 407 if (text_string->value().length() > kMaxClipboardWriteSize) |
| 408 return PP_ERROR_NOSPACE; |
| 409 |
| 410 scw->WriteText(UTF8ToUTF16(text_string->value())); |
| 411 return PP_OK; |
| 412 } |
| 413 case PP_FLASH_CLIPBOARD_FORMAT_HTML: { |
| 414 StringVar* text_string = StringVar::FromPPVar(data); |
| 415 if (!text_string) |
| 416 return PP_ERROR_BADARGUMENT; |
| 417 |
| 418 if (text_string->value().length() > kMaxClipboardWriteSize) |
| 419 return PP_ERROR_NOSPACE; |
| 420 |
| 421 scw->WriteHTML(UTF8ToUTF16(text_string->value()), ""); |
| 422 return PP_OK; |
| 423 } |
| 424 case PP_FLASH_CLIPBOARD_FORMAT_RTF: { |
| 425 ::ppapi::ArrayBufferVar* rtf_data = |
| 426 ::ppapi::ArrayBufferVar::FromPPVar(data); |
| 427 if (!rtf_data) |
| 428 return PP_ERROR_BADARGUMENT; |
| 429 |
| 430 if (rtf_data->ByteLength() > kMaxClipboardWriteSize) |
| 431 return PP_ERROR_NOSPACE; |
| 432 |
| 433 scw->WriteRTF(std::string(static_cast<char*>(rtf_data->Map()), |
| 434 rtf_data->ByteLength())); |
| 435 return PP_OK; |
| 436 } |
| 437 case PP_FLASH_CLIPBOARD_FORMAT_INVALID: |
| 438 break; |
| 439 } |
| 440 |
| 441 return PP_ERROR_BADARGUMENT; |
| 442 } |
| 443 |
236 } // namespace ppapi | 444 } // namespace ppapi |
237 } // namespace webkit | 445 } // namespace webkit |
OLD | NEW |