Extract SIGPIPE ignoring code to a common place.
[chromium-blink-merge.git] / content / renderer / renderer_clipboard_client.cc
blob82dd4faa1b9f859c26c0ca4d696be55470c5da9b
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 random webkit glue functions.
7 #include "content/renderer/renderer_clipboard_client.h"
9 #include "base/shared_memory.h"
10 #include "base/string16.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 "ui/base/clipboard/clipboard.h"
15 #include "ui/gfx/size.h"
16 #include "webkit/glue/scoped_clipboard_writer_glue.h"
18 namespace content {
20 namespace {
22 class RendererClipboardWriteContext :
23 public webkit_glue::ClipboardClient::WriteContext {
24 public:
25 RendererClipboardWriteContext();
26 virtual ~RendererClipboardWriteContext();
27 virtual void WriteBitmapFromPixels(ui::Clipboard::ObjectMap* objects,
28 const void* pixels,
29 const gfx::Size& size);
30 virtual void Flush(const ui::Clipboard::ObjectMap& objects);
32 private:
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
44 // across processes.
45 void RendererClipboardWriteContext::WriteBitmapFromPixels(
46 ui::Clipboard::ObjectMap* objects,
47 const void* pixels,
48 const gfx::Size& size) {
49 // Do not try to write a bitmap more than once
50 if (shared_buf_.get())
51 return;
53 uint32 buf_size = 4 * size.width() * size.height();
55 // Allocate a shared memory buffer to hold the bitmap bits.
56 shared_buf_.reset(ChildThread::current()->AllocateSharedMemory(buf_size));
57 if (!shared_buf_.get())
58 return;
60 // Copy the bits into shared memory
61 DCHECK(shared_buf_->memory());
62 memcpy(shared_buf_->memory(), pixels, buf_size);
63 shared_buf_->Unmap();
65 ui::Clipboard::ObjectMapParam size_param;
66 const char* size_data = reinterpret_cast<const char*>(&size);
67 for (size_t i = 0; i < sizeof(gfx::Size); ++i)
68 size_param.push_back(size_data[i]);
70 ui::Clipboard::ObjectMapParams params;
72 // The first parameter is replaced on the receiving end with a pointer to
73 // a shared memory object containing the bitmap. We reserve space for it here.
74 ui::Clipboard::ObjectMapParam place_holder_param;
75 params.push_back(place_holder_param);
76 params.push_back(size_param);
77 (*objects)[ui::Clipboard::CBF_SMBITMAP] = params;
80 // Flushes the objects to the clipboard with an IPC.
81 void RendererClipboardWriteContext::Flush(
82 const ui::Clipboard::ObjectMap& objects) {
83 if (shared_buf_.get()) {
84 RenderThreadImpl::current()->Send(
85 new ClipboardHostMsg_WriteObjectsSync(objects, shared_buf_->handle()));
86 } else {
87 RenderThreadImpl::current()->Send(
88 new ClipboardHostMsg_WriteObjectsAsync(objects));
92 } // anonymous namespace
94 RendererClipboardClient::RendererClipboardClient() {
97 RendererClipboardClient::~RendererClipboardClient() {
100 ui::Clipboard* RendererClipboardClient::GetClipboard() {
101 return NULL;
104 uint64 RendererClipboardClient::GetSequenceNumber(
105 ui::Clipboard::Buffer buffer) {
106 uint64 sequence_number = 0;
107 RenderThreadImpl::current()->Send(
108 new ClipboardHostMsg_GetSequenceNumber(buffer,
109 &sequence_number));
110 return sequence_number;
113 bool RendererClipboardClient::IsFormatAvailable(
114 const ui::Clipboard::FormatType& format,
115 ui::Clipboard::Buffer buffer) {
116 bool result;
117 RenderThreadImpl::current()->Send(
118 new ClipboardHostMsg_IsFormatAvailable(format, buffer, &result));
119 return result;
122 void RendererClipboardClient::Clear(ui::Clipboard::Buffer buffer) {
123 RenderThreadImpl::current()->Send(new ClipboardHostMsg_Clear(buffer));
126 void RendererClipboardClient::ReadAvailableTypes(
127 ui::Clipboard::Buffer buffer,
128 std::vector<string16>* types,
129 bool* contains_filenames) {
130 RenderThreadImpl::current()->Send(new ClipboardHostMsg_ReadAvailableTypes(
131 buffer, types, contains_filenames));
134 void RendererClipboardClient::ReadText(ui::Clipboard::Buffer buffer,
135 string16* result) {
136 RenderThreadImpl::current()->Send(
137 new ClipboardHostMsg_ReadText(buffer, result));
140 void RendererClipboardClient::ReadAsciiText(ui::Clipboard::Buffer buffer,
141 std::string* result) {
142 RenderThreadImpl::current()->Send(
143 new ClipboardHostMsg_ReadAsciiText(buffer, result));
146 void RendererClipboardClient::ReadHTML(ui::Clipboard::Buffer buffer,
147 string16* markup,
148 GURL* url, uint32* fragment_start,
149 uint32* fragment_end) {
150 RenderThreadImpl::current()->Send(
151 new ClipboardHostMsg_ReadHTML(buffer, markup, url, fragment_start,
152 fragment_end));
155 void RendererClipboardClient::ReadRTF(ui::Clipboard::Buffer buffer,
156 std::string* result) {
157 RenderThreadImpl::current()->Send(
158 new ClipboardHostMsg_ReadRTF(buffer, result));
161 void RendererClipboardClient::ReadImage(ui::Clipboard::Buffer buffer,
162 std::string* data) {
163 base::SharedMemoryHandle image_handle;
164 uint32 image_size;
165 RenderThreadImpl::current()->Send(
166 new ClipboardHostMsg_ReadImage(buffer, &image_handle, &image_size));
167 if (base::SharedMemory::IsHandleValid(image_handle)) {
168 base::SharedMemory buffer(image_handle, true);
169 buffer.Map(image_size);
170 data->append(static_cast<char*>(buffer.memory()), image_size);
174 void RendererClipboardClient::ReadCustomData(ui::Clipboard::Buffer buffer,
175 const string16& type,
176 string16* data) {
177 RenderThreadImpl::current()->Send(
178 new ClipboardHostMsg_ReadCustomData(buffer, type, data));
181 void RendererClipboardClient::ReadData(const ui::Clipboard::FormatType& format,
182 std::string* data) {
183 RenderThreadImpl::current()->Send(
184 new ClipboardHostMsg_ReadData(format, data));
187 webkit_glue::ClipboardClient::WriteContext*
188 RendererClipboardClient::CreateWriteContext() {
189 return new RendererClipboardWriteContext;
192 } // namespace content