File: screenshot_captured_bubble.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 (245 lines) | stat: -rw-r--r-- 9,910 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
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
// Copyright 2021 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/views/sharing_hub/screenshot/screenshot_captured_bubble.h"

#include <memory>
#include <utility>
#include <vector>

#include "base/containers/span.h"
#include "base/feature_list.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/functional/bind.h"
#include "base/metrics/user_metrics.h"
#include "base/strings/strcat.h"
#include "base/task/thread_pool.h"
#include "base/threading/thread_restrictions.h"
#include "chrome/browser/image_editor/image_editor_component_info.h"
#include "chrome/browser/image_editor/screenshot_flow.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_finder.h"
#include "chrome/browser/ui/browser_navigator.h"
#include "chrome/browser/ui/browser_navigator_params.h"
#include "chrome/browser/ui/color/chrome_color_id.h"
#include "chrome/browser/ui/views/chrome_layout_provider.h"
#include "chrome/common/webui_url_constants.h"
#include "chrome/grit/generated_resources.h"
#include "content/public/browser/download_manager.h"
#include "content/public/browser/download_request_utils.h"
#include "content/public/browser/web_contents.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/metadata/metadata_impl_macros.h"
#include "ui/base/mojom/dialog_button.mojom.h"
#include "ui/base/webui/web_ui_util.h"
#include "ui/gfx/codec/png_codec.h"
#include "ui/gfx/image/image_skia_rep.h"
#include "ui/resources/grit/ui_resources.h"
#include "ui/views/border.h"
#include "ui/views/controls/button/label_button.h"
#include "ui/views/controls/button/md_text_button.h"
#include "ui/views/controls/image_view.h"
#include "ui/views/layout/box_layout.h"
#include "ui/views/layout/table_layout_view.h"
#include "ui/views/view.h"

namespace {

// Rendered image size, pixels.
constexpr int kImageWidthPx = 336;
constexpr int kImageHeightPx = 252;

}  // namespace

