File: chrome_page_info_ui_delegate.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 (362 lines) | stat: -rw-r--r-- 14,218 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
// 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 "chrome/browser/ui/page_info/chrome_page_info_ui_delegate.h"

#include "base/feature_list.h"
#include "base/notreached.h"
#include "build/build_config.h"
#include "chrome/browser/page_info/about_this_site_tab_helper.h"
#include "chrome/browser/page_info/merchant_trust_service_factory.h"
#include "chrome/browser/page_info/page_info_features.h"
#include "chrome/browser/permissions/permission_manager_factory.h"
#include "chrome/browser/permissions/system/system_permission_settings.h"
#include "chrome/browser/privacy_sandbox/tracking_protection_settings_factory.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser_finder.h"
#include "chrome/browser/ui/chrome_pages.h"
#include "chrome/browser/ui/page_info/merchant_trust_side_panel.h"
#include "chrome/browser/ui/ui_features.h"
#include "chrome/common/chrome_features.h"
#include "chrome/common/pref_names.h"
#include "components/content_settings/core/common/content_settings_types.h"
#include "components/content_settings/core/common/features.h"
#include "components/page_info/core/about_this_site_service.h"
#include "components/page_info/core/features.h"
#include "components/page_info/core/merchant_trust_service.h"
#include "components/page_info/core/pref_names.h"
#include "components/permissions/permission_decision_auto_blocker.h"
#include "components/permissions/permission_manager.h"
#include "components/permissions/permissions_client.h"
#include "components/prefs/pref_service.h"
#include "components/privacy_sandbox/tracking_protection_settings.h"
#include "components/strings/grit/components_strings.h"
#include "content/public/browser/permission_controller.h"
#include "content/public/browser/permission_descriptor_util.h"
#include "content/public/browser/permission_result.h"
#include "content/public/browser/web_contents.h"
#include "net/base/url_util.h"
#include "services/network/public/cpp/is_potentially_trustworthy.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/window_open_disposition_utils.h"
#include "ui/events/event.h"
#include "url/gurl.h"

#if !BUILDFLAG(IS_ANDROID)
#include "chrome/browser/extensions/window_controller_list.h"  // nogncheck
#include "chrome/browser/page_info/about_this_site_service_factory.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/page_info/about_this_site_side_panel.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/browser/ui/web_applications/web_app_ui_utils.h"
#endif

#if BUILDFLAG(IS_MAC)
#include "base/mac/mac_util.h"
#include "chrome/browser/media/webrtc/system_media_capture_permissions_mac.h"
#include "chrome/browser/web_applications/os_integration/mac/app_shim_registry.h"
#include "chrome/browser/web_applications/os_integration/mac/web_app_shortcut_mac.h"
#include "chrome/browser/web_applications/web_app_tab_helper.h"
#endif

ChromePageInfoUiDelegate::ChromePageInfoUiDelegate(
    content::WebContents* web_contents,
    const GURL& site_url)
    : web_contents_(web_contents), site_url_(site_url) {}

bool ChromePageInfoUiDelegate::ShouldShowAllow(ContentSettingsType type) {
  switch (type) {
    // Notifications and idle detection do not support CONTENT_SETTING_ALLOW in
    // incognito.
    case ContentSettingsType::NOTIFICATIONS:
    case ContentSettingsType::IDLE_DETECTION:
      return !GetProfile()->IsOffTheRecord();
    // Media only supports CONTENT_SETTING_ALLOW for secure origins.
    case ContentSettingsType::MEDIASTREAM_MIC:
    case ContentSettingsType::MEDIASTREAM_CAMERA:
      return network::IsUrlPotentiallyTrustworthy(site_url_);
    // Chooser permissions do not support CONTENT_SETTING_ALLOW.
    case ContentSettingsType::SERIAL_GUARD:
    case ContentSettingsType::USB_GUARD:
    case ContentSettingsType::BLUETOOTH_GUARD:
    case ContentSettingsType::HID_GUARD:
    // Bluetooth scanning does not support CONTENT_SETTING_ALLOW.
    case ContentSettingsType::BLUETOOTH_SCANNING:
    // File system write does not support CONTENT_SETTING_ALLOW.
    case ContentSettingsType::FILE_SYSTEM_WRITE_GUARD:
      return false;
    default:
      return true;
  }
}

