Avi Drissman | 4a8573c | 2022-09-09 19:35:54 | [diff] [blame] | 1 | // Copyright 2022 The Chromium Authors |
Peter Kotwicz | 90c0dc22 | 2022-03-09 18:24:45 | [diff] [blame] | 2 | // 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 Tan | 3309272 | 2022-11-15 18:59:38 | [diff] [blame] | 7 | #include "chrome/browser/browser_features.h" |
Nicolás Peña Moreno | 0c974faf | 2022-03-10 16:05:12 | [diff] [blame] | 8 | #include "chrome/browser/content_settings/cookie_settings_factory.h" |
Peter Kotwicz | 90c0dc22 | 2022-03-09 18:24:45 | [diff] [blame] | 9 | #include "chrome/browser/content_settings/host_content_settings_map_factory.h" |
Peter Kotwicz | ae649ee | 2022-04-22 16:01:09 | [diff] [blame] | 10 | #include "chrome/browser/permissions/permission_decision_auto_blocker_factory.h" |
Nicolás Peña Moreno | 0c974faf | 2022-03-10 16:05:12 | [diff] [blame] | 11 | #include "chrome/browser/profiles/profile.h" |
Peter Kotwicz | 90c0dc22 | 2022-03-09 18:24:45 | [diff] [blame] | 12 | #include "components/content_settings/core/common/content_settings_types.h" |
Peter Kotwicz | ae649ee | 2022-04-22 16:01:09 | [diff] [blame] | 13 | #include "components/permissions/permission_decision_auto_blocker.h" |
Yi Gu | d65d98a | 2023-11-13 17:47:07 | [diff] [blame] | 14 | #include "content/public/browser/render_frame_host.h" |
Peter Kotwicz | 153898a | 2022-05-05 18:48:39 | [diff] [blame] | 15 | #include "content/public/common/content_features.h" |
Yi Gu | d65d98a | 2023-11-13 17:47:07 | [diff] [blame] | 16 | #include "net/cookies/site_for_cookies.h" |
Joshua Thomas | b05f020 | 2025-05-13 14:36:43 | [diff] [blame] | 17 | #include "third_party/blink/public/common/storage_key/storage_key.h" |
Peter Kotwicz | ae649ee | 2022-04-22 16:01:09 | [diff] [blame] | 18 | #include "url/origin.h" |
Peter Kotwicz | 90c0dc22 | 2022-03-09 18:24:45 | [diff] [blame] | 19 | |
Peter Kotwicz | 153898a | 2022-05-05 18:48:39 | [diff] [blame] | 20 | using PermissionStatus = |
| 21 | content::FederatedIdentityApiPermissionContextDelegate::PermissionStatus; |
| 22 | |
Peter Kotwicz | 90c0dc22 | 2022-03-09 18:24:45 | [diff] [blame] | 23 | FederatedIdentityApiPermissionContext::FederatedIdentityApiPermissionContext( |
| 24 | content::BrowserContext* browser_context) |
| 25 | : host_content_settings_map_( |
Nicolás Peña Moreno | 0c974faf | 2022-03-10 16:05:12 | [diff] [blame] | 26 | HostContentSettingsMapFactory::GetForProfile(browser_context)), |
| 27 | cookie_settings_(CookieSettingsFactory::GetForProfile( |
Peter Kotwicz | ae649ee | 2022-04-22 16:01:09 | [diff] [blame] | 28 | Profile::FromBrowserContext(browser_context))), |
| 29 | permission_autoblocker_( |
| 30 | PermissionDecisionAutoBlockerFactory::GetForProfile( |
| 31 | Profile::FromBrowserContext(browser_context))) {} |
Peter Kotwicz | 90c0dc22 | 2022-03-09 18:24:45 | [diff] [blame] | 32 | |
| 33 | FederatedIdentityApiPermissionContext:: |
| 34 | ~FederatedIdentityApiPermissionContext() = default; |
| 35 | |
Peter Kotwicz | 153898a | 2022-05-05 18:48:39 | [diff] [blame] | 36 | content::FederatedIdentityApiPermissionContextDelegate::PermissionStatus |
| 37 | FederatedIdentityApiPermissionContext::GetApiPermissionStatus( |
Peter Kotwicz | 51d5ddf | 2022-08-19 23:20:40 | [diff] [blame] | 38 | const url::Origin& relying_party_embedder) { |
Peter Kotwicz | 153898a | 2022-05-05 18:48:39 | [diff] [blame] | 39 | if (!base::FeatureList::IsEnabled(features::kFedCm)) |
| 40 | return PermissionStatus::BLOCKED_VARIATIONS; |
| 41 | |
Peter Kotwicz | af746d3 | 2022-12-07 02:18:36 | [diff] [blame] | 42 | const GURL rp_embedder_url = relying_party_embedder.GetURL(); |
| 43 | |
Peter Kotwicz | d4900eb8 | 2022-04-22 21:55:59 | [diff] [blame] | 44 | const ContentSetting setting = host_content_settings_map_->GetContentSetting( |
Peter Kotwicz | 51d5ddf | 2022-08-19 23:20:40 | [diff] [blame] | 45 | rp_embedder_url, rp_embedder_url, |
| 46 | ContentSettingsType::FEDERATED_IDENTITY_API); |
Peter Kotwicz | d4900eb8 | 2022-04-22 21:55:59 | [diff] [blame] | 47 | switch (setting) { |
| 48 | case CONTENT_SETTING_ALLOW: |
| 49 | break; |
| 50 | case CONTENT_SETTING_BLOCK: |
Peter Kotwicz | 153898a | 2022-05-05 18:48:39 | [diff] [blame] | 51 | return PermissionStatus::BLOCKED_SETTINGS; |
Peter Kotwicz | d4900eb8 | 2022-04-22 21:55:59 | [diff] [blame] | 52 | default: |
Peter Boström | 9be37efa | 2024-11-06 23:34:18 | [diff] [blame] | 53 | NOTREACHED(); |
Peter Kotwicz | ae649ee | 2022-04-22 16:01:09 | [diff] [blame] | 54 | } |
| 55 | |
Peter Kotwicz | 63559a8 | 2022-06-07 03:46:35 | [diff] [blame] | 56 | if (permission_autoblocker_->IsEmbargoed( |
Peter Kotwicz | 51d5ddf | 2022-08-19 23:20:40 | [diff] [blame] | 57 | rp_embedder_url, ContentSettingsType::FEDERATED_IDENTITY_API)) { |
Peter Kotwicz | 153898a | 2022-05-05 18:48:39 | [diff] [blame] | 58 | return PermissionStatus::BLOCKED_EMBARGO; |
Peter Kotwicz | 63559a8 | 2022-06-07 03:46:35 | [diff] [blame] | 59 | } |
Christian Biesinger | 49ab631 | 2023-08-10 20:07:33 | [diff] [blame] | 60 | |
Peter Kotwicz | 153898a | 2022-05-05 18:48:39 | [diff] [blame] | 61 | return PermissionStatus::GRANTED; |
Nicolás Peña Moreno | 0c974faf | 2022-03-10 16:05:12 | [diff] [blame] | 62 | } |
Peter Kotwicz | ae649ee | 2022-04-22 16:01:09 | [diff] [blame] | 63 | |
| 64 | void FederatedIdentityApiPermissionContext::RecordDismissAndEmbargo( |
Peter Kotwicz | 51d5ddf | 2022-08-19 23:20:40 | [diff] [blame] | 65 | const url::Origin& relying_party_embedder) { |
| 66 | const GURL rp_embedder_url = relying_party_embedder.GetURL(); |
Yi Gu | 6050ed9b1 | 2025-02-28 15:56:43 | [diff] [blame] | 67 | // 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 Kotwicz | 7236eac | 2022-07-11 22:05:04 | [diff] [blame] | 70 | // default content setting state. |
| 71 | const ContentSetting setting = host_content_settings_map_->GetContentSetting( |
Peter Kotwicz | 51d5ddf | 2022-08-19 23:20:40 | [diff] [blame] | 72 | rp_embedder_url, rp_embedder_url, |
| 73 | ContentSettingsType::FEDERATED_IDENTITY_API); |
Peter Kotwicz | 7236eac | 2022-07-11 22:05:04 | [diff] [blame] | 74 | if (setting == CONTENT_SETTING_ALLOW) { |
| 75 | host_content_settings_map_->SetContentSettingDefaultScope( |
Peter Kotwicz | 51d5ddf | 2022-08-19 23:20:40 | [diff] [blame] | 76 | rp_embedder_url, rp_embedder_url, |
| 77 | ContentSettingsType::FEDERATED_IDENTITY_API, CONTENT_SETTING_DEFAULT); |
Peter Kotwicz | 7236eac | 2022-07-11 22:05:04 | [diff] [blame] | 78 | } |
Peter Kotwicz | ae649ee | 2022-04-22 16:01:09 | [diff] [blame] | 79 | permission_autoblocker_->RecordDismissAndEmbargo( |
Peter Kotwicz | 51d5ddf | 2022-08-19 23:20:40 | [diff] [blame] | 80 | rp_embedder_url, ContentSettingsType::FEDERATED_IDENTITY_API, |
Yi Gu | 6050ed9b1 | 2025-02-28 15:56:43 | [diff] [blame] | 81 | /*dismissed_prompt_was_quiet=*/false); |
Peter Kotwicz | ae649ee | 2022-04-22 16:01:09 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | void FederatedIdentityApiPermissionContext::RemoveEmbargoAndResetCounts( |
Peter Kotwicz | 51d5ddf | 2022-08-19 23:20:40 | [diff] [blame] | 85 | const url::Origin& relying_party_embedder) { |
Peter Kotwicz | ae649ee | 2022-04-22 16:01:09 | [diff] [blame] | 86 | permission_autoblocker_->RemoveEmbargoAndResetCounts( |
Peter Kotwicz | 51d5ddf | 2022-08-19 23:20:40 | [diff] [blame] | 87 | relying_party_embedder.GetURL(), |
| 88 | ContentSettingsType::FEDERATED_IDENTITY_API); |
Peter Kotwicz | ae649ee | 2022-04-22 16:01:09 | [diff] [blame] | 89 | } |
Yi Gu | d65d98a | 2023-11-13 17:47:07 | [diff] [blame] | 90 | |
Yi Gu | 6050ed9b1 | 2025-02-28 15:56:43 | [diff] [blame] | 91 | void 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 Gu | d65d98a | 2023-11-13 17:47:07 | [diff] [blame] | 111 | bool 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 Thomas | b05f020 | 2025-05-13 14:36:43 | [diff] [blame] | 120 | host.GetCookieSettingOverrides(), |
| 121 | host.GetStorageKey().ToCookiePartitionKey()); |
Yi Gu | d65d98a | 2023-11-13 17:47:07 | [diff] [blame] | 122 | } |
Yi Gu | 8f970c1 | 2024-12-18 17:24:09 | [diff] [blame] | 123 | |
| 124 | bool FederatedIdentityApiPermissionContext:: |
| 125 | AreThirdPartyCookiesEnabledInSettings() const { |
| 126 | return !cookie_settings_->ShouldBlockThirdPartyCookies(); |
| 127 | } |