namespace sharing_hub {

ScreenshotCapturedBubble::ScreenshotCapturedBubble(
    views::View* anchor_view,
    content::WebContents* web_contents,
    const gfx::Image& image,
    Profile* profile)
    : LocationBarBubbleDelegateView(anchor_view, nullptr),
      image_(image),
      web_contents_(web_contents->GetWeakPtr()),
      profile_(profile) {
  SetButtons(static_cast<int>(ui::mojom::DialogButton::kNone));
  SetTitle(IDS_BROWSER_SHARING_SCREENSHOT_POST_CAPTURE_TITLE);
}

ScreenshotCapturedBubble::~ScreenshotCapturedBubble() = default;

void ScreenshotCapturedBubble::OnThemeChanged() {
  LocationBarBubbleDelegateView::OnThemeChanged();

  const int border_radius = ChromeLayoutProvider::Get()->GetCornerRadiusMetric(
      views::Emphasis::kHigh);
  const auto* const color_provider = GetColorProvider();
  image_view_->SetBorder(views::CreateRoundedRectBorder(
      /*thickness=*/2, border_radius,
      color_provider->GetColor(kColorScreenshotCapturedImageBorder)));
  image_view_->SetBackground(views::CreateRoundedRectBackground(
      color_provider->GetColor(kColorScreenshotCapturedImageBackground),
      border_radius, 2));
}

void ScreenshotCapturedBubble::Show() {
  ShowForReason(USER_GESTURE);
}

views::View* ScreenshotCapturedBubble::GetInitiallyFocusedView() {
  return download_button_;
}

bool ScreenshotCapturedBubble::ShouldShowCloseButton() const {
  return true;
}

void ScreenshotCapturedBubble::WindowClosing() {}

void ScreenshotCapturedBubble::Init() {
  auto* layout_provider = ChromeLayoutProvider::Get();

  // Requesting TEXT for trailing prevents extra padding at bottom of dialog.
  gfx::Insets insets = layout_provider->GetDialogInsetsForContentType(
      views::DialogContentType::kControl, views::DialogContentType::kText);
  const int border_radius =
      layout_provider->GetCornerRadiusMetric(views::Emphasis::kHigh);

  int width_padding =
      (kImageWidthPx + border_radius - GetImageSize().width()) / 2;

  using Alignment = views::ImageView::Alignment;
  auto builder =
      views::Builder<ScreenshotCapturedBubble>(this)
          .set_margins(insets)
          .SetLayoutManager(std::make_unique<views::BoxLayout>(
              views::BoxLayout::Orientation::kVertical, gfx::Insets(),
              layout_provider->GetDistanceMetric(
                  DISTANCE_UNRELATED_CONTROL_VERTICAL_LARGE)))
          .AddChild(
              views::Builder<views::TableLayoutView>()
                  .AddPaddingColumn(views::TableLayout::kFixedSize,
                                    width_padding)
                  .AddColumn(views::LayoutAlignment::kCenter,
                             views::LayoutAlignment::kCenter, 1.0,
                             views::TableLayout::ColumnSize::kUsePreferred, 0,
                             0)
                  .AddPaddingColumn(views::TableLayout::kFixedSize,
                                    width_padding)
                  .AddRows(1, views::TableLayout::kFixedSize, 0)
                  .AddChild(views::Builder<views::ImageView>()
                                .SetHorizontalAlignment(Alignment::kCenter)
                                .SetVerticalAlignment(Alignment::kCenter)
                                .SetImageSize(GetImageSize())
                                .SetPreferredSize(
                                    GetImageSize() +
                                    gfx::Size(border_radius, border_radius))
                                .SetImage(ui::ImageModel::FromImage(image_))
                                .SetVisible(true)
                                .CopyAddressTo(&image_view_)));

  auto download_button =
      views::Builder<views::MdTextButton>()
          .SetCallback(base::BindRepeating(
              &ScreenshotCapturedBubble::DownloadButtonPressed,
              weak_factory_.GetWeakPtr()))
          .SetText(l10n_util::GetStringUTF16(
              IDS_BROWSER_SHARING_SCREENSHOT_DIALOG_DOWNLOAD_BUTTON_LABEL))
          .SetStyle(ui::ButtonStyle::kProminent)
          .Build();

  auto download_row = views::Builder<views::TableLayoutView>();

  // Column for download button
  download_row
      .AddColumn(views::LayoutAlignment::kEnd, views::LayoutAlignment::kCenter,
                 1.0, views::TableLayout::ColumnSize::kUsePreferred, 0, 0)
      .AddRows(1, views::TableLayout::kFixedSize, 0);
  download_row.AddChild(
      views::Builder<views::MdTextButton>(std::move(download_button))
          .CopyAddressTo(&download_button_));

  std::move(builder).AddChild(std::move(download_row)).BuildChildren();
}

/*static*/
const std::u16string ScreenshotCapturedBubble::GetFilenameForURL(
    const GURL& url) {
  if (!url.has_host() || url.HostIsIPAddress()) {
    return u"chrome_screenshot.png";
  }

  return base::ASCIIToUTF16(
      base::StrCat({"chrome_screenshot_", url.host(), ".png"}));
}

void ScreenshotCapturedBubble::DownloadButtonPressed() {
  // Returns closest scaling to parameter (1.0).
  const SkBitmap& bitmap =
      image_view_->GetImage().GetRepresentation(1.0f).GetBitmap();
  const GURL data_url = GURL(webui::GetBitmapDataUrl(bitmap));

  if (!web_contents_) {
    return;
  }

  Browser* browser = chrome::FindBrowserWithTab(web_contents_.get());
  content::DownloadManager* download_manager =
      browser->profile()->GetDownloadManager();
  // TODO(crbug.com/40753957): Update the annotation's |setting| and
  // |chrome_policy| fields once the Sharing Hub is landed.
  net::NetworkTrafficAnnotationTag traffic_annotation =
      net::DefineNetworkTrafficAnnotation("desktop_screenshot_save", R"(
      semantics {
        sender: "Desktop Screenshots"
        description:
          "The user may capture a selection of the current page. This bubble "
          "view has a download button to save the generated image via a data "
          "URL to the disk on the local client. The feature is only for Mac, "
          "Windows and Linux OS."
        trigger: "User clicks 'download' in a bubble view launched from the "
          "omnibox after the 'Screenshot' option is selected in the sharing "
          "hub and a selection is made on the page. "
        data: "A capture of a portion of the current webpage."
        destination: LOCAL
      }
      policy {
        cookies_allowed: NO
        setting:
          "No user-visible setting for this feature. Experiment and rollout to "
          "be coordinated via Chrome Variations. This feature reads settings "
          "from prefs::kDisableScreenshots which is controlled by this policy."
        policy_exception_justification:
          "Not implemented, considered not required."
      })");
  std::unique_ptr<download::DownloadUrlParameters> params =
      content::DownloadRequestUtils::CreateDownloadForWebContentsMainFrame(
          web_contents_.get(), data_url, traffic_annotation);
  // Suggest a name incorporating the hostname. Protocol, TLD, etc are
  // not taken into consideration. Duplicate names get automatic suffixes.
  params->set_suggested_name(
      GetFilenameForURL(web_contents_->GetLastCommittedURL()));
  download_manager->DownloadUrl(std::move(params));
  base::RecordAction(base::UserMetricsAction(
      "SharingDesktopScreenshot.ScreenshotSavedViaBubble"));
}

// Calculates the size of the image with padding.
gfx::Size ScreenshotCapturedBubble::GetImageSize() {
  float scale_factor_x =
      static_cast<float>(kImageWidthPx) / static_cast<float>(image_.Width());
  float scale_factor_y =
      static_cast<float>(kImageHeightPx) / static_cast<float>(image_.Height());
  float scale_factor =
      scale_factor_x < scale_factor_y ? scale_factor_x : scale_factor_y;
  return gfx::Size(scale_factor * image_.Width(),
                   scale_factor * image_.Height());
}

BEGIN_METADATA(ScreenshotCapturedBubble)
END_METADATA

}  // namespace sharing_hub