Index: webkit/plugins/ppapi/ppb_flash_clipboard_impl.cc |
diff --git a/webkit/plugins/ppapi/ppb_flash_clipboard_impl.cc b/webkit/plugins/ppapi/ppb_flash_clipboard_impl.cc |
index 3f2e083d2c5895de85759a4668896be7f20fe9ed..66097b9ad858965c1973d1fad2f63ef1846ccc4b 100644 |
--- a/webkit/plugins/ppapi/ppb_flash_clipboard_impl.cc |
+++ b/webkit/plugins/ppapi/ppb_flash_clipboard_impl.cc |
@@ -9,6 +9,8 @@ |
#include "base/logging.h" |
#include "base/memory/ref_counted.h" |
+#include "base/utf_string_conversions.h" |
+#include "content/renderer/renderer_clipboard_client.h" |
#include "ppapi/c/pp_errors.h" |
#include "ppapi/c/private/ppb_flash_clipboard.h" |
#include "ppapi/shared_impl/var.h" |
@@ -17,6 +19,7 @@ |
#include "third_party/WebKit/Source/WebKit/chromium/public/WebKit.h" |
#include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebKitPlatformSupport.h" |
#include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebString.h" |
+#include "webkit/glue/scoped_clipboard_writer_glue.h" |
#include "webkit/plugins/ppapi/common.h" |
#include "webkit/plugins/ppapi/host_globals.h" |
#include "webkit/plugins/ppapi/ppapi_plugin_instance.h" |
@@ -30,37 +33,25 @@ namespace { |
const size_t kMaxClipboardWriteSize = 1000000; |
-WebKit::WebClipboard::Buffer ConvertClipboardType( |
+ui::Clipboard::Buffer ConvertClipboardType( |
PP_Flash_Clipboard_Type type) { |
switch (type) { |
case PP_FLASH_CLIPBOARD_TYPE_STANDARD: |
- return WebKit::WebClipboard::BufferStandard; |
+ return ui::Clipboard::BUFFER_STANDARD; |
+ break; |
case PP_FLASH_CLIPBOARD_TYPE_SELECTION: |
- return WebKit::WebClipboard::BufferSelection; |
+ return ui::Clipboard::BUFFER_SELECTION; |
default: |
NOTREACHED(); |
- return WebKit::WebClipboard::BufferStandard; |
- } |
-} |
- |
-WebKit::WebClipboard::Format ConvertClipboardFormat( |
- PP_Flash_Clipboard_Format format) { |
- switch (format) { |
- case PP_FLASH_CLIPBOARD_FORMAT_PLAINTEXT: |
- return WebKit::WebClipboard::FormatPlainText; |
- case PP_FLASH_CLIPBOARD_FORMAT_HTML: |
- return WebKit::WebClipboard::FormatHTML; |
- case PP_FLASH_CLIPBOARD_FORMAT_INVALID: |
- default: |
- NOTREACHED(); |
- return WebKit::WebClipboard::FormatPlainText; // Gotta return something. |
+ return ui::Clipboard::BUFFER_STANDARD; |
} |
} |
} // namespace |
PPB_Flash_Clipboard_Impl::PPB_Flash_Clipboard_Impl(PluginInstance* instance) |
- : instance_(instance) { |
+ : instance_(instance), |
+ client_(new RendererClipboardClient) { |
} |
PPB_Flash_Clipboard_Impl::~PPB_Flash_Clipboard_Impl() { |
@@ -75,56 +66,166 @@ PP_Bool PPB_Flash_Clipboard_Impl::IsFormatAvailable( |
PP_Instance instance, |
PP_Flash_Clipboard_Type clipboard_type, |
PP_Flash_Clipboard_Format format) { |
- WebKit::WebClipboard* web_clipboard = |
- WebKit::webKitPlatformSupport()->clipboard(); |
- if (!web_clipboard) { |
- NOTREACHED(); |
+ if (clipboard_type != PP_FLASH_CLIPBOARD_TYPE_STANDARD) { |
+ NOTIMPLEMENTED(); |
return PP_FALSE; |
} |
- return BoolToPPBool( |
- web_clipboard->isFormatAvailable(ConvertClipboardFormat(format), |
- ConvertClipboardType(clipboard_type))); |
+ |
+ ui::Clipboard::Buffer buffer_type = ConvertClipboardType(clipboard_type); |
+ switch(format) { |
+ case PP_FLASH_CLIPBOARD_FORMAT_PLAINTEXT: { |
+ bool plain = client_->IsFormatAvailable( |
+ ui::Clipboard::GetPlainTextFormatType(), buffer_type); |
+ bool plainw = client_->IsFormatAvailable( |
+ ui::Clipboard::GetPlainTextWFormatType(), buffer_type); |
+ return BoolToPPBool(plain || plainw); |
+ } |
+ case PP_FLASH_CLIPBOARD_FORMAT_HTML: { |
+ return BoolToPPBool(client_->IsFormatAvailable( |
+ ui::Clipboard::GetHtmlFormatType(), buffer_type)); |
+ } |
+ case PP_FLASH_CLIPBOARD_FORMAT_INVALID: { |
+ default: |
+ NOTREACHED(); |
+ return PP_FALSE; |
+ } |
+ } |
} |
PP_Var PPB_Flash_Clipboard_Impl::ReadPlainText( |
PP_Instance instance, |
PP_Flash_Clipboard_Type clipboard_type) { |
- WebKit::WebClipboard* web_clipboard = |
- WebKit::webKitPlatformSupport()->clipboard(); |
- if (!web_clipboard) { |
- NOTREACHED(); |
- return PP_MakeNull(); |
- } |
- WebKit::WebCString s = |
- web_clipboard->readPlainText(ConvertClipboardType(clipboard_type)).utf8(); |
- return StringVar::StringToPPVar(s); |
+ return ReadData(instance, clipboard_type, |
+ PP_FLASH_CLIPBOARD_FORMAT_PLAINTEXT); |
} |
int32_t PPB_Flash_Clipboard_Impl::WritePlainText( |
PP_Instance instance, |
PP_Flash_Clipboard_Type clipboard_type, |
const PP_Var& text) { |
- StringVar* text_string = StringVar::FromPPVar(text); |
- if (!text_string) |
- return PP_ERROR_BADARGUMENT; |
+ PP_Flash_Clipboard_Data_Item item = |
+ {PP_FLASH_CLIPBOARD_FORMAT_PLAINTEXT, text}; |
+ return WriteData(instance, clipboard_type, 1, &item); |
+} |
- if (text_string->value().length() > kMaxClipboardWriteSize) |
- return PP_ERROR_NOSPACE; |
+PP_Var PPB_Flash_Clipboard_Impl::ReadData( |
+ PP_Instance instance, |
+ PP_Flash_Clipboard_Type clipboard_type, |
+ PP_Flash_Clipboard_Format format) { |
if (clipboard_type != PP_FLASH_CLIPBOARD_TYPE_STANDARD) { |
NOTIMPLEMENTED(); |
- return PP_ERROR_FAILED; |
+ return PP_MakeNull(); |
} |
- WebKit::WebClipboard* web_clipboard = |
- WebKit::webKitPlatformSupport()->clipboard(); |
- if (!web_clipboard) { |
- NOTREACHED(); |
+ if (!IsFormatAvailable(instance, clipboard_type, format)) { |
+ return PP_MakeNull(); |
+ } |
+ |
+ ui::Clipboard::Buffer buffer_type = ConvertClipboardType(clipboard_type); |
+ |
+ switch(format) { |
+ case PP_FLASH_CLIPBOARD_FORMAT_PLAINTEXT: { |
+ if (client_->IsFormatAvailable(ui::Clipboard::GetPlainTextWFormatType(), |
+ buffer_type)) { |
+ string16 text; |
+ client_->ReadText(buffer_type, &text); |
+ if (!text.empty()) |
+ return StringVar::StringToPPVar(UTF16ToUTF8(text)); |
+ } |
+ |
+ if (client_->IsFormatAvailable(ui::Clipboard::GetPlainTextFormatType(), |
+ buffer_type)) { |
+ std::string text; |
+ client_->ReadAsciiText(buffer_type, &text); |
+ if (!text.empty()) |
+ return StringVar::StringToPPVar(text); |
+ } |
+ return PP_MakeNull(); |
+ } |
+ case PP_FLASH_CLIPBOARD_FORMAT_HTML: { |
+ string16 html_stdstr; |
+ GURL gurl; |
+ unsigned fragment_start; |
+ unsigned fragment_end; |
+ client_->ReadHTML(buffer_type, &html_stdstr, &gurl, |
+ static_cast<uint32*>(&fragment_start), |
+ static_cast<uint32*>(&fragment_end)); |
+ return StringVar::StringToPPVar(UTF16ToUTF8(html_stdstr)); |
+ break; |
+ } |
+ case PP_FLASH_CLIPBOARD_FORMAT_INVALID: |
+ default: { |
+ NOTREACHED(); |
+ return PP_MakeUndefined(); |
+ } |
+ } |
+ |
+ return PP_MakeNull(); |
+} |
+ |
+int32_t PPB_Flash_Clipboard_Impl::WriteDataItem( |
+ const PP_Flash_Clipboard_Format format, |
+ const PP_Var &data, |
+ ScopedClipboardWriterGlue &scw) { |
dcheng
2012/01/27 19:55:32
We should probably use a pointer here instead of a
|
+ switch(format) { |
+ case PP_FLASH_CLIPBOARD_FORMAT_PLAINTEXT: { |
+ StringVar* text_string = StringVar::FromPPVar(data); |
+ if (!text_string) |
+ return PP_ERROR_BADARGUMENT; |
+ |
+ if (text_string->value().length() > kMaxClipboardWriteSize) |
+ return PP_ERROR_NOSPACE; |
+ |
+ scw.WriteText(UTF8ToUTF16(text_string->value())); |
+ break; |
+ } |
+ case PP_FLASH_CLIPBOARD_FORMAT_HTML: { |
+ StringVar* text_string = StringVar::FromPPVar(data); |
+ if (!text_string) |
+ return PP_ERROR_BADARGUMENT; |
+ |
+ if (text_string->value().length() > kMaxClipboardWriteSize) |
+ return PP_ERROR_NOSPACE; |
+ |
+ scw.WriteHTML(UTF8ToUTF16(text_string->value()), ""); |
+ break; |
+ } |
+ case PP_FLASH_CLIPBOARD_FORMAT_INVALID: |
+ default: { |
+ NOTREACHED(); |
+ return PP_ERROR_BADARGUMENT; |
+ } |
+ } |
+ return PP_OK; |
+} |
+ |
+int32_t PPB_Flash_Clipboard_Impl::WriteData( |
+ PP_Instance instance, |
+ PP_Flash_Clipboard_Type clipboard_type, |
+ uint32_t data_item_count, |
+ const struct PP_Flash_Clipboard_Data_Item data_items[]) { |
+ if (clipboard_type != PP_FLASH_CLIPBOARD_TYPE_STANDARD) { |
+ NOTIMPLEMENTED(); |
return PP_ERROR_FAILED; |
} |
+ ScopedClipboardWriterGlue scw(client_); |
+ if (data_item_count == 0) { |
+ // This is a hack to clear the clipboard. Should probably implement |
+ // Clear() in the Clipboard interface and expose it through |
+ // ClipboardClient. |
+ scw.WriteText(string16()); |
dcheng
2012/01/27 19:55:32
Does this work? WriteText doesn't push anything in
|
+ return PP_OK; |
+ } |
+ for (uint32_t i = 0; i < data_item_count; ++i) { |
+ int32_t res = WriteDataItem(data_items[i].format, data_items[i].data, scw); |
+ if (res != PP_OK) { |
+ // Need to clear the objects so nothing is written. |
+ scw.Clear(); |
+ return res; |
+ } |
+ } |
- web_clipboard->writePlainText( |
- WebKit::WebCString(text_string->value()).utf16()); |
return PP_OK; |
} |