File: data_source.h

package info (click to toggle)
chromium 120.0.6099.224-1~deb11u1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 6,112,112 kB
  • sloc: cpp: 32,907,025; ansic: 8,148,123; javascript: 3,679,536; python: 2,031,248; asm: 959,718; java: 804,675; xml: 617,256; sh: 111,417; objc: 100,835; perl: 88,443; cs: 53,032; makefile: 29,579; fortran: 24,137; php: 21,162; tcl: 21,147; sql: 20,809; ruby: 17,735; pascal: 12,864; yacc: 8,045; lisp: 3,388; lex: 1,323; ada: 727; awk: 329; jsp: 267; csh: 117; exp: 43; sed: 37
file content (162 lines) | stat: -rw-r--r-- 6,218 bytes parent folder | download
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef COMPONENTS_EXO_DATA_SOURCE_H_
#define COMPONENTS_EXO_DATA_SOURCE_H_

#include <string>

#include "base/containers/flat_set.h"
#include "base/functional/callback.h"
#include "base/memory/raw_ptr.h"
#include "base/observer_list.h"
#include "components/exo/surface.h"
#include "third_party/abseil-cpp/absl/types/optional.h"

namespace exo {

class DataSourceDelegate;
class DataSourceObserver;
enum class DndAction;

// Object representing transferred data offered by a client.
class DataSource {
 public:
  // The maximum number of different data types that will be read by
  // GetDataForPreferredMimeTypes (plain text, RTF, HTML, image, text/uri-list,
  // application/octet-stream, chromium/x-web-custom-data).
  static constexpr int kMaxDataTypes = 7;

  explicit DataSource(DataSourceDelegate* delegate);

  DataSource(const DataSource&) = delete;
  DataSource& operator=(const DataSource&) = delete;

  ~DataSource();

  void AddObserver(DataSourceObserver* observer);
  void RemoveObserver(DataSourceObserver* observer);

  // Notifies to DataSource that the client offers new mime type.
  void Offer(const std::string& mime_type);

  // Notifies the possible drag and drop actions selected by the data source to
  // DataSource.
  void SetActions(const base::flat_set<DndAction>& dnd_actions);

  const base::flat_set<DndAction>& GetActions() const { return dnd_actions_; }

  // Notifies the data source is cancelled. e.g. Replaced with another data
  // source.
  void Cancelled();

  // Notifies the client of the mime type that will be used by the
  // recipient. Only used during drag drop operations.
  void Target(const absl::optional<std::string>& mime_type);

  // Notifies the client of the dnd action that will be performed if the
  // currently running drag operation ends now. Only used during drag drop
  // operations.
  void Action(DndAction action);

  // Notifies the client that the user has released the current drag. At this
  // point the target and action are considered final, but it is still possible
  // for the recipient to reject the transfer.
  void DndDropPerformed();

  // Notifies the client that the drag was completed successfully. The data
  // source must not be used by the client after this point except to delete it.
  void DndFinished();

  // Search the set of offered MIME types for the most preferred of each of the
  // following categories: text/plain*, text/rtf, text/html*, image/*,
  // text/uri-list, chromium/x-web-custom-data. If any usable MIME types in a
  // given category are available, the corresponding
  // |*_reader| input callback will be called with the best one and the
  // corresponding data. For any category that has no available MIME types,
  // |failure_callback| is run. |failure_callback| may therefore be run as many
  // as seven times.
  using ReadDataCallback =
      base::OnceCallback<void(const std::string&, const std::vector<uint8_t>&)>;
  using ReadTextDataCallback =
      base::OnceCallback<void(const std::string&, std::u16string)>;
  using ReadFileContentsDataCallback =
      base::OnceCallback<void(const std::string&,
                              const base::FilePath&,
                              const std::vector<uint8_t>&)>;
  using ReadWebCustomDataCallback =
      base::OnceCallback<void(const std::string&, const std::vector<uint8_t>&)>;
  void ReadDataTransferEndpoint(ReadTextDataCallback dte_reader,
                                base::RepeatingClosure failure_callback);
  void GetDataForPreferredMimeTypes(
      ReadTextDataCallback text_reader,
      ReadDataCallback rtf_reader,
      ReadTextDataCallback html_reader,
      ReadDataCallback image_reader,
      ReadDataCallback filenames_reader,
      ReadFileContentsDataCallback file_contents_reader,
      ReadDataCallback web_custom_data_reader,
      base::RepeatingClosure failure_callback);

  void ReadDataForTesting(
      const std::string& mime_type,
      ReadDataCallback callback,
      base::RepeatingClosure failure_callback = base::DoNothing());

  bool CanBeDataSourceForCopy(Surface* surface) const;

 private:
  // Reads data from the source. Then |callback| is invoked with read data. If
  // Cancelled() is invoked or DataSource is destroyed before completion,
  // |callback| is never called, and |failure_callback| is run instead.
  void ReadData(const std::string& mime_type,
                ReadDataCallback callback,
                base::OnceClosure failure_callback);

  static void OnDataRead(base::WeakPtr<DataSource> data_source_ptr,
                         ReadDataCallback callback,
                         const std::string& mime_type,
                         base::OnceClosure failure_callback,
                         const absl::optional<std::vector<uint8_t>>& data);

  void OnTextRead(ReadTextDataCallback callback,
                  const std::string& mime_type,
                  const std::vector<uint8_t>& data);

  void OnFileContentsRead(ReadFileContentsDataCallback callback,
                          const std::string& mime_type,
                          const std::vector<uint8_t>& data);

  // This can be a dangling pointer with AutoclickBrowserTest.ClickAndDrag
  // when run in browser_tests_require_lacros.
  const raw_ptr<DataSourceDelegate, DanglingUntriaged> delegate_;
  base::ObserverList<DataSourceObserver> observers_;

  // Mime types which has been offered.
  std::set<std::string> mime_types_;
  bool finished_;

  base::flat_set<DndAction> dnd_actions_;

  base::WeakPtrFactory<DataSource> read_data_weak_ptr_factory_{this};
};

class ScopedDataSource {
 public:
  ScopedDataSource(DataSource* data_source, DataSourceObserver* observer);

  ScopedDataSource(const ScopedDataSource&) = delete;
  ScopedDataSource& operator=(const ScopedDataSource&) = delete;

  ~ScopedDataSource();
  DataSource* get() { return data_source_; }

 private:
  const raw_ptr<DataSource, ExperimentalAsh> data_source_;
  const raw_ptr<DataSourceObserver, ExperimentalAsh> observer_;
};

}  // namespace exo

#endif  // COMPONENTS_EXO_DATA_SOURCE_H_