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_client.h"
9 #include "base/memory/shared_memory.h"
10 #include "base/numerics/safe_math.h"
11 #include "base/strings/string16.h"
12 #include "content/common/clipboard_messages.h"
13 #include "content/public/renderer/content_renderer_client.h"
14 #include "content/renderer/render_thread_impl.h"
15 #include "content/renderer/scoped_clipboard_writer_glue.h"
16 #include "ui/base/clipboard/clipboard.h"
17 #include "ui/gfx/size.h"
23 class RendererClipboardWriteContext
: public ClipboardClient::WriteContext
{
25 RendererClipboardWriteContext();
26 virtual ~RendererClipboardWriteContext();
27 virtual void WriteBitmapFromPixels(ui::Clipboard::ObjectMap
* objects
,
29 const gfx::Size
& size
) OVERRIDE
;
30 virtual void Flush(const ui::Clipboard::ObjectMap
& objects
) OVERRIDE
;
33 scoped_ptr
<base::SharedMemory
> shared_buf_
;
34 DISALLOW_COPY_AND_ASSIGN(RendererClipboardWriteContext
);
37 RendererClipboardWriteContext::RendererClipboardWriteContext() {
40 RendererClipboardWriteContext::~RendererClipboardWriteContext() {
43 // This definition of WriteBitmapFromPixels uses shared memory to communicate
45 void RendererClipboardWriteContext::WriteBitmapFromPixels(
46 ui::Clipboard::ObjectMap
* objects
,
48 const gfx::Size
& size
) {
49 // Do not try to write a bitmap more than once
53 base::CheckedNumeric
<uint32
> checked_buf_size
= 4;
54 checked_buf_size
*= size
.width();
55 checked_buf_size
*= size
.height();
56 if (!checked_buf_size
.IsValid())
59 uint32 buf_size
= checked_buf_size
.ValueOrDie();
61 // Allocate a shared memory buffer to hold the bitmap bits.
62 shared_buf_
.reset(ChildThread::current()->AllocateSharedMemory(buf_size
));
66 // Copy the bits into shared memory
67 DCHECK(shared_buf_
->memory());
68 memcpy(shared_buf_
->memory(), pixels
, buf_size
);
71 ui::Clipboard::ObjectMapParam size_param
;
72 const char* size_data
= reinterpret_cast<const char*>(&size
);
73 for (size_t i
= 0; i
< sizeof(gfx::Size
); ++i
)
74 size_param
.push_back(size_data
[i
]);
76 ui::Clipboard::ObjectMapParams params
;
78 // The first parameter is replaced on the receiving end with a pointer to
79 // a shared memory object containing the bitmap. We reserve space for it here.
80 ui::Clipboard::ObjectMapParam place_holder_param
;
81 params
.push_back(place_holder_param
);
82 params
.push_back(size_param
);
83 (*objects
)[ui::Clipboard::CBF_SMBITMAP
] = params
;
86 // Flushes the objects to the clipboard with an IPC.
87 void RendererClipboardWriteContext::Flush(
88 const ui::Clipboard::ObjectMap
& objects
) {
90 RenderThreadImpl::current()->Send(
91 new ClipboardHostMsg_WriteObjectsSync(objects
, shared_buf_
->handle()));
93 RenderThreadImpl::current()->Send(
94 new ClipboardHostMsg_WriteObjectsAsync(objects
));
98 } // anonymous namespace
100 RendererClipboardClient::RendererClipboardClient() {
103 RendererClipboardClient::~RendererClipboardClient() {
106 ui::Clipboard
* RendererClipboardClient::GetClipboard() {
110 uint64
RendererClipboardClient::GetSequenceNumber(ui::ClipboardType type
) {
111 uint64 sequence_number
= 0;
112 RenderThreadImpl::current()->Send(
113 new ClipboardHostMsg_GetSequenceNumber(type
, &sequence_number
));
114 return sequence_number
;
117 bool RendererClipboardClient::IsFormatAvailable(content::ClipboardFormat format
,
118 ui::ClipboardType type
) {
120 RenderThreadImpl::current()->Send(
121 new ClipboardHostMsg_IsFormatAvailable(format
, type
, &result
));
125 void RendererClipboardClient::Clear(ui::ClipboardType type
) {
126 RenderThreadImpl::current()->Send(new ClipboardHostMsg_Clear(type
));
129 void RendererClipboardClient::ReadAvailableTypes(
130 ui::ClipboardType type
,
131 std::vector
<base::string16
>* types
,
132 bool* contains_filenames
) {
133 RenderThreadImpl::current()->Send(new ClipboardHostMsg_ReadAvailableTypes(
134 type
, types
, contains_filenames
));
137 void RendererClipboardClient::ReadText(ui::ClipboardType type
,
138 base::string16
* result
) {
139 RenderThreadImpl::current()->Send(
140 new ClipboardHostMsg_ReadText(type
, result
));
143 void RendererClipboardClient::ReadHTML(ui::ClipboardType type
,
144 base::string16
* markup
,
145 GURL
* url
, uint32
* fragment_start
,
146 uint32
* fragment_end
) {
147 RenderThreadImpl::current()->Send(new ClipboardHostMsg_ReadHTML(
148 type
, markup
, url
, fragment_start
, fragment_end
));
151 void RendererClipboardClient::ReadRTF(ui::ClipboardType type
,
152 std::string
* result
) {
153 RenderThreadImpl::current()->Send(new ClipboardHostMsg_ReadRTF(type
, result
));
156 void RendererClipboardClient::ReadImage(ui::ClipboardType type
,
158 base::SharedMemoryHandle image_handle
;
159 uint32 image_size
= 0;
160 RenderThreadImpl::current()->Send(
161 new ClipboardHostMsg_ReadImage(type
, &image_handle
, &image_size
));
162 if (base::SharedMemory::IsHandleValid(image_handle
)) {
163 base::SharedMemory
buffer(image_handle
, true);
164 buffer
.Map(image_size
);
165 data
->append(static_cast<char*>(buffer
.memory()), image_size
);
169 void RendererClipboardClient::ReadCustomData(ui::ClipboardType clipboard_type
,
170 const base::string16
& type
,
171 base::string16
* data
) {
172 RenderThreadImpl::current()->Send(
173 new ClipboardHostMsg_ReadCustomData(clipboard_type
, type
, data
));
176 ClipboardClient::WriteContext
* RendererClipboardClient::CreateWriteContext() {
177 return new RendererClipboardWriteContext
;
180 } // namespace content