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; | |
viettrungluu
2012/02/09 00:47:37
no 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 case PP_FLASH_CLIPBOARD_FORMAT_INVALID: | |
98 break; | |
99 } | |
100 NOTREACHED(); | |
101 return PP_FALSE; | |
87 } | 102 } |
88 | 103 |
89 PP_Var PPB_Flash_Clipboard_Impl::ReadPlainText( | 104 PP_Var PPB_Flash_Clipboard_Impl::ReadData( |
90 PP_Instance instance, | 105 PP_Instance instance, |
91 PP_Flash_Clipboard_Type clipboard_type) { | 106 PP_Flash_Clipboard_Type clipboard_type, |
92 WebKit::WebClipboard* web_clipboard = | 107 PP_Flash_Clipboard_Format format) { |
93 WebKit::webKitPlatformSupport()->clipboard(); | 108 if (!Init()) |
94 if (!web_clipboard) { | 109 return PP_MakeNull(); |
95 NOTREACHED(); | 110 |
111 if (clipboard_type != PP_FLASH_CLIPBOARD_TYPE_STANDARD) { | |
112 NOTIMPLEMENTED(); | |
96 return PP_MakeNull(); | 113 return PP_MakeNull(); |
97 } | 114 } |
98 WebKit::WebCString s = | 115 |
99 web_clipboard->readPlainText(ConvertClipboardType(clipboard_type)).utf8(); | 116 if (!IsFormatAvailable(instance, clipboard_type, format)) { |
100 return StringVar::StringToPPVar(s); | 117 return PP_MakeNull(); |
118 } | |
119 | |
120 ui::Clipboard::Buffer buffer_type = ConvertClipboardType(clipboard_type); | |
121 | |
122 switch(format) { | |
123 case PP_FLASH_CLIPBOARD_FORMAT_PLAINTEXT: { | |
124 if (client_->IsFormatAvailable(ui::Clipboard::GetPlainTextWFormatType(), | |
125 buffer_type)) { | |
126 string16 text; | |
127 client_->ReadText(buffer_type, &text); | |
128 if (!text.empty()) | |
129 return StringVar::StringToPPVar(UTF16ToUTF8(text)); | |
130 } | |
131 | |
132 if (client_->IsFormatAvailable(ui::Clipboard::GetPlainTextFormatType(), | |
133 buffer_type)) { | |
134 std::string text; | |
135 client_->ReadAsciiText(buffer_type, &text); | |
136 if (!text.empty()) | |
137 return StringVar::StringToPPVar(text); | |
138 } | |
139 | |
140 return PP_MakeNull(); | |
141 } | |
142 case PP_FLASH_CLIPBOARD_FORMAT_HTML: { | |
143 string16 html_stdstr; | |
144 GURL gurl; | |
145 unsigned fragment_start; | |
viettrungluu
2012/02/09 00:47:37
-> uint32, and get rid of the casts
also, look in
raymes
2012/02/09 17:28:22
The method comment says the following:
// Reads HT
| |
146 unsigned fragment_end; | |
147 client_->ReadHTML(buffer_type, &html_stdstr, &gurl, | |
148 static_cast<uint32*>(&fragment_start), | |
149 static_cast<uint32*>(&fragment_end)); | |
150 return StringVar::StringToPPVar(UTF16ToUTF8(html_stdstr)); | |
151 } | |
152 case PP_FLASH_CLIPBOARD_FORMAT_INVALID: | |
153 break; | |
154 } | |
155 | |
156 NOTREACHED(); | |
157 return PP_MakeUndefined(); | |
101 } | 158 } |
102 | 159 |
103 int32_t PPB_Flash_Clipboard_Impl::WritePlainText( | 160 int32_t PPB_Flash_Clipboard_Impl::WriteDataItem( |
161 const PP_Flash_Clipboard_Format format, | |
162 const PP_Var& data, | |
163 ScopedClipboardWriterGlue* scw) { | |
164 switch(format) { | |
165 case PP_FLASH_CLIPBOARD_FORMAT_PLAINTEXT: { | |
166 StringVar* text_string = StringVar::FromPPVar(data); | |
167 if (!text_string) | |
168 return PP_ERROR_BADARGUMENT; | |
169 | |
170 if (text_string->value().length() > kMaxClipboardWriteSize) | |
171 return PP_ERROR_NOSPACE; | |
172 | |
173 scw->WriteText(UTF8ToUTF16(text_string->value())); | |
174 return PP_OK; | |
175 } | |
176 case PP_FLASH_CLIPBOARD_FORMAT_HTML: { | |
177 StringVar* text_string = StringVar::FromPPVar(data); | |
178 if (!text_string) | |
179 return PP_ERROR_BADARGUMENT; | |
180 | |
181 if (text_string->value().length() > kMaxClipboardWriteSize) | |
182 return PP_ERROR_NOSPACE; | |
183 | |
184 scw->WriteHTML(UTF8ToUTF16(text_string->value()), ""); | |
185 return PP_OK; | |
186 } | |
187 case PP_FLASH_CLIPBOARD_FORMAT_INVALID: | |
188 break; | |
189 } | |
190 | |
191 NOTREACHED(); | |
192 return PP_ERROR_BADARGUMENT; | |
193 } | |
194 | |
195 int32_t PPB_Flash_Clipboard_Impl::WriteData( | |
104 PP_Instance instance, | 196 PP_Instance instance, |
105 PP_Flash_Clipboard_Type clipboard_type, | 197 PP_Flash_Clipboard_Type clipboard_type, |
106 const PP_Var& text) { | 198 uint32_t data_item_count, |
107 StringVar* text_string = StringVar::FromPPVar(text); | 199 const struct PP_Flash_Clipboard_Data_Item data_items[]) { |
108 if (!text_string) | 200 if (!Init()) |
109 return PP_ERROR_BADARGUMENT; | 201 return PP_ERROR_FAILED; |
110 | |
111 if (text_string->value().length() > kMaxClipboardWriteSize) | |
112 return PP_ERROR_NOSPACE; | |
113 | 202 |
114 if (clipboard_type != PP_FLASH_CLIPBOARD_TYPE_STANDARD) { | 203 if (clipboard_type != PP_FLASH_CLIPBOARD_TYPE_STANDARD) { |
115 NOTIMPLEMENTED(); | 204 NOTIMPLEMENTED(); |
116 return PP_ERROR_FAILED; | 205 return PP_ERROR_FAILED; |
117 } | 206 } |
118 | 207 |
119 WebKit::WebClipboard* web_clipboard = | 208 ScopedClipboardWriterGlue scw(client_.get()); |
120 WebKit::webKitPlatformSupport()->clipboard(); | 209 if (data_item_count == 0) { |
121 if (!web_clipboard) { | 210 // TODO: implement clear() |
viettrungluu
2012/02/09 00:47:37
-> TODO(raymes), to assign responsibility ;)
raymes
2012/02/09 17:28:22
haha. Done.
| |
122 NOTREACHED(); | 211 return PP_OK; |
123 return PP_ERROR_FAILED; | 212 } |
213 for (uint32_t i = 0; i < data_item_count; ++i) { | |
214 int32_t res = WriteDataItem(data_items[i].format, data_items[i].data, | |
215 &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 |