File: dlp_internals_page_handler.cc

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; 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,811; 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 (334 lines) | stat: -rw-r--r-- 13,557 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
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
// Copyright 2023 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/ui/webui/ash/dlp_internals/dlp_internals_page_handler.h"

#include <sys/stat.h>

#include "base/check.h"
#include "base/files/file_path.h"
#include "base/path_service.h"
#include "chrome/browser/chromeos/policy/dlp/dlp_content_manager.h"
#include "chrome/browser/chromeos/policy/dlp/dlp_content_restriction_set.h"
#include "chrome/browser/chromeos/policy/dlp/dlp_rules_manager.h"
#include "chrome/browser/chromeos/policy/dlp/dlp_rules_manager_factory.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/webui/ash/dlp_internals/dlp_internals.mojom.h"
#include "chrome/common/chrome_paths.h"
#include "chromeos/dbus/dlp/dlp_client.h"
#include "components/enterprise/common/proto/synced/dlp_policy_event.pb.h"
#include "components/enterprise/data_controls/core/browser/rule.h"
#include "mojo/public/cpp/bindings/pending_receiver.h"
#include "mojo/public/cpp/bindings/receiver.h"
#include "ui/base/clipboard/clipboard.h"

namespace policy {

namespace {
dlp_internals::mojom::Level DlpLevelToMojo(data_controls::Rule::Level level) {
  switch (level) {
    case data_controls::Rule::Level::kNotSet:
      return dlp_internals::mojom::Level::kNotSet;
    case data_controls::Rule::Level::kReport:
      return dlp_internals::mojom::Level::kReport;
    case data_controls::Rule::Level::kWarn:
      return dlp_internals::mojom::Level::kWarn;
    case data_controls::Rule::Level::kBlock:
      return dlp_internals::mojom::Level::kBlock;
    case data_controls::Rule::Level::kAllow:
      return dlp_internals::mojom::Level::kAllow;
  }
}

dlp_internals::mojom::ContentRestriction ContentRestrictionToMojo(
    DlpContentRestriction restriction) {
  switch (restriction) {
    case DlpContentRestriction::kScreenshot:
      return dlp_internals::mojom::ContentRestriction::kScreenshot;
    case DlpContentRestriction::kPrivacyScreen:
      return dlp_internals::mojom::ContentRestriction::kPrivacyScreen;
    case DlpContentRestriction::kPrint:
      return dlp_internals::mojom::ContentRestriction::kPrint;
    case DlpContentRestriction::kScreenShare:
      return dlp_internals::mojom::ContentRestriction::kScreenShare;
  }
}

std::vector<dlp_internals::mojom::ContentRestrictionInfoPtr>
RestrictionSetToMojo(DlpContentRestrictionSet restriction_set) {
  std::vector<dlp_internals::mojom::ContentRestrictionInfoPtr>
      restrictions_mojo_arr;

  // An array of all content restrictions. Please keep it up to date with
  // DlpContentRestriction enum.
  std::array restrictions = {
      DlpContentRestriction::kScreenshot, DlpContentRestriction::kPrivacyScreen,
      DlpContentRestriction::kPrint, DlpContentRestriction::kScreenShare};
  for (const auto& restriction : restrictions) {
    auto lvl_and_url = restriction_set.GetRestrictionLevelAndUrl(restriction);
    restrictions_mojo_arr.push_back(
        dlp_internals::mojom::ContentRestrictionInfo::New(
            ContentRestrictionToMojo(restriction),
            DlpLevelToMojo(lvl_and_url.level), lvl_and_url.url));
  }
  return restrictions_mojo_arr;
}

std::vector<dlp_internals::mojom::RenderFrameHostInfoPtr> RfhInfoToMojo(
    std::vector<DlpContentTabHelper::RfhInfo> rfh_info_vector) {
  std::vector<dlp_internals::mojom::RenderFrameHostInfoPtr> rfh_info_mojo_arr;
  for (const auto& rfh_info : rfh_info_vector) {
    DCHECK(rfh_info.first);
    rfh_info_mojo_arr.push_back(dlp_internals::mojom::RenderFrameHostInfo::New(
        rfh_info.first->GetLastCommittedURL(),
        RestrictionSetToMojo(rfh_info.second)));
  }
  return rfh_info_mojo_arr;
}

dlp_internals::mojom::DlpEvent::Restriction EventRestrictionToMojo(
    DlpPolicyEvent::Restriction restriction) {
  switch (restriction) {
    case DlpPolicyEvent_Restriction_UNDEFINED_RESTRICTION:
      return dlp_internals::mojom::DlpEvent::Restriction::kUndefinedRestriction;
    case DlpPolicyEvent_Restriction_CLIPBOARD:
      return dlp_internals::mojom::DlpEvent::Restriction::kClipboard;
    case DlpPolicyEvent_Restriction_SCREENSHOT:
      return dlp_internals::mojom::DlpEvent::Restriction::kScreenshot;
    case DlpPolicyEvent_Restriction_SCREENCAST:
      return dlp_internals::mojom::DlpEvent::Restriction::kScreencast;
    case DlpPolicyEvent_Restriction_PRINTING:
      return dlp_internals::mojom::DlpEvent::Restriction::kPrinting;
    case DlpPolicyEvent_Restriction_EPRIVACY:
      return dlp_internals::mojom::DlpEvent::Restriction::kEprivacy;
    case DlpPolicyEvent_Restriction_FILES:
      return dlp_internals::mojom::DlpEvent::Restriction::kFiles;
    case DlpPolicyEvent_Restriction_DlpPolicyEvent_Restriction_INT_MIN_SENTINEL_DO_NOT_USE_:
    case DlpPolicyEvent_Restriction_DlpPolicyEvent_Restriction_INT_MAX_SENTINEL_DO_NOT_USE_:
      NOTREACHED();
  }
}

dlp_internals::mojom::DlpEvent::UserType EventUserTypeToMojo(
    DlpPolicyEvent::UserType restriction) {
  switch (restriction) {
    case DlpPolicyEvent_UserType_REGULAR:
      return dlp_internals::mojom::DlpEvent::UserType::kRegular;
    case DlpPolicyEvent_UserType_MANAGED_GUEST:
      return dlp_internals::mojom::DlpEvent::UserType::kManagedGuest;
    case DlpPolicyEvent_UserType_KIOSK:
      return dlp_internals::mojom::DlpEvent::UserType::kKiosk;
    case DlpPolicyEvent_UserType_UNDEFINED_USER_TYPE:
      return dlp_internals::mojom::DlpEvent::UserType::kUndefinedUserType;
    case DlpPolicyEvent_UserType_DlpPolicyEvent_UserType_INT_MAX_SENTINEL_DO_NOT_USE_:
    case DlpPolicyEvent_UserType_DlpPolicyEvent_UserType_INT_MIN_SENTINEL_DO_NOT_USE_:
      NOTREACHED();
  }
}

dlp_internals::mojom::DlpEvent::Mode EventModeToMojo(
    DlpPolicyEvent::Mode mode) {
  switch (mode) {
    case DlpPolicyEvent_Mode_UNDEFINED_MODE:
      return dlp_internals::mojom::DlpEvent::Mode::kUndefinedMode;
    case DlpPolicyEvent_Mode_BLOCK:
      return dlp_internals::mojom::DlpEvent::Mode::kBlock;
    case DlpPolicyEvent_Mode_REPORT:
      return dlp_internals::mojom::DlpEvent::Mode::kReport;
    case DlpPolicyEvent_Mode_WARN:
      return dlp_internals::mojom::DlpEvent::Mode::kWarn;
    case DlpPolicyEvent_Mode_WARN_PROCEED:
      return dlp_internals::mojom::DlpEvent::Mode::kWarnProceed;
    case DlpPolicyEvent_Mode_DlpPolicyEvent_Mode_INT_MIN_SENTINEL_DO_NOT_USE_:
    case DlpPolicyEvent_Mode_DlpPolicyEvent_Mode_INT_MAX_SENTINEL_DO_NOT_USE_:
      NOTREACHED();
  }
}

dlp_internals::mojom::EventDestination::Component
EventDestinationComponentToMojo(
    DlpPolicyEventDestination::Component component) {
  switch (component) {
    case DlpPolicyEventDestination_Component_UNDEFINED_COMPONENT:
      return dlp_internals::mojom::EventDestination::Component::
          kUndefinedComponent;
    case DlpPolicyEventDestination_Component_ARC:
      return dlp_internals::mojom::EventDestination::Component::kArc;
    case DlpPolicyEventDestination_Component_CROSTINI:
      return dlp_internals::mojom::EventDestination::Component::kCrostini;
    case DlpPolicyEventDestination_Component_PLUGIN_VM:
      return dlp_internals::mojom::EventDestination::Component::kPluginVm;
    case DlpPolicyEventDestination_Component_USB:
      return dlp_internals::mojom::EventDestination::Component::kUsb;
    case DlpPolicyEventDestination_Component_DRIVE:
      return dlp_internals::mojom::EventDestination::Component::kDrive;
    case DlpPolicyEventDestination_Component_ONEDRIVE:
      return dlp_internals::mojom::EventDestination::Component::kOnedrive;
    case DlpPolicyEventDestination_Component_DlpPolicyEventDestination_Component_INT_MIN_SENTINEL_DO_NOT_USE_:
    case DlpPolicyEventDestination_Component_DlpPolicyEventDestination_Component_INT_MAX_SENTINEL_DO_NOT_USE_:
      NOTREACHED();
  }
}

dlp_internals::mojom::EventDestinationPtr EventDestinationToMojo(
    DlpPolicyEventDestination destination) {
  auto destination_mojo = dlp_internals::mojom::EventDestination::New();

  destination_mojo->component =
      EventDestinationComponentToMojo(destination.component());
  destination_mojo->url_pattern = destination.url();
  return destination_mojo;
}

}  // namespace

DlpInternalsPageHandler::DlpInternalsPageHandler(
    mojo::PendingReceiver<dlp_internals::mojom::PageHandler> receiver,
    Profile* profile)
    : receiver_(this, std::move(receiver)), profile_(profile) {
  DCHECK(profile_);

  auto* rules_manager = DlpRulesManagerFactory::GetForPrimaryProfile();
  auto* reporting_manager =
      rules_manager ? rules_manager->GetReportingManager() : nullptr;
  if (reporting_manager) {
    reporting_observation_.Observe(reporting_manager);
  }
}

DlpInternalsPageHandler::~DlpInternalsPageHandler() = default;

void DlpInternalsPageHandler::GetClipboardDataSource(
    GetClipboardDataSourceCallback callback) {
  auto source = ui::Clipboard::GetForCurrentThread()->GetSource(
      ui::ClipboardBuffer::kCopyPaste);
  if (!source) {
    std::move(callback).Run(std::move(nullptr));
    return;
  }

  auto mojom_source = dlp_internals::mojom::DataTransferEndpoint::New();
  switch (source->type()) {
    case ui::EndpointType::kDefault:
      mojom_source->type = dlp_internals::mojom::EndpointType::kDefault;
      break;

    case ui::EndpointType::kUrl:
      mojom_source->type = dlp_internals::mojom::EndpointType::kUrl;
      break;

    case ui::EndpointType::kClipboardHistory:
      mojom_source->type =
          dlp_internals::mojom::EndpointType::kClipboardHistory;
      break;

    case ui::EndpointType::kUnknownVm:
      mojom_source->type = dlp_internals::mojom::EndpointType::kUnknownVm;
      break;

    case ui::EndpointType::kArc:
      mojom_source->type = dlp_internals::mojom::EndpointType::kArc;
      break;

    case ui::EndpointType::kBorealis:
      mojom_source->type = dlp_internals::mojom::EndpointType::kBorealis;
      break;

    case ui::EndpointType::kCrostini:
      mojom_source->type = dlp_internals::mojom::EndpointType::kCrostini;
      break;

    case ui::EndpointType::kPluginVm:
      mojom_source->type = dlp_internals::mojom::EndpointType::kPluginVm;
      break;
  }

  if (source->IsUrlType()) {
    mojom_source->url = *source->GetURL();
  }

  std::move(callback).Run(std::move(mojom_source));
}

void DlpInternalsPageHandler::GetContentRestrictionsInfo(
    GetContentRestrictionsInfoCallback callback) {
  auto* content_manager = DlpContentManager::Get();
  if (!content_manager) {
    std::move(callback).Run({});
  }

  auto info_vector = content_manager->GetWebContentsInfo();
  std::vector<dlp_internals::mojom::WebContentsInfoPtr> info_mojo_array;

  for (const auto& web_contents_info : info_vector) {
    DCHECK(web_contents_info.web_contents);
    info_mojo_array.push_back(dlp_internals::mojom::WebContentsInfo::New(
        web_contents_info.web_contents->GetLastCommittedURL(),
        RestrictionSetToMojo(web_contents_info.restriction_set),
        RfhInfoToMojo(web_contents_info.rfh_info_vector)));
  }
  std::move(callback).Run(std::move(info_mojo_array));
}

void DlpInternalsPageHandler::ObserveReporting(
    mojo::PendingRemote<dlp_internals::mojom::ReportingObserver> observer) {
  reporting_observers_.Add(std::move(observer));
}

void DlpInternalsPageHandler::GetFilesDatabaseEntries(
    GetFilesDatabaseEntriesCallback callback) {
  if (!chromeos::DlpClient::Get() || !chromeos::DlpClient::Get()->IsAlive()) {
    std::move(callback).Run({});
    return;
  }
  chromeos::DlpClient::Get()->GetDatabaseEntries(
      base::BindOnce(&DlpInternalsPageHandler::ProcessDatabaseEntries,
                     weak_ptr_factory_.GetWeakPtr(), std::move(callback)));
}

void DlpInternalsPageHandler::GetFileInode(const std::string& file_name,
                                           GetFileInodeCallback callback) {
  base::FilePath downloads_path;
  base::PathService::Get(chrome::DIR_DEFAULT_DOWNLOADS_SAFE, &downloads_path);
  auto file_path = downloads_path.Append(file_name);

  struct stat file_stats;
  if (stat(file_path.value().c_str(), &file_stats) != 0) {
    std::move(callback).Run(0);
    return;
  }

  std::move(callback).Run(file_stats.st_ino);
}

void DlpInternalsPageHandler::OnReportEvent(DlpPolicyEvent event) {
  dlp_internals::mojom::DlpEventPtr event_mojo =
      dlp_internals::mojom::DlpEvent::New();
  event_mojo->source_pattern = event.source().url();
  event_mojo->destination = EventDestinationToMojo(event.destination());
  event_mojo->restriction = EventRestrictionToMojo(event.restriction());
  event_mojo->mode = EventModeToMojo(event.mode());
  event_mojo->timestamp_micro = event.timestamp_micro();
  event_mojo->user_type = EventUserTypeToMojo(event.user_type());
  event_mojo->content_name = event.content_name();
  event_mojo->triggered_rule_name = event.triggered_rule_name();
  event_mojo->triggered_rule_id = event.triggered_rule_id();
  for (auto& observer : reporting_observers_) {
    observer->OnReportEvent(event_mojo.Clone());
  }
}

void DlpInternalsPageHandler::ProcessDatabaseEntries(
    GetFilesDatabaseEntriesCallback callback,
    ::dlp::GetDatabaseEntriesResponse response_proto) {
  std::vector<dlp_internals::mojom::FileDatabaseEntryPtr> database_entries;
  for (const auto& file_entry : response_proto.files_entries()) {
    database_entries.push_back(dlp_internals::mojom::FileDatabaseEntry::New(
        file_entry.inode(), file_entry.crtime(), file_entry.source_url(),
        file_entry.referrer_url()));
  }
  std::move(callback).Run(std::move(database_entries));
}

}  // namespace policy