OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2011 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 random webkit glue functions. | |
6 | |
7 #include "content/renderer/renderer_clipboard_client.h" | |
8 | |
9 #include "build/build_config.h" | |
10 | |
11 #include <string> | |
12 #include <vector> | |
13 | |
14 #include "base/shared_memory.h" | |
15 #include "base/string16.h" | |
16 #include "content/common/clipboard_messages.h" | |
17 #include "content/public/renderer/content_renderer_client.h" | |
18 #include "content/renderer/render_thread_impl.h" | |
19 #include "ui/base/clipboard/clipboard.h" | |
20 #include "ui/gfx/size.h" | |
21 #include "webkit/glue/scoped_clipboard_writer_glue.h" | |
22 | |
23 namespace { | |
24 | |
25 class RendererClipboardWriteContext: | |
sky
2011/11/21 15:13:17
nit: space between 't' and ':'
piman
2011/11/21 20:27:14
Done.
| |
26 public webkit_glue::ClipboardClient::WriteContext { | |
27 public: | |
28 RendererClipboardWriteContext(); | |
29 virtual ~RendererClipboardWriteContext(); | |
30 virtual void WriteBitmapFromPixels(ui::Clipboard::ObjectMap* objects, | |
31 const void* pixels, | |
32 const gfx::Size& size); | |
33 virtual void FlushAndDestroy(const ui::Clipboard::ObjectMap& objects); | |
34 | |
35 private: | |
36 scoped_ptr<base::SharedMemory> shared_buf_; | |
37 DISALLOW_COPY_AND_ASSIGN(RendererClipboardWriteContext); | |
38 }; | |
39 | |
40 RendererClipboardWriteContext::RendererClipboardWriteContext() { | |
41 } | |
42 | |
43 RendererClipboardWriteContext::~RendererClipboardWriteContext() { | |
44 } | |
45 | |
46 // This definition of WriteBitmapFromPixels uses shared memory to communicate | |
47 // across processes. | |
48 void RendererClipboardWriteContext::WriteBitmapFromPixels( | |
49 ui::Clipboard::ObjectMap* objects, | |
50 const void* pixels, | |
51 const gfx::Size& size) { | |
52 // Do not try to write a bitmap more than once | |
53 if (shared_buf_.get()) | |
54 return; | |
55 | |
56 uint32 buf_size = 4 * size.width() * size.height(); | |
57 | |
58 // Allocate a shared memory buffer to hold the bitmap bits. | |
59 shared_buf_.reset(ChildThread::current()->AllocateSharedMemory(buf_size)); | |
60 if (!shared_buf_.get()) | |
61 return; | |
62 | |
63 // Copy the bits into shared memory | |
64 DCHECK(shared_buf_->memory()); | |
65 memcpy(shared_buf_->memory(), pixels, buf_size); | |
66 shared_buf_->Unmap(); | |
67 | |
68 ui::Clipboard::ObjectMapParam size_param; | |
69 const char* size_data = reinterpret_cast<const char*>(&size); | |
70 for (size_t i = 0; i < sizeof(gfx::Size); ++i) | |
71 size_param.push_back(size_data[i]); | |
72 | |
73 ui::Clipboard::ObjectMapParams params; | |
74 | |
75 // The first parameter is replaced on the receiving end with a pointer to | |
76 // a shared memory object containing the bitmap. We reserve space for it here. | |
77 ui::Clipboard::ObjectMapParam place_holder_param; | |
78 params.push_back(place_holder_param); | |
79 params.push_back(size_param); | |
80 (*objects)[ui::Clipboard::CBF_SMBITMAP] = params; | |
81 } | |
82 | |
83 // Define a destructor that makes IPCs to flush the contents to the | |
84 // system clipboard. | |
85 void RendererClipboardWriteContext::FlushAndDestroy( | |
86 const ui::Clipboard::ObjectMap& objects) { | |
87 scoped_ptr<RendererClipboardWriteContext> delete_on_return(this); | |
88 if (objects.empty()) | |
89 return; | |
90 | |
91 if (shared_buf_.get()) { | |
92 RenderThreadImpl::current()->Send( | |
93 new ClipboardHostMsg_WriteObjectsSync(objects, shared_buf_->handle())); | |
94 } else { | |
95 RenderThreadImpl::current()->Send( | |
96 new ClipboardHostMsg_WriteObjectsAsync(objects)); | |
97 } | |
98 } | |
99 | |
100 } // anonymous namespace | |
101 | |
102 RendererClipboardClient::RendererClipboardClient() { | |
103 } | |
104 | |
105 RendererClipboardClient::~RendererClipboardClient() { | |
106 } | |
107 | |
108 ui::Clipboard* RendererClipboardClient::GetClipboard() { | |
109 return NULL; | |
110 } | |
111 | |
112 uint64 RendererClipboardClient::GetSequenceNumber( | |
113 ui::Clipboard::Buffer buffer) { | |
114 uint64 sequence_number = 0; | |
115 RenderThreadImpl::current()->Send( | |
116 new ClipboardHostMsg_GetSequenceNumber(buffer, | |
117 &sequence_number)); | |
118 return sequence_number; | |
119 } | |
120 | |
121 bool RendererClipboardClient::IsFormatAvailable( | |
122 const ui::Clipboard::FormatType& format, | |
123 ui::Clipboard::Buffer buffer) { | |
124 bool result; | |
125 RenderThreadImpl::current()->Send( | |
126 new ClipboardHostMsg_IsFormatAvailable(format, buffer, &result)); | |
127 return result; | |
128 } | |
129 | |
130 void RendererClipboardClient::ReadAvailableTypes( | |
131 ui::Clipboard::Buffer buffer, | |
132 std::vector<string16>* types, | |
133 bool* contains_filenames) { | |
134 RenderThreadImpl::current()->Send(new ClipboardHostMsg_ReadAvailableTypes( | |
135 buffer, types, contains_filenames)); | |
136 } | |
137 | |
138 void RendererClipboardClient::ReadText(ui::Clipboard::Buffer buffer, | |
139 string16* result) { | |
140 RenderThreadImpl::current()->Send( | |
141 new ClipboardHostMsg_ReadText(buffer, result)); | |
142 } | |
143 | |
144 void RendererClipboardClient::ReadAsciiText(ui::Clipboard::Buffer buffer, | |
145 std::string* result) { | |
146 RenderThreadImpl::current()->Send( | |
147 new ClipboardHostMsg_ReadAsciiText(buffer, result)); | |
148 } | |
149 | |
150 void RendererClipboardClient::ReadHTML(ui::Clipboard::Buffer buffer, | |
151 string16* markup, | |
152 GURL* url, uint32* fragment_start, | |
153 uint32* fragment_end) { | |
154 RenderThreadImpl::current()->Send( | |
155 new ClipboardHostMsg_ReadHTML(buffer, markup, url, fragment_start, | |
156 fragment_end)); | |
157 } | |
158 | |
159 void RendererClipboardClient::ReadImage(ui::Clipboard::Buffer buffer, | |
160 std::string* data) { | |
161 base::SharedMemoryHandle image_handle; | |
162 uint32 image_size; | |
163 RenderThreadImpl::current()->Send( | |
164 new ClipboardHostMsg_ReadImage(buffer, &image_handle, &image_size)); | |
165 if (base::SharedMemory::IsHandleValid(image_handle)) { | |
166 base::SharedMemory buffer(image_handle, true); | |
167 buffer.Map(image_size); | |
168 data->append(static_cast<char*>(buffer.memory()), image_size); | |
169 } | |
170 } | |
171 | |
172 webkit_glue::ClipboardClient::WriteContext* | |
173 RendererClipboardClient::CreateWriteContext() { | |
174 return new RendererClipboardWriteContext; | |
175 } | |
OLD | NEW |