| 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 <set> |
| 9 | 10 |
| 10 #include "base/lazy_instance.h" | 11 #include "base/lazy_instance.h" |
| 11 #include "base/logging.h" | 12 #include "base/logging.h" |
| 12 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
| 13 #include "base/synchronization/lock.h" | 14 #include "base/synchronization/lock.h" |
| 14 #include "third_party/skia/include/core/SkBitmap.h" | 15 #include "third_party/skia/include/core/SkBitmap.h" |
| 15 #include "ui/gfx/size.h" | 16 #include "ui/gfx/size.h" |
| 16 | 17 |
| 17 namespace ui { | 18 namespace ui { |
| 18 | 19 |
| (...skipping 25 matching lines...) Expand all Loading... |
| 44 | 45 |
| 45 // Mapping from threads to clipboard objects. | 46 // Mapping from threads to clipboard objects. |
| 46 typedef std::map<base::PlatformThreadId, Clipboard*> ClipboardMap; | 47 typedef std::map<base::PlatformThreadId, Clipboard*> ClipboardMap; |
| 47 static base::LazyInstance<ClipboardMap> g_clipboard_map = | 48 static base::LazyInstance<ClipboardMap> g_clipboard_map = |
| 48 LAZY_INSTANCE_INITIALIZER; | 49 LAZY_INSTANCE_INITIALIZER; |
| 49 | 50 |
| 50 // Mutex that controls access to |g_clipboard_map|. | 51 // Mutex that controls access to |g_clipboard_map|. |
| 51 static base::LazyInstance<base::Lock>::Leaky | 52 static base::LazyInstance<base::Lock>::Leaky |
| 52 g_clipboard_map_lock = LAZY_INSTANCE_INITIALIZER; | 53 g_clipboard_map_lock = LAZY_INSTANCE_INITIALIZER; |
| 53 | 54 |
| 55 // Set of registered custom formats. |
| 56 static base::LazyInstance<std::set<Clipboard::FormatType> > |
| 57 g_registered_formats = LAZY_INSTANCE_INITIALIZER; |
| 58 |
| 59 // Mutex that controls access to |g_registered_formats|. |
| 60 static base::LazyInstance<base::Lock>::Leaky g_registered_formats_lock = |
| 61 LAZY_INSTANCE_INITIALIZER; |
| 62 |
| 54 } // namespace | 63 } // namespace |
| 55 | 64 |
| 56 // static | 65 // static |
| 57 void Clipboard::SetAllowedThreads( | 66 void Clipboard::SetAllowedThreads( |
| 58 const std::vector<base::PlatformThreadId>& allowed_threads) { | 67 const std::vector<base::PlatformThreadId>& allowed_threads) { |
| 59 base::AutoLock lock(g_clipboard_map_lock.Get()); | 68 base::AutoLock lock(g_clipboard_map_lock.Get()); |
| 60 | 69 |
| 61 g_allowed_threads.Get().clear(); | 70 g_allowed_threads.Get().clear(); |
| 62 std::copy(allowed_threads.begin(), allowed_threads.end(), | 71 std::copy(allowed_threads.begin(), allowed_threads.end(), |
| 63 std::back_inserter(g_allowed_threads.Get())); | 72 std::back_inserter(g_allowed_threads.Get())); |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 98 | 107 |
| 99 ClipboardMap* clipboard_map = g_clipboard_map.Pointer(); | 108 ClipboardMap* clipboard_map = g_clipboard_map.Pointer(); |
| 100 base::PlatformThreadId id = base::PlatformThread::CurrentId(); | 109 base::PlatformThreadId id = base::PlatformThread::CurrentId(); |
| 101 ClipboardMap::iterator it = clipboard_map->find(id); | 110 ClipboardMap::iterator it = clipboard_map->find(id); |
| 102 if (it != clipboard_map->end()) { | 111 if (it != clipboard_map->end()) { |
| 103 delete it->second; | 112 delete it->second; |
| 104 clipboard_map->erase(it); | 113 clipboard_map->erase(it); |
| 105 } | 114 } |
| 106 } | 115 } |
| 107 | 116 |
| 117 // static |
| 118 Clipboard::FormatType Clipboard::GetFormatType( |
| 119 const std::string& format_string) { |
| 120 FormatType format = GetFormatTypeInternal(format_string); |
| 121 { |
| 122 base::AutoLock lock(g_registered_formats_lock.Get()); |
| 123 g_registered_formats.Get().insert(format); |
| 124 } |
| 125 return format; |
| 126 } |
| 127 |
| 128 bool Clipboard::IsRegisteredFormatType(const FormatType& format) { |
| 129 // Terrible hack to force web/pepper custom types to be registered. |
| 130 // TODO(dcheng): There's got to a better way... |
| 131 GetWebCustomDataFormatType(); |
| 132 GetPepperCustomDataFormatType(); |
| 133 |
| 134 base::AutoLock lock(g_registered_formats_lock.Get()); |
| 135 const std::set<FormatType>& registered_formats = g_registered_formats.Get(); |
| 136 return registered_formats.find(format) != registered_formats.end(); |
| 137 } |
| 138 |
| 108 void Clipboard::DispatchObject(ObjectType type, const ObjectMapParams& params) { | 139 void Clipboard::DispatchObject(ObjectType type, const ObjectMapParams& params) { |
| 109 // All types apart from CBF_WEBKIT need at least 1 non-empty param. | 140 // All types apart from CBF_WEBKIT need at least 1 non-empty param. |
| 110 if (type != CBF_WEBKIT && (params.empty() || params[0].empty())) | 141 if (type != CBF_WEBKIT && (params.empty() || params[0].empty())) |
| 111 return; | 142 return; |
| 112 // Some other types need a non-empty 2nd param. | 143 // Some other types need a non-empty 2nd param. |
| 113 if ((type == CBF_BOOKMARK || type == CBF_SMBITMAP || type == CBF_DATA) && | 144 if ((type == CBF_BOOKMARK || type == CBF_SMBITMAP || type == CBF_DATA) && |
| 114 (params.size() != 2 || params[1].empty())) | 145 (params.size() != 2 || params[1].empty())) |
| 115 return; | 146 return; |
| 116 switch (type) { | 147 switch (type) { |
| 117 case CBF_TEXT: | 148 case CBF_TEXT: |
| (...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 225 iter->second[0].clear(); | 256 iter->second[0].clear(); |
| 226 for (size_t i = 0; i < sizeof(SharedMemory*); ++i) | 257 for (size_t i = 0; i < sizeof(SharedMemory*); ++i) |
| 227 iter->second[0].push_back(reinterpret_cast<char*>(&bitmap)[i]); | 258 iter->second[0].push_back(reinterpret_cast<char*>(&bitmap)[i]); |
| 228 has_shared_bitmap = true; | 259 has_shared_bitmap = true; |
| 229 } | 260 } |
| 230 } | 261 } |
| 231 return true; | 262 return true; |
| 232 } | 263 } |
| 233 | 264 |
| 234 } // namespace ui | 265 } // namespace ui |
| OLD | NEW |