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 char bytes[kSourceTagSize]; | |
dcheng
2013/02/06 22:31:40
Nit: uint8 instead of char for byte strings.
vasilii
2013/02/07 17:30:05
Done.
| |
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 return ObjectMapParam(helper.bytes, helper.bytes + kSourceTagSize); | |
116 } | |
117 | |
118 // static | |
119 Clipboard::SourceTag Clipboard::Binary2SourceTag(const std::string& binary) { | |
120 if (binary.size() != kSourceTagSize) | |
121 return SourceTag(); | |
122 SourceTag2BinaryHelper helper; | |
123 memcpy(helper.bytes, binary.c_str(), kSourceTagSize); | |
124 return helper.tag; | |
125 } | |
126 | |
127 // static | |
105 void Clipboard::SetAllowedThreads( | 128 void Clipboard::SetAllowedThreads( |
106 const std::vector<base::PlatformThreadId>& allowed_threads) { | 129 const std::vector<base::PlatformThreadId>& allowed_threads) { |
107 base::AutoLock lock(g_clipboard_map_lock.Get()); | 130 base::AutoLock lock(g_clipboard_map_lock.Get()); |
108 | 131 |
109 g_allowed_threads.Get().clear(); | 132 g_allowed_threads.Get().clear(); |
110 std::copy(allowed_threads.begin(), allowed_threads.end(), | 133 std::copy(allowed_threads.begin(), allowed_threads.end(), |
111 std::back_inserter(g_allowed_threads.Get())); | 134 std::back_inserter(g_allowed_threads.Get())); |
112 } | 135 } |
113 | 136 |
114 // static | 137 // static |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
146 | 169 |
147 ClipboardMap* clipboard_map = g_clipboard_map.Pointer(); | 170 ClipboardMap* clipboard_map = g_clipboard_map.Pointer(); |
148 base::PlatformThreadId id = base::PlatformThread::CurrentId(); | 171 base::PlatformThreadId id = base::PlatformThread::CurrentId(); |
149 ClipboardMap::iterator it = clipboard_map->find(id); | 172 ClipboardMap::iterator it = clipboard_map->find(id); |
150 if (it != clipboard_map->end()) { | 173 if (it != clipboard_map->end()) { |
151 delete it->second; | 174 delete it->second; |
152 clipboard_map->erase(it); | 175 clipboard_map->erase(it); |
153 } | 176 } |
154 } | 177 } |
155 | 178 |
179 void Clipboard::WriteObjects(Buffer buffer, | |
180 const ObjectMap& objects, | |
181 SourceTag tag) { | |
182 WriteObjectsImpl(buffer, objects, tag); | |
183 FOR_EACH_OBSERVER(ClipboardObserverForTesting, | |
184 observer_list_, | |
185 OnWriteObjects(buffer)); | |
186 } | |
187 | |
156 void Clipboard::DispatchObject(ObjectType type, const ObjectMapParams& params) { | 188 void Clipboard::DispatchObject(ObjectType type, const ObjectMapParams& params) { |
157 // All types apart from CBF_WEBKIT need at least 1 non-empty param. | 189 // All types apart from CBF_WEBKIT need at least 1 non-empty param. |
158 if (type != CBF_WEBKIT && (params.empty() || params[0].empty())) | 190 if (type != CBF_WEBKIT && (params.empty() || params[0].empty())) |
159 return; | 191 return; |
160 // Some other types need a non-empty 2nd param. | 192 // Some other types need a non-empty 2nd param. |
161 if ((type == CBF_BOOKMARK || type == CBF_BITMAP || | 193 if ((type == CBF_BOOKMARK || type == CBF_BITMAP || |
162 type == CBF_SMBITMAP || type == CBF_DATA) && | 194 type == CBF_SMBITMAP || type == CBF_DATA) && |
163 (params.size() != 2 || params[1].empty())) | 195 (params.size() != 2 || params[1].empty())) |
164 return; | 196 return; |
165 switch (type) { | 197 switch (type) { |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
258 // UI thread (see DispatchObject()). | 290 // UI thread (see DispatchObject()). |
259 iter->second[0].clear(); | 291 iter->second[0].clear(); |
260 for (size_t i = 0; i < sizeof(SharedMemory*); ++i) | 292 for (size_t i = 0; i < sizeof(SharedMemory*); ++i) |
261 iter->second[0].push_back(reinterpret_cast<char*>(&bitmap)[i]); | 293 iter->second[0].push_back(reinterpret_cast<char*>(&bitmap)[i]); |
262 has_shared_bitmap = true; | 294 has_shared_bitmap = true; |
263 } | 295 } |
264 } | 296 } |
265 } | 297 } |
266 | 298 |
267 } // namespace ui | 299 } // namespace ui |
OLD | NEW |