File: data_protection_page_user_data.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 (122 lines) | stat: -rw-r--r-- 3,903 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
// 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/data_protection_page_user_data.h"

#include "base/i18n/time_formatting.h"
#include "base/no_destructor.h"
#include "base/strings/strcat.h"
#include "content/public/browser/page.h"

namespace enterprise_data_protection {

static base::Time TimestampToTime(safe_browsing::Timestamp timestamp) {
  return base::Time::UnixEpoch() + base::Seconds(timestamp.seconds()) +
         base::Nanoseconds(timestamp.nanos());
}

bool UrlSettings::operator==(const UrlSettings& other) const {
  return watermark_text == other.watermark_text &&
         allow_screenshots == other.allow_screenshots;
}

// static
const UrlSettings& UrlSettings::None() {
  static base::NoDestructor<UrlSettings> empty;
  return *empty.get();
}

// static
void DataProtectionPageUserData::UpdateRTLookupResponse(
    content::Page& page,
    const std::string& identifier,
    std::unique_ptr<safe_browsing::RTLookupResponse> rt_lookup_response) {
  auto* ud = GetForPage(page);
  if (ud) {
    // UpdateWatermarkStringInSettings() should be called after settings
    // the lookup response.
    ud->set_rt_lookup_response(std::move(rt_lookup_response));
    return;
  }
  CreateForPage(page, identifier, UrlSettings(), std::move(rt_lookup_response));
}

// static
void DataProtectionPageUserData::UpdateDataControlsScreenshotState(
    content::Page& page,
    const std::string& identifier,
    bool allow) {
  auto* ud = GetForPage(page);
  if (ud) {
    ud->data_controls_settings_.allow_screenshots = allow;
    return;
  }

  UrlSettings data_controls_settings;
  data_controls_settings.allow_screenshots = allow;
  CreateForPage(page, identifier, data_controls_settings, nullptr);
}

DataProtectionPageUserData::DataProtectionPageUserData(
    content::Page& page,
    const std::string& identifier,
    UrlSettings data_controls_settings,
    std::unique_ptr<safe_browsing::RTLookupResponse> rt_lookup_response)
    : PageUserData(page),
      identifier_(identifier),
      data_controls_settings_(std::move(data_controls_settings)),
      rt_lookup_response_(std::move(rt_lookup_response)) {}

DataProtectionPageUserData::~DataProtectionPageUserData() = default;

UrlSettings DataProtectionPageUserData::settings() const {
  if (!rt_lookup_response_ || rt_lookup_response_->threat_info().empty()) {
    return data_controls_settings_;
  }
  UrlSettings merged_settings;
  const auto& threat_infos = rt_lookup_response_->threat_info();

  merged_settings.allow_screenshots = data_controls_settings_.allow_screenshots;
  for (const auto& threat_info : threat_infos) {
    if (!threat_info.has_matched_url_navigation_rule()) {
      continue;
    }

    const auto& rule = threat_info.matched_url_navigation_rule();
    if (merged_settings.watermark_text.empty()) {
      merged_settings.watermark_text = GetWatermarkString(identifier_, rule);
    }
    if (merged_settings.allow_screenshots) {
      merged_settings.allow_screenshots = !rule.block_screenshot();
    }
  }

  return merged_settings;
}

PAGE_USER_DATA_KEY_IMPL(DataProtectionPageUserData);

std::string GetWatermarkString(
    const std::string& identifier,
    const safe_browsing::MatchedUrlNavigationRule& rule) {
  if (!rule.has_watermark_message()) {
    return std::string();
  }

  const safe_browsing::MatchedUrlNavigationRule::WatermarkMessage& watermark =
      rule.watermark_message();

  std::string watermark_text = base::StrCat(
      {identifier, "\n",
       base::TimeFormatAsIso8601(TimestampToTime(watermark.timestamp()))});

  if (!watermark.watermark_message().empty()) {
    watermark_text =
        base::StrCat({watermark.watermark_message(), "\n", watermark_text});
  }

  return watermark_text;
}

}  // namespace enterprise_data_protection