OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 // This file provides the embedder's side of the Clipboard interface. | |
6 | |
7 #include "content/renderer/renderer_clipboard_delegate.h" | |
8 | |
9 #include "base/memory/shared_memory.h" | |
10 #include "base/numerics/safe_math.h" | |
11 #include "content/common/clipboard_messages.h" | |
12 #include "content/public/renderer/content_renderer_client.h" | |
13 #include "content/renderer/render_thread_impl.h" | |
14 #include "third_party/skia/include/core/SkBitmap.h" | |
15 #include "ui/base/clipboard/clipboard.h" | |
16 #include "ui/gfx/size.h" | |
17 | |
18 namespace content { | |
19 | |
20 RendererClipboardDelegate::RendererClipboardDelegate() { | |
21 } | |
22 | |
23 uint64 RendererClipboardDelegate::GetSequenceNumber(ui::ClipboardType type) { | |
24 uint64 sequence_number = 0; | |
25 RenderThreadImpl::current()->Send( | |
26 new ClipboardHostMsg_GetSequenceNumber(type, &sequence_number)); | |
27 return sequence_number; | |
28 } | |
29 | |
30 bool RendererClipboardDelegate::IsFormatAvailable( | |
31 content::ClipboardFormat format, | |
32 ui::ClipboardType type) { | |
33 bool result = false; | |
34 RenderThreadImpl::current()->Send( | |
35 new ClipboardHostMsg_IsFormatAvailable(format, type, &result)); | |
36 return result; | |
37 } | |
38 | |
39 void RendererClipboardDelegate::Clear(ui::ClipboardType type) { | |
40 RenderThreadImpl::current()->Send(new ClipboardHostMsg_Clear(type)); | |
41 } | |
42 | |
43 void RendererClipboardDelegate::ReadAvailableTypes( | |
44 ui::ClipboardType type, | |
45 std::vector<base::string16>* types, | |
46 bool* contains_filenames) { | |
47 RenderThreadImpl::current()->Send( | |
48 new ClipboardHostMsg_ReadAvailableTypes(type, types, contains_filenames)); | |
49 } | |
50 | |
51 void RendererClipboardDelegate::ReadText(ui::ClipboardType type, | |
52 base::string16* result) { | |
53 RenderThreadImpl::current()->Send( | |
54 new ClipboardHostMsg_ReadText(type, result)); | |
55 } | |
56 | |
57 void RendererClipboardDelegate::ReadHTML(ui::ClipboardType type, | |
58 base::string16* markup, | |
59 GURL* url, | |
60 uint32* fragment_start, | |
61 uint32* fragment_end) { | |
62 RenderThreadImpl::current()->Send(new ClipboardHostMsg_ReadHTML( | |
63 type, markup, url, fragment_start, fragment_end)); | |
64 } | |
65 | |
66 void RendererClipboardDelegate::ReadRTF(ui::ClipboardType type, | |
67 std::string* result) { | |
68 RenderThreadImpl::current()->Send(new ClipboardHostMsg_ReadRTF(type, result)); | |
69 } | |
70 | |
71 void RendererClipboardDelegate::ReadImage(ui::ClipboardType type, | |
72 std::string* data) { | |
73 base::SharedMemoryHandle image_handle; | |
74 uint32 image_size = 0; | |
75 RenderThreadImpl::current()->Send( | |
76 new ClipboardHostMsg_ReadImage(type, &image_handle, &image_size)); | |
77 if (base::SharedMemory::IsHandleValid(image_handle)) { | |
78 base::SharedMemory buffer(image_handle, true); | |
79 buffer.Map(image_size); | |
80 data->append(static_cast<char*>(buffer.memory()), image_size); | |
81 } | |
82 } | |
83 | |
84 void RendererClipboardDelegate::ReadCustomData(ui::ClipboardType clipboard_type, | |
85 const base::string16& type, | |
86 base::string16* data) { | |
87 RenderThreadImpl::current()->Send( | |
88 new ClipboardHostMsg_ReadCustomData(clipboard_type, type, data)); | |
89 } | |
90 | |
91 void RendererClipboardDelegate::WriteText(ui::ClipboardType clipboard_type, | |
92 const base::string16& text) { | |
93 RenderThreadImpl::current()->Send( | |
94 new ClipboardHostMsg_WriteText(clipboard_type, text)); | |
95 } | |
96 | |
97 void RendererClipboardDelegate::WriteHTML(ui::ClipboardType clipboard_type, | |
98 const base::string16& markup, | |
99 const GURL& url) { | |
100 RenderThreadImpl::current()->Send( | |
101 new ClipboardHostMsg_WriteHTML(clipboard_type, markup, url)); | |
102 } | |
103 | |
104 void RendererClipboardDelegate::WriteSmartPasteMarker( | |
105 ui::ClipboardType clipboard_type) { | |
106 RenderThreadImpl::current()->Send( | |
107 new ClipboardHostMsg_WriteSmartPasteMarker(clipboard_type)); | |
108 } | |
109 | |
110 void RendererClipboardDelegate::WriteCustomData( | |
111 ui::ClipboardType clipboard_type, | |
112 const std::map<base::string16, base::string16>& data) { | |
113 RenderThreadImpl::current()->Send( | |
114 new ClipboardHostMsg_WriteCustomData(clipboard_type, data)); | |
115 } | |
116 | |
117 void RendererClipboardDelegate::WriteBookmark(ui::ClipboardType clipboard_type, | |
118 const GURL& url, | |
119 const base::string16& title) { | |
120 RenderThreadImpl::current()->Send( | |
121 new ClipboardHostMsg_WriteBookmark(clipboard_type, url, title)); | |
122 } | |
123 | |
124 bool RendererClipboardDelegate::WriteImage(ui::ClipboardType clipboard_type, | |
125 const SkBitmap& bitmap) { | |
126 // Only 32-bit bitmaps are supported. | |
127 DCHECK_EQ(bitmap.colorType(), kN32_SkColorType); | |
128 | |
129 const gfx::Size size(bitmap.width(), bitmap.height()); | |
130 scoped_ptr<base::SharedMemory> shared_buf; | |
131 { | |
132 SkAutoLockPixels locked(bitmap); | |
133 void* pixels = bitmap.getPixels(); | |
134 // TODO(piman): this should not be NULL, but it is. crbug.com/369621 | |
135 if (!pixels) | |
136 return false; | |
137 | |
138 base::CheckedNumeric<uint32> checked_buf_size = 4; | |
139 checked_buf_size *= size.width(); | |
140 checked_buf_size *= size.height(); | |
141 if (!checked_buf_size.IsValid()) | |
142 return false; | |
143 | |
144 // Allocate a shared memory buffer to hold the bitmap bits. | |
145 uint32 buf_size = checked_buf_size.ValueOrDie(); | |
146 shared_buf.reset(ChildThread::current()->AllocateSharedMemory(buf_size)); | |
147 if (!shared_buf) | |
148 return false; | |
149 // Copy the bits into shared memory | |
150 DCHECK(shared_buf->memory()); | |
151 memcpy(shared_buf->memory(), pixels, buf_size); | |
152 shared_buf->Unmap(); | |
153 } | |
154 | |
155 RenderThreadImpl::current()->Send(new ClipboardHostMsg_WriteImage( | |
156 clipboard_type, size, shared_buf->handle())); | |
157 return true; | |
158 } | |
159 | |
160 void RendererClipboardDelegate::CommitWrite(ui::ClipboardType clipboard_type) { | |
161 RenderThreadImpl::current()->Send( | |
162 new ClipboardHostMsg_CommitWrite(clipboard_type)); | |
163 } | |
164 | |
165 } // namespace content | |
OLD | NEW |