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

ash / clipboard / clipboard_history_item.cc [blame]

// 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 "ash/clipboard/clipboard_history_item.h"

#include <optional>
#include <string>
#include <string_view>
#include <vector>

#include "ash/clipboard/clipboard_history_util.h"
#include "ash/strings/grit/ash_strings.h"
#include "base/callback_list.h"
#include "base/check.h"
#include "base/containers/contains.h"
#include "base/notreached.h"
#include "base/strings/escape.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "base/time/time.h"
#include "chromeos/crosapi/mojom/clipboard_history.mojom.h"
#include "chromeos/ui/clipboard_history/clipboard_history_util.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/models/image_model.h"
#include "ui/gfx/image/image.h"
#include "ui/strings/grit/ui_strings.h"

namespace ash {

namespace {

crosapi::mojom::ClipboardHistoryDisplayFormat CalculateDisplayFormat(
    const ClipboardHistoryItem& item) {
  switch (item.main_format()) {
    case ui::ClipboardInternalFormat::kPng:
      return crosapi::mojom::ClipboardHistoryDisplayFormat::kPng;
    case ui::ClipboardInternalFormat::kHtml:
      if (!base::Contains(item.data().markup_data(), "<img") &&
          !base::Contains(item.data().markup_data(), "<table")) {
        return crosapi::mojom::ClipboardHistoryDisplayFormat::kText;
      }
      return crosapi::mojom::ClipboardHistoryDisplayFormat::kHtml;
    case ui::ClipboardInternalFormat::kText:
    case ui::ClipboardInternalFormat::kSvg:
    case ui::ClipboardInternalFormat::kRtf:
    case ui::ClipboardInternalFormat::kBookmark:
    case ui::ClipboardInternalFormat::kWeb:
      return crosapi::mojom::ClipboardHistoryDisplayFormat::kText;
    case ui::ClipboardInternalFormat::kFilenames:
      return crosapi::mojom::ClipboardHistoryDisplayFormat::kFile;
    case ui::ClipboardInternalFormat::kCustom:
      return clipboard_history_util::ContainsFileSystemData(item.data())
                 ? crosapi::mojom::ClipboardHistoryDisplayFormat::kFile
                 : crosapi::mojom::ClipboardHistoryDisplayFormat::kText;
  }
}

std::optional<ui::ImageModel> DetermineDisplayImage(
    const ClipboardHistoryItem& item) {
  std::optional<ui::ImageModel> maybe_image;
  switch (item.display_format()) {
    case crosapi::mojom::ClipboardHistoryDisplayFormat::kUnknown:
      NOTREACHED();
    case crosapi::mojom::ClipboardHistoryDisplayFormat::kText:
    case crosapi::mojom::ClipboardHistoryDisplayFormat::kFile:
      break;
    case crosapi::mojom::ClipboardHistoryDisplayFormat::kPng: {
      gfx::Image image;
      if (const auto& maybe_png = item.data().maybe_png()) {
        image = gfx::Image::CreateFrom1xPNGBytes(maybe_png.value());
      } else {
        // If we have not yet encoded the bitmap to a PNG, just create the
        // image using the available bitmap. No information is lost here.
        auto maybe_bitmap = item.data().GetBitmapIfPngNotEncoded();
        DCHECK(maybe_bitmap.has_value());
        image = gfx::Image::CreateFrom1xBitmap(maybe_bitmap.value());
      }
      maybe_image = ui::ImageModel::FromImage(image);
      break;
    }
    case crosapi::mojom::ClipboardHistoryDisplayFormat::kHtml:
      // The `ClipboardHistoryResourceManager` will update this preview once an
      // image model is rendered.
      maybe_image = clipboard_history_util::GetHtmlPreviewPlaceholder();
      break;
  }
  return maybe_image;
}

// Returns the text to display for the file system data contained within `data`.
std::u16string DetermineDisplayTextForFileSystemData(
    const ui::ClipboardData& data) {
  // This code should not be reached if `data` doesn't contain file system data.
  std::u16string sources;
  std::vector<std::u16string_view> source_list;
  clipboard_history_util::GetSplitFileSystemData(data, &source_list, &sources);
  CHECK(!sources.empty());

  size_t file_count = source_list.size();
  if (file_count > 1u) {
    return l10n_util::GetPluralStringFUTF16(
        IDS_ASH_CLIPBOARD_HISTORY_FILE_COUNT, file_count);
  }

  // Strip path information, so all that's left are file names.
  for (auto& source : source_list) {
    source = source.substr(source.find_last_of(u"/") + 1);
  }

  // Join file names, unescaping encoded character sequences for display. This
  // ensures that "My%20File.txt" will display as "My File.txt".
  return base::UTF8ToUTF16(base::UnescapeURLComponent(
      base::UTF16ToUTF8(base::JoinString(source_list, u", ")),
      base::UnescapeRule::SPACES));
}

std::u16string DetermineDisplayText(const ClipboardHistoryItem& item) {
  switch (item.main_format()) {
    case ui::ClipboardInternalFormat::kPng:
      return l10n_util::GetStringUTF16(IDS_CLIPBOARD_MENU_IMAGE);
    case ui::ClipboardInternalFormat::kText:
      return base::UTF8ToUTF16(item.data().text());
    case ui::ClipboardInternalFormat::kHtml:
      // Show plain text if it exists. Otherwise, show the placeholder.
      if (!item.data().text().empty()) {
        return base::UTF8ToUTF16(item.data().text());
      }

      return l10n_util::GetStringUTF16(IDS_CLIPBOARD_MENU_HTML);
    case ui::ClipboardInternalFormat::kSvg:
      return base::UTF8ToUTF16(item.data().svg_data());
    case ui::ClipboardInternalFormat::kRtf:
      return l10n_util::GetStringUTF16(IDS_CLIPBOARD_MENU_RTF_CONTENT);
    case ui::ClipboardInternalFormat::kBookmark:
      return base::UTF8ToUTF16(item.data().bookmark_title());
    case ui::ClipboardInternalFormat::kWeb:
      return l10n_util::GetStringUTF16(IDS_CLIPBOARD_MENU_WEB_SMART_PASTE);
    case ui::ClipboardInternalFormat::kFilenames:
    case ui::ClipboardInternalFormat::kCustom:
      // Currently, the only supported type of custom data is file system data.
      return DetermineDisplayTextForFileSystemData(item.data());
  }
}

std::optional<gfx::ElideBehavior> DetermineDisplayTextElideBehavior(
    const ClipboardHistoryItem& item) {
  return chromeos::clipboard_history::IsUrl(item.display_text())
             ? std::make_optional(gfx::ELIDE_MIDDLE)
             : std::nullopt;
}

std::optional<size_t> DetermineDisplayTextMaxLines(
    const ClipboardHistoryItem& item) {
  return chromeos::clipboard_history::IsUrl(item.display_text())
             ? std::make_optional(1u)
             : std::nullopt;
}

std::optional<ui::ImageModel> DetermineIcon(const ClipboardHistoryItem& item) {
  return chromeos::clipboard_history::GetIconForDescriptor(
      clipboard_history_util::ItemToDescriptor(item));
}

}  // namespace

ClipboardHistoryItem::ClipboardHistoryItem(ui::ClipboardData data)
    : id_(base::UnguessableToken::Create()),
      data_(std::move(data)),
      time_copied_(base::Time::Now()),
      main_format_(clipboard_history_util::CalculateMainFormat(data_).value()),
      display_format_(CalculateDisplayFormat(*this)),
      display_image_(DetermineDisplayImage(*this)),
      display_text_(DetermineDisplayText(*this)),
      display_text_elide_behavior_(DetermineDisplayTextElideBehavior(*this)),
      display_text_max_lines_(DetermineDisplayTextMaxLines(*this)),
      file_count_(clipboard_history_util::GetCountOfCopiedFiles(data_)),
      icon_(DetermineIcon(*this)) {}

ClipboardHistoryItem::ClipboardHistoryItem(const ClipboardHistoryItem& other)
    : id_(other.id_),
      data_(other.data_),
      time_copied_(other.time_copied_),
      main_format_(other.main_format_),
      display_format_(other.display_format_),
      display_image_(other.display_image_),
      display_text_(other.display_text_),
      display_text_elide_behavior_(other.display_text_elide_behavior_),
      display_text_max_lines_(other.display_text_max_lines_),
      file_count_(other.file_count_),
      icon_(other.icon_),
      secondary_display_text_(other.secondary_display_text_) {}

ClipboardHistoryItem::ClipboardHistoryItem(ClipboardHistoryItem&& other)
    : id_(std::move(other.id_)),
      data_(std::move(other.data_)),
      time_copied_(std::move(other.time_copied_)),
      main_format_(std::move(other.main_format_)),
      display_format_(std::move(other.display_format_)),
      display_image_(std::move(other.display_image_)),
      display_text_(std::move(other.display_text_)),
      display_text_elide_behavior_(
          std::move(other.display_text_elide_behavior_)),
      display_text_max_lines_(std::move(other.display_text_max_lines_)),
      file_count_(std::move(other.file_count_)),
      icon_(std::move(other.icon_)),
      secondary_display_text_(std::move(other.secondary_display_text_)) {}

ClipboardHistoryItem::~ClipboardHistoryItem() = default;

ui::ClipboardData ClipboardHistoryItem::ReplaceEquivalentData(
    ui::ClipboardData&& new_data) {
  DCHECK(data_ == new_data);
  time_copied_ = base::Time::Now();
  // If work has already been done to encode an image belonging to both data
  // instances, make sure it is not lost.
  if (data_.maybe_png() && !new_data.maybe_png())
    new_data.SetPngDataAfterEncoding(*data_.maybe_png());
  return std::exchange(data_, std::move(new_data));
}

void ClipboardHistoryItem::SetDisplayImage(
    const ui::ImageModel& display_image) {
  CHECK(display_image.IsImage());
  display_image_ = display_image;
  display_image_updated_callbacks_.Notify();
}

base::CallbackListSubscription
ClipboardHistoryItem::AddDisplayImageUpdatedCallback(
    base::RepeatingClosure callback) const {
  return display_image_updated_callbacks_.Add(std::move(callback));
}

}  // namespace ash