File: text_attachment.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 (160 lines) | stat: -rw-r--r-- 5,626 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
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <algorithm>
#include <utility>

#include "base/strings/strcat.h"
#include "chrome/browser/nearby_sharing/share_target.h"
#include "chrome/browser/nearby_sharing/text_attachment.h"
#include "components/drive/drive_api_util.h"
#include "url/gurl.h"

namespace {

// Tries to get a valid host name from the |text|. Returns nullopt otherwise.
std::optional<std::string> GetHostFromText(const std::string& text) {
  GURL url(text);
  if (!url.is_valid() || !url.has_host())
    return std::nullopt;

  return url.host();
}

// Masks the given |number| depending on the string length:
//  - length <= 4: Masks all characters
//  - 4 < length <= 6 Masks the last 4 characters
//  - 6 < length <= 10: Skips the first 2 and masks the following 4 characters
//  - length > 10: Skips the first 2 and last 4 characters. Masks the rest of
//    the string
// Note: We're assuming a formatted phone number and won't try to reformat to
// E164 like on Android as there's no easy way of determining the intended
// region for the phone number.
std::string MaskPhoneNumber(const std::string& number) {
  constexpr int kMinMaskedDigits = 4;
  constexpr int kMaxLeadingDigits = 2;
  constexpr int kMaxTailingDigits = 4;
  constexpr int kLengthWithNoLeadingDigits = kMinMaskedDigits;
  constexpr int kLengthWithNoTailingDigits =
      kMinMaskedDigits + kMaxLeadingDigits;

  if (number.empty())
    return number;

  std::string result = number;
  bool has_plus = false;
  if (number[0] == '+') {
    result = number.substr(1);
    has_plus = true;
  }

  // First calculate how many digits we would mask having exactly
  // kMinMaskedDigits digits masked.
  int leading_digits = 0;
  if (result.length() > kLengthWithNoLeadingDigits)
    leading_digits = result.length() - kLengthWithNoLeadingDigits;

  int tailing_digits = 0;
  if (result.length() > kLengthWithNoTailingDigits)
    tailing_digits = result.length() - kLengthWithNoTailingDigits;

  // Now limit resulting numbers of digits to maximally allowed values.
  leading_digits = std::min(kMaxLeadingDigits, leading_digits);
  tailing_digits = std::min(kMaxTailingDigits, tailing_digits);
  int masked_digits = result.length() - leading_digits - tailing_digits;

  return base::StrCat({(has_plus ? "+" : ""), result.substr(0, leading_digits),
                       std::string(masked_digits, 'x'),
                       result.substr(result.length() - tailing_digits)});
}

std::string GetTextTitle(const std::string& text_body,
                         TextAttachment::Type type) {
  constexpr size_t kMaxPreviewTextLength = 32;

  switch (type) {
    case TextAttachment::Type::kUrl: {
      std::optional<std::string> host = GetHostFromText(text_body);
      if (host)
        return *host;

      break;
    }
    case TextAttachment::Type::kPhoneNumber:
      return MaskPhoneNumber(text_body);
    default:
      break;
  }

  if (text_body.size() > kMaxPreviewTextLength)
    return base::StrCat({text_body.substr(0, kMaxPreviewTextLength), "…"});

  return text_body;
}

}  // namespace

TextAttachment::TextAttachment(Type type,
                               std::string text_body,
                               std::optional<std::string> text_title,
                               std::optional<std::string> mime_type)
    : Attachment(Attachment::Family::kText, text_body.size()),
      type_(type),
      text_title_(text_title && !text_title->empty()
                      ? *text_title
                      : GetTextTitle(text_body, type)),
      text_body_(std::move(text_body)),
      mime_type_(mime_type ? *mime_type : std::string()) {}

TextAttachment::TextAttachment(int64_t id,
                               Type type,
                               std::string text_title,
                               int64_t size)
    : Attachment(id, Attachment::Family::kText, size),
      type_(type),
      text_title_(std::move(text_title)) {}

TextAttachment::TextAttachment(const TextAttachment&) = default;

TextAttachment::TextAttachment(TextAttachment&&) = default;

TextAttachment& TextAttachment::operator=(const TextAttachment&) = default;

TextAttachment& TextAttachment::operator=(TextAttachment&&) = default;

TextAttachment::~TextAttachment() = default;

void TextAttachment::MoveToShareTarget(ShareTarget& share_target) {
  share_target.text_attachments.push_back(std::move(*this));
}

const std::string& TextAttachment::GetDescription() const {
  return text_title_;
}

nearby_share::mojom::ShareType TextAttachment::GetShareType() const {
  switch (type()) {
    case TextAttachment::Type::kUrl:
      if (mime_type_ == drive::util::kGoogleDocumentMimeType) {
        return nearby_share::mojom::ShareType::kGoogleDocsFile;
      } else if (mime_type_ == drive::util::kGoogleSpreadsheetMimeType) {
        return nearby_share::mojom::ShareType::kGoogleSheetsFile;
      } else if (mime_type_ == drive::util::kGooglePresentationMimeType) {
        return nearby_share::mojom::ShareType::kGoogleSlidesFile;
      } else {
        return nearby_share::mojom::ShareType::kUrl;
      }
    case TextAttachment::Type::kAddress:
      return nearby_share::mojom::ShareType::kAddress;
    case TextAttachment::Type::kPhoneNumber:
      return nearby_share::mojom::ShareType::kPhone;
    default:
      return nearby_share::mojom::ShareType::kText;
  }
}

void TextAttachment::set_text_body(std::string text_body) {
  text_body_ = std::move(text_body);
  text_title_ = GetTextTitle(text_body_, type_);
}