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 "base/lazy_instance.h" |
7 #include "base/logging.h" | 8 #include "base/logging.h" |
8 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
| 10 #include "base/synchronization/lock.h" |
9 #include "ui/gfx/size.h" | 11 #include "ui/gfx/size.h" |
10 | 12 |
11 namespace ui { | 13 namespace ui { |
12 | 14 |
13 namespace { | 15 namespace { |
14 | 16 |
15 // A compromised renderer could send us bad data, so validate it. | 17 // A compromised renderer could send us bad data, so validate it. |
16 // This function only checks that the size parameter makes sense, the caller | 18 // This function only checks that the size parameter makes sense, the caller |
17 // is responsible for further validating the bitmap buffer against | 19 // is responsible for further validating the bitmap buffer against |
18 // |bitmap_bytes|. | 20 // |bitmap_bytes|. |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
65 if (!bitmap_data || !SharedMemory::IsHandleValid(bitmap_data->handle())) | 67 if (!bitmap_data || !SharedMemory::IsHandleValid(bitmap_data->handle())) |
66 return false; | 68 return false; |
67 | 69 |
68 if (!bitmap_data->Map(bitmap_bytes)) { | 70 if (!bitmap_data->Map(bitmap_bytes)) { |
69 PLOG(ERROR) << "Failed to map bitmap memory"; | 71 PLOG(ERROR) << "Failed to map bitmap memory"; |
70 return false; | 72 return false; |
71 } | 73 } |
72 return true; | 74 return true; |
73 } | 75 } |
74 | 76 |
| 77 // Mapping from threads to clipboard objects. |
| 78 typedef std::map<base::PlatformThreadId, Clipboard*> ClipboardMap; |
| 79 static base::LazyInstance<ClipboardMap> g_clipboard_map = |
| 80 LAZY_INSTANCE_INITIALIZER; |
| 81 |
| 82 // Mutex that controls access to |g_clipboard_map|. |
| 83 static base::LazyInstance<base::Lock>::Leaky |
| 84 g_clipboard_map_lock = LAZY_INSTANCE_INITIALIZER; |
| 85 |
75 } // namespace | 86 } // namespace |
76 | 87 |
77 const char Clipboard::kMimeTypeText[] = "text/plain"; | 88 const char Clipboard::kMimeTypeText[] = "text/plain"; |
78 const char Clipboard::kMimeTypeURIList[] = "text/uri-list"; | 89 const char Clipboard::kMimeTypeURIList[] = "text/uri-list"; |
79 const char Clipboard::kMimeTypeDownloadURL[] = "downloadurl"; | 90 const char Clipboard::kMimeTypeDownloadURL[] = "downloadurl"; |
80 const char Clipboard::kMimeTypeHTML[] = "text/html"; | 91 const char Clipboard::kMimeTypeHTML[] = "text/html"; |
81 const char Clipboard::kMimeTypeRTF[] = "text/rtf"; | 92 const char Clipboard::kMimeTypeRTF[] = "text/rtf"; |
82 const char Clipboard::kMimeTypePNG[] = "image/png"; | 93 const char Clipboard::kMimeTypePNG[] = "image/png"; |
83 | 94 |
| 95 // static |
| 96 Clipboard* Clipboard::GetForCurrentThread() { |
| 97 base::AutoLock lock(g_clipboard_map_lock.Get()); |
| 98 |
| 99 base::PlatformThreadId id = base::PlatformThread::CurrentId(); |
| 100 ClipboardMap* clipboard_map = g_clipboard_map.Pointer(); |
| 101 ClipboardMap::iterator it = clipboard_map->find(id); |
| 102 if (it != clipboard_map->end()) |
| 103 return it->second; |
| 104 |
| 105 Clipboard* clipboard = new ui::Clipboard; |
| 106 clipboard_map->insert(std::make_pair(id, clipboard)); |
| 107 return clipboard; |
| 108 } |
| 109 |
| 110 void Clipboard::DestroyClipboardForCurrentThread() { |
| 111 base::AutoLock lock(g_clipboard_map_lock.Get()); |
| 112 |
| 113 ClipboardMap* clipboard_map = g_clipboard_map.Pointer(); |
| 114 base::PlatformThreadId id = base::PlatformThread::CurrentId(); |
| 115 ClipboardMap::iterator it = clipboard_map->find(id); |
| 116 if (it != clipboard_map->end()) { |
| 117 delete it->second; |
| 118 clipboard_map->erase(it); |
| 119 } |
| 120 } |
| 121 |
84 void Clipboard::DispatchObject(ObjectType type, const ObjectMapParams& params) { | 122 void Clipboard::DispatchObject(ObjectType type, const ObjectMapParams& params) { |
85 // All types apart from CBF_WEBKIT need at least 1 non-empty param. | 123 // All types apart from CBF_WEBKIT need at least 1 non-empty param. |
86 if (type != CBF_WEBKIT && (params.empty() || params[0].empty())) | 124 if (type != CBF_WEBKIT && (params.empty() || params[0].empty())) |
87 return; | 125 return; |
88 // Some other types need a non-empty 2nd param. | 126 // Some other types need a non-empty 2nd param. |
89 if ((type == CBF_BOOKMARK || type == CBF_BITMAP || | 127 if ((type == CBF_BOOKMARK || type == CBF_BITMAP || |
90 type == CBF_SMBITMAP || type == CBF_DATA) && | 128 type == CBF_SMBITMAP || type == CBF_DATA) && |
91 (params.size() != 2 || params[1].empty())) | 129 (params.size() != 2 || params[1].empty())) |
92 return; | 130 return; |
93 switch (type) { | 131 switch (type) { |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
186 // UI thread (see DispatchObject()). | 224 // UI thread (see DispatchObject()). |
187 iter->second[0].clear(); | 225 iter->second[0].clear(); |
188 for (size_t i = 0; i < sizeof(SharedMemory*); ++i) | 226 for (size_t i = 0; i < sizeof(SharedMemory*); ++i) |
189 iter->second[0].push_back(reinterpret_cast<char*>(&bitmap)[i]); | 227 iter->second[0].push_back(reinterpret_cast<char*>(&bitmap)[i]); |
190 has_shared_bitmap = true; | 228 has_shared_bitmap = true; |
191 } | 229 } |
192 } | 230 } |
193 } | 231 } |
194 | 232 |
195 } // namespace ui | 233 } // namespace ui |
OLD | NEW |