File: page_info_permission_content_view.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 (415 lines) | stat: -rw-r--r-- 17,306 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
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
// 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/page_info/page_info_permission_content_view.h"

#include <algorithm>

#include "base/debug/dump_without_crashing.h"
#include "base/feature_list.h"
#include "base/metrics/histogram_functions.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/file_system_access/chrome_file_system_access_permission_context.h"
#include "chrome/browser/file_system_access/file_system_access_features.h"
#include "chrome/browser/file_system_access/file_system_access_permission_context_factory.h"
#include "chrome/browser/ui/chrome_pages.h"
#include "chrome/browser/ui/color/chrome_color_id.h"
#include "chrome/browser/ui/layout_constants.h"
#include "chrome/browser/ui/page_info/chrome_page_info_ui_delegate.h"
#include "chrome/browser/ui/ui_features.h"
#include "chrome/browser/ui/views/accessibility/non_accessible_image_view.h"
#include "chrome/browser/ui/views/chrome_layout_provider.h"
#include "chrome/browser/ui/views/controls/rich_hover_button.h"
#include "chrome/browser/ui/views/file_system_access/file_system_access_scroll_panel.h"
#include "chrome/browser/ui/views/page_info/page_info_view_factory.h"
#include "components/page_info/page_info.h"
#include "components/permissions/permission_util.h"
#include "components/strings/grit/components_strings.h"
#include "content/public/browser/web_contents.h"
#include "third_party/blink/public/common/features.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/metadata/metadata_impl_macros.h"
#include "ui/views/accessibility/view_accessibility.h"
#include "ui/views/controls/button/checkbox.h"
#include "ui/views/controls/button/toggle_button.h"
#include "ui/views/controls/label.h"
#include "ui/views/controls/separator.h"
#include "ui/views/layout/flex_layout.h"

#if !BUILDFLAG(IS_CHROMEOS)
#include "chrome/browser/ui/views/media_preview/media_preview_feature.h"
#endif

namespace {
std::u16string PageInfoSubpageText(ContentSettingsType type) {
  // Without this, the title and toggle accessibility text inside the submenu of
  // |CAPTURED_SURFACE_CONTROL| permission type would be the same as in the main
  // page info. This block sets the submenu text to a different one.
  return (type == ContentSettingsType::CAPTURED_SURFACE_CONTROL)
             ? l10n_util::GetStringUTF16(
                   IDS_SITE_SETTINGS_TYPE_CAPTURED_SURFACE_CONTROL_SUB_MENU)
             : PageInfoUI::PermissionTypeToUIString(type);
}
}  // namespace

