• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 #ifndef UI_BASE_X_SELECTION_UTILS_H_
6 #define UI_BASE_X_SELECTION_UTILS_H_
7 
8 #include <X11/Xlib.h>
9 
10 // Get rid of a macro from Xlib.h that conflicts with Aura's RootWindow class.
11 #undef RootWindow
12 
13 #include <map>
14 
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"
20 
21 namespace ui {
22 class SelectionData;
23 class X11AtomCache;
24 
25 extern const char kMimeTypeMozillaURL[];
26 extern const char kString[];
27 extern const char kText[];
28 extern const char kUtf8String[];
29 
30 // Returns a list of all text atoms that we handle.
31 UI_BASE_EXPORT std::vector< ::Atom> GetTextAtomsFrom(
32     const X11AtomCache* atom_cache);
33 
34 UI_BASE_EXPORT std::vector< ::Atom> GetURLAtomsFrom(
35     const X11AtomCache* atom_cache);
36 
37 UI_BASE_EXPORT std::vector< ::Atom> GetURIListAtomsFrom(
38     const X11AtomCache* atom_cache);
39 
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);
44 
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);
48 
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);
51 
52 UI_BASE_EXPORT std::string RefCountedMemoryToString(
53     const scoped_refptr<base::RefCountedMemory>& memory);
54 
55 UI_BASE_EXPORT base::string16 RefCountedMemoryToString16(
56     const scoped_refptr<base::RefCountedMemory>& memory);
57 
58 ///////////////////////////////////////////////////////////////////////////////
59 
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 {
63  public:
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;
67 
68   SelectionFormatMap();
69   ~SelectionFormatMap();
70   // Copy and assignment deliberately open.
71 
72   // Adds the selection in the format |atom|. Ownership of |data| is passed to
73   // us.
74   void Insert(::Atom atom, const scoped_refptr<base::RefCountedMemory>& item);
75 
76   // Returns the first of the requested_types or NULL if missing.
77   ui::SelectionData GetFirstOf(
78       const std::vector< ::Atom>& requested_types) const;
79 
80   // Returns all the selected types.
81   std::vector< ::Atom> GetTypes() const;
82 
83   // Pass through to STL map. Only allow non-mutation access.
begin()84   const_iterator begin() const { return data_.begin(); }
end()85   const_iterator end() const { return data_.end(); }
find(::Atom atom)86   const_iterator find(::Atom atom) const { return data_.find(atom); }
size()87   size_t size() const { return data_.size(); }
88 
89  private:
90   InternalMap data_;
91 };
92 
93 ///////////////////////////////////////////////////////////////////////////////
94 
95 // A holder for data with optional X11 deletion semantics.
96 class UI_BASE_EXPORT SelectionData {
97  public:
98   // |atom_cache| is still owned by caller.
99   SelectionData();
100   SelectionData(::Atom type,
101                 const scoped_refptr<base::RefCountedMemory>& memory);
102   SelectionData(const SelectionData& rhs);
103   ~SelectionData();
104   SelectionData& operator=(const SelectionData& rhs);
105 
106   bool IsValid() const;
107   ::Atom GetType() const;
108   const unsigned char* GetData() const;
109   size_t GetSize() const;
110 
111   // If |type_| is a string type, convert the data to UTF8 and return it.
112   std::string GetText() const;
113 
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;
117 
118   // Assigns the raw data to the string.
119   void AssignTo(std::string* result) const;
120   void AssignTo(base::string16* result) const;
121 
122  private:
123   ::Atom type_;
124   scoped_refptr<base::RefCountedMemory> memory_;
125 
126   X11AtomCache atom_cache_;
127 };
128 
129 }  // namespace ui
130 
131 #endif  // UI_BASE_X_SELECTION_UTILS_H_
132