| 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 #ifndef UI_BASE_X_SELECTION_UTILS_H_ | 5 #ifndef UI_BASE_X_SELECTION_UTILS_H_ |
| 6 #define UI_BASE_X_SELECTION_UTILS_H_ | 6 #define UI_BASE_X_SELECTION_UTILS_H_ |
| 7 | 7 |
| 8 #include <X11/Xlib.h> | 8 #include <X11/Xlib.h> |
| 9 | 9 |
| 10 // Get rid of a macro from Xlib.h that conflicts with Aura's RootWindow class. | 10 // Get rid of a macro from Xlib.h that conflicts with Aura's RootWindow class. |
| 11 #undef RootWindow | 11 #undef RootWindow |
| 12 | 12 |
| 13 #include <map> | 13 #include <map> |
| 14 | 14 |
| 15 #include "base/basictypes.h" | 15 #include "base/basictypes.h" |
| 16 #include "base/memory/ref_counted_memory.h" |
| 16 #include "ui/base/clipboard/clipboard.h" | 17 #include "ui/base/clipboard/clipboard.h" |
| 17 #include "ui/base/ui_export.h" | 18 #include "ui/base/ui_export.h" |
| 18 #include "ui/base/x/x11_atom_cache.h" | 19 #include "ui/base/x/x11_atom_cache.h" |
| 19 | 20 |
| 20 namespace ui { | 21 namespace ui { |
| 21 class SelectionData; | 22 class SelectionData; |
| 22 class X11AtomCache; | 23 class X11AtomCache; |
| 23 | 24 |
| 24 extern const char kMimeTypeMozillaURL[]; | 25 extern const char kMimeTypeMozillaURL[]; |
| 25 extern const char kString[]; | 26 extern const char kString[]; |
| 26 extern const char kText[]; | 27 extern const char kText[]; |
| 27 extern const char kUtf8String[]; | 28 extern const char kUtf8String[]; |
| 28 | 29 |
| 29 // Returns a list of all text atoms that we handle. | 30 // Returns a list of all text atoms that we handle. |
| 30 UI_EXPORT std::vector< ::Atom> GetTextAtomsFrom(const X11AtomCache* atom_cache); | 31 UI_EXPORT std::vector< ::Atom> GetTextAtomsFrom(const X11AtomCache* atom_cache); |
| 31 | 32 |
| 32 UI_EXPORT std::vector< ::Atom> GetURLAtomsFrom(const X11AtomCache* atom_cache); | 33 UI_EXPORT std::vector< ::Atom> GetURLAtomsFrom(const X11AtomCache* atom_cache); |
| 33 | 34 |
| 34 // Places the intersection of |one| and |two| into |output|. | 35 // Places the intersection of |one| and |two| into |output|. |
| 35 UI_EXPORT void GetAtomIntersection(const std::vector< ::Atom>& one, | 36 UI_EXPORT void GetAtomIntersection(const std::vector< ::Atom>& one, |
| 36 const std::vector< ::Atom>& two, | 37 const std::vector< ::Atom>& two, |
| 37 std::vector< ::Atom>* output); | 38 std::vector< ::Atom>* output); |
| 38 | 39 |
| 40 // Takes the raw bytes of the string16 and copies them into |bytes|. |
| 41 UI_EXPORT void AddString16ToVector(const string16& str, |
| 42 std::vector<unsigned char>* bytes); |
| 43 |
| 44 UI_EXPORT std::string RefCountedMemoryToString( |
| 45 const scoped_refptr<base::RefCountedMemory>& memory); |
| 46 |
| 47 UI_EXPORT string16 RefCountedMemoryToString16( |
| 48 const scoped_refptr<base::RefCountedMemory>& memory); |
| 49 |
| 39 /////////////////////////////////////////////////////////////////////////////// | 50 /////////////////////////////////////////////////////////////////////////////// |
| 40 | 51 |
| 41 // Represents the selection in different data formats. Binary data passed in is | 52 // Represents the selection in different data formats. Binary data passed in is |
| 42 // assumed to be allocated with new char[], and is owned by SelectionFormatMap. | 53 // assumed to be allocated with new char[], and is owned by SelectionFormatMap. |
| 43 class UI_EXPORT SelectionFormatMap { | 54 class UI_EXPORT SelectionFormatMap { |
| 44 public: | 55 public: |
| 45 // Our internal data store, which we only expose through iterators. | 56 // Our internal data store, which we only expose through iterators. |
| 46 typedef std::map< ::Atom, std::pair<char*, size_t> > InternalMap; | 57 typedef std::map< ::Atom, scoped_refptr<base::RefCountedMemory> > InternalMap; |
| 47 typedef std::map< ::Atom, std::pair<char*, size_t> >::const_iterator | 58 typedef InternalMap::const_iterator const_iterator; |
| 48 const_iterator; | |
| 49 | 59 |
| 50 SelectionFormatMap(); | 60 SelectionFormatMap(); |
| 51 ~SelectionFormatMap(); | 61 ~SelectionFormatMap(); |
| 62 // Copy and assignment deliberately open. |
| 52 | 63 |
| 53 // Adds the selection in the format |atom|. Ownership of |data| is passed to | 64 // Adds the selection in the format |atom|. Ownership of |data| is passed to |
| 54 // us. | 65 // us. |
| 55 void Insert(::Atom atom, char* data, size_t size); | 66 void Insert(::Atom atom, const scoped_refptr<base::RefCountedMemory>& item); |
| 56 | 67 |
| 57 // Returns the first of the requested_types or NULL if missing. | 68 // Returns the first of the requested_types or NULL if missing. |
| 58 ui::SelectionData* GetFirstOf( | 69 ui::SelectionData GetFirstOf( |
| 59 const std::vector< ::Atom>& requested_types) const; | 70 const std::vector< ::Atom>& requested_types) const; |
| 60 | 71 |
| 61 // Returns all the selected types. | 72 // Returns all the selected types. |
| 62 std::vector< ::Atom> GetTypes() const; | 73 std::vector< ::Atom> GetTypes() const; |
| 63 | 74 |
| 64 // Creates a copy of the selection data. | |
| 65 scoped_ptr<SelectionFormatMap> Clone() const; | |
| 66 | |
| 67 // Pass through to STL map. Only allow non-mutation access. | 75 // Pass through to STL map. Only allow non-mutation access. |
| 68 const_iterator begin() const { return data_.begin(); } | 76 const_iterator begin() const { return data_.begin(); } |
| 69 const_iterator end() const { return data_.end(); } | 77 const_iterator end() const { return data_.end(); } |
| 70 const_iterator find(::Atom atom) const { return data_.find(atom); } | 78 const_iterator find(::Atom atom) const { return data_.find(atom); } |
| 71 size_t size() const { return data_.size(); } | 79 size_t size() const { return data_.size(); } |
| 72 | 80 |
| 73 private: | 81 private: |
| 74 InternalMap data_; | 82 InternalMap data_; |
| 75 | |
| 76 DISALLOW_COPY_AND_ASSIGN(SelectionFormatMap); | |
| 77 }; | 83 }; |
| 78 | 84 |
| 79 /////////////////////////////////////////////////////////////////////////////// | 85 /////////////////////////////////////////////////////////////////////////////// |
| 80 | 86 |
| 81 // A holder for data with optional X11 deletion semantics. | 87 // A holder for data with optional X11 deletion semantics. |
| 82 class UI_EXPORT SelectionData { | 88 class UI_EXPORT SelectionData { |
| 83 public: | 89 public: |
| 84 // |atom_cache| is still owned by caller. | 90 // |atom_cache| is still owned by caller. |
| 85 SelectionData(); | 91 SelectionData(); |
| 92 SelectionData(::Atom type, |
| 93 const scoped_refptr<base::RefCountedMemory>& memory); |
| 94 SelectionData(const SelectionData& rhs); |
| 86 ~SelectionData(); | 95 ~SelectionData(); |
| 96 SelectionData& operator=(const SelectionData& rhs); |
| 87 | 97 |
| 88 ::Atom type() const { return type_; } | 98 bool IsValid() const; |
| 89 char* data() const { return data_; } | 99 ::Atom GetType() const; |
| 90 size_t size() const { return size_; } | 100 const unsigned char* GetData() const; |
| 91 | 101 size_t GetSize() const; |
| 92 void Set(::Atom type, char* data, size_t size, bool owned); | |
| 93 | 102 |
| 94 // If |type_| is a string type, convert the data to UTF8 and return it. | 103 // If |type_| is a string type, convert the data to UTF8 and return it. |
| 95 std::string GetText() const; | 104 std::string GetText() const; |
| 96 | 105 |
| 97 // If |type_| is the HTML type, returns the data as a string16. This detects | 106 // If |type_| is the HTML type, returns the data as a string16. This detects |
| 98 // guesses the character encoding of the source. | 107 // guesses the character encoding of the source. |
| 99 string16 GetHtml() const; | 108 string16 GetHtml() const; |
| 100 | 109 |
| 101 // Assigns the raw data to the string. | 110 // Assigns the raw data to the string. |
| 102 void AssignTo(std::string* result) const; | 111 void AssignTo(std::string* result) const; |
| 103 void AssignTo(string16* result) const; | 112 void AssignTo(string16* result) const; |
| 104 | 113 |
| 105 private: | 114 private: |
| 106 ::Atom type_; | 115 ::Atom type_; |
| 107 char* data_; | 116 scoped_refptr<base::RefCountedMemory> memory_; |
| 108 size_t size_; | |
| 109 bool owned_; | |
| 110 | 117 |
| 111 X11AtomCache atom_cache_; | 118 X11AtomCache atom_cache_; |
| 112 | |
| 113 DISALLOW_COPY_AND_ASSIGN(SelectionData); | |
| 114 }; | 119 }; |
| 115 | 120 |
| 116 } // namespace ui | 121 } // namespace ui |
| 117 | 122 |
| 118 #endif // UI_BASE_X_SELECTION_UTILS_H_ | 123 #endif // UI_BASE_X_SELECTION_UTILS_H_ |
| OLD | NEW |