Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(385)

Side by Side Diff: content/renderer/webclipboard_impl.cc

Issue 574273002: Rewrite clipboard write IPC handling to be easier to understand. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: . Created 6 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2013 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 "content/renderer/webclipboard_impl.h" 5 #include "content/renderer/webclipboard_impl.h"
6 6
7 #include "base/logging.h" 7 #include "base/logging.h"
8 #include "base/pickle.h"
9 #include "base/strings/string_util.h" 8 #include "base/strings/string_util.h"
10 #include "base/strings/utf_string_conversions.h" 9 #include "base/strings/utf_string_conversions.h"
11 #include "content/common/clipboard_format.h" 10 #include "content/common/clipboard_format.h"
12 #include "content/public/common/drop_data.h" 11 #include "content/public/common/drop_data.h"
13 #include "content/renderer/clipboard_utils.h" 12 #include "content/renderer/clipboard_utils.h"
14 #include "content/renderer/drop_data_builder.h" 13 #include "content/renderer/drop_data_builder.h"
15 #include "content/renderer/scoped_clipboard_writer_glue.h" 14 #include "content/renderer/renderer_clipboard_client.h"
16 #include "third_party/WebKit/public/platform/WebData.h" 15 #include "third_party/WebKit/public/platform/WebData.h"
17 #include "third_party/WebKit/public/platform/WebDragData.h" 16 #include "third_party/WebKit/public/platform/WebDragData.h"
18 #include "third_party/WebKit/public/platform/WebImage.h" 17 #include "third_party/WebKit/public/platform/WebImage.h"
19 #include "third_party/WebKit/public/platform/WebSize.h" 18 #include "third_party/WebKit/public/platform/WebSize.h"
20 #include "third_party/WebKit/public/platform/WebString.h" 19 #include "third_party/WebKit/public/platform/WebString.h"
21 #include "third_party/WebKit/public/platform/WebURL.h" 20 #include "third_party/WebKit/public/platform/WebURL.h"
22 #include "third_party/WebKit/public/platform/WebVector.h" 21 #include "third_party/WebKit/public/platform/WebVector.h"
23 #include "third_party/skia/include/core/SkBitmap.h"
24 #include "ui/base/clipboard/clipboard.h"
25 #include "ui/base/clipboard/custom_data_helper.h"
26 #include "url/gurl.h" 22 #include "url/gurl.h"
27 23
28 using blink::WebClipboard; 24 using blink::WebClipboard;
29 using blink::WebData; 25 using blink::WebData;
30 using blink::WebDragData; 26 using blink::WebDragData;
31 using blink::WebImage; 27 using blink::WebImage;
32 using blink::WebString; 28 using blink::WebString;
33 using blink::WebURL; 29 using blink::WebURL;
34 using blink::WebVector; 30 using blink::WebVector;
35 31
36 namespace content { 32 namespace content {
37 33
38 WebClipboardImpl::WebClipboardImpl(ClipboardClient* client) 34 WebClipboardImpl::WebClipboardImpl(RendererClipboardClient* client)
39 : client_(client) { 35 : client_(client) {
36 DCHECK(client);
40 } 37 }
41 38
42 WebClipboardImpl::~WebClipboardImpl() { 39 WebClipboardImpl::~WebClipboardImpl() {
43 } 40 }
44 41
45 uint64 WebClipboardImpl::sequenceNumber(Buffer buffer) { 42 uint64 WebClipboardImpl::sequenceNumber(Buffer buffer) {
46 ui::ClipboardType clipboard_type; 43 ui::ClipboardType clipboard_type;
47 if (!ConvertBufferType(buffer, &clipboard_type)) 44 if (!ConvertBufferType(buffer, &clipboard_type))
48 return 0; 45 return 0;
49 46
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after
126 ui::ClipboardType clipboard_type; 123 ui::ClipboardType clipboard_type;
127 if (!ConvertBufferType(buffer, &clipboard_type)) 124 if (!ConvertBufferType(buffer, &clipboard_type))
128 return WebString(); 125 return WebString();
129 126
130 base::string16 data; 127 base::string16 data;
131 client_->ReadCustomData(clipboard_type, type, &data); 128 client_->ReadCustomData(clipboard_type, type, &data);
132 return data; 129 return data;
133 } 130 }
134 131
135 void WebClipboardImpl::writePlainText(const WebString& plain_text) { 132 void WebClipboardImpl::writePlainText(const WebString& plain_text) {
136 ScopedClipboardWriterGlue scw(client_); 133 client_->WriteText(ui::CLIPBOARD_TYPE_COPY_PASTE, plain_text);
137 scw.WriteText(plain_text); 134 client_->CommitWrite(ui::CLIPBOARD_TYPE_COPY_PASTE);
138 } 135 }
139 136
140 void WebClipboardImpl::writeHTML( 137 void WebClipboardImpl::writeHTML(
141 const WebString& html_text, const WebURL& source_url, 138 const WebString& html_text, const WebURL& source_url,
142 const WebString& plain_text, bool write_smart_paste) { 139 const WebString& plain_text, bool write_smart_paste) {
143 ScopedClipboardWriterGlue scw(client_); 140 client_->WriteHTML(ui::CLIPBOARD_TYPE_COPY_PASTE, html_text, source_url);
144 scw.WriteHTML(html_text, source_url.spec()); 141 client_->WriteText(ui::CLIPBOARD_TYPE_COPY_PASTE, plain_text);
145 scw.WriteText(plain_text);
146 142
147 if (write_smart_paste) 143 if (write_smart_paste)
148 scw.WriteWebSmartPaste(); 144 client_->WriteSmartPasteMarker(ui::CLIPBOARD_TYPE_COPY_PASTE);
145 client_->CommitWrite(ui::CLIPBOARD_TYPE_COPY_PASTE);
149 } 146 }
150 147
151 void WebClipboardImpl::writeImage(const WebImage& image, 148 void WebClipboardImpl::writeImage(const WebImage& image,
152 const WebURL& url, 149 const WebURL& url,
153 const WebString& title) { 150 const WebString& title) {
154 ScopedClipboardWriterGlue scw(client_); 151 DCHECK(!image.isNull());
dcheng 2014/09/18 17:02:22 I'm not really sure if we need this check. It's wo
155 152 const SkBitmap& bitmap = image.getSkBitmap();
156 if (!image.isNull()) { 153 if (!client_->WriteImage(ui::CLIPBOARD_TYPE_COPY_PASTE, bitmap))
157 const SkBitmap& bitmap = image.getSkBitmap(); 154 return;
158 // WriteBitmapFromPixels expects 32-bit data.
159 DCHECK_EQ(bitmap.colorType(), kN32_SkColorType);
160
161 SkAutoLockPixels locked(bitmap);
162 void *pixels = bitmap.getPixels();
163 // TODO(piman): this should not be NULL, but it is. crbug.com/369621
164 if (!pixels)
165 return;
166 scw.WriteBitmapFromPixels(pixels, image.size());
167 }
168 155
169 if (!url.isEmpty()) { 156 if (!url.isEmpty()) {
170 scw.WriteBookmark(title, url.spec()); 157 client_->WriteBookmark(ui::CLIPBOARD_TYPE_COPY_PASTE, url, title);
171 #if !defined(OS_MACOSX) 158 #if !defined(OS_MACOSX)
172 // When writing the image, we also write the image markup so that pasting 159 // When writing the image, we also write the image markup so that pasting
173 // into rich text editors, such as Gmail, reveals the image. We also don't 160 // into rich text editors, such as Gmail, reveals the image. We also don't
174 // want to call writeText(), since some applications (WordPad) don't pick 161 // want to call writeText(), since some applications (WordPad) don't pick
175 // the image if there is also a text format on the clipboard. 162 // the image if there is also a text format on the clipboard.
176 // We also don't want to write HTML on a Mac, since Mail.app prefers to use 163 // We also don't want to write HTML on a Mac, since Mail.app prefers to use
177 // the image markup over attaching the actual image. See 164 // the image markup over attaching the actual image. See
178 // http://crbug.com/33016 for details. 165 // http://crbug.com/33016 for details.
179 scw.WriteHTML(base::UTF8ToUTF16(URLToImageMarkup(url, title)), 166 client_->WriteHTML(ui::CLIPBOARD_TYPE_COPY_PASTE,
180 std::string()); 167 base::UTF8ToUTF16(URLToImageMarkup(url, title)),
168 GURL());
181 #endif 169 #endif
182 } 170 }
171 client_->CommitWrite(ui::CLIPBOARD_TYPE_COPY_PASTE);
183 } 172 }
184 173
185 void WebClipboardImpl::writeDataObject(const WebDragData& data) { 174 void WebClipboardImpl::writeDataObject(const WebDragData& data) {
186 ScopedClipboardWriterGlue scw(client_);
187
188 const DropData& data_object = DropDataBuilder::Build(data); 175 const DropData& data_object = DropDataBuilder::Build(data);
189 // TODO(dcheng): Properly support text/uri-list here. 176 // TODO(dcheng): Properly support text/uri-list here.
177 // Avoid calling the WriteFoo functions if there is no data associated with a
178 // type. This prevents stomping on clipboard contents that might have been
179 // written by extension functions such as chrome.bookmarkManagerPrivate.copy.
190 if (!data_object.text.is_null()) 180 if (!data_object.text.is_null())
191 scw.WriteText(data_object.text.string()); 181 client_->WriteText(ui::CLIPBOARD_TYPE_COPY_PASTE,
182 data_object.text.string());
192 if (!data_object.html.is_null()) 183 if (!data_object.html.is_null())
193 scw.WriteHTML(data_object.html.string(), std::string()); 184 client_->WriteHTML(
194 // If there is no custom data, avoid calling WritePickledData. This ensures 185 ui::CLIPBOARD_TYPE_COPY_PASTE, data_object.html.string(), GURL());
195 // that ScopedClipboardWriterGlue's dtor remains a no-op if the page didn't 186 if (!data_object.custom_data.empty())
196 // modify the DataTransfer object, which is important to avoid stomping on 187 client_->WriteCustomData(ui::CLIPBOARD_TYPE_COPY_PASTE,
197 // any clipboard contents written by extension functions such as 188 data_object.custom_data);
198 // chrome.bookmarkManagerPrivate.copy. 189 client_->CommitWrite(ui::CLIPBOARD_TYPE_COPY_PASTE);
199 if (!data_object.custom_data.empty()) {
200 Pickle pickle;
201 ui::WriteCustomDataToPickle(data_object.custom_data, &pickle);
202 scw.WritePickledData(pickle, ui::Clipboard::GetWebCustomDataFormatType());
203 }
204 } 190 }
205 191
206 bool WebClipboardImpl::ConvertBufferType(Buffer buffer, 192 bool WebClipboardImpl::ConvertBufferType(Buffer buffer,
207 ui::ClipboardType* result) { 193 ui::ClipboardType* result) {
208 *result = ui::CLIPBOARD_TYPE_COPY_PASTE; 194 *result = ui::CLIPBOARD_TYPE_COPY_PASTE;
209 switch (buffer) { 195 switch (buffer) {
210 case BufferStandard: 196 case BufferStandard:
211 break; 197 break;
212 case BufferSelection: 198 case BufferSelection:
213 #if defined(USE_X11) && !defined(OS_CHROMEOS) 199 #if defined(USE_X11) && !defined(OS_CHROMEOS)
214 *result = ui::CLIPBOARD_TYPE_SELECTION; 200 *result = ui::CLIPBOARD_TYPE_SELECTION;
215 break; 201 break;
216 #else 202 #else
217 // Chrome OS and non-X11 unix builds do not support 203 // Chrome OS and non-X11 unix builds do not support
218 // the X selection clipboad. 204 // the X selection clipboad.
219 // TODO: remove the need for this case, see http://crbug.com/361753 205 // TODO: remove the need for this case, see http://crbug.com/361753
220 return false; 206 return false;
221 #endif 207 #endif
222 default: 208 default:
223 NOTREACHED(); 209 NOTREACHED();
224 return false; 210 return false;
225 } 211 }
226 return true; 212 return true;
227 } 213 }
228 214
229 } // namespace content 215 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698