std::u16string ChromePageInfoUiDelegate::GetAutomaticallyBlockedReason(
    ContentSettingsType type) {
  switch (type) {
    // Notifications and idle detection do not support CONTENT_SETTING_ALLOW in
    // incognito.
    case ContentSettingsType::NOTIFICATIONS:
    case ContentSettingsType::IDLE_DETECTION: {
      if (GetProfile()->IsOffTheRecord()) {
        return l10n_util::GetStringUTF16(
            GetProfile()->IsGuestSession()
                ? IDS_PAGE_INFO_STATE_TEXT_NOT_ALLOWED_IN_GUEST
                : IDS_PAGE_INFO_STATE_TEXT_NOT_ALLOWED_IN_INCOGNITO);
      }
      break;
    }
    // Media only supports CONTENT_SETTING_ALLOW for secure origins.
    // TODO(crbug.com/40189322): This string can probably be removed.
    case ContentSettingsType::MEDIASTREAM_MIC:
    case ContentSettingsType::MEDIASTREAM_CAMERA: {
      if (!network::IsUrlPotentiallyTrustworthy(site_url_)) {
        return l10n_util::GetStringUTF16(
            IDS_PAGE_INFO_STATE_TEXT_NOT_ALLOWED_INSECURE);
      }
      break;
    }
    default:
      break;
  }

  return std::u16string();
}

#if !BUILDFLAG(IS_ANDROID)
std::optional<page_info::proto::SiteInfo>
ChromePageInfoUiDelegate::GetAboutThisSiteInfo() {
  Browser* browser = chrome::FindBrowserWithTab(web_contents_);
  if (!browser || !browser->is_type_normal()) {
    // TODO(crbug.com/40904874): SidePanel is not available. Evaluate if we can
    //                          show ATP in a different way.
    return std::nullopt;
  }
  if (auto* service =
          AboutThisSiteServiceFactory::GetForProfile(GetProfile())) {
    return service->GetAboutThisSiteInfo(
        site_url_, web_contents_->GetPrimaryMainFrame()->GetPageUkmSourceId(),
        AboutThisSiteTabHelper::FromWebContents(web_contents_));
  }

  return std::nullopt;
}

void ChromePageInfoUiDelegate::OpenMoreAboutThisPageUrl(
    const GURL& url,
    const ui::Event& event) {
  DCHECK(page_info::IsAboutThisSiteFeatureEnabled());
  ShowAboutThisSiteSidePanel(web_contents_, url);
}
#endif

bool ChromePageInfoUiDelegate::ShouldShowAsk(ContentSettingsType type) {
  return permissions::PermissionUtil::IsGuardContentSetting(type);
}

#if !BUILDFLAG(IS_ANDROID)
bool ChromePageInfoUiDelegate::ShouldShowSiteSettings(int* link_text_id,
                                                      int* tooltip_text_id) {
  if (GetProfile()->IsGuestSession()) {
    return false;
  }

  if (web_app::GetLabelIdsForAppManagementLinkInPageInfo(
          web_contents_, link_text_id, tooltip_text_id)) {
    return true;
  }

  *link_text_id = IDS_PAGE_INFO_SITE_SETTINGS_LINK;
  *tooltip_text_id = IDS_PAGE_INFO_SITE_SETTINGS_TOOLTIP;

  return true;
}

