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_() { | |
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; | |
64 } | 63 } |
65 | 64 |
66 PPB_Flash_Clipboard_Impl::~PPB_Flash_Clipboard_Impl() { | 65 PPB_Flash_Clipboard_Impl::~PPB_Flash_Clipboard_Impl() { |
67 } | 66 } |
68 | 67 |
69 ::ppapi::thunk::PPB_Flash_Clipboard_FunctionAPI* | 68 ::ppapi::thunk::PPB_Flash_Clipboard_FunctionAPI* |
70 PPB_Flash_Clipboard_Impl::AsPPB_Flash_Clipboard_FunctionAPI() { | 69 PPB_Flash_Clipboard_Impl::AsPPB_Flash_Clipboard_FunctionAPI() { |
71 return this; | 70 return this; |
72 } | 71 } |
73 | 72 |
74 PP_Bool PPB_Flash_Clipboard_Impl::IsFormatAvailable( | 73 PP_Bool PPB_Flash_Clipboard_Impl::IsFormatAvailable( |
75 PP_Instance instance, | 74 PP_Instance instance, |
76 PP_Flash_Clipboard_Type clipboard_type, | 75 PP_Flash_Clipboard_Type clipboard_type, |
77 PP_Flash_Clipboard_Format format) { | 76 PP_Flash_Clipboard_Format format) { |
78 WebKit::WebClipboard* web_clipboard = | 77 if (!Init()) |
79 WebKit::webKitPlatformSupport()->clipboard(); | 78 return PP_FALSE; |
80 if (!web_clipboard) { | 79 |
81 NOTREACHED(); | 80 if (clipboard_type != PP_FLASH_CLIPBOARD_TYPE_STANDARD) { |
81 NOTIMPLEMENTED(); | |
82 return PP_FALSE; | 82 return PP_FALSE; |
83 } | 83 } |
84 return BoolToPPBool( | 84 |
85 web_clipboard->isFormatAvailable(ConvertClipboardFormat(format), | 85 ui::Clipboard::Buffer buffer_type = ConvertClipboardType(clipboard_type); |
86 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 } | |
98 case PP_FLASH_CLIPBOARD_FORMAT_INVALID: { | |
99 default: | |
viettrungluu
2012/02/08 22:25:01
eh?
I'd do a |break;| for the PP_FLASH_CLIPBOARD_F
raymes
2012/02/09 00:20:27
Done.
| |
100 NOTREACHED(); | |
101 return PP_FALSE; | |
102 } | |
103 } | |
87 } | 104 } |
88 | 105 |
89 PP_Var PPB_Flash_Clipboard_Impl::ReadPlainText( | 106 PP_Var PPB_Flash_Clipboard_Impl::ReadPlainText( |
90 PP_Instance instance, | 107 PP_Instance instance, |
91 PP_Flash_Clipboard_Type clipboard_type) { | 108 PP_Flash_Clipboard_Type clipboard_type) { |
92 WebKit::WebClipboard* web_clipboard = | 109 return ReadData(instance, clipboard_type, |
93 WebKit::webKitPlatformSupport()->clipboard(); | 110 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 } | 111 } |
102 | 112 |
103 int32_t PPB_Flash_Clipboard_Impl::WritePlainText( | 113 int32_t PPB_Flash_Clipboard_Impl::WritePlainText( |
104 PP_Instance instance, | 114 PP_Instance instance, |
105 PP_Flash_Clipboard_Type clipboard_type, | 115 PP_Flash_Clipboard_Type clipboard_type, |
106 const PP_Var& text) { | 116 const PP_Var& text) { |
107 StringVar* text_string = StringVar::FromPPVar(text); | 117 PP_Flash_Clipboard_Data_Item item = |
108 if (!text_string) | 118 {PP_FLASH_CLIPBOARD_FORMAT_PLAINTEXT, text}; |
109 return PP_ERROR_BADARGUMENT; | 119 return WriteData(instance, clipboard_type, 1, &item); |
120 } | |
110 | 121 |
111 if (text_string->value().length() > kMaxClipboardWriteSize) | 122 |
112 return PP_ERROR_NOSPACE; | 123 PP_Var PPB_Flash_Clipboard_Impl::ReadData( |
124 PP_Instance instance, | |
125 PP_Flash_Clipboard_Type clipboard_type, | |
126 PP_Flash_Clipboard_Format format) { | |
127 if (!Init()) | |
128 return PP_MakeNull(); | |
129 | |
130 if (clipboard_type != PP_FLASH_CLIPBOARD_TYPE_STANDARD) { | |
131 NOTIMPLEMENTED(); | |
132 return PP_MakeNull(); | |
133 } | |
134 | |
135 if (!IsFormatAvailable(instance, clipboard_type, format)) { | |
136 return PP_MakeNull(); | |
137 } | |
138 | |
139 ui::Clipboard::Buffer buffer_type = ConvertClipboardType(clipboard_type); | |
140 | |
141 switch(format) { | |
142 case PP_FLASH_CLIPBOARD_FORMAT_PLAINTEXT: { | |
143 if (client_->IsFormatAvailable(ui::Clipboard::GetPlainTextWFormatType(), | |
144 buffer_type)) { | |
145 string16 text; | |
146 client_->ReadText(buffer_type, &text); | |
147 if (!text.empty()) | |
148 return StringVar::StringToPPVar(UTF16ToUTF8(text)); | |
149 } | |
150 | |
151 if (client_->IsFormatAvailable(ui::Clipboard::GetPlainTextFormatType(), | |
152 buffer_type)) { | |
153 std::string text; | |
154 client_->ReadAsciiText(buffer_type, &text); | |
155 if (!text.empty()) | |
156 return StringVar::StringToPPVar(text); | |
157 } | |
158 return PP_MakeNull(); | |
159 } | |
160 case PP_FLASH_CLIPBOARD_FORMAT_HTML: { | |
161 string16 html_stdstr; | |
162 GURL gurl; | |
163 unsigned fragment_start; | |
164 unsigned fragment_end; | |
165 client_->ReadHTML(buffer_type, &html_stdstr, &gurl, | |
166 static_cast<uint32*>(&fragment_start), | |
167 static_cast<uint32*>(&fragment_end)); | |
168 return StringVar::StringToPPVar(UTF16ToUTF8(html_stdstr)); | |
169 break; | |
viettrungluu
2012/02/08 22:25:01
no break
raymes
2012/02/09 00:20:27
Done.
| |
170 } | |
171 case PP_FLASH_CLIPBOARD_FORMAT_INVALID: | |
172 default: { | |
viettrungluu
2012/02/08 22:25:01
This one makes more sense (though the braces are u
raymes
2012/02/09 00:20:27
Done. The default style was copied from the existi
| |
173 NOTREACHED(); | |
174 return PP_MakeUndefined(); | |
175 } | |
176 } | |
177 | |
178 return PP_MakeNull(); | |
179 } | |
180 | |
181 int32_t PPB_Flash_Clipboard_Impl::WriteDataItem( | |
182 const PP_Flash_Clipboard_Format format, | |
183 const PP_Var& data, | |
184 ScopedClipboardWriterGlue* scw) { | |
185 switch(format) { | |
186 case PP_FLASH_CLIPBOARD_FORMAT_PLAINTEXT: { | |
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->WriteText(UTF8ToUTF16(text_string->value())); | |
195 break; | |
196 } | |
197 case PP_FLASH_CLIPBOARD_FORMAT_HTML: { | |
198 StringVar* text_string = StringVar::FromPPVar(data); | |
199 if (!text_string) | |
200 return PP_ERROR_BADARGUMENT; | |
201 | |
202 if (text_string->value().length() > kMaxClipboardWriteSize) | |
203 return PP_ERROR_NOSPACE; | |
204 | |
205 scw->WriteHTML(UTF8ToUTF16(text_string->value()), ""); | |
206 break; | |
207 } | |
208 case PP_FLASH_CLIPBOARD_FORMAT_INVALID: | |
209 default: { | |
viettrungluu
2012/02/08 22:25:01
"
raymes
2012/02/09 00:20:27
Done.
| |
210 NOTREACHED(); | |
211 return PP_ERROR_BADARGUMENT; | |
212 } | |
213 } | |
214 return PP_OK; | |
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 struct PP_Flash_Clipboard_Data_Item data_items[]) { | |
222 if (!Init()) | |
223 return PP_ERROR_FAILED; | |
113 | 224 |
114 if (clipboard_type != PP_FLASH_CLIPBOARD_TYPE_STANDARD) { | 225 if (clipboard_type != PP_FLASH_CLIPBOARD_TYPE_STANDARD) { |
115 NOTIMPLEMENTED(); | 226 NOTIMPLEMENTED(); |
116 return PP_ERROR_FAILED; | 227 return PP_ERROR_FAILED; |
117 } | 228 } |
118 | 229 |
119 WebKit::WebClipboard* web_clipboard = | 230 ScopedClipboardWriterGlue scw(client_.get()); |
120 WebKit::webKitPlatformSupport()->clipboard(); | 231 if (data_item_count == 0) { |
121 if (!web_clipboard) { | 232 // TODO: implement clear() |
122 NOTREACHED(); | 233 return PP_OK; |
123 return PP_ERROR_FAILED; | 234 } |
235 for (uint32_t i = 0; i < data_item_count; ++i) { | |
236 int32_t res = WriteDataItem(data_items[i].format, data_items[i].data, | |
237 &scw); | |
238 if (res != PP_OK) { | |
239 // Need to clear the objects so nothing is written. | |
240 scw.Reset(); | |
241 return res; | |
242 } | |
124 } | 243 } |
125 | 244 |
126 web_clipboard->writePlainText( | |
127 WebKit::WebCString(text_string->value()).utf16()); | |
128 return PP_OK; | 245 return PP_OK; |
129 } | 246 } |
130 | 247 |
131 } // namespace ppapi | 248 } // namespace ppapi |
132 } // namespace webkit | 249 } // namespace webkit |
OLD | NEW |