PageInfoPermissionContentView::PageInfoPermissionContentView(
    PageInfo* presenter,
    ChromePageInfoUiDelegate* ui_delegate,
    ContentSettingsType type,
    content::WebContents* web_contents)
    : presenter_(presenter), type_(type), ui_delegate_(ui_delegate) {
  CHECK(web_contents);
  web_contents_ = web_contents->GetWeakPtr();

  ChromeLayoutProvider* layout_provider = ChromeLayoutProvider::Get();
  const int bottom_margin =
      layout_provider->GetDistanceMetric(DISTANCE_CONTENT_LIST_VERTICAL_MULTI);
  // The last view is a RichHoverButton, which overrides the bottom
  // dialog inset in favor of its own.
  SetProperty(views::kMarginsKey, gfx::Insets::TLBR(0, 0, bottom_margin, 0));

  // Use the same insets as buttons and permission rows in the main page for
  // consistency.
  const auto button_insets =
      layout_provider->GetInsetsMetric(INSETS_PAGE_INFO_HOVER_BUTTON);
  const int controls_spacing = layout_provider->GetDistanceMetric(
      views::DISTANCE_RELATED_CONTROL_VERTICAL);

  auto* layout_manager =
      SetLayoutManager(std::make_unique<views::FlexLayout>());
  layout_manager->SetOrientation(views::LayoutOrientation::kVertical);

  auto* permission_info_container =
      AddChildView(std::make_unique<views::View>());
  permission_info_container
      ->SetLayoutManager(std::make_unique<views::FlexLayout>())
      ->SetCrossAxisAlignment(views::LayoutAlignment::kStart)
      .SetInteriorMargin(button_insets);

  icon_ = permission_info_container->AddChildView(
      std::make_unique<NonAccessibleImageView>());

  auto* label_wrapper = permission_info_container->AddChildView(
      PageInfoViewFactory::CreateLabelWrapper());
  title_ = label_wrapper->AddChildView(std::make_unique<views::Label>(
      PageInfoSubpageText(type), views::style::CONTEXT_DIALOG_BODY_TEXT));
  title_->SetHorizontalAlignment(gfx::ALIGN_LEFT);
  title_->SetTextStyle(views::style::STYLE_BODY_3_MEDIUM);
  title_->SetEnabledColor(kColorPageInfoForeground);

  state_label_ = label_wrapper->AddChildView(std::make_unique<views::Label>(
      std::u16string(), views::style::CONTEXT_LABEL,
      views::style::STYLE_BODY_4));
  state_label_->SetEnabledColor(kColorPageInfoSubtitleForeground);
  state_label_->SetHorizontalAlignment(gfx::ALIGN_LEFT);

  // Add extra details as sublabel.
  std::u16string detail = ui_delegate_->GetPermissionDetail(type);
  if (!detail.empty()) {
    auto detail_label = std::make_unique<views::Label>(
        detail, views::style::CONTEXT_LABEL, views::style::STYLE_BODY_4);
    detail_label->SetHorizontalAlignment(gfx::ALIGN_LEFT);
    detail_label->SetEnabledColor(kColorPageInfoSubtitleForeground);
    label_wrapper->AddChildView(std::move(detail_label));
  }

  if (type == ContentSettingsType::FILE_SYSTEM_WRITE_GUARD &&
      base::FeatureList::IsEnabled(
          features::kFileSystemAccessPersistentPermissions)) {
    std::vector<base::FilePath> granted_file_paths;
    auto* context =
        FileSystemAccessPermissionContextFactory::GetForProfileIfExists(
            web_contents->GetBrowserContext());
    if (context) {
      granted_file_paths = context->GetGrantedPaths(
          url::Origin::Create(web_contents->GetLastCommittedURL()));
    }
    if (!granted_file_paths.empty()) {
      std::unique_ptr<views::ScrollView> scroll_panel =
          FileSystemAccessScrollPanel::Create(granted_file_paths);
      scroll_panel->SetID(
          PageInfoViewFactory::
              VIEW_ID_PAGE_INFO_PERMISSION_SUBPAGE_FILE_SYSTEM_SCROLL_PANEL);
      label_wrapper->AddChildView(std::move(scroll_panel));
    }
  }
  remember_setting_ =
      label_wrapper->AddChildView(std::make_unique<views::Checkbox>(
          l10n_util::GetStringUTF16(
              IDS_PAGE_INFO_PERMISSIONS_SUBPAGE_REMEMBER_THIS_SETTING),
          base::BindRepeating(
              &PageInfoPermissionContentView::OnRememberSettingPressed,
              base::Unretained(this)),
          views::style::CONTEXT_DIALOG_BODY_TEXT));
  remember_setting_->SetID(
      PageInfoViewFactory::
          VIEW_ID_PAGE_INFO_PERMISSION_SUBPAGE_REMEMBER_CHECKBOX);
  remember_setting_->SetProperty(views::kMarginsKey,
                                 gfx::Insets::TLBR(controls_spacing, 0, 0, 0));

  const int title_height =
      title_->GetPreferredSize(views::SizeBounds(title_->width(), {})).height();
  toggle_button_ = permission_info_container->AddChildView(
      std::make_unique<views::ToggleButton>(base::BindRepeating(
          &PageInfoPermissionContentView::OnToggleButtonPressed,
          base::Unretained(this))));
  toggle_button_->GetViewAccessibility().SetName(l10n_util::GetStringFUTF16(
      IDS_PAGE_INFO_SELECTOR_TOOLTIP, PageInfoSubpageText(type)));
  toggle_button_->SetPreferredSize(
      gfx::Size(toggle_button_->GetPreferredSize().width(), title_height));

  // Calculate difference between label height and icon size to align icons
  // and label in the first row.
  const int margin =
      (title_height - GetLayoutConstant(PAGE_INFO_ICON_SIZE)) / 2;
  icon_->SetProperty(views::kMarginsKey, gfx::Insets::VH(margin, 0));
  toggle_button_->SetProperty(views::kMarginsKey, gfx::Insets::VH(margin, 0));

  auto* separator = AddChildView(PageInfoViewFactory::CreateSeparator(
      ChromeLayoutProvider::Get()->GetDistanceMetric(
          DISTANCE_HORIZONTAL_SEPARATOR_PADDING_PAGE_INFO_VIEW)));

  MaybeAddMediaPreview(web_contents, *separator);

  // TODO(crbug.com/40775890): Consider to use permission specific text.
  auto* subpage_manage_button = AddChildView(std::make_unique<RichHoverButton>(
      base::BindRepeating(
          [](PageInfoPermissionContentView* view) {
            view->presenter_->OpenContentSettingsExceptions(view->type_);
          },
          this),
      PageInfoViewFactory::GetSiteSettingsIcon(),
      l10n_util::GetStringUTF16(
          IDS_PAGE_INFO_PERMISSIONS_SUBPAGE_MANAGE_BUTTON),
      std::u16string(), PageInfoViewFactory::GetLaunchIcon()));
  subpage_manage_button->SetID(
      PageInfoViewFactory::VIEW_ID_PAGE_INFO_PERMISSION_SUBPAGE_MANAGE_BUTTON);
  subpage_manage_button->SetTooltipText(l10n_util::GetStringUTF16(
      IDS_PAGE_INFO_PERMISSIONS_SUBPAGE_MANAGE_BUTTON_TOOLTIP));
  subpage_manage_button->SetTitleTextStyleAndColor(
      views::style::STYLE_BODY_3_MEDIUM, kColorPageInfoForeground);
  presenter_->InitializeUiState(this, base::DoNothing());
}

