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