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" |
Peter Kotwicz | ae649ee | 2022-04-22 16:01:09 | [diff] [blame] | 17 | #include "url/origin.h" |
Peter Kotwicz | 90c0dc22 | 2022-03-09 18:24:45 | [diff] [blame] | 18 | |
Peter Kotwicz | 153898a | 2022-05-05 18:48:39 | [diff] [blame] | 19 | using PermissionStatus = |
| 20 | content::FederatedIdentityApiPermissionContextDelegate::PermissionStatus; |
| 21 | |
Peter Kotwicz | 90c0dc22 | 2022-03-09 18:24:45 | [diff] [blame] | 22 | FederatedIdentityApiPermissionContext::FederatedIdentityApiPermissionContext( |
| 23 | content::BrowserContext* browser_context) |
| 24 | : host_content_settings_map_( |
Nicolás Peña Moreno | 0c974faf | 2022-03-10 16:05:12 | [diff] [blame] | 25 | HostContentSettingsMapFactory::GetForProfile(browser_context)), |
| 26 | cookie_settings_(CookieSettingsFactory::GetForProfile( |
Peter Kotwicz | ae649ee | 2022-04-22 16:01:09 | [diff] [blame] | 27 | Profile::FromBrowserContext(browser_context))), |
| 28 | permission_autoblocker_( |
| 29 | PermissionDecisionAutoBlockerFactory::GetForProfile( |
| 30 | Profile::FromBrowserContext(browser_context))) {} |
Peter Kotwicz | 90c0dc22 | 2022-03-09 18:24:45 | [diff] [blame] | 31 | |
| 32 | FederatedIdentityApiPermissionContext:: |
| 33 | ~FederatedIdentityApiPermissionContext() = default; |
| 34 | |
Peter Kotwicz | 153898a | 2022-05-05 18:48:39 | [diff] [blame] | 35 | content::FederatedIdentityApiPermissionContextDelegate::PermissionStatus |
| 36 | FederatedIdentityApiPermissionContext::GetApiPermissionStatus( |
Peter Kotwicz | 51d5ddf | 2022-08-19 23:20:40 | [diff] [blame] | 37 | const url::Origin& relying_party_embedder) { |
Peter Kotwicz | 153898a | 2022-05-05 18:48:39 | [diff] [blame] | 38 | if (!base::FeatureList::IsEnabled(features::kFedCm)) |
| 39 | return PermissionStatus::BLOCKED_VARIATIONS; |
| 40 | |
Peter Kotwicz | af746d3 | 2022-12-07 02:18:36 | [diff] [blame] | 41 | const GURL rp_embedder_url = relying_party_embedder.GetURL(); |
| 42 | |
Peter Kotwicz | d4900eb8 | 2022-04-22 21:55:59 | [diff] [blame] | 43 | const ContentSetting setting = host_content_settings_map_->GetContentSetting( |
Peter Kotwicz | 51d5ddf | 2022-08-19 23:20:40 | [diff] [blame] | 44 | rp_embedder_url, rp_embedder_url, |
| 45 | ContentSettingsType::FEDERATED_IDENTITY_API); |
Peter Kotwicz | d4900eb8 | 2022-04-22 21:55:59 | [diff] [blame] | 46 | switch (setting) { |
| 47 | case CONTENT_SETTING_ALLOW: |
| 48 | break; |
| 49 | case CONTENT_SETTING_BLOCK: |
Peter Kotwicz | 153898a | 2022-05-05 18:48:39 | [diff] [blame] | 50 | return PermissionStatus::BLOCKED_SETTINGS; |
Peter Kotwicz | d4900eb8 | 2022-04-22 21:55:59 | [diff] [blame] | 51 | default: |
Peter Boström | 9be37efa | 2024-11-06 23:34:18 | [diff] [blame] | 52 | NOTREACHED(); |
Peter Kotwicz | ae649ee | 2022-04-22 16:01:09 | [diff] [blame] | 53 | } |
| 54 | |
Peter Kotwicz | 63559a8 | 2022-06-07 03:46:35 | [diff] [blame] | 55 | if (permission_autoblocker_->IsEmbargoed( |
Peter Kotwicz | 51d5ddf | 2022-08-19 23:20:40 | [diff] [blame] | 56 | rp_embedder_url, ContentSettingsType::FEDERATED_IDENTITY_API)) { |
Peter Kotwicz | 153898a | 2022-05-05 18:48:39 | [diff] [blame] | 57 | return PermissionStatus::BLOCKED_EMBARGO; |
Peter Kotwicz | 63559a8 | 2022-06-07 03:46:35 | [diff] [blame] | 58 | } |
Christian Biesinger | 49ab631 | 2023-08-10 20:07:33 | [diff] [blame] | 59 | |
Peter Kotwicz | 153898a | 2022-05-05 18:48:39 | [diff] [blame] | 60 | return PermissionStatus::GRANTED; |
Nicolás Peña Moreno | 0c974faf | 2022-03-10 16:05:12 | [diff] [blame] | 61 | } |
Peter Kotwicz | ae649ee | 2022-04-22 16:01:09 | [diff] [blame] | 62 | |
| 63 | void FederatedIdentityApiPermissionContext::RecordDismissAndEmbargo( |
Peter Kotwicz | 51d5ddf | 2022-08-19 23:20:40 | [diff] [blame] | 64 | const url::Origin& relying_party_embedder) { |
| 65 | const GURL rp_embedder_url = relying_party_embedder.GetURL(); |
Yi Gu | 6050ed9b1 | 2025-02-28 15:56:43 | [diff] [blame^] | 66 | // 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 Kotwicz | 7236eac | 2022-07-11 22:05:04 | [diff] [blame] | 69 | // default content setting state. |
| 70 | const ContentSetting setting = host_content_settings_map_->GetContentSetting( |
Peter Kotwicz | 51d5ddf | 2022-08-19 23:20:40 | [diff] [blame] | 71 | rp_embedder_url, rp_embedder_url, |
| 72 | ContentSettingsType::FEDERATED_IDENTITY_API); |
Peter Kotwicz | 7236eac | 2022-07-11 22:05:04 | [diff] [blame] | 73 | if (setting == CONTENT_SETTING_ALLOW) { |
| 74 | host_content_settings_map_->SetContentSettingDefaultScope( |
Peter Kotwicz | 51d5ddf | 2022-08-19 23:20:40 | [diff] [blame] | 75 | rp_embedder_url, rp_embedder_url, |
| 76 | ContentSettingsType::FEDERATED_IDENTITY_API, CONTENT_SETTING_DEFAULT); |
Peter Kotwicz | 7236eac | 2022-07-11 22:05:04 | [diff] [blame] | 77 | } |
Peter Kotwicz | ae649ee | 2022-04-22 16:01:09 | [diff] [blame] | 78 | permission_autoblocker_->RecordDismissAndEmbargo( |
Peter Kotwicz | 51d5ddf | 2022-08-19 23:20:40 | [diff] [blame] | 79 | rp_embedder_url, ContentSettingsType::FEDERATED_IDENTITY_API, |
Yi Gu | 6050ed9b1 | 2025-02-28 15:56:43 | [diff] [blame^] | 80 | /*dismissed_prompt_was_quiet=*/false); |
Peter Kotwicz | ae649ee | 2022-04-22 16:01:09 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | void FederatedIdentityApiPermissionContext::RemoveEmbargoAndResetCounts( |
Peter Kotwicz | 51d5ddf | 2022-08-19 23:20:40 | [diff] [blame] | 84 | const url::Origin& relying_party_embedder) { |
Peter Kotwicz | ae649ee | 2022-04-22 16:01:09 | [diff] [blame] | 85 | permission_autoblocker_->RemoveEmbargoAndResetCounts( |
Peter Kotwicz | 51d5ddf | 2022-08-19 23:20:40 | [diff] [blame] | 86 | relying_party_embedder.GetURL(), |
| 87 | ContentSettingsType::FEDERATED_IDENTITY_API); |
Peter Kotwicz | ae649ee | 2022-04-22 16:01:09 | [diff] [blame] | 88 | } |
Yi Gu | d65d98a | 2023-11-13 17:47:07 | [diff] [blame] | 89 | |
Yi Gu | 6050ed9b1 | 2025-02-28 15:56:43 | [diff] [blame^] | 90 | void 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, |
|
|