| 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> |
| 8 |
| 9 #include "base/lazy_instance.h" |
| 7 #include "base/logging.h" | 10 #include "base/logging.h" |
| 8 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
| 12 #include "base/synchronization/lock.h" |
| 9 #include "ui/gfx/size.h" | 13 #include "ui/gfx/size.h" |
| 10 | 14 |
| 11 namespace ui { | 15 namespace ui { |
| 12 | 16 |
| 13 namespace { | 17 namespace { |
| 14 | 18 |
| 15 // A compromised renderer could send us bad data, so validate it. | 19 // A compromised renderer could send us bad data, so validate it. |
| 16 // This function only checks that the size parameter makes sense, the caller | 20 // This function only checks that the size parameter makes sense, the caller |
| 17 // is responsible for further validating the bitmap buffer against | 21 // is responsible for further validating the bitmap buffer against |
| 18 // |bitmap_bytes|. | 22 // |bitmap_bytes|. |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 65 if (!bitmap_data || !SharedMemory::IsHandleValid(bitmap_data->handle())) | 69 if (!bitmap_data || !SharedMemory::IsHandleValid(bitmap_data->handle())) |
| 66 return false; | 70 return false; |
| 67 | 71 |
| 68 if (!bitmap_data->Map(bitmap_bytes)) { | 72 if (!bitmap_data->Map(bitmap_bytes)) { |
| 69 PLOG(ERROR) << "Failed to map bitmap memory"; | 73 PLOG(ERROR) << "Failed to map bitmap memory"; |
| 70 return false; | 74 return false; |
| 71 } | 75 } |
| 72 return true; | 76 return true; |
| 73 } | 77 } |
| 74 | 78 |
| 79 // A list of allowed threads. By default, this is empty and no thread checking |
| 80 // is done (in the unit test case), but a user (like content) can set which |
| 81 // threads are allowed to call this method. |
| 82 typedef std::vector<base::PlatformThreadId> AllowedThreadsVector; |
| 83 static base::LazyInstance<AllowedThreadsVector> g_allowed_threads = |
| 84 LAZY_INSTANCE_INITIALIZER; |
| 85 |
| 86 // Mapping from threads to clipboard objects. |
| 87 typedef std::map<base::PlatformThreadId, Clipboard*> ClipboardMap; |
| 88 static base::LazyInstance<ClipboardMap> g_clipboard_map = |
| 89 LAZY_INSTANCE_INITIALIZER; |
| 90 |
| 91 // Mutex that controls access to |g_clipboard_map|. |
| 92 static base::LazyInstance<base::Lock>::Leaky |
| 93 g_clipboard_map_lock = LAZY_INSTANCE_INITIALIZER; |
| 94 |
| 75 } // namespace | 95 } // namespace |
| 76 | 96 |
| 77 const char Clipboard::kMimeTypeText[] = "text/plain"; | 97 const char Clipboard::kMimeTypeText[] = "text/plain"; |
| 78 const char Clipboard::kMimeTypeURIList[] = "text/uri-list"; | 98 const char Clipboard::kMimeTypeURIList[] = "text/uri-list"; |
| 79 const char Clipboard::kMimeTypeDownloadURL[] = "downloadurl"; | 99 const char Clipboard::kMimeTypeDownloadURL[] = "downloadurl"; |
| 80 const char Clipboard::kMimeTypeHTML[] = "text/html"; | 100 const char Clipboard::kMimeTypeHTML[] = "text/html"; |
| 81 const char Clipboard::kMimeTypeRTF[] = "text/rtf"; | 101 const char Clipboard::kMimeTypeRTF[] = "text/rtf"; |
| 82 const char Clipboard::kMimeTypePNG[] = "image/png"; | 102 const char Clipboard::kMimeTypePNG[] = "image/png"; |
| 83 | 103 |
| 104 // static |
| 105 void Clipboard::SetAllowedThreads( |
| 106 const std::vector<base::PlatformThreadId>& allowed_threads) { |
| 107 base::AutoLock lock(g_clipboard_map_lock.Get()); |
| 108 |
| 109 g_allowed_threads.Get().clear(); |
| 110 std::copy(allowed_threads.begin(), allowed_threads.end(), |
| 111 std::back_inserter(g_allowed_threads.Get())); |
| 112 } |
| 113 |
| 114 // static |
| 115 Clipboard* Clipboard::GetForCurrentThread() { |
| 116 base::AutoLock lock(g_clipboard_map_lock.Get()); |
| 117 |
| 118 base::PlatformThreadId id = base::PlatformThread::CurrentId(); |
| 119 |
| 120 AllowedThreadsVector* allowed_threads = g_allowed_threads.Pointer(); |
| 121 if (!allowed_threads->empty()) { |
| 122 bool found = false; |
| 123 for (AllowedThreadsVector::const_iterator it = allowed_threads->begin(); |
| 124 it != allowed_threads->end(); ++it) { |
| 125 if (*it == id) { |
| 126 found = true; |
| 127 break; |
| 128 } |
| 129 } |
| 130 |
| 131 DCHECK(found); |
| 132 } |
| 133 |
| 134 ClipboardMap* clipboard_map = g_clipboard_map.Pointer(); |
| 135 ClipboardMap::iterator it = clipboard_map->find(id); |
| 136 if (it != clipboard_map->end()) |
| 137 return it->second; |
| 138 |
| 139 Clipboard* clipboard = new ui::Clipboard; |
| 140 clipboard_map->insert(std::make_pair(id, clipboard)); |
| 141 return clipboard; |
| 142 } |
| 143 |
| 144 void Clipboard::DestroyClipboardForCurrentThread() { |
| 145 base::AutoLock lock(g_clipboard_map_lock.Get()); |
| 146 |
| 147 ClipboardMap* clipboard_map = g_clipboard_map.Pointer(); |
| 148 base::PlatformThreadId id = base::PlatformThread::CurrentId(); |
| 149 ClipboardMap::iterator it = clipboard_map->find(id); |
| 150 if (it != clipboard_map->end()) { |
| 151 delete it->second; |
| 152 clipboard_map->erase(it); |
| 153 } |
| 154 } |
| 155 |
| 84 void Clipboard::DispatchObject(ObjectType type, const ObjectMapParams& params) { | 156 void Clipboard::DispatchObject(ObjectType type, const ObjectMapParams& params) { |
| 85 // 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. |
| 86 if (type != CBF_WEBKIT && (params.empty() || params[0].empty())) | 158 if (type != CBF_WEBKIT && (params.empty() || params[0].empty())) |
| 87 return; | 159 return; |
| 88 // Some other types need a non-empty 2nd param. | 160 // Some other types need a non-empty 2nd param. |
| 89 if ((type == CBF_BOOKMARK || type == CBF_BITMAP || | 161 if ((type == CBF_BOOKMARK || type == CBF_BITMAP || |
| 90 type == CBF_SMBITMAP || type == CBF_DATA) && | 162 type == CBF_SMBITMAP || type == CBF_DATA) && |
| 91 (params.size() != 2 || params[1].empty())) | 163 (params.size() != 2 || params[1].empty())) |
| 92 return; | 164 return; |
| 93 switch (type) { | 165 switch (type) { |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 186 // UI thread (see DispatchObject()). | 258 // UI thread (see DispatchObject()). |
| 187 iter->second[0].clear(); | 259 iter->second[0].clear(); |
| 188 for (size_t i = 0; i < sizeof(SharedMemory*); ++i) | 260 for (size_t i = 0; i < sizeof(SharedMemory*); ++i) |
| 189 iter->second[0].push_back(reinterpret_cast<char*>(&bitmap)[i]); | 261 iter->second[0].push_back(reinterpret_cast<char*>(&bitmap)[i]); |
| 190 has_shared_bitmap = true; | 262 has_shared_bitmap = true; |
| 191 } | 263 } |
| 192 } | 264 } |
| 193 } | 265 } |
| 194 | 266 |
| 195 } // namespace ui | 267 } // namespace ui |
| OLD | NEW |