| 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 #include <memory> | 9 #include <memory> |
| 10 | 10 |
| (...skipping 14 matching lines...) Expand all Loading... |
| 25 void Clipboard::SetAllowedThreads( | 25 void Clipboard::SetAllowedThreads( |
| 26 const std::vector<base::PlatformThreadId>& allowed_threads) { | 26 const std::vector<base::PlatformThreadId>& allowed_threads) { |
| 27 base::AutoLock lock(clipboard_map_lock_.Get()); | 27 base::AutoLock lock(clipboard_map_lock_.Get()); |
| 28 | 28 |
| 29 allowed_threads_.Get().clear(); | 29 allowed_threads_.Get().clear(); |
| 30 std::copy(allowed_threads.begin(), allowed_threads.end(), | 30 std::copy(allowed_threads.begin(), allowed_threads.end(), |
| 31 std::back_inserter(allowed_threads_.Get())); | 31 std::back_inserter(allowed_threads_.Get())); |
| 32 } | 32 } |
| 33 | 33 |
| 34 // static | 34 // static |
| 35 void Clipboard::SetClipboardForCurrentThread(Clipboard* platform_clipboard) { |
| 36 base::AutoLock lock(clipboard_map_lock_.Get()); |
| 37 base::PlatformThreadId id = Clipboard::GetAndValidateThreadID(); |
| 38 |
| 39 ClipboardMap* clipboard_map = clipboard_map_.Pointer(); |
| 40 ClipboardMap::const_iterator it = clipboard_map->find(id); |
| 41 if (it != clipboard_map->end()) { |
| 42 // This is suboptimal, but because the timing of how we create |
| 43 // WindowManagerConnection, we can end up having created a Clipboard object |
| 44 // before we've established our first mojo connection to mus. |
| 45 delete it->second; |
| 46 clipboard_map->erase(it); |
| 47 } |
| 48 clipboard_map->insert(std::make_pair(id, platform_clipboard)); |
| 49 } |
| 50 |
| 51 // static |
| 35 Clipboard* Clipboard::GetForCurrentThread() { | 52 Clipboard* Clipboard::GetForCurrentThread() { |
| 36 base::AutoLock lock(clipboard_map_lock_.Get()); | 53 base::AutoLock lock(clipboard_map_lock_.Get()); |
| 37 | 54 base::PlatformThreadId id = GetAndValidateThreadID(); |
| 38 base::PlatformThreadId id = base::PlatformThread::CurrentId(); | |
| 39 | |
| 40 AllowedThreadsVector* allowed_threads = allowed_threads_.Pointer(); | |
| 41 if (!allowed_threads->empty()) { | |
| 42 bool found = false; | |
| 43 for (AllowedThreadsVector::const_iterator it = allowed_threads->begin(); | |
| 44 it != allowed_threads->end(); ++it) { | |
| 45 if (*it == id) { | |
| 46 found = true; | |
| 47 break; | |
| 48 } | |
| 49 } | |
| 50 | |
| 51 DCHECK(found); | |
| 52 } | |
| 53 | 55 |
| 54 ClipboardMap* clipboard_map = clipboard_map_.Pointer(); | 56 ClipboardMap* clipboard_map = clipboard_map_.Pointer(); |
| 55 ClipboardMap::const_iterator it = clipboard_map->find(id); | 57 ClipboardMap::const_iterator it = clipboard_map->find(id); |
| 56 if (it != clipboard_map->end()) | 58 if (it != clipboard_map->end()) |
| 57 return it->second; | 59 return it->second; |
| 58 | 60 |
| 59 Clipboard* clipboard = Clipboard::Create(); | 61 Clipboard* clipboard = Clipboard::Create(); |
| 60 clipboard_map->insert(std::make_pair(id, clipboard)); | 62 clipboard_map->insert(std::make_pair(id, clipboard)); |
| 61 return clipboard; | 63 return clipboard; |
| 62 } | 64 } |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 125 std::string(&(params[0].front()), params[0].size())), | 127 std::string(&(params[0].front()), params[0].size())), |
| 126 &(params[1].front()), | 128 &(params[1].front()), |
| 127 params[1].size()); | 129 params[1].size()); |
| 128 break; | 130 break; |
| 129 | 131 |
| 130 default: | 132 default: |
| 131 NOTREACHED(); | 133 NOTREACHED(); |
| 132 } | 134 } |
| 133 } | 135 } |
| 134 | 136 |
| 137 base::PlatformThreadId Clipboard::GetAndValidateThreadID() { |
| 138 base::PlatformThreadId id = base::PlatformThread::CurrentId(); |
| 139 #ifndef NDEBUG |
| 140 AllowedThreadsVector* allowed_threads = allowed_threads_.Pointer(); |
| 141 if (!allowed_threads->empty()) { |
| 142 bool found = false; |
| 143 for (AllowedThreadsVector::const_iterator it = allowed_threads->begin(); |
| 144 it != allowed_threads->end(); ++it) { |
| 145 if (*it == id) { |
| 146 found = true; |
| 147 break; |
| 148 } |
| 149 } |
| 150 |
| 151 DCHECK(found); |
| 152 } |
| 153 #endif |
| 154 |
| 155 return id; |
| 156 } |
| 157 |
| 135 } // namespace ui | 158 } // namespace ui |
| OLD | NEW |