File: selection_utils.h

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (132 lines) | stat: -rw-r--r-- 4,477 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
// Copyright 2013 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef UI_BASE_X_SELECTION_UTILS_H_
#define UI_BASE_X_SELECTION_UTILS_H_

#include <stddef.h>

#include <map>
#include <string>
#include <string_view>
#include <vector>

#include "base/component_export.h"
#include "base/containers/span.h"
#include "base/memory/ref_counted_memory.h"
#include "ui/gfx/x/xproto.h"

namespace ui {
class SelectionData;

// Returns a list of all text atoms that we handle.
COMPONENT_EXPORT(UI_BASE_X) std::vector<x11::Atom> GetTextAtomsFrom();

COMPONENT_EXPORT(UI_BASE_X) std::vector<x11::Atom> GetURLAtomsFrom();

COMPONENT_EXPORT(UI_BASE_X) std::vector<x11::Atom> GetURIListAtomsFrom();

// Places the intersection of |desired| and |offered| into |output|.
COMPONENT_EXPORT(UI_BASE_X)
void GetAtomIntersection(const std::vector<x11::Atom>& desired,
                         const std::vector<x11::Atom>& offered,
                         std::vector<x11::Atom>* output);

// Takes the raw bytes of the std::u16string and copies them into |bytes|.
COMPONENT_EXPORT(UI_BASE_X)
void AddString16ToVector(std::u16string_view str,
                         std::vector<unsigned char>* bytes);

// Tokenizes and parses the Selection Data as if it is a URI List.
COMPONENT_EXPORT(UI_BASE_X)
std::vector<std::string> ParseURIList(const SelectionData& data);

COMPONENT_EXPORT(UI_BASE_X)
std::string RefCountedMemoryToString(
    const scoped_refptr<base::RefCountedMemory>& memory);

COMPONENT_EXPORT(UI_BASE_X)
std::u16string RefCountedMemoryToString16(
    const scoped_refptr<base::RefCountedMemory>& memory);

///////////////////////////////////////////////////////////////////////////////

// Represents the selection in different data formats. Binary data passed in is
// assumed to be allocated with new char[], and is owned by SelectionFormatMap.
class COMPONENT_EXPORT(UI_BASE_X) SelectionFormatMap {
 public:
  // Our internal data store, which we only expose through iterators.
  using InternalMap =
      std::map<x11::Atom, scoped_refptr<base::RefCountedMemory>>;
  using const_iterator = InternalMap::const_iterator;

  SelectionFormatMap();
  SelectionFormatMap(const SelectionFormatMap& other);
  ~SelectionFormatMap();
  // Copy and assignment deliberately open.

  // Adds the selection in the format |atom|. Ownership of |data| is passed to
  // us.
  void Insert(x11::Atom atom,
              const scoped_refptr<base::RefCountedMemory>& item);

  // Returns the first of the |requested_types| or NULL if missing.
  ui::SelectionData GetFirstOf(
      const std::vector<x11::Atom>& requested_types) const;

  // Returns the |SelectionData| of the |requested_type| or NULL if missing.
  ui::SelectionData Get(x11::Atom requested_type) const;

  // Returns all the selected types.
  std::vector<x11::Atom> GetTypes() const;

  // Pass through to STL map. Only allow non-mutation access.
  const_iterator begin() const { return data_.begin(); }
  const_iterator end() const { return data_.end(); }
  const_iterator find(x11::Atom atom) const { return data_.find(atom); }
  size_t size() const { return data_.size(); }

 private:
  InternalMap data_;
};

///////////////////////////////////////////////////////////////////////////////

// A holder for data with optional X11 deletion semantics.
class COMPONENT_EXPORT(UI_BASE_X) SelectionData {
 public:
  // |atom_cache| is still owned by caller.
  SelectionData();
  SelectionData(x11::Atom type,
                const scoped_refptr<base::RefCountedMemory>& memory);
  SelectionData(const SelectionData& rhs);
  ~SelectionData();
  SelectionData& operator=(const SelectionData& rhs);

  bool IsValid() const;
  x11::Atom GetType() const;
  base::span<const unsigned char> GetSpan() const;

  // If |type_| is a string type, convert the data to UTF8 and return it.
  std::string GetText() const;

  // If |type_| is the HTML type, returns the data as a string16. This detects
  // guesses the character encoding of the source.
  std::u16string GetHtml() const;

  // Assigns the raw data to the string.
  void AssignTo(std::string* result) const;
  void AssignTo(std::u16string* result) const;

  // Transfers ownership of |memory_| to the caller.
  scoped_refptr<base::RefCountedBytes> TakeBytes();

 private:
  x11::Atom type_;
  scoped_refptr<base::RefCountedMemory> memory_;
};

}  // namespace ui

#endif  // UI_BASE_X_SELECTION_UTILS_H_