Fix auto-uninstall of legacy multi-install Chrome Frame.
[chromium-blink-merge.git] / content / renderer / renderer_clipboard_client.cc
blobc1c7c966167daa821863d9a8b1f38a77735d37db
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/strings/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 "content/renderer/scoped_clipboard_writer_glue.h"
15 #include "ui/base/clipboard/clipboard.h"
16 #include "ui/gfx/size.h"
18 namespace content {
20 namespace {
22 class RendererClipboardWriteContext : public ClipboardClient::WriteContext {
23 public:
24 RendererClipboardWriteContext();
25 virtual ~RendererClipboardWriteContext();
26 virtual void WriteBitmapFromPixels(ui::Clipboard::ObjectMap* objects,
27 const void* pixels,
28 const gfx::Size& size) OVERRIDE;
29 virtual void Flush(const ui::Clipboard::ObjectMap& objects) OVERRIDE;
31 private:
32 scoped_ptr<base::SharedMemory> shared_buf_;
33 DISALLOW_COPY_AND_ASSIGN(RendererClipboardWriteContext);
36 RendererClipboardWriteContext::RendererClipboardWriteContext() {
39 RendererClipboardWriteContext::~RendererClipboardWriteContext() {
42 // This definition of WriteBitmapFromPixels uses shared memory to communicate
43 // across processes.
44 void RendererClipboardWriteContext::WriteBitmapFromPixels(
45 ui::Clipboard::ObjectMap* objects,
46 const void* pixels,
47 const gfx::Size& size) {
48 // Do not try to write a bitmap more than once
49 if (shared_buf_)
50 return;
52 uint32 buf_size = 4 * size.width() * size.height();
54 // Allocate a shared memory buffer to hold the bitmap bits.
55 shared_buf_.reset(ChildThread::current()->AllocateSharedMemory(buf_size));
56 if (!shared_buf_)
57 return;
59 // Copy the bits into shared memory
60 DCHECK(shared_buf_->memory());
61 memcpy(shared_buf_->memory(), pixels, buf_size);
62 shared_buf_->Unmap();
64 ui::Clipboard::ObjectMapParam size_param;
65 const char* size_data = reinterpret_cast<const char*>(&size);
66 for (size_t i = 0; i < sizeof(gfx::Size); ++i)
67 size_param.push_back(size_data[i]);
69 ui::Clipboard::ObjectMapParams params;
71 // The first parameter is replaced on the receiving end with a pointer to
72 // a shared memory object containing the bitmap. We reserve space for it here.
73 ui::Clipboard::ObjectMapParam place_holder_param;
74 params.push_back(place_holder_param);
75 params.push_back(size_param);
76 (*objects)[ui::Clipboard::CBF_SMBITMAP] = params;
79 // Flushes the objects to the clipboard with an IPC.
80 void RendererClipboardWriteContext::Flush(
81 const ui::Clipboard::ObjectMap& objects) {
82 if (shared_buf_) {
83 RenderThreadImpl::current()->Send(
84 new ClipboardHostMsg_WriteObjectsSync(objects, shared_buf_->handle()));
85 } else {
86 RenderThreadImpl::current()->Send(
87 new ClipboardHostMsg_WriteObjectsAsync(objects));
91 } // anonymous namespace
93 RendererClipboardClient::RendererClipboardClient() {
96 RendererClipboardClient::~RendererClipboardClient() {
99 ui::Clipboard* RendererClipboardClient::GetClipboard() {
100 return NULL;
103 uint64 RendererClipboardClient::GetSequenceNumber(ui::ClipboardType type) {
104 uint64 sequence_number = 0;
105 RenderThreadImpl::current()->Send(
106 new ClipboardHostMsg_GetSequenceNumber(type, &sequence_number));
107 return sequence_number;
110 bool RendererClipboardClient::IsFormatAvailable(content::ClipboardFormat format,
111 ui::ClipboardType type) {
112 bool result;
113 RenderThreadImpl::current()->Send(
114 new ClipboardHostMsg_IsFormatAvailable(format, type, &result));
115 return result;
118 void RendererClipboardClient::Clear(ui::ClipboardType type) {
119 RenderThreadImpl::current()->Send(new ClipboardHostMsg_Clear(type));
122 void RendererClipboardClient::ReadAvailableTypes(
123 ui::ClipboardType type,
124 std::vector<base::string16>* types,
125 bool* contains_filenames) {
126 RenderThreadImpl::current()->Send(new ClipboardHostMsg_ReadAvailableTypes(
127 type, types, contains_filenames));
130 void RendererClipboardClient::ReadText(ui::ClipboardType type,
131 base::string16* result) {
132 RenderThreadImpl::current()->Send(
133 new ClipboardHostMsg_ReadText(type, result));
136 void RendererClipboardClient::ReadHTML(ui::ClipboardType type,
137 base::string16* markup,
138 GURL* url, uint32* fragment_start,
139 uint32* fragment_end) {
140 RenderThreadImpl::current()->Send(new ClipboardHostMsg_ReadHTML(
141 type, markup, url, fragment_start, fragment_end));
144 void RendererClipboardClient::ReadRTF(ui::ClipboardType type,
145 std::string* result) {
146 RenderThreadImpl::current()->Send(new ClipboardHostMsg_ReadRTF(type, result));
149 void RendererClipboardClient::ReadImage(ui::ClipboardType type,
150 std::string* data) {
151 base::SharedMemoryHandle image_handle;
152 uint32 image_size;
153 RenderThreadImpl::current()->Send(
154 new ClipboardHostMsg_ReadImage(type, &image_handle, &image_size));
155 if (base::SharedMemory::IsHandleValid(image_handle)) {
156 base::SharedMemory buffer(image_handle, true);
157 buffer.Map(image_size);
158 data->append(static_cast<char*>(buffer.memory()), image_size);
162 void RendererClipboardClient::ReadCustomData(ui::ClipboardType clipboard_type,
163 const base::string16& type,
164 base::string16* data) {
165 RenderThreadImpl::current()->Send(
166 new ClipboardHostMsg_ReadCustomData(clipboard_type, type, data));
169 ClipboardClient::WriteContext* RendererClipboardClient::CreateWriteContext() {
170 return new RendererClipboardWriteContext;
173 } // namespace content