// TODO(crbug.com/40776829): Reconcile with LastTabStandingTracker.
bool ChromePageInfoUiDelegate::IsMultipleTabsOpen() {
  int count = 0;
  auto site_origin = site_url_.DeprecatedGetOriginAsURL();
  for (extensions::WindowController* window :
       *extensions::WindowControllerList::GetInstance()) {
    for (int i = 0; i < window->GetTabCount(); ++i) {
      content::WebContents* const web_contents = window->GetWebContentsAt(i);
      if (web_contents->GetLastCommittedURL().DeprecatedGetOriginAsURL() ==
          site_origin) {
        count++;
      }
    }
  }
  return count > 1;
}

void ChromePageInfoUiDelegate::OpenSiteSettingsFileSystem() {
  chrome::ShowSiteSettingsFileSystem(GetProfile(), site_url_);
}

void ChromePageInfoUiDelegate::ShowPrivacySandboxSettings() {
  Browser* browser = chrome::FindBrowserWithTab(web_contents_);
  chrome::ShowPrivacySandboxSettings(browser);
}

std::u16string ChromePageInfoUiDelegate::GetPermissionDetail(
    ContentSettingsType type) {
  switch (type) {
    // TODO(crbug.com/40777580): Reconcile with SiteDetailsPermissionElement.
    case ContentSettingsType::ADS:
      return l10n_util::GetStringUTF16(IDS_PAGE_INFO_PERMISSION_ADS_SUBTITLE);
    default:
      return {};
  }
}

bool ChromePageInfoUiDelegate::ShouldShowSettingsLinkForPermission(
    ContentSettingsType type,
    int* text_id,
    int* link_id) {
  switch (type) {
    case ContentSettingsType::NOTIFICATIONS:
#if BUILDFLAG(IS_MAC)
      // This can be extracted into
      // SystemPermissionSettings::IsPermissionDenied() in a similar way as it
      // is done for camera and mic. I attempted to do this (see
      // https://chromium-review.googlesource.com/c/chromium/src/+/5424111/27..28
      // ), however as we don't have any testcase for this branch, the changes
      // were refused by the test coverage bot.
      // TODO(b/345431801): Add a testcase to cover this case.
      if (web_app::UseNotificationAttributionForWebAppShims()) {
        // If this notification permission is associated with a locally
        // installed web app, the corresponding app shim needs to have system
        // level notification permission for notifications to work. If system
        // permissions are missing, guide the user to system settings to fix
        // this.
        std::optional<webapps::AppId> app_id =
            web_app::WebAppTabHelper::GetAppIdForNotificationAttribution(
                web_contents_);
        if (!app_id.has_value()) {
          return false;
        }

        // If the system permission is already granted linking to system
        // settings doesn't really add anything. If system permissions is
        // "not determined", there won't be a page in system settings to link
        // to for this app, and Chrome will still be able to display a system
        // permission prompt, so there is no reason to guide the user to system
        // settings yet.
        auto system_permission_status =
            AppShimRegistry::Get()->GetNotificationPermissionStatusForApp(
                *app_id);
        if (system_permission_status ==
                mac_notifications::mojom::PermissionStatus::kGranted ||
            system_permission_status ==
                mac_notifications::mojom::PermissionStatus::kNotDetermined) {
          return false;
        }
        *text_id = IDS_PAGE_INFO_NOTIFICATIONS_SYSTEM_SETTINGS_DESCRIPTION;
        *link_id = IDS_PAGE_INFO_SYSTEM_SETTINGS_LINK;
        return true;
      }
#endif
      return false;
    case ContentSettingsType::MEDIASTREAM_CAMERA:
      if (base::FeatureList::IsEnabled(
              content_settings::features::kLeftHandSideActivityIndicators) &&
          system_permission_settings::IsDenied(type)) {
        *text_id = IDS_PAGE_INFO_CAMERA_SYSTEM_SETTINGS_DESCRIPTION;
        *link_id = IDS_PAGE_INFO_SETTINGS_OF_A_SYSTEM_LINK;
        return true;
      }
      return false;
    case ContentSettingsType::MEDIASTREAM_MIC:
      if (base::FeatureList::IsEnabled(
              content_settings::features::kLeftHandSideActivityIndicators) &&
          system_permission_settings::IsDenied(type)) {
        *text_id = IDS_PAGE_INFO_MICROPHONE_SYSTEM_SETTINGS_DESCRIPTION;
        *link_id = IDS_PAGE_INFO_SETTINGS_OF_A_SYSTEM_LINK;
        return true;
      }
      return false;
#if BUILDFLAG(IS_CHROMEOS)
    case ContentSettingsType::GEOLOCATION:
      if (base::FeatureList::IsEnabled(
              content_settings::features::
                  kCrosSystemLevelPermissionBlockedWarnings) &&
          system_permission_settings::IsDenied(type)) {
        *text_id = IDS_PAGE_INFO_LOCATION_SYSTEM_SETTINGS_DESCRIPTION;
        *link_id = IDS_PAGE_INFO_SETTINGS_OF_A_SYSTEM_LINK;
        return true;
      }
      return false;
#endif
    default:
      return false;
  }
}

