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