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" | |
13 #include "content/renderer/renderer_clipboard_client.h" | |
12 #include "ppapi/c/pp_errors.h" | 14 #include "ppapi/c/pp_errors.h" |
13 #include "ppapi/c/private/ppb_flash_clipboard.h" | 15 #include "ppapi/c/private/ppb_flash_clipboard.h" |
14 #include "ppapi/shared_impl/var.h" | 16 #include "ppapi/shared_impl/var.h" |
15 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebClipboard .h" | 17 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebClipboard .h" |
16 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebCString.h " | 18 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebCString.h " |
17 #include "third_party/WebKit/Source/WebKit/chromium/public/WebKit.h" | 19 #include "third_party/WebKit/Source/WebKit/chromium/public/WebKit.h" |
18 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebKitPlatfo rmSupport.h" | 20 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebKitPlatfo rmSupport.h" |
19 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebString.h" | 21 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebString.h" |
22 #include "webkit/glue/scoped_clipboard_writer_glue.h" | |
20 #include "webkit/plugins/ppapi/common.h" | 23 #include "webkit/plugins/ppapi/common.h" |
21 #include "webkit/plugins/ppapi/host_globals.h" | 24 #include "webkit/plugins/ppapi/host_globals.h" |
22 #include "webkit/plugins/ppapi/ppapi_plugin_instance.h" | 25 #include "webkit/plugins/ppapi/ppapi_plugin_instance.h" |
23 | 26 |
24 using ppapi::StringVar; | 27 using ppapi::StringVar; |
25 | 28 |
26 namespace webkit { | 29 namespace webkit { |
27 namespace ppapi { | 30 namespace ppapi { |
28 | 31 |
29 namespace { | 32 namespace { |
30 | 33 |
31 const size_t kMaxClipboardWriteSize = 1000000; | 34 const size_t kMaxClipboardWriteSize = 1000000; |
32 | 35 |
33 WebKit::WebClipboard::Buffer ConvertClipboardType( | 36 ui::Clipboard::Buffer ConvertClipboardType( |
34 PP_Flash_Clipboard_Type type) { | 37 PP_Flash_Clipboard_Type type) { |
35 switch (type) { | 38 switch (type) { |
36 case PP_FLASH_CLIPBOARD_TYPE_STANDARD: | 39 case PP_FLASH_CLIPBOARD_TYPE_STANDARD: |
37 return WebKit::WebClipboard::BufferStandard; | 40 return ui::Clipboard::BUFFER_STANDARD; |
41 break; | |
38 case PP_FLASH_CLIPBOARD_TYPE_SELECTION: | 42 case PP_FLASH_CLIPBOARD_TYPE_SELECTION: |
39 return WebKit::WebClipboard::BufferSelection; | 43 return ui::Clipboard::BUFFER_SELECTION; |
40 default: | 44 default: |
41 NOTREACHED(); | 45 NOTREACHED(); |
42 return WebKit::WebClipboard::BufferStandard; | 46 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 } | 47 } |
58 } | 48 } |
59 | 49 |
60 } // namespace | 50 } // namespace |
61 | 51 |
62 PPB_Flash_Clipboard_Impl::PPB_Flash_Clipboard_Impl(PluginInstance* instance) | 52 PPB_Flash_Clipboard_Impl::PPB_Flash_Clipboard_Impl(PluginInstance* instance) |
63 : instance_(instance) { | 53 : instance_(instance), |
54 client_(new RendererClipboardClient) { | |
64 } | 55 } |
65 | 56 |
66 PPB_Flash_Clipboard_Impl::~PPB_Flash_Clipboard_Impl() { | 57 PPB_Flash_Clipboard_Impl::~PPB_Flash_Clipboard_Impl() { |
67 } | 58 } |
68 | 59 |
69 ::ppapi::thunk::PPB_Flash_Clipboard_FunctionAPI* | 60 ::ppapi::thunk::PPB_Flash_Clipboard_FunctionAPI* |
70 PPB_Flash_Clipboard_Impl::AsPPB_Flash_Clipboard_FunctionAPI() { | 61 PPB_Flash_Clipboard_Impl::AsPPB_Flash_Clipboard_FunctionAPI() { |
71 return this; | 62 return this; |
72 } | 63 } |
73 | 64 |
74 PP_Bool PPB_Flash_Clipboard_Impl::IsFormatAvailable( | 65 PP_Bool PPB_Flash_Clipboard_Impl::IsFormatAvailable( |
75 PP_Instance instance, | 66 PP_Instance instance, |
76 PP_Flash_Clipboard_Type clipboard_type, | 67 PP_Flash_Clipboard_Type clipboard_type, |
77 PP_Flash_Clipboard_Format format) { | 68 PP_Flash_Clipboard_Format format) { |
78 WebKit::WebClipboard* web_clipboard = | 69 if (clipboard_type != PP_FLASH_CLIPBOARD_TYPE_STANDARD) { |
79 WebKit::webKitPlatformSupport()->clipboard(); | 70 NOTIMPLEMENTED(); |
80 if (!web_clipboard) { | |
81 NOTREACHED(); | |
82 return PP_FALSE; | 71 return PP_FALSE; |
83 } | 72 } |
84 return BoolToPPBool( | 73 |
85 web_clipboard->isFormatAvailable(ConvertClipboardFormat(format), | 74 ui::Clipboard::Buffer buffer_type = ConvertClipboardType(clipboard_type); |
86 ConvertClipboardType(clipboard_type))); | 75 switch(format) { |
76 case PP_FLASH_CLIPBOARD_FORMAT_PLAINTEXT: { | |
77 bool plain = client_->IsFormatAvailable( | |
78 ui::Clipboard::GetPlainTextFormatType(), buffer_type); | |
79 bool plainw = client_->IsFormatAvailable( | |
80 ui::Clipboard::GetPlainTextWFormatType(), buffer_type); | |
81 return BoolToPPBool(plain || plainw); | |
82 } | |
83 case PP_FLASH_CLIPBOARD_FORMAT_HTML: { | |
84 return BoolToPPBool(client_->IsFormatAvailable( | |
85 ui::Clipboard::GetHtmlFormatType(), buffer_type)); | |
86 } | |
87 case PP_FLASH_CLIPBOARD_FORMAT_INVALID: { | |
88 default: | |
89 NOTREACHED(); | |
90 return PP_FALSE; | |
91 } | |
92 } | |
87 } | 93 } |
88 | 94 |
89 PP_Var PPB_Flash_Clipboard_Impl::ReadPlainText( | 95 PP_Var PPB_Flash_Clipboard_Impl::ReadPlainText( |
90 PP_Instance instance, | 96 PP_Instance instance, |
91 PP_Flash_Clipboard_Type clipboard_type) { | 97 PP_Flash_Clipboard_Type clipboard_type) { |
92 WebKit::WebClipboard* web_clipboard = | 98 return ReadData(instance, clipboard_type, |
93 WebKit::webKitPlatformSupport()->clipboard(); | 99 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 } | 100 } |
102 | 101 |
103 int32_t PPB_Flash_Clipboard_Impl::WritePlainText( | 102 int32_t PPB_Flash_Clipboard_Impl::WritePlainText( |
104 PP_Instance instance, | 103 PP_Instance instance, |
105 PP_Flash_Clipboard_Type clipboard_type, | 104 PP_Flash_Clipboard_Type clipboard_type, |
106 const PP_Var& text) { | 105 const PP_Var& text) { |
107 StringVar* text_string = StringVar::FromPPVar(text); | 106 PP_Flash_Clipboard_Data_Item item = |
108 if (!text_string) | 107 {PP_FLASH_CLIPBOARD_FORMAT_PLAINTEXT, text}; |
109 return PP_ERROR_BADARGUMENT; | 108 return WriteData(instance, clipboard_type, 1, &item); |
109 } | |
110 | 110 |
111 if (text_string->value().length() > kMaxClipboardWriteSize) | |
112 return PP_ERROR_NOSPACE; | |
113 | 111 |
112 PP_Var PPB_Flash_Clipboard_Impl::ReadData( | |
113 PP_Instance instance, | |
114 PP_Flash_Clipboard_Type clipboard_type, | |
115 PP_Flash_Clipboard_Format format) { | |
116 if (clipboard_type != PP_FLASH_CLIPBOARD_TYPE_STANDARD) { | |
117 NOTIMPLEMENTED(); | |
118 return PP_MakeNull(); | |
119 } | |
120 | |
121 if (!IsFormatAvailable(instance, clipboard_type, format)) { | |
122 return PP_MakeNull(); | |
123 } | |
124 | |
125 ui::Clipboard::Buffer buffer_type = ConvertClipboardType(clipboard_type); | |
126 | |
127 switch(format) { | |
128 case PP_FLASH_CLIPBOARD_FORMAT_PLAINTEXT: { | |
129 if (client_->IsFormatAvailable(ui::Clipboard::GetPlainTextWFormatType(), | |
130 buffer_type)) { | |
131 string16 text; | |
132 client_->ReadText(buffer_type, &text); | |
133 if (!text.empty()) | |
134 return StringVar::StringToPPVar(UTF16ToUTF8(text)); | |
135 } | |
136 | |
137 if (client_->IsFormatAvailable(ui::Clipboard::GetPlainTextFormatType(), | |
138 buffer_type)) { | |
139 std::string text; | |
140 client_->ReadAsciiText(buffer_type, &text); | |
141 if (!text.empty()) | |
142 return StringVar::StringToPPVar(text); | |
143 } | |
144 return PP_MakeNull(); | |
145 } | |
146 case PP_FLASH_CLIPBOARD_FORMAT_HTML: { | |
147 string16 html_stdstr; | |
148 GURL gurl; | |
149 unsigned fragment_start; | |
150 unsigned fragment_end; | |
151 client_->ReadHTML(buffer_type, &html_stdstr, &gurl, | |
152 static_cast<uint32*>(&fragment_start), | |
153 static_cast<uint32*>(&fragment_end)); | |
154 return StringVar::StringToPPVar(UTF16ToUTF8(html_stdstr)); | |
155 break; | |
156 } | |
157 case PP_FLASH_CLIPBOARD_FORMAT_INVALID: | |
158 default: { | |
159 NOTREACHED(); | |
160 return PP_MakeUndefined(); | |
161 } | |
162 } | |
163 | |
164 return PP_MakeNull(); | |
165 } | |
166 | |
167 int32_t PPB_Flash_Clipboard_Impl::WriteDataItem( | |
168 const PP_Flash_Clipboard_Format format, | |
169 const PP_Var &data, | |
170 ScopedClipboardWriterGlue &scw) { | |
dcheng
2012/01/27 19:55:32
We should probably use a pointer here instead of a
| |
171 switch(format) { | |
172 case PP_FLASH_CLIPBOARD_FORMAT_PLAINTEXT: { | |
173 StringVar* text_string = StringVar::FromPPVar(data); | |
174 if (!text_string) | |
175 return PP_ERROR_BADARGUMENT; | |
176 | |
177 if (text_string->value().length() > kMaxClipboardWriteSize) | |
178 return PP_ERROR_NOSPACE; | |
179 | |
180 scw.WriteText(UTF8ToUTF16(text_string->value())); | |
181 break; | |
182 } | |
183 case PP_FLASH_CLIPBOARD_FORMAT_HTML: { | |
184 StringVar* text_string = StringVar::FromPPVar(data); | |
185 if (!text_string) | |
186 return PP_ERROR_BADARGUMENT; | |
187 | |
188 if (text_string->value().length() > kMaxClipboardWriteSize) | |
189 return PP_ERROR_NOSPACE; | |
190 | |
191 scw.WriteHTML(UTF8ToUTF16(text_string->value()), ""); | |
192 break; | |
193 } | |
194 case PP_FLASH_CLIPBOARD_FORMAT_INVALID: | |
195 default: { | |
196 NOTREACHED(); | |
197 return PP_ERROR_BADARGUMENT; | |
198 } | |
199 } | |
200 return PP_OK; | |
201 } | |
202 | |
203 int32_t PPB_Flash_Clipboard_Impl::WriteData( | |
204 PP_Instance instance, | |
205 PP_Flash_Clipboard_Type clipboard_type, | |
206 uint32_t data_item_count, | |
207 const struct PP_Flash_Clipboard_Data_Item data_items[]) { | |
114 if (clipboard_type != PP_FLASH_CLIPBOARD_TYPE_STANDARD) { | 208 if (clipboard_type != PP_FLASH_CLIPBOARD_TYPE_STANDARD) { |
115 NOTIMPLEMENTED(); | 209 NOTIMPLEMENTED(); |
116 return PP_ERROR_FAILED; | 210 return PP_ERROR_FAILED; |
117 } | 211 } |
118 | 212 ScopedClipboardWriterGlue scw(client_); |
119 WebKit::WebClipboard* web_clipboard = | 213 if (data_item_count == 0) { |
120 WebKit::webKitPlatformSupport()->clipboard(); | 214 // This is a hack to clear the clipboard. Should probably implement |
121 if (!web_clipboard) { | 215 // Clear() in the Clipboard interface and expose it through |
122 NOTREACHED(); | 216 // ClipboardClient. |
123 return PP_ERROR_FAILED; | 217 scw.WriteText(string16()); |
dcheng
2012/01/27 19:55:32
Does this work? WriteText doesn't push anything in
| |
218 return PP_OK; | |
219 } | |
220 for (uint32_t i = 0; i < data_item_count; ++i) { | |
221 int32_t res = WriteDataItem(data_items[i].format, data_items[i].data, scw); | |
222 if (res != PP_OK) { | |
223 // Need to clear the objects so nothing is written. | |
224 scw.Clear(); | |
225 return res; | |
226 } | |
124 } | 227 } |
125 | 228 |
126 web_clipboard->writePlainText( | |
127 WebKit::WebCString(text_string->value()).utf16()); | |
128 return PP_OK; | 229 return PP_OK; |
129 } | 230 } |
130 | 231 |
131 } // namespace ppapi | 232 } // namespace ppapi |
132 } // namespace webkit | 233 } // namespace webkit |
OLD | NEW |