summaryrefslogtreecommitdiff
path: root/chromium/content/renderer/renderer_clipboard_client.cc
blob: 9df9508be90d246ac5ea76c6f5a477878d16a7e3 (plain)
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
// 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_client.h"

#include "base/memory/shared_memory.h"
#include "base/strings/string16.h"
#include "content/common/clipboard_messages.h"
#include "content/public/renderer/content_renderer_client.h"
#include "content/renderer/render_thread_impl.h"
#include "content/renderer/scoped_clipboard_writer_glue.h"
#include "ui/base/clipboard/clipboard.h"
#include "ui/gfx/size.h"

namespace content {

namespace {

class RendererClipboardWriteContext : public ClipboardClient::WriteContext {
 public:
  RendererClipboardWriteContext();
  virtual ~RendererClipboardWriteContext();
  virtual void WriteBitmapFromPixels(ui::Clipboard::ObjectMap* objects,
                                     const void* pixels,
                                     const gfx::Size& size) OVERRIDE;
  virtual void Flush(const ui::Clipboard::ObjectMap& objects) OVERRIDE;

 private:
  scoped_ptr<base::SharedMemory> shared_buf_;
  DISALLOW_COPY_AND_ASSIGN(RendererClipboardWriteContext);
};

RendererClipboardWriteContext::RendererClipboardWriteContext() {
}

RendererClipboardWriteContext::~RendererClipboardWriteContext() {
}

// This definition of WriteBitmapFromPixels uses shared memory to communicate
// across processes.
void RendererClipboardWriteContext::WriteBitmapFromPixels(
    ui::Clipboard::ObjectMap* objects,
    const void* pixels,
    const gfx::Size& size) {
  // Do not try to write a bitmap more than once
  if (shared_buf_)
    return;

  uint32 buf_size = 4 * size.width() * size.height();

  // Allocate a shared memory buffer to hold the bitmap bits.
  shared_buf_.reset(ChildThread::current()->AllocateSharedMemory(buf_size));
  if (!shared_buf_)
    return;

  // Copy the bits into shared memory
  DCHECK(shared_buf_->memory());
  memcpy(shared_buf_->memory(), pixels, buf_size);
  shared_buf_->Unmap();

  ui::Clipboard::ObjectMapParam size_param;
  const char* size_data = reinterpret_cast<const char*>(&size);
  for (size_t i = 0; i < sizeof(gfx::Size); ++i)
    size_param.push_back(size_data[i]);

  ui::Clipboard::ObjectMapParams params;

  // The first parameter is replaced on the receiving end with a pointer to
  // a shared memory object containing the bitmap. We reserve space for it here.
  ui::Clipboard::ObjectMapParam place_holder_param;
  params.push_back(place_holder_param);
  params.push_back(size_param);
  (*objects)[ui::Clipboard::CBF_SMBITMAP] = params;
}

// Flushes the objects to the clipboard with an IPC.
void RendererClipboardWriteContext::Flush(
    const ui::Clipboard::ObjectMap& objects) {
  if (shared_buf_) {
    RenderThreadImpl::current()->Send(
        new ClipboardHostMsg_WriteObjectsSync(objects, shared_buf_->handle()));
  } else {
    RenderThreadImpl::current()->Send(
        new ClipboardHostMsg_WriteObjectsAsync(objects));
  }
}

}  // anonymous namespace

RendererClipboardClient::RendererClipboardClient() {
}

RendererClipboardClient::~RendererClipboardClient() {
}

ui::Clipboard* RendererClipboardClient::GetClipboard() {
  return NULL;
}

uint64 RendererClipboardClient::GetSequenceNumber(
    ui::Clipboard::Buffer buffer) {
  uint64 sequence_number = 0;
  RenderThreadImpl::current()->Send(
      new ClipboardHostMsg_GetSequenceNumber(buffer,
                                             &sequence_number));
  return sequence_number;
}

bool RendererClipboardClient::IsFormatAvailable(
    const ui::Clipboard::FormatType& format,
    ui::Clipboard::Buffer buffer) {
  bool result;
  RenderThreadImpl::current()->Send(
      new ClipboardHostMsg_IsFormatAvailable(format, buffer, &result));
  return result;
}

void RendererClipboardClient::Clear(ui::Clipboard::Buffer buffer) {
  RenderThreadImpl::current()->Send(new ClipboardHostMsg_Clear(buffer));
}

void RendererClipboardClient::ReadAvailableTypes(
    ui::Clipboard::Buffer buffer,
    std::vector<string16>* types,
    bool* contains_filenames) {
  RenderThreadImpl::current()->Send(new ClipboardHostMsg_ReadAvailableTypes(
      buffer, types, contains_filenames));
}

void RendererClipboardClient::ReadText(ui::Clipboard::Buffer buffer,
                                       string16* result) {
  RenderThreadImpl::current()->Send(
      new ClipboardHostMsg_ReadText(buffer, result));
}

void RendererClipboardClient::ReadAsciiText(ui::Clipboard::Buffer buffer,
                                            std::string* result) {
  RenderThreadImpl::current()->Send(
      new ClipboardHostMsg_ReadAsciiText(buffer, result));
}

void RendererClipboardClient::ReadHTML(ui::Clipboard::Buffer buffer,
                                       string16* markup,
                                       GURL* url, uint32* fragment_start,
                                       uint32* fragment_end) {
  RenderThreadImpl::current()->Send(
      new ClipboardHostMsg_ReadHTML(buffer, markup, url, fragment_start,
                                    fragment_end));
}

void RendererClipboardClient::ReadRTF(ui::Clipboard::Buffer buffer,
                                      std::string* result) {
  RenderThreadImpl::current()->Send(
      new ClipboardHostMsg_ReadRTF(buffer, result));
}

void RendererClipboardClient::ReadImage(ui::Clipboard::Buffer buffer,
                                        std::string* data) {
  base::SharedMemoryHandle image_handle;
  uint32 image_size;
  RenderThreadImpl::current()->Send(
      new ClipboardHostMsg_ReadImage(buffer, &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 RendererClipboardClient::ReadCustomData(ui::Clipboard::Buffer buffer,
                                             const string16& type,
                                             string16* data) {
  RenderThreadImpl::current()->Send(
      new ClipboardHostMsg_ReadCustomData(buffer, type, data));
}

void RendererClipboardClient::ReadData(const ui::Clipboard::FormatType& format,
                                       std::string* data) {
  RenderThreadImpl::current()->Send(
      new ClipboardHostMsg_ReadData(format, data));
}

ClipboardClient::WriteContext* RendererClipboardClient::CreateWriteContext() {
  return new RendererClipboardWriteContext;
}

}  // namespace content