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.
5 // This file provides the embedder's side of the Clipboard interface.
7 #include "content/renderer/renderer_clipboard_delegate.h"
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/geometry/size.h"
20 RendererClipboardDelegate::RendererClipboardDelegate() {
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
;
30 bool RendererClipboardDelegate::IsFormatAvailable(
31 content::ClipboardFormat format
,
32 ui::ClipboardType type
) {
34 RenderThreadImpl::current()->Send(
35 new ClipboardHostMsg_IsFormatAvailable(format
, type
, &result
));
39 void RendererClipboardDelegate::Clear(ui::ClipboardType type
) {
40 RenderThreadImpl::current()->Send(new ClipboardHostMsg_Clear(type
));
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
));
51 void RendererClipboardDelegate::ReadText(ui::ClipboardType type
,
52 base::string16
* result
) {
53 RenderThreadImpl::current()->Send(
54 new ClipboardHostMsg_ReadText(type
, result
));
57 void RendererClipboardDelegate::ReadHTML(ui::ClipboardType type
,
58 base::string16
* markup
,
60 uint32
* fragment_start
,
61 uint32
* fragment_end
) {
62 RenderThreadImpl::current()->Send(new ClipboardHostMsg_ReadHTML(
63 type
, markup
, url
, fragment_start
, fragment_end
));
66 void RendererClipboardDelegate::ReadRTF(ui::ClipboardType type
,
67 std::string
* result
) {
68 RenderThreadImpl::current()->Send(new ClipboardHostMsg_ReadRTF(type
, result
));
71 void RendererClipboardDelegate::ReadImage(ui::ClipboardType type
,
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
);
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
));
91 void RendererClipboardDelegate::WriteText(ui::ClipboardType clipboard_type
,
92 const base::string16
& text
) {
93 RenderThreadImpl::current()->Send(
94 new ClipboardHostMsg_WriteText(clipboard_type
, text
));
97 void RendererClipboardDelegate::WriteHTML(ui::ClipboardType clipboard_type
,
98 const base::string16
& markup
,
100 RenderThreadImpl::current()->Send(
101 new ClipboardHostMsg_WriteHTML(clipboard_type
, markup
, url
));
104 void RendererClipboardDelegate::WriteSmartPasteMarker(
105 ui::ClipboardType clipboard_type
) {
106 RenderThreadImpl::current()->Send(
107 new ClipboardHostMsg_WriteSmartPasteMarker(clipboard_type
));
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
));
117 void RendererClipboardDelegate::WriteBookmark(ui::ClipboardType clipboard_type
,
119 const base::string16
& title
) {
120 RenderThreadImpl::current()->Send(
121 new ClipboardHostMsg_WriteBookmark(clipboard_type
, url
.spec(), title
));
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
);
129 const gfx::Size
size(bitmap
.width(), bitmap
.height());
130 scoped_ptr
<base::SharedMemory
> shared_buf
;
132 SkAutoLockPixels
locked(bitmap
);
133 void* pixels
= bitmap
.getPixels();
134 // TODO(piman): this should not be NULL, but it is. crbug.com/369621
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())
144 // Allocate a shared memory buffer to hold the bitmap bits.
145 uint32 buf_size
= checked_buf_size
.ValueOrDie();
146 shared_buf
= ChildThreadImpl::current()->AllocateSharedMemory(buf_size
);
149 if (!shared_buf
->Map(buf_size
))
151 // Copy the bits into shared memory
152 DCHECK(shared_buf
->memory());
153 memcpy(shared_buf
->memory(), pixels
, buf_size
);
157 RenderThreadImpl::current()->Send(new ClipboardHostMsg_WriteImage(
158 clipboard_type
, size
, shared_buf
->handle()));
162 void RendererClipboardDelegate::CommitWrite(ui::ClipboardType clipboard_type
) {
163 RenderThreadImpl::current()->Send(
164 new ClipboardHostMsg_CommitWrite(clipboard_type
));
167 } // namespace content