blob: ce90752628b9d34ba9fa62157df8fa00bb468c34 [file] [log] [blame]
Avi Drissman4a8573c2022-09-09 19:35:541// Copyright 2022 The Chromium Authors
Peter Kotwicz90c0dc222022-03-09 18:24:452// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "chrome/browser/webid/federated_identity_api_permission_context.h"
6
Zachary Tan33092722022-11-15 18:59:387#include "chrome/browser/browser_features.h"
Nicolás Peña Moreno0c974faf2022-03-10 16:05:128#include "chrome/browser/content_settings/cookie_settings_factory.h"
Peter Kotwicz90c0dc222022-03-09 18:24:459#include "chrome/browser/content_settings/host_content_settings_map_factory.h"
Peter Kotwiczae649ee2022-04-22 16:01:0910#include "chrome/browser/permissions/permission_decision_auto_blocker_factory.h"
Nicolás Peña Moreno0c974faf2022-03-10 16:05:1211#include "chrome/browser/profiles/profile.h"
Peter Kotwicz90c0dc222022-03-09 18:24:4512#include "components/content_settings/core/common/content_settings_types.h"
Peter Kotwiczae649ee2022-04-22 16:01:0913#include "components/permissions/permission_decision_auto_blocker.h"
Yi Gud65d98a2023-11-13 17:47:0714#include "content/public/browser/render_frame_host.h"
Peter Kotwicz153898a2022-05-05 18:48:3915#include "content/public/common/content_features.h"
Yi Gud65d98a2023-11-13 17:47:0716#include "net/cookies/site_for_cookies.h"
Peter Kotwiczae649ee2022-04-22 16:01:0917#include "url/origin.h"
Peter Kotwicz90c0dc222022-03-09 18:24:4518
Peter Kotwicz153898a2022-05-05 18:48:3919using PermissionStatus =
20 content::FederatedIdentityApiPermissionContextDelegate::PermissionStatus;
21
Peter Kotwicz90c0dc222022-03-09 18:24:4522FederatedIdentityApiPermissionContext::FederatedIdentityApiPermissionContext(
23 content::BrowserContext* browser_context)
24 : host_content_settings_map_(
Nicolás Peña Moreno0c974faf2022-03-10 16:05:1225 HostContentSettingsMapFactory::GetForProfile(browser_context)),
26 cookie_settings_(CookieSettingsFactory::GetForProfile(
Peter Kotwiczae649ee2022-04-22 16:01:0927 Profile::FromBrowserContext(browser_context))),
28 permission_autoblocker_(
29 PermissionDecisionAutoBlockerFactory::GetForProfile(
30 Profile::FromBrowserContext(browser_context))) {}
Peter Kotwicz90c0dc222022-03-09 18:24:4531
32FederatedIdentityApiPermissionContext::
33 ~FederatedIdentityApiPermissionContext() = default;
34
Peter Kotwicz153898a2022-05-05 18:48:3935content::FederatedIdentityApiPermissionContextDelegate::PermissionStatus
36FederatedIdentityApiPermissionContext::GetApiPermissionStatus(
Peter Kotwicz51d5ddf2022-08-19 23:20:4037 const url::Origin& relying_party_embedder) {
Peter Kotwicz153898a2022-05-05 18:48:3938 if (!base::FeatureList::IsEnabled(features::kFedCm))
39 return PermissionStatus::BLOCKED_VARIATIONS;
40
Peter Kotwiczaf746d32022-12-07 02:18:3641 const GURL rp_embedder_url = relying_party_embedder.GetURL();
42
Peter Kotwiczd4900eb82022-04-22 21:55:5943 const ContentSetting setting = host_content_settings_map_->GetContentSetting(
Peter Kotwicz51d5ddf2022-08-19 23:20:4044 rp_embedder_url, rp_embedder_url,
45 ContentSettingsType::FEDERATED_IDENTITY_API);
Peter Kotwiczd4900eb82022-04-22 21:55:5946 switch (setting) {
47 case CONTENT_SETTING_ALLOW:
48 break;
49 case CONTENT_SETTING_BLOCK:
Peter Kotwicz153898a2022-05-05 18:48:3950 return PermissionStatus::BLOCKED_SETTINGS;
Peter Kotwiczd4900eb82022-04-22 21:55:5951 default:
Peter Boström9be37efa2024-11-06 23:34:1852 NOTREACHED();
Peter Kotwiczae649ee2022-04-22 16:01:0953 }
54
Peter Kotwicz63559a82022-06-07 03:46:3555 if (permission_autoblocker_->IsEmbargoed(
Peter Kotwicz51d5ddf2022-08-19 23:20:4056 rp_embedder_url, ContentSettingsType::FEDERATED_IDENTITY_API)) {
Peter Kotwicz153898a2022-05-05 18:48:3957 return PermissionStatus::BLOCKED_EMBARGO;
Peter Kotwicz63559a82022-06-07 03:46:3558 }
Christian Biesinger49ab6312023-08-10 20:07:3359
Peter Kotwicz153898a2022-05-05 18:48:3960 return PermissionStatus::GRANTED;
Nicolás Peña Moreno0c974faf2022-03-10 16:05:1261}
Peter Kotwiczae649ee2022-04-22 16:01:0962
63void FederatedIdentityApiPermissionContext::RecordDismissAndEmbargo(
Peter Kotwicz51d5ddf2022-08-19 23:20:4064 const url::Origin& relying_party_embedder) {
65 const GURL rp_embedder_url = relying_party_embedder.GetURL();
Yi Gu6050ed9b12025-02-28 15:56:4366 // If content setting is allowed for `rp_embedder_url` but is disabled
67 // globally, reset it first to make sure the toggle in PageInfo is correct.
68 // See crbug.com/40230194 for why the resetting is not conditional on the
Peter Kotwicz7236eac2022-07-11 22:05:0469 // default content setting state.
70 const ContentSetting setting = host_content_settings_map_->GetContentSetting(
Peter Kotwicz51d5ddf2022-08-19 23:20:4071 rp_embedder_url, rp_embedder_url,
72 ContentSettingsType::FEDERATED_IDENTITY_API);
Peter Kotwicz7236eac2022-07-11 22:05:0473 if (setting == CONTENT_SETTING_ALLOW) {
74 host_content_settings_map_->SetContentSettingDefaultScope(
Peter Kotwicz51d5ddf2022-08-19 23:20:4075 rp_embedder_url, rp_embedder_url,
76 ContentSettingsType::FEDERATED_IDENTITY_API, CONTENT_SETTING_DEFAULT);
Peter Kotwicz7236eac2022-07-11 22:05:0477 }
Peter Kotwiczae649ee2022-04-22 16:01:0978 permission_autoblocker_->RecordDismissAndEmbargo(
Peter Kotwicz51d5ddf2022-08-19 23:20:4079 rp_embedder_url, ContentSettingsType::FEDERATED_IDENTITY_API,
Yi Gu6050ed9b12025-02-28 15:56:4380 /*dismissed_prompt_was_quiet=*/false);
Peter Kotwiczae649ee2022-04-22 16:01:0981}
82
83void FederatedIdentityApiPermissionContext::RemoveEmbargoAndResetCounts(
Peter Kotwicz51d5ddf2022-08-19 23:20:4084 const url::Origin& relying_party_embedder) {
Peter Kotwiczae649ee2022-04-22 16:01:0985 permission_autoblocker_->RemoveEmbargoAndResetCounts(
Peter Kotwicz51d5ddf2022-08-19 23:20:4086 relying_party_embedder.GetURL(),
87 ContentSettingsType::FEDERATED_IDENTITY_API);
Peter Kotwiczae649ee2022-04-22 16:01:0988}
Yi Gud65d98a2023-11-13 17:47:0789
Yi Gu6050ed9b12025-02-28 15:56:4390void FederatedIdentityApiPermissionContext::RecordIgnoreAndEmbargo(
91 const url::Origin& relying_party_embedder) {
92 const GURL rp_embedder_url = relying_party_embedder.GetURL();
93 // If content setting is allowed for `rp_embedder_url` but is disabled
94 // globally, reset it first to make sure the toggle in PageInfo is correct.
95 // See crbug.com/40230194 for why the resetting is not conditional on the
96 // default content setting state.
97 const ContentSetting setting = host_content_settings_map_->GetContentSetting(
98 rp_embedder_url, rp_embedder_url,
99 ContentSettingsType::FEDERATED_IDENTITY_API);
100 if (setting == CONTENT_SETTING_ALLOW) {
101 host_content_settings_map_->SetContentSettingDefaultScope(
102 rp_embedder_url, rp_embedder_url,