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
3 // found in the LICENSE file.
5 #ifndef UI_BASE_X_SELECTION_UTILS_H_
6 #define UI_BASE_X_SELECTION_UTILS_H_
10 // Get rid of a macro from Xlib.h that conflicts with Aura's RootWindow class.
15 #include "base/basictypes.h"
16 #include "base/memory/ref_counted_memory.h"
17 #include "ui/base/clipboard/clipboard.h"
18 #include "ui/base/ui_export.h"
19 #include "ui/gfx/x/x11_atom_cache.h"
25 extern const char kMimeTypeMozillaURL[];
26 extern const char kString[];
27 extern const char kText[];
28 extern const char kUtf8String[];
30 // Returns a list of all text atoms that we handle.
31 UI_EXPORT std::vector< ::Atom> GetTextAtomsFrom(const X11AtomCache* atom_cache);
33 UI_EXPORT std::vector< ::Atom> GetURLAtomsFrom(const X11AtomCache* atom_cache);
35 // Places the intersection of |desired| and |offered| into |output|.
36 UI_EXPORT void GetAtomIntersection(const std::vector< ::Atom>& desired,
37 const std::vector< ::Atom>& offered,
38 std::vector< ::Atom>* output);
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);
44 UI_EXPORT std::string RefCountedMemoryToString(
45 const scoped_refptr<base::RefCountedMemory>& memory);
47 UI_EXPORT string16 RefCountedMemoryToString16(
48 const scoped_refptr<base::RefCountedMemory>& memory);
50 ///////////////////////////////////////////////////////////////////////////////
52 // Represents the selection in different data formats. Binary data passed in is
53 // assumed to be allocated with new char[], and is owned by SelectionFormatMap.
54 class UI_EXPORT SelectionFormatMap {
56 // Our internal data store, which we only expose through iterators.
57 typedef std::map< ::Atom, scoped_refptr<base::RefCountedMemory> > InternalMap;
58 typedef InternalMap::const_iterator const_iterator;
61 ~SelectionFormatMap();
62 // Copy and assignment deliberately open.
64 // Adds the selection in the format |atom|. Ownership of |data| is passed to
66 void Insert(::Atom atom, const scoped_refptr<base::RefCountedMemory>& item);
68 // Returns the first of the requested_types or NULL if missing.
69 ui::SelectionData GetFirstOf(
70 const std::vector< ::Atom>& requested_types) const;
72 // Returns all the selected types.
73 std::vector< ::Atom> GetTypes() const;
75 // Pass through to STL map. Only allow non-mutation access.
76 const_iterator begin() const { return data_.begin(); }
77 const_iterator end() const { return data_.end(); }
78 const_iterator find(::Atom atom) const { return data_.find(atom); }
79 size_t size() const { return data_.size(); }
85 ///////////////////////////////////////////////////////////////////////////////
87 // A holder for data with optional X11 deletion semantics.
88 class UI_EXPORT SelectionData {
90 // |atom_cache| is still owned by caller.
92 SelectionData(::Atom type,
93 const scoped_refptr<base::RefCountedMemory>& memory);
94 SelectionData(const SelectionData& rhs);
96 SelectionData& operator=(const SelectionData& rhs);
99 ::Atom GetType() const;
100 const unsigned char* GetData() const;
101 size_t GetSize() const;
103 // If |type_| is a string type, convert the data to UTF8 and return it.
104 std::string GetText() const;
106 // If |type_| is the HTML type, returns the data as a string16. This detects
107 // guesses the character encoding of the source.
108 string16 GetHtml() const;
110 // Assigns the raw data to the string.
111 void AssignTo(std::string* result) const;
112 void AssignTo(string16* result) const;
116 scoped_refptr<base::RefCountedMemory> memory_;
118 X11AtomCache atom_cache_;
123 #endif // UI_BASE_X_SELECTION_UTILS_H_