blob: a5e11e0aab6806f66b65aa3c24e219076d0ecc85 [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"
Joshua Thomasb05f0202025-05-13 14:36:4317#include "third_party/blink/public/common/storage_key/storage_key.h"
Peter Kotwiczae649ee2022-04-22 16:01:0918#include "url/origin.h"
Peter Kotwicz90c0dc222022-03-09 18:24:4519
Peter Kotwicz153898a2022-05-05 18:48:3920using PermissionStatus =
21 content::FederatedIdentityApiPermissionContextDelegate::PermissionStatus;
22
Peter Kotwicz90c0dc222022-03-09 18:24:4523FederatedIdentityApiPermissionContext::FederatedIdentityApiPermissionContext(
24 content::BrowserContext* browser_context)
25 : host_content_settings_map_(
Nicolás Peña Moreno0c974faf2022-03-10 16:05:1226 HostContentSettingsMapFactory::GetForProfile(browser_context)),
27 cookie_settings_(CookieSettingsFactory::GetForProfile(
Peter Kotwiczae649ee2022-04-22 16:01:0928 Profile::FromBrowserContext(browser_context))),
29 permission_autoblocker_(
30 PermissionDecisionAutoBlockerFactory::GetForProfile(
31 Profile::FromBrowserContext(browser_context))) {}
Peter Kotwicz90c0dc222022-03-09 18:24:4532
33FederatedIdentityApiPermissionContext::
34 ~FederatedIdentityApiPermissionContext() = default;
35
Peter Kotwicz153898a2022-05-05 18:48:3936content::FederatedIdentityApiPermissionContextDelegate::PermissionStatus
37FederatedIdentityApiPermissionContext::GetApiPermissionStatus(
Peter Kotwicz51d5ddf2022-08-19 23:20:4038 const url::Origin& relying_party_embedder) {
Peter Kotwicz153898a2022-05-05 18:48:3939 if (!base::FeatureList::IsEnabled(features::kFedCm))
40 return PermissionStatus::BLOCKED_VARIATIONS;
41
Peter Kotwiczaf746d32022-12-07 02:18:3642 const GURL rp_embedder_url = relying_party_embedder.GetURL();
43
Peter Kotwiczd4900eb82022-04-22 21:55:5944 const ContentSetting setting = host_content_settings_map_->GetContentSetting(
Peter Kotwicz51d5ddf2022-08-19 23:20:4045 rp_embedder_url, rp_embedder_url,
46 ContentSettingsType::FEDERATED_IDENTITY_API);
Peter Kotwiczd4900eb82022-04-22 21:55:5947 switch (setting) {
48 case CONTENT_SETTING_ALLOW:
49 break;
50 case CONTENT_SETTING_BLOCK:
Peter Kotwicz153898a2022-05-05 18:48:3951 return PermissionStatus::BLOCKED_SETTINGS;
Peter Kotwiczd4900eb82022-04-22 21:55:5952 default:
Peter Boström9be37efa2024-11-06 23:34:1853 NOTREACHED();
Peter Kotwiczae649ee2022-04-22 16:01:0954 }
55
Peter Kotwicz63559a82022-06-07 03:46:3556 if (permission_autoblocker_->IsEmbargoed(
Peter Kotwicz51d5ddf2022-08-19 23:20:4057 rp_embedder_url, ContentSettingsType::FEDERATED_IDENTITY_API)) {
Peter Kotwicz153898a2022-05-05 18:48:3958 return PermissionStatus::BLOCKED_EMBARGO;
Peter Kotwicz63559a82022-06-07 03:46:3559 }
Christian Biesinger49ab6312023-08-10 20:07:3360
Peter Kotwicz153898a2022-05-05 18:48:3961 return PermissionStatus::GRANTED;
Nicolás Peña Moreno0c974faf2022-03-10 16:05:1262}
Peter Kotwiczae649ee2022-04-22 16:01:0963
64void FederatedIdentityApiPermissionContext::RecordDismissAndEmbargo(
Peter Kotwicz51d5ddf2022-08-19 23:20:4065 const url::Origin& relying_party_embedder) {
66 const GURL rp_embedder_url = relying_party_embedder.GetURL();
Yi Gu6050ed9b12025-02-28 15:56:4367 // If content setting is allowed for `rp_embedder_url` but is disabled
68 // globally, reset it first to make sure the toggle in PageInfo is correct.
69 // See crbug.com/40230194 for why the resetting is not conditional on the
Peter Kotwicz7236eac2022-07-11 22:05:0470 // default content setting state.
71 const ContentSetting setting = host_content_settings_map_->GetContentSetting(
Peter Kotwicz51d5ddf2022-08-19 23:20:4072 rp_embedder_url, rp_embedder_url,
73 ContentSettingsType::FEDERATED_IDENTITY_API);
Peter Kotwicz7236eac2022-07-11 22:05:0474 if (setting == CONTENT_SETTING_ALLOW) {
75 host_content_settings_map_->SetContentSettingDefaultScope(
Peter Kotwicz51d5ddf2022-08-19 23:20:4076 rp_embedder_url, rp_embedder_url,
77 ContentSettingsType::FEDERATED_IDENTITY_API, CONTENT_SETTING_DEFAULT);
Peter Kotwicz7236eac2022-07-11 22:05:0478 }
Peter Kotwiczae649ee2022-04-22 16:01:0979 permission_autoblocker_->RecordDismissAndEmbargo(
Peter Kotwicz51d5ddf2022-08-19 23:20:4080 rp_embedder_url, ContentSettingsType::FEDERATED_IDENTITY_API,
Yi Gu6050ed9b12025-02-28 15:56:4381 /*dismissed_prompt_was_quiet=*/false);
Peter Kotwiczae649ee2022-04-22 16:01:0982}
83
84void FederatedIdentityApiPermissionContext::RemoveEmbargoAndResetCounts(
Peter Kotwicz51d5ddf2022-08-19 23:20:4085 const url::Origin& relying_party_embedder) {
Peter Kotwiczae649ee2022-04-22 16:01:0986 permission_autoblocker_->RemoveEmbargoAndResetCounts(
Peter Kotwicz51d5ddf2022-08-19 23:20:4087 relying_party_embedder.GetURL(),
88 ContentSettingsType::FEDERATED_IDENTITY_API);
Peter Kotwiczae649ee2022-04-22 16:01:0989}
Yi Gud65d98a2023-11-13 17:47:0790
Yi Gu6050ed9b12025-02-28 15:56:4391void FederatedIdentityApiPermissionContext::RecordIgnoreAndEmbargo(
92 const url::Origin& relying_party_embedder) {
93 const GURL rp_embedder_url = relying_party_embedder.GetURL();
94 // If content setting is allowed for `rp_embedder_url` but is disabled
95 // globally, reset it first to make sure the toggle in PageInfo is correct.
96 // See crbug.com/40230194 for why the resetting is not conditional on the
97 // default content setting state.
98 const ContentSetting setting = host_content_settings_map_->GetContentSetting(
99 rp_embedder_url, rp_embedder_url,
100 ContentSettingsType::FEDERATED_IDENTITY_API);
101 if (setting == CONTENT_SETTING_ALLOW) {
102 host_content_settings_map_->SetContentSettingDefaultScope(
103 rp_embedder_url, rp_embedder_url,
104 ContentSettingsType::FEDERATED_IDENTITY_API, CONTENT_SETTING_DEFAULT);
105 }
106 permission_autoblocker_->RecordIgnoreAndEmbargo(
107 rp_embedder_url, ContentSettingsType::FEDERATED_IDENTITY_API,
108 /*ignored_prompt_was_quiet=*/false);
109}
110
Yi Gud65d98a2023-11-13 17:47:07111bool FederatedIdentityApiPermissionContext::HasThirdPartyCookiesAccess(
112 content::RenderFrameHost& host,
113 const GURL& provider_url,
114 const url::Origin& relying_party_embedder) const {
115 return cookie_settings_->IsFullCookieAccessAllowed(
116 /*request_url=*/provider_url,
117 /*first_party_url=*/
118 net::SiteForCookies::FromOrigin(relying_party_embedder),
119 /*top_frame_origin=*/relying_party_embedder,
Joshua Thomasb05f0202025-05-13 14:36:43120 host.GetCookieSettingOverrides(),
121 host.GetStorageKey().ToCookiePartitionKey());
Yi Gud65d98a2023-11-13 17:47:07122}
Yi Gu8f970c12024-12-18 17:24:09123
124bool FederatedIdentityApiPermissionContext::
125 AreThirdPartyCookiesEnabledInSettings() const {
126 return !cookie_settings_->ShouldBlockThirdPartyCookies();
127}