Home | History | Annotate | Download | only in x
      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_export.h"
     19 #include "ui/base/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_EXPORT std::vector< ::Atom> GetTextAtomsFrom(const X11AtomCache* atom_cache);
     32 
     33 UI_EXPORT std::vector< ::Atom> GetURLAtomsFrom(const X11AtomCache* atom_cache);
     34 
     35 // Places the intersection of |one| and |two| into |output|.
     36 UI_EXPORT void GetAtomIntersection(const std::vector< ::Atom>& one,
     37                                    const std::vector< ::Atom>& two,
     38                                    std::vector< ::Atom>* output);
     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 
     50 ///////////////////////////////////////////////////////////////////////////////
     51 
     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 {
     55  public:
     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;
     59 
     60   SelectionFormatMap();
     61   ~SelectionFormatMap();
     62   // Copy and assignment deliberately open.
     63 
     64   // Adds the selection in the format |atom|. Ownership of |data| is passed to
     65   // us.
     66   void Insert(::Atom atom, const scoped_refptr<base::RefCountedMemory>& item);
     67 
     68   // Returns the first of the requested_types or NULL if missing.
     69   ui::SelectionData GetFirstOf(
     70       const std::vector< ::Atom>& requested_types) const;
     71 
     72   // Returns all the selected types.
     73   std::vector< ::Atom> GetTypes() const;
     74 
     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(); }
     80 
     81  private:
     82   InternalMap data_;
     83 };
     84 
     85 ///////////////////////////////////////////////////////////////////////////////
     86 
     87 // A holder for data with optional X11 deletion semantics.
     88 class UI_EXPORT SelectionData {
     89  public:
     90   // |atom_cache| is still owned by caller.
     91   SelectionData();
     92   SelectionData(::Atom type,
     93                 const scoped_refptr<base::RefCountedMemory>& memory);
     94   SelectionData(const SelectionData& rhs);
     95   ~SelectionData();
     96   SelectionData& operator=(const SelectionData& rhs);
     97 
     98   bool IsValid() const;
     99   ::Atom GetType() const;
    100   const unsigned char* GetData() const;
    101   size_t GetSize() const;
    102 
    103   // If |type_| is a string type, convert the data to UTF8 and return it.
    104   std::string GetText() const;
    105 
    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;
    109 
    110   // Assigns the raw data to the string.
    111   void AssignTo(std::string* result) const;
    112   void AssignTo(string16* result) const;
    113 
    114  private:
    115   ::Atom type_;
    116   scoped_refptr<base::RefCountedMemory> memory_;
    117 
    118   X11AtomCache atom_cache_;
    119 };
    120 
    121 }  // namespace ui
    122 
    123 #endif  // UI_BASE_X_SELECTION_UTILS_H_
    124