void ChromePageInfoUiDelegate::SettingsLinkClicked(ContentSettingsType type) {
  system_permission_settings::OpenSystemSettings(web_contents_, type);
}

bool ChromePageInfoUiDelegate::IsBlockAutoPlayEnabled() {
  return GetProfile()->GetPrefs()->GetBoolean(prefs::kBlockAutoplayEnabled);
}
#endif

content::PermissionResult ChromePageInfoUiDelegate::GetPermissionResult(
    blink::PermissionType permission) {
  return GetProfile()
      ->GetPermissionController()
      ->GetPermissionResultForOriginWithoutContext(
          content::PermissionDescriptorUtil::
              CreatePermissionDescriptorForPermissionType(permission),
          url::Origin::Create(site_url_));
}

std::optional<content::PermissionResult>
ChromePageInfoUiDelegate::GetEmbargoResult(ContentSettingsType type) {
  return permissions::PermissionsClient::Get()
      ->GetPermissionDecisionAutoBlocker(GetProfile())
      ->GetEmbargoResult(site_url_, type);
}

#if !BUILDFLAG(IS_ANDROID)
void ChromePageInfoUiDelegate::OpenMerchantTrustSidePanel(const GURL& url) {
  DCHECK(page_info::IsMerchantTrustFeatureEnabled());
  ShowMerchantTrustSidePanel(web_contents_, url);
}
#endif

void ChromePageInfoUiDelegate::GetMerchantTrustInfo(
    page_info::MerchantDataCallback callback) {
  if (auto* service =
          MerchantTrustServiceFactory::GetForProfile(GetProfile())) {
    service->GetMerchantTrustInfo(web_contents_->GetVisibleURL(),
                                  std::move(callback));
  }
}

void ChromePageInfoUiDelegate::RecordPageInfoWithMerchantTrustOpenTime() {
  GetProfile()->GetPrefs()->SetTime(prefs::kMerchantTrustPageInfoLastOpenTime,
                                    clock_->Now());
}

void ChromePageInfoUiDelegate::RecordMerchantTrustButtonShown() {
  if (auto* service =
          MerchantTrustServiceFactory::GetForProfile(GetProfile())) {
    service->RecordMerchantTrustInteraction(
        web_contents_->GetVisibleURL(),
        page_info::MerchantTrustInteraction::kPageInfoRowShown);
  }
}

void ChromePageInfoUiDelegate::RecordMerchantTrustSidePanelOpened() {
  if (auto* service =
          MerchantTrustServiceFactory::GetForProfile(GetProfile())) {
    service->RecordMerchantTrustInteraction(
        web_contents_->GetVisibleURL(),
        page_info::MerchantTrustInteraction::kSidePanelOpened);
  }
}

Profile* ChromePageInfoUiDelegate::GetProfile() const {
  return Profile::FromBrowserContext(web_contents_->GetBrowserContext());
}