| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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/x/selection_utils.h" | 5 #include "ui/base/x/selection_utils.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 | 8 |
| 9 #include <algorithm> | 9 #include <algorithm> |
| 10 #include <set> | 10 #include <set> |
| 11 | 11 |
| 12 #include "base/i18n/icu_string_conversions.h" | 12 #include "base/i18n/icu_string_conversions.h" |
| 13 #include "base/logging.h" | 13 #include "base/logging.h" |
| 14 #include "base/strings/string_split.h" | 14 #include "base/strings/string_split.h" |
| 15 #include "base/strings/string_util.h" | 15 #include "base/strings/string_util.h" |
| 16 #include "base/strings/utf_string_conversions.h" | 16 #include "base/strings/utf_string_conversions.h" |
| 17 #include "ui/base/clipboard/clipboard.h" | 17 #include "ui/base/clipboard/clipboard.h" |
| 18 #include "ui/base/x/x11_util.h" | 18 #include "ui/base/x/x11_util.h" |
| 19 #include "ui/gfx/x/x11_atom_cache.h" | |
| 20 | 19 |
| 21 namespace ui { | 20 namespace ui { |
| 22 | 21 |
| 23 const char kString[] = "STRING"; | 22 const char kString[] = "STRING"; |
| 24 const char kText[] = "TEXT"; | 23 const char kText[] = "TEXT"; |
| 25 const char kTextPlain[] = "text/plain"; | 24 const char kTextPlain[] = "text/plain"; |
| 26 const char kTextPlainUtf8[] = "text/plain;charset=utf-8"; | 25 const char kTextPlainUtf8[] = "text/plain;charset=utf-8"; |
| 27 const char kUtf8String[] = "UTF8_STRING"; | 26 const char kUtf8String[] = "UTF8_STRING"; |
| 28 | 27 |
| 29 const char* kSelectionDataAtoms[] = { | 28 std::vector<::Atom> GetTextAtomsFrom() { |
| 30 Clipboard::kMimeTypeHTML, | |
| 31 kString, | |
| 32 kText, | |
| 33 kTextPlain, | |
| 34 kTextPlainUtf8, | |
| 35 kUtf8String, | |
| 36 NULL | |
| 37 }; | |
| 38 | |
| 39 std::vector< ::Atom> GetTextAtomsFrom(const X11AtomCache* atom_cache) { | |
| 40 std::vector< ::Atom> atoms; | 29 std::vector< ::Atom> atoms; |
| 41 atoms.push_back(atom_cache->GetAtom(kUtf8String)); | 30 atoms.push_back(GetAtom(kUtf8String)); |
| 42 atoms.push_back(atom_cache->GetAtom(kString)); | 31 atoms.push_back(GetAtom(kString)); |
| 43 atoms.push_back(atom_cache->GetAtom(kText)); | 32 atoms.push_back(GetAtom(kText)); |
| 44 atoms.push_back(atom_cache->GetAtom(kTextPlain)); | 33 atoms.push_back(GetAtom(kTextPlain)); |
| 45 atoms.push_back(atom_cache->GetAtom(kTextPlainUtf8)); | 34 atoms.push_back(GetAtom(kTextPlainUtf8)); |
| 46 return atoms; | 35 return atoms; |
| 47 } | 36 } |
| 48 | 37 |
| 49 std::vector< ::Atom> GetURLAtomsFrom(const X11AtomCache* atom_cache) { | 38 std::vector<::Atom> GetURLAtomsFrom() { |
| 50 std::vector< ::Atom> atoms; | 39 std::vector< ::Atom> atoms; |
| 51 atoms.push_back(atom_cache->GetAtom(Clipboard::kMimeTypeURIList)); | 40 atoms.push_back(GetAtom(Clipboard::kMimeTypeURIList)); |
| 52 atoms.push_back(atom_cache->GetAtom(Clipboard::kMimeTypeMozillaURL)); | 41 atoms.push_back(GetAtom(Clipboard::kMimeTypeMozillaURL)); |
| 53 return atoms; | 42 return atoms; |
| 54 } | 43 } |
| 55 | 44 |
| 56 std::vector< ::Atom> GetURIListAtomsFrom(const X11AtomCache* atom_cache) { | 45 std::vector<::Atom> GetURIListAtomsFrom() { |
| 57 std::vector< ::Atom> atoms; | 46 std::vector< ::Atom> atoms; |
| 58 atoms.push_back(atom_cache->GetAtom(Clipboard::kMimeTypeURIList)); | 47 atoms.push_back(GetAtom(Clipboard::kMimeTypeURIList)); |
| 59 return atoms; | 48 return atoms; |
| 60 } | 49 } |
| 61 | 50 |
| 62 void GetAtomIntersection(const std::vector< ::Atom>& desired, | 51 void GetAtomIntersection(const std::vector< ::Atom>& desired, |
| 63 const std::vector< ::Atom>& offered, | 52 const std::vector< ::Atom>& offered, |
| 64 std::vector< ::Atom>* output) { | 53 std::vector< ::Atom>* output) { |
| 65 for (std::vector< ::Atom>::const_iterator it = desired.begin(); | 54 for (std::vector< ::Atom>::const_iterator it = desired.begin(); |
| 66 it != desired.end(); ++it) { | 55 it != desired.end(); ++it) { |
| 67 std::vector< ::Atom>::const_iterator jt = | 56 std::vector< ::Atom>::const_iterator jt = |
| 68 std::find(offered.begin(), offered.end(), *it); | 57 std::find(offered.begin(), offered.end(), *it); |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 148 std::vector< ::Atom> SelectionFormatMap::GetTypes() const { | 137 std::vector< ::Atom> SelectionFormatMap::GetTypes() const { |
| 149 std::vector< ::Atom> atoms; | 138 std::vector< ::Atom> atoms; |
| 150 for (const_iterator it = data_.begin(); it != data_.end(); ++it) | 139 for (const_iterator it = data_.begin(); it != data_.end(); ++it) |
| 151 atoms.push_back(it->first); | 140 atoms.push_back(it->first); |
| 152 | 141 |
| 153 return atoms; | 142 return atoms; |
| 154 } | 143 } |
| 155 | 144 |
| 156 /////////////////////////////////////////////////////////////////////////////// | 145 /////////////////////////////////////////////////////////////////////////////// |
| 157 | 146 |
| 158 SelectionData::SelectionData() | 147 SelectionData::SelectionData() : type_(None) {} |
| 159 : type_(None), | |
| 160 atom_cache_(gfx::GetXDisplay(), kSelectionDataAtoms) { | |
| 161 } | |
| 162 | 148 |
| 163 SelectionData::SelectionData( | 149 SelectionData::SelectionData( |
| 164 ::Atom type, | 150 ::Atom type, |
| 165 const scoped_refptr<base::RefCountedMemory>& memory) | 151 const scoped_refptr<base::RefCountedMemory>& memory) |
| 166 : type_(type), | 152 : type_(type), memory_(memory) {} |
| 167 memory_(memory), | |
| 168 atom_cache_(gfx::GetXDisplay(), kSelectionDataAtoms) { | |
| 169 } | |
| 170 | 153 |
| 171 SelectionData::SelectionData(const SelectionData& rhs) | 154 SelectionData::SelectionData(const SelectionData& rhs) |
| 172 : type_(rhs.type_), | 155 : type_(rhs.type_), memory_(rhs.memory_) {} |
| 173 memory_(rhs.memory_), | |
| 174 atom_cache_(gfx::GetXDisplay(), kSelectionDataAtoms) { | |
| 175 } | |
| 176 | 156 |
| 177 SelectionData::~SelectionData() {} | 157 SelectionData::~SelectionData() {} |
| 178 | 158 |
| 179 SelectionData& SelectionData::operator=(const SelectionData& rhs) { | 159 SelectionData& SelectionData::operator=(const SelectionData& rhs) { |
| 180 type_ = rhs.type_; | 160 type_ = rhs.type_; |
| 181 memory_ = rhs.memory_; | 161 memory_ = rhs.memory_; |
| 182 // TODO(erg): In some future where we have to support multiple X Displays, | 162 // TODO(erg): In some future where we have to support multiple X Displays, |
| 183 // the following will also need to deal with the display. | 163 // the following will also need to deal with the display. |
| 184 return *this; | 164 return *this; |
| 185 } | 165 } |
| 186 | 166 |
| 187 bool SelectionData::IsValid() const { | 167 bool SelectionData::IsValid() const { |
| 188 return type_ != None; | 168 return type_ != None; |
| 189 } | 169 } |
| 190 | 170 |
| 191 ::Atom SelectionData::GetType() const { | 171 ::Atom SelectionData::GetType() const { |
| 192 return type_; | 172 return type_; |
| 193 } | 173 } |
| 194 | 174 |
| 195 const unsigned char* SelectionData::GetData() const { | 175 const unsigned char* SelectionData::GetData() const { |
| 196 return memory_.get() ? memory_->front() : NULL; | 176 return memory_.get() ? memory_->front() : NULL; |
| 197 } | 177 } |
| 198 | 178 |
| 199 size_t SelectionData::GetSize() const { | 179 size_t SelectionData::GetSize() const { |
| 200 return memory_.get() ? memory_->size() : 0; | 180 return memory_.get() ? memory_->size() : 0; |
| 201 } | 181 } |
| 202 | 182 |
| 203 std::string SelectionData::GetText() const { | 183 std::string SelectionData::GetText() const { |
| 204 if (type_ == atom_cache_.GetAtom(kUtf8String) || | 184 if (type_ == GetAtom(kUtf8String) || type_ == GetAtom(kText) || |
| 205 type_ == atom_cache_.GetAtom(kText) || | 185 type_ == GetAtom(kTextPlainUtf8)) { |
| 206 type_ == atom_cache_.GetAtom(kTextPlainUtf8)) { | |
| 207 return RefCountedMemoryToString(memory_); | 186 return RefCountedMemoryToString(memory_); |
| 208 } else if (type_ == atom_cache_.GetAtom(kString) || | 187 } else if (type_ == GetAtom(kString) || type_ == GetAtom(kTextPlain)) { |
| 209 type_ == atom_cache_.GetAtom(kTextPlain)) { | |
| 210 std::string result; | 188 std::string result; |
| 211 base::ConvertToUtf8AndNormalize(RefCountedMemoryToString(memory_), | 189 base::ConvertToUtf8AndNormalize(RefCountedMemoryToString(memory_), |
| 212 base::kCodepageLatin1, | 190 base::kCodepageLatin1, |
| 213 &result); | 191 &result); |
| 214 return result; | 192 return result; |
| 215 } else { | 193 } else { |
| 216 // BTW, I looked at COMPOUND_TEXT, and there's no way we're going to | 194 // BTW, I looked at COMPOUND_TEXT, and there's no way we're going to |
| 217 // support that. Yuck. | 195 // support that. Yuck. |
| 218 NOTREACHED(); | 196 NOTREACHED(); |
| 219 return std::string(); | 197 return std::string(); |
| 220 } | 198 } |
| 221 } | 199 } |
| 222 | 200 |
| 223 base::string16 SelectionData::GetHtml() const { | 201 base::string16 SelectionData::GetHtml() const { |
| 224 base::string16 markup; | 202 base::string16 markup; |
| 225 | 203 |
| 226 if (type_ == atom_cache_.GetAtom(Clipboard::kMimeTypeHTML)) { | 204 if (type_ == GetAtom(Clipboard::kMimeTypeHTML)) { |
| 227 const unsigned char* data = GetData(); | 205 const unsigned char* data = GetData(); |
| 228 size_t size = GetSize(); | 206 size_t size = GetSize(); |
| 229 | 207 |
| 230 // If the data starts with 0xFEFF, i.e., Byte Order Mark, assume it is | 208 // If the data starts with 0xFEFF, i.e., Byte Order Mark, assume it is |
| 231 // UTF-16, otherwise assume UTF-8. | 209 // UTF-16, otherwise assume UTF-8. |
| 232 if (size >= 2 && | 210 if (size >= 2 && |
| 233 reinterpret_cast<const uint16_t*>(data)[0] == 0xFEFF) { | 211 reinterpret_cast<const uint16_t*>(data)[0] == 0xFEFF) { |
| 234 markup.assign(reinterpret_cast<const uint16_t*>(data) + 1, | 212 markup.assign(reinterpret_cast<const uint16_t*>(data) + 1, |
| 235 (size / 2) - 1); | 213 (size / 2) - 1); |
| 236 } else { | 214 } else { |
| (...skipping 13 matching lines...) Expand all Loading... |
| 250 | 228 |
| 251 void SelectionData::AssignTo(std::string* result) const { | 229 void SelectionData::AssignTo(std::string* result) const { |
| 252 *result = RefCountedMemoryToString(memory_); | 230 *result = RefCountedMemoryToString(memory_); |
| 253 } | 231 } |
| 254 | 232 |
| 255 void SelectionData::AssignTo(base::string16* result) const { | 233 void SelectionData::AssignTo(base::string16* result) const { |
| 256 *result = RefCountedMemoryToString16(memory_); | 234 *result = RefCountedMemoryToString16(memory_); |
| 257 } | 235 } |
| 258 | 236 |
| 259 } // namespace ui | 237 } // namespace ui |
| OLD | NEW |