File: content_analysis_info.cc

package info (click to toggle)
chromium 139.0.7258.138-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,120,676 kB
  • sloc: cpp: 35,100,869; 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 (219 lines) | stat: -rw-r--r-- 6,291 bytes parent folder | download | duplicates (3)
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
// Copyright 2025 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/connectors/analysis/content_analysis_info.h"

#include "base/containers/fixed_flat_set.h"
#include "base/feature_list.h"
#include "base/strings/string_number_conversions.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/signin/identity_manager_factory.h"
#include "components/enterprise/connectors/core/features.h"
#include "components/enterprise/connectors/core/reporting_utils.h"
#include "components/safe_browsing/core/common/features.h"
#include "components/signin/public/identity_manager/accounts_in_cookie_jar_info.h"
#include "components/signin/public/identity_manager/identity_manager.h"
#include "net/base/url_util.h"
#include "third_party/re2/src/re2/re2.h"

namespace enterprise_connectors {

namespace {

bool IncludeContentAreaAccountEmail(const GURL& url) {
  if (!base::FeatureList::IsEnabled(kEnterpriseActiveUserDetection)) {
    return false;
  }

  static constexpr auto kWorkspaceDomains =
      base::MakeFixedFlatSet<std::string_view>({
          "mail.google.com",
          "meet.google.com",
          "calendar.google.com",
          "drive.google.com",
          "docs.google.com",
          "sites.google.com",
          "keep.google.com",
          "script.google.com",
          "cloudsearch.google.com",
          "console.cloud.google.com",
          "datastudio.google.com",
      });

  for (const auto& domain : kWorkspaceDomains) {
    if (url.DomainIs(domain)) {
      return true;
    }
  }
  return false;
}

std::optional<size_t> GetUserIndex(const GURL& url) {
  const re2::RE2 kUserPathRegex{"/u/(\\d+)/"};

  int account_id = 0;
  if (re2::RE2::PartialMatch(url.path_piece(), kUserPathRegex, &account_id)) {
    return account_id;
  }

  std::string account_id_str;
  if (net::GetValueForKeyInQuery(url, "authuser", &account_id_str) &&
      base::StringToInt(account_id_str, &account_id)) {
    return account_id;
  }

  return std::nullopt;
}

}  // namespace

void ContentAnalysisInfo::InitializeRequest(
    safe_browsing::BinaryUploadService::Request* request,
    bool include_enterprise_only_fields) {
  if (include_enterprise_only_fields) {
    if (settings().cloud_or_local_settings.is_cloud_analysis()) {
      request->set_device_token(settings().cloud_or_local_settings.dm_token());
    }

    // Include tab page title in local content analysis requests.
    if (settings().cloud_or_local_settings.is_local_analysis()) {
      request->set_tab_title(tab_title());
    }

    if (settings().client_metadata) {
      request->set_client_metadata(*settings().client_metadata);
    }

    request->set_per_profile_request(settings().per_profile);

    if (reason() != ContentAnalysisRequest::UNKNOWN) {
      request->set_reason(reason());
    }

    if (base::FeatureList::IsEnabled(safe_browsing::kEnhancedFieldsForSecOps)) {
      request->set_referrer_chain(referrer_chain());
    }

    std::string email = GetContentAreaAccountEmail();
    if (!email.empty()) {
      request->set_content_area_account_email(email);
    }

    if (base::FeatureList::IsEnabled(kEnterpriseIframeDlpRulesSupport)) {
      request->set_frame_url_chain(frame_url_chain());
    }
  }

  request->set_user_action_requests_count(user_action_requests_count());
  request->set_user_action_id(user_action_id());
  request->set_email(email());
  request->set_url(url());
  request->set_tab_url(tab_url());

  for (const auto& tag : settings().tags) {
    request->add_tag(tag.first);
  }

  request->set_blocking(settings().block_until_verdict !=
                        BlockUntilVerdict::kNoBlock);
}

std::string ContentAnalysisInfo::GetContentAreaAccountEmail() const {
  if (!IncludeContentAreaAccountEmail(tab_url())) {
    return "";
  }

  auto* im = identity_manager();
  if (!im) {
    return "";
  }

  auto accounts = im->GetAccountsInCookieJar();

  if (accounts.GetAllAccounts().size() == 1) {
    return accounts.GetAllAccounts()[0].email;
  }

  size_t user_index = GetUserIndex(tab_url()).value_or(0);
  if (user_index >= accounts.GetAllAccounts().size()) {
    return "";
  }

  return accounts.GetAllAccounts()[user_index].email;
}

// static
std::string ContentAreaUserProvider::GetUser(Profile* profile,
                                             const GURL& tab_url) {
  return ContentAreaUserProvider(IdentityManagerFactory::GetForProfile(profile),
                                 tab_url)
      .GetContentAreaAccountEmail();
}

// static
std::string ContentAreaUserProvider::GetUser(
    const content::ClipboardEndpoint& source) {
  if (!source.data_transfer_endpoint() ||
      !source.data_transfer_endpoint()->IsUrlType() ||
      !source.data_transfer_endpoint()->GetURL() || !source.browser_context()) {
    return "";
  }

  return GetUser(Profile::FromBrowserContext(source.browser_context()),
                 *source.data_transfer_endpoint()->GetURL());
}

const GURL& ContentAreaUserProvider::tab_url() const {
  return *tab_url_;
}

signin::IdentityManager* ContentAreaUserProvider::identity_manager() const {
  return im_;
}

const enterprise_connectors::AnalysisSettings&
ContentAreaUserProvider::settings() const {
  NOTREACHED();
}

int ContentAreaUserProvider::user_action_requests_count() const {
  NOTREACHED();
}

std::string ContentAreaUserProvider::tab_title() const {
  NOTREACHED();
}

std::string ContentAreaUserProvider::user_action_id() const {
  NOTREACHED();
}

std::string ContentAreaUserProvider::email() const {
  NOTREACHED();
}

std::string ContentAreaUserProvider::url() const {
  NOTREACHED();
}

enterprise_connectors::ContentAnalysisRequest::Reason
ContentAreaUserProvider::reason() const {
  NOTREACHED();
}

google::protobuf::RepeatedPtrField<::safe_browsing::ReferrerChainEntry>
ContentAreaUserProvider::referrer_chain() const {
  NOTREACHED();
}

google::protobuf::RepeatedPtrField<std::string>
ContentAreaUserProvider::frame_url_chain() const {
  NOTREACHED();
}

ContentAreaUserProvider::ContentAreaUserProvider(signin::IdentityManager* im,
                                                 const GURL& tab_url)
    : im_(im), tab_url_(tab_url) {}

}  // namespace enterprise_connectors