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 #include "ui/base/clipboard/clipboard.h"
10 #include "base/logging.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "third_party/skia/include/core/SkBitmap.h"
13 #include "ui/gfx/geometry/size.h"
17 base::LazyInstance
<Clipboard::AllowedThreadsVector
>
18 Clipboard::allowed_threads_
= LAZY_INSTANCE_INITIALIZER
;
19 base::LazyInstance
<Clipboard::ClipboardMap
> Clipboard::clipboard_map_
=
20 LAZY_INSTANCE_INITIALIZER
;
21 base::LazyInstance
<base::Lock
>::Leaky
Clipboard::clipboard_map_lock_
=
22 LAZY_INSTANCE_INITIALIZER
;
25 void Clipboard::SetAllowedThreads(
26 const std::vector
<base::PlatformThreadId
>& allowed_threads
) {
27 base::AutoLock
lock(clipboard_map_lock_
.Get());
29 allowed_threads_
.Get().clear();
30 std::copy(allowed_threads
.begin(), allowed_threads
.end(),
31 std::back_inserter(allowed_threads_
.Get()));
35 Clipboard
* Clipboard::GetForCurrentThread() {
36 base::AutoLock
lock(clipboard_map_lock_
.Get());
38 base::PlatformThreadId id
= base::PlatformThread::CurrentId();
40 AllowedThreadsVector
* allowed_threads
= allowed_threads_
.Pointer();
41 if (!allowed_threads
->empty()) {
43 for (AllowedThreadsVector::const_iterator it
= allowed_threads
->begin();
44 it
!= allowed_threads
->end(); ++it
) {
54 ClipboardMap
* clipboard_map
= clipboard_map_
.Pointer();
55 ClipboardMap::const_iterator it
= clipboard_map
->find(id
);
56 if (it
!= clipboard_map
->end())
59 Clipboard
* clipboard
= Clipboard::Create();
60 clipboard_map
->insert(std::make_pair(id
, clipboard
));
64 void Clipboard::DestroyClipboardForCurrentThread() {
65 base::AutoLock
lock(clipboard_map_lock_
.Get());
67 ClipboardMap
* clipboard_map
= clipboard_map_
.Pointer();
68 base::PlatformThreadId id
= base::PlatformThread::CurrentId();
69 ClipboardMap::iterator it
= clipboard_map
->find(id
);
70 if (it
!= clipboard_map
->end()) {
72 clipboard_map
->erase(it
);
76 void Clipboard::DispatchObject(ObjectType type
, const ObjectMapParams
& params
) {
77 // Ignore writes with empty parameters.
78 for (const auto& param
: params
) {
85 WriteText(&(params
[0].front()), params
[0].size());
89 if (params
.size() == 2) {
90 if (params
[1].empty())
92 WriteHTML(&(params
[0].front()), params
[0].size(),
93 &(params
[1].front()), params
[1].size());
94 } else if (params
.size() == 1) {
95 WriteHTML(&(params
[0].front()), params
[0].size(), NULL
, 0);
100 WriteRTF(&(params
[0].front()), params
[0].size());
104 WriteBookmark(&(params
[0].front()), params
[0].size(),
105 &(params
[1].front()), params
[1].size());
109 WriteWebSmartPaste();
113 // Usually, the params are just UTF-8 strings. However, for images,
114 // ScopedClipboardWriter actually sizes the buffer to sizeof(SkBitmap*),
115 // aliases the contents of the vector to a SkBitmap**, and writes the
116 // pointer to the actual SkBitmap in the clipboard object param.
117 const char* packed_pointer_buffer
= ¶ms
[0].front();
118 WriteBitmap(**reinterpret_cast<SkBitmap
* const*>(packed_pointer_buffer
));
124 FormatType::Deserialize(
125 std::string(&(params
[0].front()), params
[0].size())),
126 &(params
[1].front()),