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 #include <limits> | 8 #include <limits> |
9 | 9 |
10 #include "base/lazy_instance.h" | |
11 #include "base/logging.h" | 10 #include "base/logging.h" |
12 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
13 #include "base/synchronization/lock.h" | |
14 #include "third_party/skia/include/core/SkBitmap.h" | 12 #include "third_party/skia/include/core/SkBitmap.h" |
15 #include "ui/gfx/size.h" | 13 #include "ui/gfx/size.h" |
16 | 14 |
17 namespace ui { | 15 namespace ui { |
18 | 16 |
19 namespace { | 17 namespace { |
20 | 18 |
21 // Valides a shared bitmap on the clipboard. | 19 // Valides a shared bitmap on the clipboard. |
22 // Returns true if the clipboard data makes sense and it's safe to access the | 20 // Returns true if the clipboard data makes sense and it's safe to access the |
23 // bitmap. | 21 // bitmap. |
24 bool ValidateAndMapSharedBitmap(size_t bitmap_bytes, | 22 bool ValidateAndMapSharedBitmap(size_t bitmap_bytes, |
25 base::SharedMemory* bitmap_data) { | 23 base::SharedMemory* bitmap_data) { |
26 using base::SharedMemory; | 24 using base::SharedMemory; |
27 | 25 |
28 if (!bitmap_data || !SharedMemory::IsHandleValid(bitmap_data->handle())) | 26 if (!bitmap_data || !SharedMemory::IsHandleValid(bitmap_data->handle())) |
29 return false; | 27 return false; |
30 | 28 |
31 if (!bitmap_data->Map(bitmap_bytes)) { | 29 if (!bitmap_data->Map(bitmap_bytes)) { |
32 PLOG(ERROR) << "Failed to map bitmap memory"; | 30 PLOG(ERROR) << "Failed to map bitmap memory"; |
33 return false; | 31 return false; |
34 } | 32 } |
35 return true; | 33 return true; |
36 } | 34 } |
37 | 35 |
38 // A list of allowed threads. By default, this is empty and no thread checking | 36 } // namespace |
39 // is done (in the unit test case), but a user (like content) can set which | 37 |
40 // threads are allowed to call this method. | 38 base::LazyInstance<Clipboard::AllowedThreadsVector> |
41 typedef std::vector<base::PlatformThreadId> AllowedThreadsVector; | 39 Clipboard::allowed_threads_ = LAZY_INSTANCE_INITIALIZER; |
42 static base::LazyInstance<AllowedThreadsVector> g_allowed_threads = | 40 base::LazyInstance<Clipboard::ClipboardMap> Clipboard::clipboard_map_ = |
43 LAZY_INSTANCE_INITIALIZER; | 41 LAZY_INSTANCE_INITIALIZER; |
44 | 42 base::LazyInstance<base::Lock>::Leaky Clipboard::clipboard_map_lock_ = |
45 // Mapping from threads to clipboard objects. | |
46 typedef std::map<base::PlatformThreadId, Clipboard*> ClipboardMap; | |
47 static base::LazyInstance<ClipboardMap> g_clipboard_map = | |
48 LAZY_INSTANCE_INITIALIZER; | 43 LAZY_INSTANCE_INITIALIZER; |
49 | 44 |
50 // Mutex that controls access to |g_clipboard_map|. | |
51 static base::LazyInstance<base::Lock>::Leaky | |
52 g_clipboard_map_lock = LAZY_INSTANCE_INITIALIZER; | |
53 | |
54 } // namespace | |
55 | |
56 // static | 45 // static |
57 void Clipboard::SetAllowedThreads( | 46 void Clipboard::SetAllowedThreads( |
58 const std::vector<base::PlatformThreadId>& allowed_threads) { | 47 const std::vector<base::PlatformThreadId>& allowed_threads) { |
59 base::AutoLock lock(g_clipboard_map_lock.Get()); | 48 base::AutoLock lock(clipboard_map_lock_.Get()); |
60 | 49 |
61 g_allowed_threads.Get().clear(); | 50 allowed_threads_.Get().clear(); |
62 std::copy(allowed_threads.begin(), allowed_threads.end(), | 51 std::copy(allowed_threads.begin(), allowed_threads.end(), |
63 std::back_inserter(g_allowed_threads.Get())); | 52 std::back_inserter(allowed_threads_.Get())); |
64 } | 53 } |
65 | 54 |
66 // static | 55 // static |
67 Clipboard* Clipboard::GetForCurrentThread() { | 56 Clipboard* Clipboard::GetForCurrentThread() { |
68 base::AutoLock lock(g_clipboard_map_lock.Get()); | 57 base::AutoLock lock(clipboard_map_lock_.Get()); |
69 | 58 |
70 base::PlatformThreadId id = base::PlatformThread::CurrentId(); | 59 base::PlatformThreadId id = base::PlatformThread::CurrentId(); |
71 | 60 |
72 AllowedThreadsVector* allowed_threads = g_allowed_threads.Pointer(); | 61 AllowedThreadsVector* allowed_threads = allowed_threads_.Pointer(); |
73 if (!allowed_threads->empty()) { | 62 if (!allowed_threads->empty()) { |
74 bool found = false; | 63 bool found = false; |
75 for (AllowedThreadsVector::const_iterator it = allowed_threads->begin(); | 64 for (AllowedThreadsVector::const_iterator it = allowed_threads->begin(); |
76 it != allowed_threads->end(); ++it) { | 65 it != allowed_threads->end(); ++it) { |
77 if (*it == id) { | 66 if (*it == id) { |
78 found = true; | 67 found = true; |
79 break; | 68 break; |
80 } | 69 } |
81 } | 70 } |
82 | 71 |
83 DCHECK(found); | 72 DCHECK(found); |
84 } | 73 } |
85 | 74 |
86 ClipboardMap* clipboard_map = g_clipboard_map.Pointer(); | 75 ClipboardMap* clipboard_map = clipboard_map_.Pointer(); |
87 ClipboardMap::iterator it = clipboard_map->find(id); | 76 ClipboardMap::const_iterator it = clipboard_map->find(id); |
88 if (it != clipboard_map->end()) | 77 if (it != clipboard_map->end()) |
89 return it->second; | 78 return it->second; |
90 | 79 |
91 Clipboard* clipboard = Clipboard::Create(); | 80 Clipboard* clipboard = Clipboard::Create(); |
92 clipboard_map->insert(std::make_pair(id, clipboard)); | 81 clipboard_map->insert(std::make_pair(id, clipboard)); |
93 return clipboard; | 82 return clipboard; |
94 } | 83 } |
95 | 84 |
96 void Clipboard::DestroyClipboardForCurrentThread() { | 85 void Clipboard::DestroyClipboardForCurrentThread() { |
97 base::AutoLock lock(g_clipboard_map_lock.Get()); | 86 base::AutoLock lock(clipboard_map_lock_.Get()); |
98 | 87 |
99 ClipboardMap* clipboard_map = g_clipboard_map.Pointer(); | 88 ClipboardMap* clipboard_map = clipboard_map_.Pointer(); |
100 base::PlatformThreadId id = base::PlatformThread::CurrentId(); | 89 base::PlatformThreadId id = base::PlatformThread::CurrentId(); |
101 ClipboardMap::iterator it = clipboard_map->find(id); | 90 ClipboardMap::iterator it = clipboard_map->find(id); |
102 if (it != clipboard_map->end()) { | 91 if (it != clipboard_map->end()) { |
103 delete it->second; | 92 delete it->second; |
104 clipboard_map->erase(it); | 93 clipboard_map->erase(it); |
105 } | 94 } |
106 } | 95 } |
107 | 96 |
108 void Clipboard::DispatchObject(ObjectType type, const ObjectMapParams& params) { | 97 void Clipboard::DispatchObject(ObjectType type, const ObjectMapParams& params) { |
109 // All types apart from CBF_WEBKIT need at least 1 non-empty param. | 98 // All types apart from CBF_WEBKIT need at least 1 non-empty param. |
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
224 iter->second[0].clear(); | 213 iter->second[0].clear(); |
225 for (size_t i = 0; i < sizeof(SharedMemory*); ++i) | 214 for (size_t i = 0; i < sizeof(SharedMemory*); ++i) |
226 iter->second[0].push_back(reinterpret_cast<char*>(&bitmap)[i]); | 215 iter->second[0].push_back(reinterpret_cast<char*>(&bitmap)[i]); |
227 has_shared_bitmap = true; | 216 has_shared_bitmap = true; |
228 } | 217 } |
229 } | 218 } |
230 return true; | 219 return true; |
231 } | 220 } |
232 | 221 |
233 } // namespace ui | 222 } // namespace ui |
OLD | NEW |