Drive: Add BatchableRequest subclass.
[chromium-blink-merge.git] / ui / base / clipboard / clipboard.cc
blobd7b71f8035a953b186ce2632113088526b0670e0
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"
7 #include <iterator>
8 #include <limits>
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"
15 namespace ui {
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;
24 // static
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()));
34 // static
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()) {
42 bool found = false;
43 for (AllowedThreadsVector::const_iterator it = allowed_threads->begin();
44 it != allowed_threads->end(); ++it) {
45 if (*it == id) {
46 found = true;
47 break;
51 DCHECK(found);
54 ClipboardMap* clipboard_map = clipboard_map_.Pointer();
55 ClipboardMap::const_iterator it = clipboard_map->find(id);
56 if (it != clipboard_map->end())
57 return it->second;
59 Clipboard* clipboard = Clipboard::Create();
60 clipboard_map->insert(std::make_pair(id, clipboard));
61 return 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()) {
71 delete it->second;
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) {
79 if (param.empty())
80 return;
83 switch (type) {
84 case CBF_TEXT:
85 WriteText(&(params[0].front()), params[0].size());
86 break;
88 case CBF_HTML:
89 if (params.size() == 2) {
90 if (params[1].empty())
91 return;
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);
97 break;
99 case CBF_RTF:
100 WriteRTF(&(params[0].front()), params[0].size());
101 break;
103 case CBF_BOOKMARK:
104 WriteBookmark(&(params[0].front()), params[0].size(),
105 &(params[1].front()), params[1].size());
106 break;
108 case CBF_WEBKIT:
109 WriteWebSmartPaste();
110 break;
112 case CBF_SMBITMAP: {
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 = &params[0].front();
118 WriteBitmap(**reinterpret_cast<SkBitmap* const*>(packed_pointer_buffer));
119 break;
122 case CBF_DATA:
123 WriteData(
124 FormatType::Deserialize(
125 std::string(&(params[0].front()), params[0].size())),
126 &(params[1].front()),
127 params[1].size());
128 break;
130 default:
131 NOTREACHED();
135 } // namespace ui