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_base_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_BASE_EXPORT
std::vector
< ::Atom
> GetTextAtomsFrom(
32 const X11AtomCache
* atom_cache
);
34 UI_BASE_EXPORT
std::vector
< ::Atom
> GetURLAtomsFrom(
35 const X11AtomCache
* atom_cache
);
37 UI_BASE_EXPORT
std::vector
< ::Atom
> GetURIListAtomsFrom(
38 const X11AtomCache
* atom_cache
);
40 // Places the intersection of |desired| and |offered| into |output|.
41 UI_BASE_EXPORT
void GetAtomIntersection(const std::vector
< ::Atom
>& desired
,
42 const std::vector
< ::Atom
>& offered
,
43 std::vector
< ::Atom
>* output
);
45 // Takes the raw bytes of the base::string16 and copies them into |bytes|.
46 UI_BASE_EXPORT
void AddString16ToVector(const base::string16
& str
,
47 std::vector
<unsigned char>* bytes
);
49 // Tokenizes and parses the Selection Data as if it is a URI List.
50 UI_BASE_EXPORT
std::vector
<std::string
> ParseURIList(const SelectionData
& data
);
52 UI_BASE_EXPORT
std::string
RefCountedMemoryToString(
53 const scoped_refptr
<base::RefCountedMemory
>& memory
);
55 UI_BASE_EXPORT
base::string16
RefCountedMemoryToString16(
56 const scoped_refptr
<base::RefCountedMemory
>& memory
);
58 ///////////////////////////////////////////////////////////////////////////////
60 // Represents the selection in different data formats. Binary data passed in is
61 // assumed to be allocated with new char[], and is owned by SelectionFormatMap.
62 class UI_BASE_EXPORT SelectionFormatMap
{
64 // Our internal data store, which we only expose through iterators.
65 typedef std::map
< ::Atom
, scoped_refptr
<base::RefCountedMemory
> > InternalMap
;
66 typedef InternalMap::const_iterator const_iterator
;
69 ~SelectionFormatMap();
70 // Copy and assignment deliberately open.
72 // Adds the selection in the format |atom|. Ownership of |data| is passed to
74 void Insert(::Atom atom
, const scoped_refptr
<base::RefCountedMemory
>& item
);
76 // Returns the first of the requested_types or NULL if missing.
77 ui::SelectionData
GetFirstOf(
78 const std::vector
< ::Atom
>& requested_types
) const;
80 // Returns all the selected types.
81 std::vector
< ::Atom
> GetTypes() const;
83 // Pass through to STL map. Only allow non-mutation access.
84 const_iterator
begin() const { return data_
.begin(); }
85 const_iterator
end() const { return data_
.end(); }
86 const_iterator
find(::Atom atom
) const { return data_
.find(atom
); }
87 size_t size() const { return data_
.size(); }
93 ///////////////////////////////////////////////////////////////////////////////
95 // A holder for data with optional X11 deletion semantics.
96 class UI_BASE_EXPORT SelectionData
{
98 // |atom_cache| is still owned by caller.
100 SelectionData(::Atom type
,
101 const scoped_refptr
<base::RefCountedMemory
>& memory
);
102 SelectionData(const SelectionData
& rhs
);
104 SelectionData
& operator=(const SelectionData
& rhs
);
106 bool IsValid() const;
107 ::Atom
GetType() const;
108 const unsigned char* GetData() const;
109 size_t GetSize() const;
111 // If |type_| is a string type, convert the data to UTF8 and return it.
112 std::string
GetText() const;
114 // If |type_| is the HTML type, returns the data as a string16. This detects
115 // guesses the character encoding of the source.
116 base::string16
GetHtml() const;
118 // Assigns the raw data to the string.
119 void AssignTo(std::string
* result
) const;
120 void AssignTo(base::string16
* result
) const;
124 scoped_refptr
<base::RefCountedMemory
> memory_
;
126 X11AtomCache atom_cache_
;
131 #endif // UI_BASE_X_SELECTION_UTILS_H_