PageInfoPermissionContentView::~PageInfoPermissionContentView() {
#if !BUILDFLAG(IS_CHROMEOS)
  if (previews_coordinator_) {
    previews_coordinator_->UpdateDevicePreferenceRanking();
  }
#endif
}

void PageInfoPermissionContentView::SetPermissionInfo(
    const PermissionInfoList& permission_info_list,
    ChosenObjectInfoList chosen_object_info_list) {
  auto permission_it = std::ranges::find(permission_info_list, type_,
                                         &PageInfo::PermissionInfo::type);

  CHECK(permission_it != permission_info_list.end());

  permission_ = *permission_it;
  icon_->SetImage(PageInfoViewFactory::GetPermissionIcon(permission_));

  std::u16string auto_blocked_label =
      PageInfoUI::PermissionAutoBlockedToUIString(ui_delegate_, permission_);
  // TODO(olesiamarukhno): For pending request if available show a longer
  // version of auto-block explanation here instead (same as in content
  // settings bubble).
  if (!auto_blocked_label.empty()) {
    state_label_->SetText(auto_blocked_label);
  } else {
    // TODO(crbug.com/325020452): Update label for File System Access to be
    // displayed on this view to meet UX requirements for the Persistent
    // Permissions feature.
    if (type_ != ContentSettingsType::FILE_SYSTEM_WRITE_GUARD ||
        !base::FeatureList::IsEnabled(
            features::kFileSystemAccessPersistentPermissions)) {
      state_label_->SetText(
          PageInfoUI::PermissionStateToUIString(ui_delegate_, permission_));
    }
  }

  bool is_toggle_on = PageInfoUI::IsToggleOn(permission_);
  toggle_button_->SetIsOn(is_toggle_on);

#if !BUILDFLAG(IS_CHROMEOS)
  if (previews_coordinator_) {
    previews_coordinator_->OnPermissionChange(is_toggle_on);
  }
#endif

  if (type_ == ContentSettingsType::FILE_SYSTEM_WRITE_GUARD &&
      base::FeatureList::IsEnabled(
          features::kFileSystemAccessPersistentPermissions)) {
    if (web_contents_.MaybeValid()) {
      auto* context =
          FileSystemAccessPermissionContextFactory::GetForProfileIfExists(
              web_contents_->GetBrowserContext());
      remember_setting_->SetVisible(context && permission_.setting !=
                                                   CONTENT_SETTING_BLOCK);
      remember_setting_->SetChecked(
          context && context->OriginHasExtendedPermission(url::Origin::Create(
                         web_contents_->GetLastCommittedURL())));
    }
  } else {
    remember_setting_->SetChecked(!permission_.is_one_time &&
                                  permission_.setting !=
                                      CONTENT_SETTING_DEFAULT);
    remember_setting_->SetVisible(
        (permissions::PermissionUtil::IsPermission(type_) &&
         permissions::PermissionUtil::DoesSupportTemporaryGrants(
             permission_.type)) &&
        (permission_.setting != CONTENT_SETTING_BLOCK));
  }
  PreferredSizeChanged();
}

