File: paste_allowed_request.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (189 lines) | stat: -rw-r--r-- 6,121 bytes parent folder | download | duplicates (6)
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/enterprise/data_protection/paste_allowed_request.h"

#include "chrome/browser/enterprise/data_protection/data_protection_clipboard_utils.h"
#include "content/public/browser/global_routing_id.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/web_contents.h"

namespace enterprise_data_protection {

namespace {

PasteAllowedRequest::RequestsMap& RequestsMapStorage() {
  static base::NoDestructor<PasteAllowedRequest::RequestsMap> requests;
  return *requests.get();
}

// Completion callback of `StartPasteAllowedRequest()`. Sets the allowed
// status for the clipboard data corresponding to sequence number `seqno`.
void FinishPasteIfAllowed(
    const content::GlobalRenderFrameHostId& rfh_id,
    const ui::ClipboardSequenceNumberToken& seqno,
    std::optional<content::ClipboardPasteData> clipboard_paste_data) {
  if (!RequestsMapStorage().contains(rfh_id) ||
      !RequestsMapStorage().at(rfh_id).contains(seqno)) {
    return;
  }

  auto& request = RequestsMapStorage()[rfh_id][seqno];
  request.Complete(std::move(clipboard_paste_data));
}

}  // namespace

// The amount of time that the result of a content allow request is cached
// and reused for the same clipboard `seqno`.
// TODO(b/294844565): Update this once multi-format pastes are handled
// correctly.
constexpr base::TimeDelta PasteAllowedRequest::kIsPasteAllowedRequestTooOld =
    base::Seconds(5);

// static
void PasteAllowedRequest::StartPasteAllowedRequest(
    const content::ClipboardEndpoint& source,
    const content::ClipboardEndpoint& destination,
    const content::ClipboardMetadata& metadata,
    content::ClipboardPasteData clipboard_paste_data,
    IsClipboardPasteAllowedCallback callback) {
  DCHECK(destination.web_contents());
  DCHECK(destination.web_contents()->GetPrimaryMainFrame());

  CleanupObsoleteRequests();

  ui::ClipboardSequenceNumberToken seqno = metadata.seqno;
  content::GlobalRenderFrameHostId rfh_id =
      destination.web_contents()->GetPrimaryMainFrame()->GetGlobalId();

  // Add |callback| to the callbacks associated to the sequence number, adding
  // an entry to the map if one does not exist.
  PasteAllowedRequest& request = RequestsMapStorage()[rfh_id][seqno];

  // If this request has already completed, invoke the callback immediately
  // and return.
  if (request.is_complete()) {
    request.InvokeCallback(std::move(clipboard_paste_data),
                           std::move(callback));
    return;
  }

  if (request.AddCallback(std::move(callback))) {
    PasteIfAllowedByPolicy(
        source, destination, metadata, std::move(clipboard_paste_data),
        base::BindOnce(&FinishPasteIfAllowed, rfh_id, seqno));
  } else {
    request.AddData(std::move(clipboard_paste_data));
  }
}

// static
void PasteAllowedRequest::CleanupObsoleteRequests() {
  for (auto rfh_it = RequestsMapStorage().begin();
       rfh_it != RequestsMapStorage().end();) {
    for (auto seqno_request_it = rfh_it->second.begin();
         seqno_request_it != rfh_it->second.end();) {
      seqno_request_it = seqno_request_it->second.IsObsolete(base::Time::Now())
                             ? rfh_it->second.erase(seqno_request_it)
                             : std::next(seqno_request_it);
    }
    rfh_it = rfh_it->second.empty() ? RequestsMapStorage().erase(rfh_it)
                                    : std::next(rfh_it);
  }
}

// static
void PasteAllowedRequest::CleanupRequestsForTesting() {
  RequestsMapStorage().clear();
}

// static
void PasteAllowedRequest::AddRequestToCacheForTesting(
    content::GlobalRenderFrameHostId rfh_id,
    ui::ClipboardSequenceNumberToken seqno,
    PasteAllowedRequest request) {
  RequestsMapStorage()[rfh_id].emplace(seqno, std::move(request));
}

// static
size_t PasteAllowedRequest::requests_count_for_testing() {
  size_t total = 0;
  for (auto& entry : RequestsMapStorage()) {
    total += entry.second.size();
  }
  return total;
}

PasteAllowedRequest::PasteAllowedRequest() = default;
PasteAllowedRequest::PasteAllowedRequest(PasteAllowedRequest&&) = default;
PasteAllowedRequest& PasteAllowedRequest::operator=(PasteAllowedRequest&&) =
    default;
PasteAllowedRequest::~PasteAllowedRequest() = default;

bool PasteAllowedRequest::AddCallback(
    IsClipboardPasteAllowedCallback callback) {
  callbacks_.push_back(std::move(callback));

  // If this is the first callback registered tell the caller to start the scan.
  return callbacks_.size() == 1;
}

void PasteAllowedRequest::AddData(content::ClipboardPasteData data) {
  data_.Merge(std::move(data));
}

void PasteAllowedRequest::InvokeCallback(
    content::ClipboardPasteData data,
    IsClipboardPasteAllowedCallback callback) {
  DCHECK(is_complete());

  if (*data_allowed_) {
    // It's possible the completed request had its `data_` replaced, so merging
    // will override `data` with any non-empty field in `data_` as needed.
    data.Merge(data_);
    std::move(callback).Run(std::move(data));
  } else {
    std::move(callback).Run(std::nullopt);
  }
}

void PasteAllowedRequest::Complete(
    std::optional<content::ClipboardPasteData> data) {
  completed_time_ = base::Time::Now();
  data_allowed_ = data.has_value();
  if (*data_allowed_) {
    AddData(std::move(*data));
  }
  InvokeCallbacks();
}

bool PasteAllowedRequest::IsObsolete(base::Time now) {
  if (!is_complete()) {
    return false;
  }
  return (now - completed_time_) > kIsPasteAllowedRequestTooOld;
}

base::Time PasteAllowedRequest::completed_time() const {
  DCHECK(is_complete());
  return completed_time_;
}

void PasteAllowedRequest::InvokeCallbacks() {
  DCHECK(data_allowed_.has_value());

  auto callbacks = std::move(callbacks_);
  for (auto& callback : callbacks) {
    if (!callback.is_null()) {
      if (*data_allowed_) {
        std::move(callback).Run(data_);
      } else {
        std::move(callback).Run(std::nullopt);
      }
    }
  }
}

}  // namespace enterprise_data_protection