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/glue/webclipboard_impl.h" | 5 #include "webkit/glue/webclipboard_impl.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/string_util.h" | 8 #include "base/string_util.h" |
9 #include "base/utf_string_conversions.h" | 9 #include "base/utf_string_conversions.h" |
10 #include "googleurl/src/gurl.h" | 10 #include "googleurl/src/gurl.h" |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
55 markup.append("\""); | 55 markup.append("\""); |
56 if (!title.isEmpty()) { | 56 if (!title.isEmpty()) { |
57 markup.append(" alt=\""); | 57 markup.append(" alt=\""); |
58 markup.append(net::EscapeForHTML(UTF16ToUTF8(title))); | 58 markup.append(net::EscapeForHTML(UTF16ToUTF8(title))); |
59 markup.append("\""); | 59 markup.append("\""); |
60 } | 60 } |
61 markup.append("/>"); | 61 markup.append("/>"); |
62 return markup; | 62 return markup; |
63 } | 63 } |
64 | 64 |
| 65 WebClipboardImpl::WebClipboardImpl(ClipboardClient* client) |
| 66 : client_(client) { |
| 67 } |
| 68 |
65 WebClipboardImpl::~WebClipboardImpl() { | 69 WebClipboardImpl::~WebClipboardImpl() { |
66 } | 70 } |
67 | 71 |
68 uint64 WebClipboardImpl::getSequenceNumber() { | 72 uint64 WebClipboardImpl::getSequenceNumber() { |
69 return sequenceNumber(BufferStandard); | 73 return sequenceNumber(BufferStandard); |
70 } | 74 } |
71 | 75 |
72 uint64 WebClipboardImpl::sequenceNumber(Buffer buffer) { | 76 uint64 WebClipboardImpl::sequenceNumber(Buffer buffer) { |
73 ui::Clipboard::Buffer buffer_type; | 77 ui::Clipboard::Buffer buffer_type; |
74 if (!ConvertBufferType(buffer, &buffer_type)) | 78 if (!ConvertBufferType(buffer, &buffer_type)) |
75 return 0; | 79 return 0; |
76 | 80 |
77 return ClipboardGetSequenceNumber(buffer_type); | 81 return client_->GetSequenceNumber(buffer_type); |
78 } | 82 } |
79 | 83 |
80 bool WebClipboardImpl::isFormatAvailable(Format format, Buffer buffer) { | 84 bool WebClipboardImpl::isFormatAvailable(Format format, Buffer buffer) { |
81 ui::Clipboard::FormatType format_type; | 85 ui::Clipboard::FormatType format_type; |
82 ui::Clipboard::Buffer buffer_type; | 86 ui::Clipboard::Buffer buffer_type; |
83 | 87 |
84 if (!ConvertBufferType(buffer, &buffer_type)) | 88 if (!ConvertBufferType(buffer, &buffer_type)) |
85 return false; | 89 return false; |
86 | 90 |
87 switch (format) { | 91 switch (format) { |
88 case FormatPlainText: | 92 case FormatPlainText: |
89 return ClipboardIsFormatAvailable(ui::Clipboard::GetPlainTextFormatType(), | 93 return client_->IsFormatAvailable(ui::Clipboard::GetPlainTextFormatType(), |
90 buffer_type) || | 94 buffer_type) || |
91 ClipboardIsFormatAvailable(ui::Clipboard::GetPlainTextWFormatType(), | 95 client_->IsFormatAvailable(ui::Clipboard::GetPlainTextWFormatType(), |
92 buffer_type); | 96 buffer_type); |
93 case FormatHTML: | 97 case FormatHTML: |
94 format_type = ui::Clipboard::GetHtmlFormatType(); | 98 format_type = ui::Clipboard::GetHtmlFormatType(); |
95 break; | 99 break; |
96 case FormatSmartPaste: | 100 case FormatSmartPaste: |
97 format_type = ui::Clipboard::GetWebKitSmartPasteFormatType(); | 101 format_type = ui::Clipboard::GetWebKitSmartPasteFormatType(); |
98 break; | 102 break; |
99 case FormatBookmark: | 103 case FormatBookmark: |
100 #if defined(OS_WIN) || defined(OS_MACOSX) | 104 #if defined(OS_WIN) || defined(OS_MACOSX) |
101 format_type = ui::Clipboard::GetUrlWFormatType(); | 105 format_type = ui::Clipboard::GetUrlWFormatType(); |
102 break; | 106 break; |
103 #endif | 107 #endif |
104 default: | 108 default: |
105 NOTREACHED(); | 109 NOTREACHED(); |
106 return false; | 110 return false; |
107 } | 111 } |
108 | 112 |
109 return ClipboardIsFormatAvailable(format_type, buffer_type); | 113 return client_->IsFormatAvailable(format_type, buffer_type); |
110 } | 114 } |
111 | 115 |
112 WebVector<WebString> WebClipboardImpl::readAvailableTypes( | 116 WebVector<WebString> WebClipboardImpl::readAvailableTypes( |
113 Buffer buffer, bool* contains_filenames) { | 117 Buffer buffer, bool* contains_filenames) { |
114 ui::Clipboard::Buffer buffer_type; | 118 ui::Clipboard::Buffer buffer_type; |
115 std::vector<string16> types; | 119 std::vector<string16> types; |
116 if (ConvertBufferType(buffer, &buffer_type)) { | 120 if (ConvertBufferType(buffer, &buffer_type)) { |
117 ClipboardReadAvailableTypes(buffer_type, &types, contains_filenames); | 121 client_->ReadAvailableTypes(buffer_type, &types, contains_filenames); |
118 } | 122 } |
119 return types; | 123 return types; |
120 } | 124 } |
121 | 125 |
122 WebString WebClipboardImpl::readPlainText(Buffer buffer) { | 126 WebString WebClipboardImpl::readPlainText(Buffer buffer) { |
123 ui::Clipboard::Buffer buffer_type; | 127 ui::Clipboard::Buffer buffer_type; |
124 if (!ConvertBufferType(buffer, &buffer_type)) | 128 if (!ConvertBufferType(buffer, &buffer_type)) |
125 return WebString(); | 129 return WebString(); |
126 | 130 |
127 if (ClipboardIsFormatAvailable(ui::Clipboard::GetPlainTextWFormatType(), | 131 if (client_->IsFormatAvailable(ui::Clipboard::GetPlainTextWFormatType(), |
128 buffer_type)) { | 132 buffer_type)) { |
129 string16 text; | 133 string16 text; |
130 ClipboardReadText(buffer_type, &text); | 134 client_->ReadText(buffer_type, &text); |
131 if (!text.empty()) | 135 if (!text.empty()) |
132 return text; | 136 return text; |
133 } | 137 } |
134 | 138 |
135 if (ClipboardIsFormatAvailable(ui::Clipboard::GetPlainTextFormatType(), | 139 if (client_->IsFormatAvailable(ui::Clipboard::GetPlainTextFormatType(), |
136 buffer_type)) { | 140 buffer_type)) { |
137 std::string text; | 141 std::string text; |
138 ClipboardReadAsciiText(buffer_type, &text); | 142 client_->ReadAsciiText(buffer_type, &text); |
139 if (!text.empty()) | 143 if (!text.empty()) |
140 return ASCIIToUTF16(text); | 144 return ASCIIToUTF16(text); |
141 } | 145 } |
142 | 146 |
143 return WebString(); | 147 return WebString(); |
144 } | 148 } |
145 | 149 |
146 WebString WebClipboardImpl::readHTML(Buffer buffer, WebURL* source_url, | 150 WebString WebClipboardImpl::readHTML(Buffer buffer, WebURL* source_url, |
147 unsigned* fragment_start, | 151 unsigned* fragment_start, |
148 unsigned* fragment_end) { | 152 unsigned* fragment_end) { |
149 ui::Clipboard::Buffer buffer_type; | 153 ui::Clipboard::Buffer buffer_type; |
150 if (!ConvertBufferType(buffer, &buffer_type)) | 154 if (!ConvertBufferType(buffer, &buffer_type)) |
151 return WebString(); | 155 return WebString(); |
152 | 156 |
153 string16 html_stdstr; | 157 string16 html_stdstr; |
154 GURL gurl; | 158 GURL gurl; |
155 ClipboardReadHTML(buffer_type, &html_stdstr, &gurl, | 159 client_->ReadHTML(buffer_type, &html_stdstr, &gurl, |
156 static_cast<uint32*>(fragment_start), | 160 static_cast<uint32*>(fragment_start), |
157 static_cast<uint32*>(fragment_end)); | 161 static_cast<uint32*>(fragment_end)); |
158 *source_url = gurl; | 162 *source_url = gurl; |
159 return html_stdstr; | 163 return html_stdstr; |
160 } | 164 } |
161 | 165 |
162 WebData WebClipboardImpl::readImage(Buffer buffer) { | 166 WebData WebClipboardImpl::readImage(Buffer buffer) { |
163 ui::Clipboard::Buffer buffer_type; | 167 ui::Clipboard::Buffer buffer_type; |
164 if (!ConvertBufferType(buffer, &buffer_type)) | 168 if (!ConvertBufferType(buffer, &buffer_type)) |
165 return WebData(); | 169 return WebData(); |
166 | 170 |
167 std::string png_data; | 171 std::string png_data; |
168 ClipboardReadImage(buffer_type, &png_data); | 172 client_->ReadImage(buffer_type, &png_data); |
169 return WebData(png_data); | 173 return WebData(png_data); |
170 } | 174 } |
171 | 175 |
172 void WebClipboardImpl::writeHTML( | 176 void WebClipboardImpl::writeHTML( |
173 const WebString& html_text, const WebURL& source_url, | 177 const WebString& html_text, const WebURL& source_url, |
174 const WebString& plain_text, bool write_smart_paste) { | 178 const WebString& plain_text, bool write_smart_paste) { |
175 ScopedClipboardWriterGlue scw(ClipboardGetClipboard()); | 179 ScopedClipboardWriterGlue scw(client_); |
176 scw.WriteHTML(html_text, source_url.spec()); | 180 scw.WriteHTML(html_text, source_url.spec()); |
177 scw.WriteText(plain_text); | 181 scw.WriteText(plain_text); |
178 | 182 |
179 if (write_smart_paste) | 183 if (write_smart_paste) |
180 scw.WriteWebSmartPaste(); | 184 scw.WriteWebSmartPaste(); |
181 } | 185 } |
182 | 186 |
183 void WebClipboardImpl::writePlainText(const WebString& plain_text) { | 187 void WebClipboardImpl::writePlainText(const WebString& plain_text) { |
184 ScopedClipboardWriterGlue scw(ClipboardGetClipboard()); | 188 ScopedClipboardWriterGlue scw(client_); |
185 scw.WriteText(plain_text); | 189 scw.WriteText(plain_text); |
186 } | 190 } |
187 | 191 |
188 void WebClipboardImpl::writeURL(const WebURL& url, const WebString& title) { | 192 void WebClipboardImpl::writeURL(const WebURL& url, const WebString& title) { |
189 ScopedClipboardWriterGlue scw(ClipboardGetClipboard()); | 193 ScopedClipboardWriterGlue scw(client_); |
190 | 194 |
191 scw.WriteBookmark(title, url.spec()); | 195 scw.WriteBookmark(title, url.spec()); |
192 scw.WriteHTML(UTF8ToUTF16(URLToMarkup(url, title)), ""); | 196 scw.WriteHTML(UTF8ToUTF16(URLToMarkup(url, title)), ""); |
193 scw.WriteText(UTF8ToUTF16(std::string(url.spec()))); | 197 scw.WriteText(UTF8ToUTF16(std::string(url.spec()))); |
194 } | 198 } |
195 | 199 |
196 void WebClipboardImpl::writeImage( | 200 void WebClipboardImpl::writeImage( |
197 const WebImage& image, const WebURL& url, const WebString& title) { | 201 const WebImage& image, const WebURL& url, const WebString& title) { |
198 ScopedClipboardWriterGlue scw(ClipboardGetClipboard()); | 202 ScopedClipboardWriterGlue scw(client_); |
199 | 203 |
200 if (!image.isNull()) { | 204 if (!image.isNull()) { |
201 #if WEBKIT_USING_SKIA | 205 #if WEBKIT_USING_SKIA |
202 const SkBitmap& bitmap = image.getSkBitmap(); | 206 const SkBitmap& bitmap = image.getSkBitmap(); |
203 #elif WEBKIT_USING_CG | 207 #elif WEBKIT_USING_CG |
204 const SkBitmap& bitmap = gfx::CGImageToSkBitmap(image.getCGImageRef()); | 208 const SkBitmap& bitmap = gfx::CGImageToSkBitmap(image.getCGImageRef()); |
205 #endif | 209 #endif |
206 SkAutoLockPixels locked(bitmap); | 210 SkAutoLockPixels locked(bitmap); |
207 scw.WriteBitmapFromPixels(bitmap.getPixels(), image.size()); | 211 scw.WriteBitmapFromPixels(bitmap.getPixels(), image.size()); |
208 } | 212 } |
(...skipping 10 matching lines...) Expand all Loading... |
219 // http://crbug.com/33016 for details. | 223 // http://crbug.com/33016 for details. |
220 scw.WriteHTML(UTF8ToUTF16(URLToImageMarkup(url, title)), ""); | 224 scw.WriteHTML(UTF8ToUTF16(URLToImageMarkup(url, title)), ""); |
221 #endif | 225 #endif |
222 } | 226 } |
223 } | 227 } |
224 | 228 |
225 void WebClipboardImpl::writeDataObject(const WebDragData& data) { | 229 void WebClipboardImpl::writeDataObject(const WebDragData& data) { |
226 // TODO(dcheng): This actually results in a double clear of the clipboard. | 230 // TODO(dcheng): This actually results in a double clear of the clipboard. |
227 // Once in WebKit, and once here when the clipboard writer goes out of scope. | 231 // Once in WebKit, and once here when the clipboard writer goes out of scope. |
228 // The same is true of the other WebClipboard::write* methods. | 232 // The same is true of the other WebClipboard::write* methods. |
229 ScopedClipboardWriterGlue scw(ClipboardGetClipboard()); | 233 ScopedClipboardWriterGlue scw(client_); |
230 | 234 |
231 // TODO(dcheng): Properly support text/uri-list here. | 235 // TODO(dcheng): Properly support text/uri-list here. |
232 scw.WriteText(data.plainText()); | 236 scw.WriteText(data.plainText()); |
233 scw.WriteHTML(data.htmlText(), ""); | 237 scw.WriteHTML(data.htmlText(), ""); |
234 } | 238 } |
235 | 239 |
236 bool WebClipboardImpl::ConvertBufferType(Buffer buffer, | 240 bool WebClipboardImpl::ConvertBufferType(Buffer buffer, |
237 ui::Clipboard::Buffer* result) { | 241 ui::Clipboard::Buffer* result) { |
238 switch (buffer) { | 242 switch (buffer) { |
239 case BufferStandard: | 243 case BufferStandard: |
240 *result = ui::Clipboard::BUFFER_STANDARD; | 244 *result = ui::Clipboard::BUFFER_STANDARD; |
241 break; | 245 break; |
242 case BufferSelection: | 246 case BufferSelection: |
243 #if defined(USE_X11) && !defined(USE_AURA) | 247 #if defined(USE_X11) && !defined(USE_AURA) |
244 *result = ui::Clipboard::BUFFER_SELECTION; | 248 *result = ui::Clipboard::BUFFER_SELECTION; |
245 break; | 249 break; |
246 #endif | 250 #endif |
247 default: | 251 default: |
248 NOTREACHED(); | 252 NOTREACHED(); |
249 return false; | 253 return false; |
250 } | 254 } |
251 return true; | 255 return true; |
252 } | 256 } |
253 | 257 |
254 } // namespace webkit_glue | 258 } // namespace webkit_glue |
OLD | NEW |