void PageInfoPermissionContentView::ChildPreferredSizeChanged(
    views::View* child) {
  PreferredSizeChanged();
}

void PageInfoPermissionContentView::OnToggleButtonPressed() {
  PageInfoUI::ToggleBetweenAllowAndBlock(permission_);

  // One time permissible permissions show a remember me checkbox only for the
  // non-deny state.
  if (permissions::PermissionUtil::DoesSupportTemporaryGrants(
          permission_.type)) {
    PreferredSizeChanged();
  }

  PermissionChanged();
}

void PageInfoPermissionContentView::OnRememberSettingPressed() {
  if (type_ == ContentSettingsType::FILE_SYSTEM_WRITE_GUARD) {
    ToggleFileSystemExtendedPermissions();
  } else {
    PageInfoUI::ToggleBetweenRememberAndForget(permission_);
  }
  PermissionChanged();
}

void PageInfoPermissionContentView::PermissionChanged() {
  presenter_->OnSitePermissionChanged(permission_.type, permission_.setting,
                                      permission_.requesting_origin,
                                      permission_.is_one_time);
}

#if !BUILDFLAG(IS_CHROMEOS)
void PageInfoPermissionContentView::OnAudioDevicesChanged(
    const std::optional<std::vector<media::AudioDeviceDescription>>&
        device_infos) {
  if (type_ == ContentSettingsType::MEDIASTREAM_MIC && device_infos) {
    SetTitleTextAndTooltip(
        IDS_SITE_SETTINGS_TYPE_MIC_WITH_COUNT,
        media_effects::GetRealAudioDeviceNames(device_infos.value()));
  }
}

void PageInfoPermissionContentView::OnVideoDevicesChanged(
    const std::optional<std::vector<media::VideoCaptureDeviceInfo>>&
        device_infos) {
  if (type_ == ContentSettingsType::MEDIASTREAM_CAMERA && device_infos) {
    SetTitleTextAndTooltip(
        IDS_SITE_SETTINGS_TYPE_CAMERA_WITH_COUNT,
        media_effects::GetRealVideoDeviceNames(device_infos.value()));
  } else if (type_ == ContentSettingsType::CAMERA_PAN_TILT_ZOOM &&
             device_infos) {
    SetTitleTextAndTooltip(
        IDS_SITE_SETTINGS_TYPE_CAMERA_PAN_TILT_ZOOM_WITH_COUNT,
        media_effects::GetRealVideoDeviceNames(device_infos.value()));
  }
}

