1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// This file provides the embedder's side of the Clipboard interface.
#include "content/renderer/renderer_clipboard_delegate.h"
#include "base/memory/shared_memory.h"
#include "base/numerics/safe_math.h"
#include "content/common/clipboard_messages.h"
#include "content/public/renderer/content_renderer_client.h"
#include "content/renderer/render_thread_impl.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "ui/base/clipboard/clipboard.h"
#include "ui/gfx/geometry/size.h"
namespace content {
RendererClipboardDelegate::RendererClipboardDelegate() {
}
uint64 RendererClipboardDelegate::GetSequenceNumber(ui::ClipboardType type) {
uint64 sequence_number = 0;
RenderThreadImpl::current()->Send(
new ClipboardHostMsg_GetSequenceNumber(type, &sequence_number));
return sequence_number;
}
bool RendererClipboardDelegate::IsFormatAvailable(
content::ClipboardFormat format,
ui::ClipboardType type) {
bool result = false;
RenderThreadImpl::current()->Send(
new ClipboardHostMsg_IsFormatAvailable(format, type, &result));
return result;
}
void RendererClipboardDelegate::Clear(ui::ClipboardType type) {
RenderThreadImpl::current()->Send(new ClipboardHostMsg_Clear(type));
}
void RendererClipboardDelegate::ReadAvailableTypes(
ui::ClipboardType type,
std::vector<base::string16>* types,
bool* contains_filenames) {
RenderThreadImpl::current()->Send(
new ClipboardHostMsg_ReadAvailableTypes(type, types, contains_filenames));
}
void RendererClipboardDelegate::ReadText(ui::ClipboardType type,
base::string16* result) {
RenderThreadImpl::current()->Send(
new ClipboardHostMsg_ReadText(type, result));
}
void RendererClipboardDelegate::ReadHTML(ui::ClipboardType type,
base::string16* markup,
GURL* url,
uint32* fragment_start,
uint32* fragment_end) {
RenderThreadImpl::current()->Send(new ClipboardHostMsg_ReadHTML(
type, markup, url, fragment_start, fragment_end));
}
void RendererClipboardDelegate::ReadRTF(ui::ClipboardType type,
std::string* result) {
RenderThreadImpl::current()->Send(new ClipboardHostMsg_ReadRTF(type, result));
}
void RendererClipboardDelegate::ReadImage(ui::ClipboardType type,
std::string* data) {
base::SharedMemoryHandle image_handle;
uint32 image_size = 0;
RenderThreadImpl::current()->Send(
new ClipboardHostMsg_ReadImage(type, &image_handle, &image_size));
if (base::SharedMemory::IsHandleValid(image_handle)) {
base::SharedMemory buffer(image_handle, true);
buffer.Map(image_size);
data->append(static_cast<char*>(buffer.memory()), image_size);
}
}
void RendererClipboardDelegate::ReadCustomData(ui::ClipboardType clipboard_type,
const base::string16& type,
base::string16* data) {
RenderThreadImpl::current()->Send(
new ClipboardHostMsg_ReadCustomData(clipboard_type, type, data));
}
void RendererClipboardDelegate::WriteText(ui::ClipboardType clipboard_type,
const base::string16& text) {
RenderThreadImpl::current()->Send(
new ClipboardHostMsg_WriteText(clipboard_type, text));
}
void RendererClipboardDelegate::WriteHTML(ui::ClipboardType clipboard_type,
const base::string16& markup,
const GURL& url) {
RenderThreadImpl::current()->Send(
new ClipboardHostMsg_WriteHTML(clipboard_type, markup, url));
}
void RendererClipboardDelegate::WriteSmartPasteMarker(
ui::ClipboardType clipboard_type) {
RenderThreadImpl::current()->Send(
new ClipboardHostMsg_WriteSmartPasteMarker(clipboard_type));
}
void RendererClipboardDelegate::WriteCustomData(
ui::ClipboardType clipboard_type,
const std::map<base::string16, base::string16>& data) {
RenderThreadImpl::current()->Send(
new ClipboardHostMsg_WriteCustomData(clipboard_type, data));
}
void RendererClipboardDelegate::WriteBookmark(ui::ClipboardType clipboard_type,
const GURL& url,
const base::string16& title) {
RenderThreadImpl::current()->Send(
new ClipboardHostMsg_WriteBookmark(clipboard_type, url.spec(), title));
}
bool RendererClipboardDelegate::WriteImage(ui::ClipboardType clipboard_type,
const SkBitmap& bitmap) {
// Only 32-bit bitmaps are supported.
DCHECK_EQ(bitmap.colorType(), kN32_SkColorType);
const gfx::Size size(bitmap.width(), bitmap.height());
scoped_ptr<base::SharedMemory> shared_buf;
{
SkAutoLockPixels locked(bitmap);
void* pixels = bitmap.getPixels();
// TODO(piman): this should not be NULL, but it is. crbug.com/369621
if (!pixels)
return false;
base::CheckedNumeric<uint32> checked_buf_size = 4;
checked_buf_size *= size.width();
checked_buf_size *= size.height();
if (!checked_buf_size.IsValid())
return false;
// Allocate a shared memory buffer to hold the bitmap bits.
uint32 buf_size = checked_buf_size.ValueOrDie();
shared_buf = ChildThread::current()->AllocateSharedMemory(buf_size);
if (!shared_buf)
return false;
if (!shared_buf->Map(buf_size))
return false;
// Copy the bits into shared memory
DCHECK(shared_buf->memory());
memcpy(shared_buf->memory(), pixels, buf_size);
shared_buf->Unmap();
}
RenderThreadImpl::current()->Send(new ClipboardHostMsg_WriteImage(
clipboard_type, size, shared_buf->handle()));
return true;
}
void RendererClipboardDelegate::CommitWrite(ui::ClipboardType clipboard_type) {
RenderThreadImpl::current()->Send(
new ClipboardHostMsg_CommitWrite(clipboard_type));
}
} // namespace content
|