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; |
38 case PP_FLASH_CLIPBOARD_TYPE_SELECTION: | 36 case PP_FLASH_CLIPBOARD_TYPE_SELECTION: |
39 return WebKit::WebClipboard::BufferSelection; | 37 return ui::Clipboard::BUFFER_SELECTION; |
40 default: | |
41 NOTREACHED(); | |
42 return WebKit::WebClipboard::BufferStandard; | |
43 } | 38 } |
44 } | 39 NOTREACHED(); |
45 | 40 return ui::Clipboard::BUFFER_STANDARD; |
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 } | |
58 } | 41 } |
59 | 42 |
60 } // namespace | 43 } // namespace |
61 | 44 |
62 PPB_Flash_Clipboard_Impl::PPB_Flash_Clipboard_Impl(PluginInstance* instance) | 45 PPB_Flash_Clipboard_Impl::PPB_Flash_Clipboard_Impl(PluginInstance* instance) |
63 : instance_(instance) { | 46 : instance_(instance), |
47 client_() { | |
48 } | |
49 | |
50 bool PPB_Flash_Clipboard_Impl::Init() { | |
51 // Initialize the ClipboardClient for writing to the clipboard. | |
52 if (!client_.get()) { | |
53 if (!instance_) | |
54 return false; | |
55 PluginDelegate* plugin_delegate = instance_->delegate(); | |
56 if (!plugin_delegate) | |
57 return false; | |
58 client_.reset(plugin_delegate->CreateClipboardClient()); | |
59 } | |
60 return true; | |
64 } | 61 } |
65 | 62 |
66 PPB_Flash_Clipboard_Impl::~PPB_Flash_Clipboard_Impl() { | 63 PPB_Flash_Clipboard_Impl::~PPB_Flash_Clipboard_Impl() { |
67 } | 64 } |
68 | 65 |
69 ::ppapi::thunk::PPB_Flash_Clipboard_FunctionAPI* | 66 ::ppapi::thunk::PPB_Flash_Clipboard_FunctionAPI* |
70 PPB_Flash_Clipboard_Impl::AsPPB_Flash_Clipboard_FunctionAPI() { | 67 PPB_Flash_Clipboard_Impl::AsPPB_Flash_Clipboard_FunctionAPI() { |
71 return this; | 68 return this; |
72 } | 69 } |
73 | 70 |
74 PP_Bool PPB_Flash_Clipboard_Impl::IsFormatAvailable( | 71 PP_Bool PPB_Flash_Clipboard_Impl::IsFormatAvailable( |
75 PP_Instance instance, | 72 PP_Instance instance, |
76 PP_Flash_Clipboard_Type clipboard_type, | 73 PP_Flash_Clipboard_Type clipboard_type, |
77 PP_Flash_Clipboard_Format format) { | 74 PP_Flash_Clipboard_Format format) { |
78 WebKit::WebClipboard* web_clipboard = | 75 if (!Init()) |
79 WebKit::webKitPlatformSupport()->clipboard(); | 76 return PP_FALSE; |
80 if (!web_clipboard) { | 77 |
81 NOTREACHED(); | 78 if (clipboard_type != PP_FLASH_CLIPBOARD_TYPE_STANDARD) { |
79 NOTIMPLEMENTED(); | |
82 return PP_FALSE; | 80 return PP_FALSE; |
83 } | 81 } |
84 return BoolToPPBool( | 82 |
85 web_clipboard->isFormatAvailable(ConvertClipboardFormat(format), | 83 ui::Clipboard::Buffer buffer_type = ConvertClipboardType(clipboard_type); |
86 ConvertClipboardType(clipboard_type))); | 84 switch (format) { |
85 case PP_FLASH_CLIPBOARD_FORMAT_PLAINTEXT: { | |
86 bool plain = client_->IsFormatAvailable( | |
87 ui::Clipboard::GetPlainTextFormatType(), buffer_type); | |
88 bool plainw = client_->IsFormatAvailable( | |
89 ui::Clipboard::GetPlainTextWFormatType(), buffer_type); | |
90 return BoolToPPBool(plain || plainw); | |
91 } | |
92 case PP_FLASH_CLIPBOARD_FORMAT_HTML: | |
93 return BoolToPPBool(client_->IsFormatAvailable( | |
94 ui::Clipboard::GetHtmlFormatType(), buffer_type)); | |
95 case PP_FLASH_CLIPBOARD_FORMAT_INVALID: | |
96 break; | |
97 } | |
98 | |
99 NOTREACHED(); | |
100 return PP_FALSE; | |
87 } | 101 } |
88 | 102 |
89 PP_Var PPB_Flash_Clipboard_Impl::ReadPlainText( | 103 PP_Var PPB_Flash_Clipboard_Impl::ReadData( |
90 PP_Instance instance, | 104 PP_Instance instance, |
91 PP_Flash_Clipboard_Type clipboard_type) { | 105 PP_Flash_Clipboard_Type clipboard_type, |
92 WebKit::WebClipboard* web_clipboard = | 106 PP_Flash_Clipboard_Format format) { |
93 WebKit::webKitPlatformSupport()->clipboard(); | 107 if (!Init()) |
94 if (!web_clipboard) { | 108 return PP_MakeNull(); |
95 NOTREACHED(); | 109 |
110 if (clipboard_type != PP_FLASH_CLIPBOARD_TYPE_STANDARD) { | |
111 NOTIMPLEMENTED(); | |
96 return PP_MakeNull(); | 112 return PP_MakeNull(); |
97 } | 113 } |
98 WebKit::WebCString s = | 114 |
99 web_clipboard->readPlainText(ConvertClipboardType(clipboard_type)).utf8(); | 115 if (!IsFormatAvailable(instance, clipboard_type, format)) { |
100 return StringVar::StringToPPVar(s); | 116 return PP_MakeNull(); |
117 } | |
118 | |
119 ui::Clipboard::Buffer buffer_type = ConvertClipboardType(clipboard_type); | |
120 | |
121 switch (format) { | |
122 case PP_FLASH_CLIPBOARD_FORMAT_PLAINTEXT: { | |
123 if (client_->IsFormatAvailable(ui::Clipboard::GetPlainTextWFormatType(), | |
124 buffer_type)) { | |
125 string16 text; | |
126 client_->ReadText(buffer_type, &text); | |
127 if (!text.empty()) | |
128 return StringVar::StringToPPVar(UTF16ToUTF8(text)); | |
129 } | |
130 | |
131 if (client_->IsFormatAvailable(ui::Clipboard::GetPlainTextFormatType(), | |
132 buffer_type)) { | |
133 std::string text; | |
134 client_->ReadAsciiText(buffer_type, &text); | |
135 if (!text.empty()) | |
136 return StringVar::StringToPPVar(text); | |
137 } | |
138 | |
139 return PP_MakeNull(); | |
dmichael (off chromium)
2012/02/23 18:13:25
Does it ever make sense for the clipboard to simpl
raymes
2012/02/24 07:28:28
I think an empty clipboard should just return PP_N
dmichael (off chromium)
2012/02/24 19:35:44
Makes sense, thanks for explaining.
| |
140 } | |
141 case PP_FLASH_CLIPBOARD_FORMAT_HTML: { | |
142 string16 html_stdstr; | |
143 GURL gurl; | |
144 uint32 fragment_start; | |
145 uint32 fragment_end; | |
146 client_->ReadHTML(buffer_type, | |
147 &html_stdstr, | |
148 &gurl, | |
149 &fragment_start, | |
150 &fragment_end); | |
151 return StringVar::StringToPPVar(UTF16ToUTF8(html_stdstr)); | |
152 } | |
153 case PP_FLASH_CLIPBOARD_FORMAT_INVALID: | |
154 break; | |
155 } | |
156 | |
157 NOTREACHED(); | |
158 return PP_MakeUndefined(); | |
101 } | 159 } |
102 | 160 |
103 int32_t PPB_Flash_Clipboard_Impl::WritePlainText( | 161 int32_t PPB_Flash_Clipboard_Impl::WriteDataItem( |
162 const PP_Flash_Clipboard_Format format, | |
163 const PP_Var& data, | |
164 ScopedClipboardWriterGlue* scw) { | |
165 switch (format) { | |
166 case PP_FLASH_CLIPBOARD_FORMAT_PLAINTEXT: { | |
167 StringVar* text_string = StringVar::FromPPVar(data); | |
168 if (!text_string) | |
169 return PP_ERROR_BADARGUMENT; | |
170 | |
171 if (text_string->value().length() > kMaxClipboardWriteSize) | |
172 return PP_ERROR_NOSPACE; | |
173 | |
174 scw->WriteText(UTF8ToUTF16(text_string->value())); | |
175 return PP_OK; | |
176 } | |
177 case PP_FLASH_CLIPBOARD_FORMAT_HTML: { | |
178 StringVar* text_string = StringVar::FromPPVar(data); | |
179 if (!text_string) | |
180 return PP_ERROR_BADARGUMENT; | |
181 | |
182 if (text_string->value().length() > kMaxClipboardWriteSize) | |
183 return PP_ERROR_NOSPACE; | |
184 | |
185 scw->WriteHTML(UTF8ToUTF16(text_string->value()), ""); | |
186 return PP_OK; | |
187 } | |
188 case PP_FLASH_CLIPBOARD_FORMAT_INVALID: | |
189 break; | |
190 } | |
191 | |
192 NOTREACHED(); | |
193 return PP_ERROR_BADARGUMENT; | |
194 } | |
195 | |
196 int32_t PPB_Flash_Clipboard_Impl::WriteData( | |
104 PP_Instance instance, | 197 PP_Instance instance, |
105 PP_Flash_Clipboard_Type clipboard_type, | 198 PP_Flash_Clipboard_Type clipboard_type, |
106 const PP_Var& text) { | 199 uint32_t data_item_count, |
107 StringVar* text_string = StringVar::FromPPVar(text); | 200 const struct PP_Flash_Clipboard_Data_Item data_items[]) { |
108 if (!text_string) | 201 if (!Init()) |
109 return PP_ERROR_BADARGUMENT; | 202 return PP_ERROR_FAILED; |
110 | |
111 if (text_string->value().length() > kMaxClipboardWriteSize) | |
112 return PP_ERROR_NOSPACE; | |
113 | 203 |
114 if (clipboard_type != PP_FLASH_CLIPBOARD_TYPE_STANDARD) { | 204 if (clipboard_type != PP_FLASH_CLIPBOARD_TYPE_STANDARD) { |
115 NOTIMPLEMENTED(); | 205 NOTIMPLEMENTED(); |
116 return PP_ERROR_FAILED; | 206 return PP_ERROR_FAILED; |
117 } | 207 } |
118 | 208 |
119 WebKit::WebClipboard* web_clipboard = | 209 ScopedClipboardWriterGlue scw(client_.get()); |
120 WebKit::webKitPlatformSupport()->clipboard(); | 210 if (data_item_count == 0) { |
121 if (!web_clipboard) { | 211 // TODO(raymes): implement clear() |
122 NOTREACHED(); | 212 return PP_ERROR_NOTSUPPORTED; |
123 return PP_ERROR_FAILED; | 213 } |
214 for (uint32_t i = 0; i < data_item_count; ++i) { | |
215 int32_t res = WriteDataItem(data_items[i].format, data_items[i].data, &scw); | |
216 if (res != PP_OK) { | |
217 // Need to clear the objects so nothing is written. | |
218 scw.Reset(); | |
219 return res; | |
220 } | |
124 } | 221 } |
125 | 222 |
126 web_clipboard->writePlainText( | |
127 WebKit::WebCString(text_string->value()).utf16()); | |
128 return PP_OK; | 223 return PP_OK; |
129 } | 224 } |
130 | 225 |
131 } // namespace ppapi | 226 } // namespace ppapi |
132 } // namespace webkit | 227 } // namespace webkit |
OLD | NEW |