void PageInfoPermissionContentView::SetTitleTextAndTooltip(
    int message_id,
    const std::vector<std::string>& device_names) {
  title_->SetText(l10n_util::GetStringFUTF16(
      message_id, base::NumberToString16(device_names.size())));
  title_->SetCustomTooltipText(
      base::UTF8ToUTF16(base::JoinString(device_names, "\n")));
}
#endif

void PageInfoPermissionContentView::ToggleFileSystemExtendedPermissions() {
  if (!web_contents_.MaybeValid()) {
    return;
  }
  auto* context =
      FileSystemAccessPermissionContextFactory::GetForProfileIfExists(
          web_contents_->GetBrowserContext());
  if (!context) {
    return;
  }
  bool checkbox_enabled = remember_setting_->GetChecked();
  const url::Origin site_origin =
      url::Origin::Create(web_contents_->GetLastCommittedURL());
  bool origin_has_extended_permission =
      context->OriginHasExtendedPermission(site_origin);

  // After pressing the checkbox, the current extended permissions state should
  // not match the checkbox state. In the unlikely scenario that they are the
  // same, do not update permission grants in order to align with the
  // user-visible checkbox state.
  if (origin_has_extended_permission == checkbox_enabled) {
    base::debug::DumpWithoutCrashing();
    return;
  }
  if (checkbox_enabled) {
    context->SetOriginExtendedPermissionByUser(site_origin);
  } else {
    context->RemoveOriginExtendedPermissionByUser(site_origin);
  }
  base::UmaHistogramBoolean(
      "Storage.FileSystemAccess.ToggleExtendedPermissionOutcome",
      checkbox_enabled);
}

void PageInfoPermissionContentView::MaybeAddMediaPreview(
    content::WebContents* web_contents,
    views::View& preceding_separator) {
#if !BUILDFLAG(IS_CHROMEOS)
  if (type_ != ContentSettingsType::MEDIASTREAM_CAMERA &&
      type_ != ContentSettingsType::MEDIASTREAM_MIC &&
      type_ != ContentSettingsType::CAMERA_PAN_TILT_ZOOM) {
    return;
  }

  const GURL& site_url = web_contents->GetLastCommittedURL();
  if (!media_preview_feature::ShouldShowMediaPreview(
          *web_contents->GetBrowserContext(), site_url, site_url,
          media_preview_metrics::UiLocation::kPageInfo)) {
    return;
  }

  auto* cached_device_info = media_effects::MediaDeviceInfo::GetInstance();
  devices_observer_.Observe(cached_device_info);
  if (type_ == ContentSettingsType::MEDIASTREAM_CAMERA ||
      type_ == ContentSettingsType::CAMERA_PAN_TILT_ZOOM) {
    // Initialize `title_` with the current number of cached video devices.
    OnVideoDevicesChanged(cached_device_info->GetVideoDeviceInfos());
  } else {
    // Initialize `title_` with the current number of cached audio devices.
    OnAudioDevicesChanged(cached_device_info->GetAudioDeviceInfos());
  }

  preceding_separator.GetProperty(views::kMarginsKey)->set_bottom(0);

  previews_coordinator_.emplace(web_contents, type_,
                                /*parent_view=*/this);

  AddChildView(PageInfoViewFactory::CreateSeparator(
                   ChromeLayoutProvider::Get()->GetDistanceMetric(
                       DISTANCE_HORIZONTAL_SEPARATOR_PADDING_PAGE_INFO_VIEW)))
      ->GetProperty(views::kMarginsKey)
      ->set_top(0);
#endif
}

BEGIN_METADATA(PageInfoPermissionContentView)
END_METADATA