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 | |
104 } // namespace | 95 } // namespace |
105 | 96 |
106 const char Clipboard::kMimeTypeText[] = "text/plain"; | 97 const char Clipboard::kMimeTypeText[] = "text/plain"; |
107 const char Clipboard::kMimeTypeURIList[] = "text/uri-list"; | 98 const char Clipboard::kMimeTypeURIList[] = "text/uri-list"; |
108 const char Clipboard::kMimeTypeDownloadURL[] = "downloadurl"; | 99 const char Clipboard::kMimeTypeDownloadURL[] = "downloadurl"; |
109 const char Clipboard::kMimeTypeHTML[] = "text/html"; | 100 const char Clipboard::kMimeTypeHTML[] = "text/html"; |
110 const char Clipboard::kMimeTypeRTF[] = "text/rtf"; | 101 const char Clipboard::kMimeTypeRTF[] = "text/rtf"; |
111 const char Clipboard::kMimeTypePNG[] = "image/png"; | 102 const char Clipboard::kMimeTypePNG[] = "image/png"; |
112 | 103 |
113 // static | 104 // 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 | |
132 void Clipboard::SetAllowedThreads( | 105 void Clipboard::SetAllowedThreads( |
133 const std::vector<base::PlatformThreadId>& allowed_threads) { | 106 const std::vector<base::PlatformThreadId>& allowed_threads) { |
134 base::AutoLock lock(g_clipboard_map_lock.Get()); | 107 base::AutoLock lock(g_clipboard_map_lock.Get()); |
135 | 108 |
136 g_allowed_threads.Get().clear(); | 109 g_allowed_threads.Get().clear(); |
137 std::copy(allowed_threads.begin(), allowed_threads.end(), | 110 std::copy(allowed_threads.begin(), allowed_threads.end(), |
138 std::back_inserter(g_allowed_threads.Get())); | 111 std::back_inserter(g_allowed_threads.Get())); |
139 } | 112 } |
140 | 113 |
141 // static | 114 // static |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
173 | 146 |
174 ClipboardMap* clipboard_map = g_clipboard_map.Pointer(); | 147 ClipboardMap* clipboard_map = g_clipboard_map.Pointer(); |
175 base::PlatformThreadId id = base::PlatformThread::CurrentId(); | 148 base::PlatformThreadId id = base::PlatformThread::CurrentId(); |
176 ClipboardMap::iterator it = clipboard_map->find(id); | 149 ClipboardMap::iterator it = clipboard_map->find(id); |
177 if (it != clipboard_map->end()) { | 150 if (it != clipboard_map->end()) { |
178 delete it->second; | 151 delete it->second; |
179 clipboard_map->erase(it); | 152 clipboard_map->erase(it); |
180 } | 153 } |
181 } | 154 } |
182 | 155 |
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 | |
191 void Clipboard::DispatchObject(ObjectType type, const ObjectMapParams& params) { | 156 void Clipboard::DispatchObject(ObjectType type, const ObjectMapParams& params) { |
192 // All types apart from CBF_WEBKIT need at least 1 non-empty param. | 157 // All types apart from CBF_WEBKIT need at least 1 non-empty param. |
193 if (type != CBF_WEBKIT && (params.empty() || params[0].empty())) | 158 if (type != CBF_WEBKIT && (params.empty() || params[0].empty())) |
194 return; | 159 return; |
195 // Some other types need a non-empty 2nd param. | 160 // Some other types need a non-empty 2nd param. |
196 if ((type == CBF_BOOKMARK || type == CBF_BITMAP || | 161 if ((type == CBF_BOOKMARK || type == CBF_BITMAP || |
197 type == CBF_SMBITMAP || type == CBF_DATA) && | 162 type == CBF_SMBITMAP || type == CBF_DATA) && |
198 (params.size() != 2 || params[1].empty())) | 163 (params.size() != 2 || params[1].empty())) |
199 return; | 164 return; |
200 switch (type) { | 165 switch (type) { |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
293 // UI thread (see DispatchObject()). | 258 // UI thread (see DispatchObject()). |
294 iter->second[0].clear(); | 259 iter->second[0].clear(); |
295 for (size_t i = 0; i < sizeof(SharedMemory*); ++i) | 260 for (size_t i = 0; i < sizeof(SharedMemory*); ++i) |
296 iter->second[0].push_back(reinterpret_cast<char*>(&bitmap)[i]); | 261 iter->second[0].push_back(reinterpret_cast<char*>(&bitmap)[i]); |
297 has_shared_bitmap = true; | 262 has_shared_bitmap = true; |
298 } | 263 } |
299 } | 264 } |
300 } | 265 } |
301 | 266 |
302 } // namespace ui | 267 } // namespace ui |
OLD | NEW |