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 #include "ui/base/clipboard/clipboard.h" | 5 #include "ui/base/clipboard/clipboard.h" |
6 | 6 |
7 #include <iterator> | 7 #include <iterator> |
8 | 8 |
9 #include "base/lazy_instance.h" | 9 #include "base/lazy_instance.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
85 | 85 |
86 // Mapping from threads to clipboard objects. | 86 // Mapping from threads to clipboard objects. |
87 typedef std::map<base::PlatformThreadId, Clipboard*> ClipboardMap; | 87 typedef std::map<base::PlatformThreadId, Clipboard*> ClipboardMap; |
88 static base::LazyInstance<ClipboardMap> g_clipboard_map = | 88 static base::LazyInstance<ClipboardMap> g_clipboard_map = |
89 LAZY_INSTANCE_INITIALIZER; | 89 LAZY_INSTANCE_INITIALIZER; |
90 | 90 |
91 // Mutex that controls access to |g_clipboard_map|. | 91 // Mutex that controls access to |g_clipboard_map|. |
92 static base::LazyInstance<base::Lock>::Leaky | 92 static base::LazyInstance<base::Lock>::Leaky |
93 g_clipboard_map_lock = LAZY_INSTANCE_INITIALIZER; | 93 g_clipboard_map_lock = LAZY_INSTANCE_INITIALIZER; |
94 | 94 |
| 95 const std::size_t kSourceTagSize = sizeof(Clipboard::SourceTag); |
| 96 |
| 97 union SourceTag2BinaryHelper { |
| 98 Clipboard::SourceTag tag; |
| 99 uint8 bytes[kSourceTagSize]; |
| 100 }; |
| 101 |
95 } // namespace | 102 } // namespace |
96 | 103 |
97 const char Clipboard::kMimeTypeText[] = "text/plain"; | 104 const char Clipboard::kMimeTypeText[] = "text/plain"; |
98 const char Clipboard::kMimeTypeURIList[] = "text/uri-list"; | 105 const char Clipboard::kMimeTypeURIList[] = "text/uri-list"; |
99 const char Clipboard::kMimeTypeDownloadURL[] = "downloadurl"; | 106 const char Clipboard::kMimeTypeDownloadURL[] = "downloadurl"; |
100 const char Clipboard::kMimeTypeHTML[] = "text/html"; | 107 const char Clipboard::kMimeTypeHTML[] = "text/html"; |
101 const char Clipboard::kMimeTypeRTF[] = "text/rtf"; | 108 const char Clipboard::kMimeTypeRTF[] = "text/rtf"; |
102 const char Clipboard::kMimeTypePNG[] = "image/png"; | 109 const char Clipboard::kMimeTypePNG[] = "image/png"; |
103 | 110 |
104 // static | 111 // static |
| 112 Clipboard::ObjectMapParam Clipboard::SourceTag2Binary(SourceTag tag) { |
| 113 SourceTag2BinaryHelper helper; |
| 114 helper.tag = tag; |
| 115 std::vector<char> bytes(kSourceTagSize); |
| 116 memcpy(&bytes[0], helper.bytes, kSourceTagSize); |
| 117 return bytes; |
| 118 } |
| 119 |
| 120 // static |
| 121 Clipboard::SourceTag Clipboard::Binary2SourceTag(const std::string& binary) { |
| 122 if (binary.size() != kSourceTagSize) |
| 123 return SourceTag(); |
| 124 SourceTag2BinaryHelper helper; |
| 125 memcpy(helper.bytes, binary.c_str(), kSourceTagSize); |
| 126 return helper.tag; |
| 127 } |
| 128 |
| 129 // static |
105 void Clipboard::SetAllowedThreads( | 130 void Clipboard::SetAllowedThreads( |
106 const std::vector<base::PlatformThreadId>& allowed_threads) { | 131 const std::vector<base::PlatformThreadId>& allowed_threads) { |
107 base::AutoLock lock(g_clipboard_map_lock.Get()); | 132 base::AutoLock lock(g_clipboard_map_lock.Get()); |
108 | 133 |
109 g_allowed_threads.Get().clear(); | 134 g_allowed_threads.Get().clear(); |
110 std::copy(allowed_threads.begin(), allowed_threads.end(), | 135 std::copy(allowed_threads.begin(), allowed_threads.end(), |
111 std::back_inserter(g_allowed_threads.Get())); | 136 std::back_inserter(g_allowed_threads.Get())); |
112 } | 137 } |
113 | 138 |
114 // static | 139 // static |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
146 | 171 |
147 ClipboardMap* clipboard_map = g_clipboard_map.Pointer(); | 172 ClipboardMap* clipboard_map = g_clipboard_map.Pointer(); |
148 base::PlatformThreadId id = base::PlatformThread::CurrentId(); | 173 base::PlatformThreadId id = base::PlatformThread::CurrentId(); |
149 ClipboardMap::iterator it = clipboard_map->find(id); | 174 ClipboardMap::iterator it = clipboard_map->find(id); |
150 if (it != clipboard_map->end()) { | 175 if (it != clipboard_map->end()) { |
151 delete it->second; | 176 delete it->second; |
152 clipboard_map->erase(it); | 177 clipboard_map->erase(it); |
153 } | 178 } |
154 } | 179 } |
155 | 180 |
| 181 void Clipboard::WriteObjects(Buffer buffer, |
| 182 const ObjectMap& objects, |
| 183 SourceTag tag) { |
| 184 WriteObjectsImpl(buffer, objects, tag); |
| 185 FOR_EACH_OBSERVER(ClipboardObserverForTesting, |
| 186 observer_list_, |
| 187 OnWriteObjects(buffer)); |
| 188 } |
| 189 |
156 void Clipboard::DispatchObject(ObjectType type, const ObjectMapParams& params) { | 190 void Clipboard::DispatchObject(ObjectType type, const ObjectMapParams& params) { |
157 // All types apart from CBF_WEBKIT need at least 1 non-empty param. | 191 // All types apart from CBF_WEBKIT need at least 1 non-empty param. |
158 if (type != CBF_WEBKIT && (params.empty() || params[0].empty())) | 192 if (type != CBF_WEBKIT && (params.empty() || params[0].empty())) |
159 return; | 193 return; |
160 // Some other types need a non-empty 2nd param. | 194 // Some other types need a non-empty 2nd param. |
161 if ((type == CBF_BOOKMARK || type == CBF_BITMAP || | 195 if ((type == CBF_BOOKMARK || type == CBF_BITMAP || |
162 type == CBF_SMBITMAP || type == CBF_DATA) && | 196 type == CBF_SMBITMAP || type == CBF_DATA) && |
163 (params.size() != 2 || params[1].empty())) | 197 (params.size() != 2 || params[1].empty())) |
164 return; | 198 return; |
165 switch (type) { | 199 switch (type) { |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
258 // UI thread (see DispatchObject()). | 292 // UI thread (see DispatchObject()). |
259 iter->second[0].clear(); | 293 iter->second[0].clear(); |
260 for (size_t i = 0; i < sizeof(SharedMemory*); ++i) | 294 for (size_t i = 0; i < sizeof(SharedMemory*); ++i) |
261 iter->second[0].push_back(reinterpret_cast<char*>(&bitmap)[i]); | 295 iter->second[0].push_back(reinterpret_cast<char*>(&bitmap)[i]); |
262 has_shared_bitmap = true; | 296 has_shared_bitmap = true; |
263 } | 297 } |
264 } | 298 } |
265 } | 299 } |
266 | 300 |
267 } // namespace ui | 301 } // namespace ui |
OLD | NEW |