blob: f095ea5c1abe6f02d521a1d64cb0a3c06240001b [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"
14#include "components/permissions/permission_result.h"
Peter Kotwicz153898a2022-05-05 18:48:3915#include "content/public/common/content_features.h"
Peter Kotwiczae649ee2022-04-22 16:01:0916#include "url/origin.h"
Peter Kotwicz90c0dc222022-03-09 18:24:4517
Peter Kotwicz153898a2022-05-05 18:48:3918using PermissionStatus =
19 content::FederatedIdentityApiPermissionContextDelegate::PermissionStatus;
20
Peter Kotwicz90c0dc222022-03-09 18:24:4521FederatedIdentityApiPermissionContext::FederatedIdentityApiPermissionContext(
22 content::BrowserContext* browser_context)
23 : host_content_settings_map_(
Nicolás Peña Moreno0c974faf2022-03-10 16:05:1224 HostContentSettingsMapFactory::GetForProfile(browser_context)),
25 cookie_settings_(CookieSettingsFactory::GetForProfile(
Peter Kotwiczae649ee2022-04-22 16:01:0926 Profile::FromBrowserContext(browser_context))),
27 permission_autoblocker_(
28 PermissionDecisionAutoBlockerFactory::GetForProfile(
29 Profile::FromBrowserContext(browser_context))) {}
Peter Kotwicz90c0dc222022-03-09 18:24:4530
31FederatedIdentityApiPermissionContext::
32 ~FederatedIdentityApiPermissionContext() = default;
33
Peter Kotwicz153898a2022-05-05 18:48:3934content::FederatedIdentityApiPermissionContextDelegate::PermissionStatus
35FederatedIdentityApiPermissionContext::GetApiPermissionStatus(
Peter Kotwicz51d5ddf2022-08-19 23:20:4036 const url::Origin& relying_party_embedder) {
Peter Kotwicz153898a2022-05-05 18:48:3937 if (!base::FeatureList::IsEnabled(features::kFedCm))
38 return PermissionStatus::BLOCKED_VARIATIONS;
39
Peter Kotwiczaf746d32022-12-07 02:18:3640 const GURL rp_embedder_url = relying_party_embedder.GetURL();
41
Peter Kotwicz153898a2022-05-05 18:48:3942 // TODO(npm): FedCM is currently restricted to contexts where third party
Zachary Tan00aa3ac2023-08-02 20:05:3943 // cookies are not blocked unless the FedCmWithoutThirdPartyCookies flag or
44 // FedCmIdpSigninStatusEnabled flag is enabled. Once the privacy improvements
45 // for the API are implemented, remove this restriction. See
46 // https://crbug.com/13043
Zachary Tan33092722022-11-15 18:59:3847 if (cookie_settings_->ShouldBlockThirdPartyCookies() &&
Olesia Marukhnobe5c4862023-07-18 16:29:5448 !cookie_settings_->IsThirdPartyAccessAllowed(rp_embedder_url) &&
Zachary Tan00aa3ac2023-08-02 20:05:3949 !base::FeatureList::IsEnabled(features::kFedCmWithoutThirdPartyCookies) &&
50 !base::FeatureList::IsEnabled(features::kFedCmIdpSigninStatusEnabled)) {
Peter Kotwicz153898a2022-05-05 18:48:3951 return PermissionStatus::BLOCKED_THIRD_PARTY_COOKIES_BLOCKED;
Peter Kotwiczaf746d32022-12-07 02:18:3652 }
Peter Kotwicz153898a2022-05-05 18:48:3953
Peter Kotwiczd4900eb82022-04-22 21:55:5954 const ContentSetting setting = host_content_settings_map_->GetContentSetting(
Peter Kotwicz51d5ddf2022-08-19 23:20:4055 rp_embedder_url, rp_embedder_url,
56 ContentSettingsType::FEDERATED_IDENTITY_API);
Peter Kotwiczd4900eb82022-04-22 21:55:5957 switch (setting) {
58 case CONTENT_SETTING_ALLOW:
59 break;
60 case CONTENT_SETTING_BLOCK:
Peter Kotwicz153898a2022-05-05 18:48:3961 return PermissionStatus::BLOCKED_SETTINGS;
Peter Kotwiczd4900eb82022-04-22 21:55:5962 default:
63 NOTREACHED();
Peter Kotwicz153898a2022-05-05 18:48:3964 return PermissionStatus::BLOCKED_SETTINGS;
Peter Kotwiczae649ee2022-04-22 16:01:0965 }
66
Peter Kotwicz63559a82022-06-07 03:46:3567 if (permission_autoblocker_->IsEmbargoed(
Peter Kotwicz51d5ddf2022-08-19 23:20:4068 rp_embedder_url, ContentSettingsType::FEDERATED_IDENTITY_API)) {
Peter Kotwicz153898a2022-05-05 18:48:3969 return PermissionStatus::BLOCKED_EMBARGO;
Peter Kotwicz63559a82022-06-07 03:46:3570 }
Peter Kotwicz153898a2022-05-05 18:48:3971 return PermissionStatus::GRANTED;
Nicolás Peña Moreno0c974faf2022-03-10 16:05:1272}
Peter Kotwiczae649ee2022-04-22 16:01:0973
74void FederatedIdentityApiPermissionContext::RecordDismissAndEmbargo(
Peter Kotwicz51d5ddf2022-08-19 23:20:4075 const url::Origin& relying_party_embedder) {
76 const GURL rp_embedder_url = relying_party_embedder.GetURL();
77 // If content setting is allowed for `rp_embedder_url`, reset it.
Peter Kotwicz7236eac2022-07-11 22:05:0478 // See crbug.com/1340127 for why the resetting is not conditional on the
79 // default content setting state.
80 const ContentSetting setting = host_content_settings_map_->GetContentSetting(
Peter Kotwicz51d5ddf2022-08-19 23:20:4081 rp_embedder_url, rp_embedder_url,
82 ContentSettingsType::FEDERATED_IDENTITY_API);
Peter Kotwicz7236eac2022-07-11 22:05:0483 if (setting == CONTENT_SETTING_ALLOW) {
84 host_content_settings_map_->SetContentSettingDefaultScope(
Peter Kotwicz51d5ddf2022-08-19 23:20:4085 rp_embedder_url, rp_embedder_url,
86 ContentSettingsType::FEDERATED_IDENTITY_API, CONTENT_SETTING_DEFAULT);
Peter Kotwicz7236eac2022-07-11 22:05:0487 }
Peter Kotwiczae649ee2022-04-22 16:01:0988 permission_autoblocker_->RecordDismissAndEmbargo(
Peter Kotwicz51d5ddf2022-08-19 23:20:4089 rp_embedder_url, ContentSettingsType::FEDERATED_IDENTITY_API,
Peter Kotwiczae649ee2022-04-22 16:01:0990 false /* dismissed_prompt_was_quiet */);
91}
92
93void FederatedIdentityApiPermissionContext::RemoveEmbargoAndResetCounts(
Peter Kotwicz51d5ddf2022-08-19 23:20:4094 const url::Origin& relying_party_embedder) {
Peter Kotwiczae649ee2022-04-22 16:01:0995 permission_autoblocker_->RemoveEmbargoAndResetCounts(
Peter Kotwicz51d5ddf2022-08-19 23:20:4096 relying_party_embedder.GetURL(),
97 ContentSettingsType::FEDERATED_IDENTITY_API);
Peter Kotwiczae649ee2022-04-22 16:01:0998}