| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 // This file provides the embedder's side of the Clipboard interface. | 5 // This file provides the embedder's side of the Clipboard interface. |
| 6 | 6 |
| 7 #include "content/renderer/renderer_clipboard_client.h" | 7 #include "content/renderer/renderer_clipboard_client.h" |
| 8 | 8 |
| 9 #include "base/memory/shared_memory.h" | 9 #include "base/memory/shared_memory.h" |
| 10 #include "base/numerics/safe_math.h" | 10 #include "base/numerics/safe_math.h" |
| 11 #include "base/strings/string16.h" | |
| 12 #include "content/common/clipboard_messages.h" | 11 #include "content/common/clipboard_messages.h" |
| 13 #include "content/public/renderer/content_renderer_client.h" | 12 #include "content/public/renderer/content_renderer_client.h" |
| 14 #include "content/renderer/render_thread_impl.h" | 13 #include "content/renderer/render_thread_impl.h" |
| 15 #include "content/renderer/scoped_clipboard_writer_glue.h" | 14 #include "third_party/skia/include/core/SkBitmap.h" |
| 16 #include "ui/base/clipboard/clipboard.h" | 15 #include "ui/base/clipboard/clipboard.h" |
| 17 #include "ui/gfx/size.h" | 16 #include "ui/gfx/size.h" |
| 18 | 17 |
| 19 namespace content { | 18 namespace content { |
| 20 | 19 |
| 21 namespace { | |
| 22 | |
| 23 class RendererClipboardWriteContext : public 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) OVERRIDE; | |
| 30 virtual void Flush(const ui::Clipboard::ObjectMap& objects) OVERRIDE; | |
| 31 | |
| 32 private: | |
| 33 scoped_ptr<base::SharedMemory> shared_buf_; | |
| 34 DISALLOW_COPY_AND_ASSIGN(RendererClipboardWriteContext); | |
| 35 }; | |
| 36 | |
| 37 RendererClipboardWriteContext::RendererClipboardWriteContext() { | |
| 38 } | |
| 39 | |
| 40 RendererClipboardWriteContext::~RendererClipboardWriteContext() { | |
| 41 } | |
| 42 | |
| 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_) | |
| 51 return; | |
| 52 | |
| 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()) | |
| 57 return; | |
| 58 | |
| 59 uint32 buf_size = checked_buf_size.ValueOrDie(); | |
| 60 | |
| 61 // Allocate a shared memory buffer to hold the bitmap bits. | |
| 62 shared_buf_.reset(ChildThread::current()->AllocateSharedMemory(buf_size)); | |
| 63 if (!shared_buf_) | |
| 64 return; | |
| 65 | |
| 66 // Copy the bits into shared memory | |
| 67 DCHECK(shared_buf_->memory()); | |
| 68 memcpy(shared_buf_->memory(), pixels, buf_size); | |
| 69 shared_buf_->Unmap(); | |
| 70 | |
| 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]); | |
| 75 | |
| 76 ui::Clipboard::ObjectMapParams params; | |
| 77 | |
| 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; | |
| 84 } | |
| 85 | |
| 86 // Flushes the objects to the clipboard with an IPC. | |
| 87 void RendererClipboardWriteContext::Flush( | |
| 88 const ui::Clipboard::ObjectMap& objects) { | |
| 89 if (shared_buf_) { | |
| 90 RenderThreadImpl::current()->Send( | |
| 91 new ClipboardHostMsg_WriteObjectsSync(objects, shared_buf_->handle())); | |
| 92 } else { | |
| 93 RenderThreadImpl::current()->Send( | |
| 94 new ClipboardHostMsg_WriteObjectsAsync(objects)); | |
| 95 } | |
| 96 } | |
| 97 | |
| 98 } // anonymous namespace | |
| 99 | |
| 100 RendererClipboardClient::RendererClipboardClient() { | 20 RendererClipboardClient::RendererClipboardClient() { |
| 101 } | 21 } |
| 102 | 22 |
| 103 RendererClipboardClient::~RendererClipboardClient() { | |
| 104 } | |
| 105 | |
| 106 ui::Clipboard* RendererClipboardClient::GetClipboard() { | |
| 107 return NULL; | |
| 108 } | |
| 109 | |
| 110 uint64 RendererClipboardClient::GetSequenceNumber(ui::ClipboardType type) { | 23 uint64 RendererClipboardClient::GetSequenceNumber(ui::ClipboardType type) { |
| 111 uint64 sequence_number = 0; | 24 uint64 sequence_number = 0; |
| 112 RenderThreadImpl::current()->Send( | 25 RenderThreadImpl::current()->Send( |
| 113 new ClipboardHostMsg_GetSequenceNumber(type, &sequence_number)); | 26 new ClipboardHostMsg_GetSequenceNumber(type, &sequence_number)); |
| 114 return sequence_number; | 27 return sequence_number; |
| 115 } | 28 } |
| 116 | 29 |
| 117 bool RendererClipboardClient::IsFormatAvailable(content::ClipboardFormat format, | 30 bool RendererClipboardClient::IsFormatAvailable(content::ClipboardFormat format, |
| 118 ui::ClipboardType type) { | 31 ui::ClipboardType type) { |
| 119 bool result = false; | 32 bool result = false; |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 166 } | 79 } |
| 167 } | 80 } |
| 168 | 81 |
| 169 void RendererClipboardClient::ReadCustomData(ui::ClipboardType clipboard_type, | 82 void RendererClipboardClient::ReadCustomData(ui::ClipboardType clipboard_type, |
| 170 const base::string16& type, | 83 const base::string16& type, |
| 171 base::string16* data) { | 84 base::string16* data) { |
| 172 RenderThreadImpl::current()->Send( | 85 RenderThreadImpl::current()->Send( |
| 173 new ClipboardHostMsg_ReadCustomData(clipboard_type, type, data)); | 86 new ClipboardHostMsg_ReadCustomData(clipboard_type, type, data)); |
| 174 } | 87 } |
| 175 | 88 |
| 176 ClipboardClient::WriteContext* RendererClipboardClient::CreateWriteContext() { | 89 void RendererClipboardClient::WriteText(ui::ClipboardType clipboard_type, |
| 177 return new RendererClipboardWriteContext; | 90 const base::string16& text) { |
| 91 RenderThreadImpl::current()->Send( |
| 92 new ClipboardHostMsg_WriteText(clipboard_type, text)); |
| 93 } |
| 94 |
| 95 void RendererClipboardClient::WriteHTML(ui::ClipboardType clipboard_type, |
| 96 const base::string16& markup, |
| 97 const GURL& url) { |
| 98 RenderThreadImpl::current()->Send( |
| 99 new ClipboardHostMsg_WriteHTML(clipboard_type, markup, url)); |
| 100 } |
| 101 |
| 102 void RendererClipboardClient::WriteSmartPasteMarker( |
| 103 ui::ClipboardType clipboard_type) { |
| 104 RenderThreadImpl::current()->Send( |
| 105 new ClipboardHostMsg_WriteSmartPasteMarker(clipboard_type)); |
| 106 } |
| 107 |
| 108 void RendererClipboardClient::WriteCustomData( |
| 109 ui::ClipboardType clipboard_type, |
| 110 const std::map<base::string16, base::string16>& data) { |
| 111 RenderThreadImpl::current()->Send( |
| 112 new ClipboardHostMsg_WriteCustomData(clipboard_type, data)); |
| 113 } |
| 114 |
| 115 void RendererClipboardClient::WriteBookmark(ui::ClipboardType clipboard_type, |
| 116 const GURL& url, |
| 117 const base::string16& title) { |
| 118 RenderThreadImpl::current()->Send( |
| 119 new ClipboardHostMsg_WriteBookmark(clipboard_type, url, title)); |
| 120 } |
| 121 |
| 122 bool RendererClipboardClient::WriteImage(ui::ClipboardType clipboard_type, |
| 123 const SkBitmap& bitmap) { |
| 124 // Only 32-bit bitmaps are supported. |
| 125 DCHECK_EQ(bitmap.colorType(), kN32_SkColorType); |
| 126 |
| 127 const gfx::Size size(bitmap.width(), bitmap.height()); |
| 128 scoped_ptr<base::SharedMemory> shared_buf; |
| 129 { |
| 130 SkAutoLockPixels locked(bitmap); |
| 131 void* pixels = bitmap.getPixels(); |
| 132 // TODO(piman): this should not be NULL, but it is. crbug.com/369621 |
| 133 if (!pixels) |
| 134 return false; |
| 135 |
| 136 base::CheckedNumeric<uint32> checked_buf_size = 4; |
| 137 checked_buf_size *= size.width(); |
| 138 checked_buf_size *= size.height(); |
| 139 if (!checked_buf_size.IsValid()) |
| 140 return false; |
| 141 |
| 142 // Allocate a shared memory buffer to hold the bitmap bits. |
| 143 uint32 buf_size = checked_buf_size.ValueOrDie(); |
| 144 shared_buf.reset(ChildThread::current()->AllocateSharedMemory(buf_size)); |
| 145 if (!shared_buf) |
| 146 return false; |
| 147 // Copy the bits into shared memory |
| 148 DCHECK(shared_buf->memory()); |
| 149 memcpy(shared_buf->memory(), pixels, buf_size); |
| 150 shared_buf->Unmap(); |
| 151 } |
| 152 |
| 153 RenderThreadImpl::current()->Send(new ClipboardHostMsg_WriteImage( |
| 154 clipboard_type, size, shared_buf->handle())); |
| 155 return true; |
| 156 } |
| 157 |
| 158 void RendererClipboardClient::CommitWrite(ui::ClipboardType clipboard_type) { |
| 159 RenderThreadImpl::current()->Send( |
| 160 new ClipboardHostMsg_CommitWrite(clipboard_type)); |
| 178 } | 161 } |
| 179 | 162 |
| 180 } // namespace content | 163 } // namespace content |
| OLD | NEW |