Peter Kotwicz | 90c0dc22 | 2022-03-09 18:24:45 | [diff] [blame] | 1 | // Copyright 2022 The Chromium Authors. All rights reserved. |
| 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 | |
Nicolás Peña Moreno | 0c974faf | 2022-03-10 16:05:12 | [diff] [blame] | 7 | #include "chrome/browser/content_settings/cookie_settings_factory.h" |
Peter Kotwicz | 90c0dc22 | 2022-03-09 18:24:45 | [diff] [blame] | 8 | #include "chrome/browser/content_settings/host_content_settings_map_factory.h" |
Peter Kotwicz | ae649ee | 2022-04-22 16:01:09 | [diff] [blame] | 9 | #include "chrome/browser/permissions/permission_decision_auto_blocker_factory.h" |
Nicolás Peña Moreno | 0c974faf | 2022-03-10 16:05:12 | [diff] [blame] | 10 | #include "chrome/browser/profiles/profile.h" |
Peter Kotwicz | 90c0dc22 | 2022-03-09 18:24:45 | [diff] [blame] | 11 | #include "components/content_settings/core/common/content_settings_types.h" |
Peter Kotwicz | ae649ee | 2022-04-22 16:01:09 | [diff] [blame] | 12 | #include "components/permissions/permission_decision_auto_blocker.h" |
| 13 | #include "components/permissions/permission_result.h" |
Peter Kotwicz | 153898a | 2022-05-05 18:48:39 | [diff] [blame] | 14 | #include "content/public/common/content_features.h" |
Peter Kotwicz | ae649ee | 2022-04-22 16:01:09 | [diff] [blame] | 15 | #include "url/origin.h" |
Peter Kotwicz | 90c0dc22 | 2022-03-09 18:24:45 | [diff] [blame] | 16 | |
Peter Kotwicz | 153898a | 2022-05-05 18:48:39 | [diff] [blame] | 17 | using PermissionStatus = |
| 18 | content::FederatedIdentityApiPermissionContextDelegate::PermissionStatus; |
| 19 | |
Peter Kotwicz | 90c0dc22 | 2022-03-09 18:24:45 | [diff] [blame] | 20 | FederatedIdentityApiPermissionContext::FederatedIdentityApiPermissionContext( |
| 21 | content::BrowserContext* browser_context) |
| 22 | : host_content_settings_map_( |
Nicolás Peña Moreno | 0c974faf | 2022-03-10 16:05:12 | [diff] [blame] | 23 | HostContentSettingsMapFactory::GetForProfile(browser_context)), |
| 24 | cookie_settings_(CookieSettingsFactory::GetForProfile( |
Peter Kotwicz | ae649ee | 2022-04-22 16:01:09 | [diff] [blame] | 25 | Profile::FromBrowserContext(browser_context))), |
| 26 | permission_autoblocker_( |
| 27 | PermissionDecisionAutoBlockerFactory::GetForProfile( |
| 28 | Profile::FromBrowserContext(browser_context))) {} |
Peter Kotwicz | 90c0dc22 | 2022-03-09 18:24:45 | [diff] [blame] | 29 | |
| 30 | FederatedIdentityApiPermissionContext:: |
| 31 | ~FederatedIdentityApiPermissionContext() = default; |
| 32 | |
Peter Kotwicz | 153898a | 2022-05-05 18:48:39 | [diff] [blame] | 33 | content::FederatedIdentityApiPermissionContextDelegate::PermissionStatus |
| 34 | FederatedIdentityApiPermissionContext::GetApiPermissionStatus( |
Peter Kotwicz | ae649ee | 2022-04-22 16:01:09 | [diff] [blame] | 35 | const url::Origin& rp_origin) { |
Peter Kotwicz | 153898a | 2022-05-05 18:48:39 | [diff] [blame] | 36 | if (!base::FeatureList::IsEnabled(features::kFedCm)) |
| 37 | return PermissionStatus::BLOCKED_VARIATIONS; |
| 38 | |
| 39 | // TODO(npm): FedCM is currently restricted to contexts where third party |
| 40 | // cookies are not blocked. Once the privacy improvements for the API are |
| 41 | // implemented, remove this restriction. See https://crbug.com/13043 |
| 42 | if (cookie_settings_->ShouldBlockThirdPartyCookies()) |
| 43 | return PermissionStatus::BLOCKED_THIRD_PARTY_COOKIES_BLOCKED; |
| 44 | |
Peter Kotwicz | d4900eb8 | 2022-04-22 21:55:59 | [diff] [blame] | 45 | const GURL rp_url = rp_origin.GetURL(); |
| 46 | const ContentSetting setting = host_content_settings_map_->GetContentSetting( |
| 47 | rp_url, rp_url, ContentSettingsType::FEDERATED_IDENTITY_API); |
| 48 | switch (setting) { |
| 49 | case CONTENT_SETTING_ALLOW: |
| 50 | break; |
| 51 | case CONTENT_SETTING_BLOCK: |
Peter Kotwicz | 153898a | 2022-05-05 18:48:39 | [diff] [blame] | 52 | return PermissionStatus::BLOCKED_SETTINGS; |
Peter Kotwicz | d4900eb8 | 2022-04-22 21:55:59 | [diff] [blame] | 53 | default: |
| 54 | NOTREACHED(); |
Peter Kotwicz | 153898a | 2022-05-05 18:48:39 | [diff] [blame] | 55 | return PermissionStatus::BLOCKED_SETTINGS; |
Peter Kotwicz | ae649ee | 2022-04-22 16:01:09 | [diff] [blame] | 56 | } |
| 57 | |
Peter Kotwicz | 63559a8 | 2022-06-07 03:46:35 | [diff] [blame^] | 58 | if (permission_autoblocker_->IsEmbargoed( |
| 59 | rp_url, ContentSettingsType::FEDERATED_IDENTITY_API)) { |
Peter Kotwicz | 153898a | 2022-05-05 18:48:39 | [diff] [blame] | 60 | return PermissionStatus::BLOCKED_EMBARGO; |
Peter Kotwicz | 63559a8 | 2022-06-07 03:46:35 | [diff] [blame^] | 61 | } |
Peter Kotwicz | 153898a | 2022-05-05 18:48:39 | [diff] [blame] | 62 | return PermissionStatus::GRANTED; |
Nicolás Peña Moreno | 0c974faf | 2022-03-10 16:05:12 | [diff] [blame] | 63 | } |
Peter Kotwicz | ae649ee | 2022-04-22 16:01:09 | [diff] [blame] | 64 | |
| 65 | void FederatedIdentityApiPermissionContext::RecordDismissAndEmbargo( |
| 66 | const url::Origin& rp_origin) { |
| 67 | permission_autoblocker_->RecordDismissAndEmbargo( |
| 68 | rp_origin.GetURL(), ContentSettingsType::FEDERATED_IDENTITY_API, |
| 69 | false /* dismissed_prompt_was_quiet */); |
| 70 | } |
| 71 | |
| 72 | void FederatedIdentityApiPermissionContext::RemoveEmbargoAndResetCounts( |
| 73 | const url::Origin& rp_origin) { |
| 74 | permission_autoblocker_->RemoveEmbargoAndResetCounts( |
| 75 | rp_origin.GetURL(), ContentSettingsType::FEDERATED_IDENTITY_API); |
| 76 | } |