blob: 6703d4aa033f95616335bc307cd6b3dd10988796 [file] [log] [blame]
Peter Kotwicz90c0dc222022-03-09 18:24:451// 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 Moreno0c974faf2022-03-10 16:05:127#include "chrome/browser/content_settings/cookie_settings_factory.h"
Peter Kotwicz90c0dc222022-03-09 18:24:458#include "chrome/browser/content_settings/host_content_settings_map_factory.h"
Peter Kotwiczae649ee2022-04-22 16:01:099#include "chrome/browser/permissions/permission_decision_auto_blocker_factory.h"
Nicolás Peña Moreno0c974faf2022-03-10 16:05:1210#include "chrome/browser/profiles/profile.h"
Peter Kotwicz90c0dc222022-03-09 18:24:4511#include "components/content_settings/core/common/content_settings_types.h"
Peter Kotwiczae649ee2022-04-22 16:01:0912#include "components/permissions/permission_decision_auto_blocker.h"
13#include "components/permissions/permission_result.h"
Peter Kotwicz153898a2022-05-05 18:48:3914#include "content/public/common/content_features.h"
Peter Kotwiczae649ee2022-04-22 16:01:0915#include "url/origin.h"
Peter Kotwicz90c0dc222022-03-09 18:24:4516
Peter Kotwicz153898a2022-05-05 18:48:3917using PermissionStatus =
18 content::FederatedIdentityApiPermissionContextDelegate::PermissionStatus;
19
Peter Kotwicz90c0dc222022-03-09 18:24:4520FederatedIdentityApiPermissionContext::FederatedIdentityApiPermissionContext(
21 content::BrowserContext* browser_context)
22 : host_content_settings_map_(
Nicolás Peña Moreno0c974faf2022-03-10 16:05:1223 HostContentSettingsMapFactory::GetForProfile(browser_context)),
24 cookie_settings_(CookieSettingsFactory::GetForProfile(
Peter Kotwiczae649ee2022-04-22 16:01:0925 Profile::FromBrowserContext(browser_context))),
26 permission_autoblocker_(
27 PermissionDecisionAutoBlockerFactory::GetForProfile(
28 Profile::FromBrowserContext(browser_context))) {}
Peter Kotwicz90c0dc222022-03-09 18:24:4529
30FederatedIdentityApiPermissionContext::
31 ~FederatedIdentityApiPermissionContext() = default;
32
Peter Kotwicz153898a2022-05-05 18:48:3933content::FederatedIdentityApiPermissionContextDelegate::PermissionStatus
34FederatedIdentityApiPermissionContext::GetApiPermissionStatus(
Peter Kotwiczae649ee2022-04-22 16:01:0935 const url::Origin& rp_origin) {
Peter Kotwicz153898a2022-05-05 18:48:3936 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 Kotwiczd4900eb82022-04-22 21:55:5945 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 Kotwicz153898a2022-05-05 18:48:3952 return PermissionStatus::BLOCKED_SETTINGS;
Peter Kotwiczd4900eb82022-04-22 21:55:5953 default:
54 NOTREACHED();
Peter Kotwicz153898a2022-05-05 18:48:3955 return PermissionStatus::BLOCKED_SETTINGS;
Peter Kotwiczae649ee2022-04-22 16:01:0956 }
57
Peter Kotwicz63559a82022-06-07 03:46:3558 if (permission_autoblocker_->IsEmbargoed(
59 rp_url, ContentSettingsType::FEDERATED_IDENTITY_API)) {
Peter Kotwicz153898a2022-05-05 18:48:3960 return PermissionStatus::BLOCKED_EMBARGO;
Peter Kotwicz63559a82022-06-07 03:46:3561 }
Peter Kotwicz153898a2022-05-05 18:48:3962 return PermissionStatus::GRANTED;
Nicolás Peña Moreno0c974faf2022-03-10 16:05:1263}
Peter Kotwiczae649ee2022-04-22 16:01:0964
65void 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
72void FederatedIdentityApiPermissionContext::RemoveEmbargoAndResetCounts(
73 const url::Origin& rp_origin) {
74 permission_autoblocker_->RemoveEmbargoAndResetCounts(
75 rp_origin.GetURL(), ContentSettingsType::FEDERATED_IDENTITY_API);
76}