blob: fa9691586ba8ff4dc2afc40a537cc03c7272d7fe [file] [log] [blame]
Avi Drissman8ba1bad2022-09-13 19:22:361// Copyright 2013 The Chromium Authors
[email protected]44ecb832013-08-02 09:06:452// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Richard Knoll933131062021-04-13 12:49:135#include "components/permissions/contexts/midi_permission_context.h"
6
Sina Firoozabadi95383c42023-06-23 22:41:517#include "components/content_settings/browser/page_specific_content_settings.h"
Richard Knoll933131062021-04-13 12:49:138#include "components/content_settings/core/common/content_settings.h"
9#include "components/content_settings/core/common/content_settings_types.h"
Sina Firoozabadi95383c42023-06-23 22:41:5110#include "components/permissions/features.h"
11#include "components/permissions/permission_request_id.h"
Michael Wilsonabf3a602023-07-14 20:08:3012#include "components/permissions/permissions_client.h"
Sina Firoozabadi95383c42023-06-23 22:41:5113#include "content/public/browser/child_process_security_policy.h"
14#include "third_party/blink/public/mojom/permissions_policy/permissions_policy_feature.mojom.h"
15#include "url/gurl.h"
[email protected]44ecb832013-08-02 09:06:4516
Richard Knoll933131062021-04-13 12:49:1317namespace permissions {
18
Clark DuValla11361ad32020-02-20 22:14:2719MidiPermissionContext::MidiPermissionContext(
20 content::BrowserContext* browser_context)
Charlie Hu1b9da2c2021-03-04 18:51:3021 : PermissionContextBase(
22 browser_context,
23 ContentSettingsType::MIDI,
Michael Wilsonabf3a602023-07-14 20:08:3024 blink::mojom::PermissionsPolicyFeature::kMidiFeature),
25 host_content_settings_map_(
26 permissions::PermissionsClient::Get()->GetSettingsMap(
27 browser_context)) {
28 if (base::FeatureList::IsEnabled(
29 permissions::features::kBlockMidiByDefault)) {
30 content_setting_observer_registered_by_subclass_ = true;
31 host_content_settings_map_->AddObserver(this);
32 }
33}
[email protected]44ecb832013-08-02 09:06:4534
Michael Wilsonabf3a602023-07-14 20:08:3035MidiPermissionContext::~MidiPermissionContext() {
36 if (base::FeatureList::IsEnabled(
37 permissions::features::kBlockMidiByDefault)) {
38 host_content_settings_map_->RemoveObserver(this);
39 }
40}
[email protected]44ecb832013-08-02 09:06:4541
raymesc2cf640f2017-05-29 01:35:2742ContentSetting MidiPermissionContext::GetPermissionStatusInternal(
43 content::RenderFrameHost* render_frame_host,
44 const GURL& requesting_origin,
45 const GURL& embedding_origin) const {
Sina Firoozabadi95383c42023-06-23 22:41:5146 if (base::FeatureList::IsEnabled(features::kBlockMidiByDefault)) {
47 return PermissionContextBase::GetPermissionStatusInternal(
48 render_frame_host, requesting_origin, embedding_origin);
49 }
raymesc2cf640f2017-05-29 01:35:2750 return CONTENT_SETTING_ALLOW;
[email protected]00880a02013-10-30 03:18:3051}
mlamouria31c6ff12015-06-01 15:40:5252
Michael Wilsonabf3a602023-07-14 20:08:3053void MidiPermissionContext::OnContentSettingChanged(
54 const ContentSettingsPattern& primary_pattern,
55 const ContentSettingsPattern& secondary_pattern,
56 ContentSettingsTypeSet content_type_set) {
57 PermissionContextBase::OnContentSettingChanged(
58 primary_pattern, secondary_pattern, content_type_set);
59
60 // Synchronize the MIDI SysEx permission
61 if (base::FeatureList::IsEnabled(
62 permissions::features::kBlockMidiByDefault)) {
63 if (content_type_set.Contains(ContentSettingsType::MIDI)) {
64 // TODO(crbug.com/1078272): We should not need to deduce the url from
65 // the primary pattern here. Modify the infrastructure to facilitate
66 // this particular use case better.
67 const GURL url(primary_pattern.ToString());
68 if (!url::Origin::Create(url).opaque()) {
69 const ContentSetting midi_setting =
70 host_content_settings_map_->GetContentSetting(
71 url, url, ContentSettingsType::MIDI);
72 const ContentSetting midi_sysex_setting =
73 host_content_settings_map_->GetContentSetting(
74 url, url, ContentSettingsType::MIDI_SYSEX);
75
76 switch (midi_setting) {
77 case CONTENT_SETTING_BLOCK:
78 if (midi_sysex_setting != CONTENT_SETTING_BLOCK) {
79 host_content_settings_map_->SetContentSettingCustomScope(
80 primary_pattern, secondary_pattern,
81 ContentSettingsType::MIDI_SYSEX, CONTENT_SETTING_BLOCK);
82 }
83 break;
84 case CONTENT_SETTING_ASK:
85 if (midi_sysex_setting != CONTENT_SETTING_ASK) {
86 host_content_settings_map_->SetContentSettingCustomScope(
87 primary_pattern, secondary_pattern,
88 ContentSettingsType::MIDI_SYSEX, CONTENT_SETTING_ASK);
89 }
90 break;
91 case CONTENT_SETTING_ALLOW:
92 if (midi_sysex_setting != CONTENT_SETTING_ASK &&
93 midi_sysex_setting != CONTENT_SETTING_ALLOW) {
94 host_content_settings_map_->SetContentSettingCustomScope(
95 primary_pattern, secondary_pattern,
96 ContentSettingsType::MIDI_SYSEX, CONTENT_SETTING_ASK);
97 }
98 break;
99 default:
100 break;
101 }
102 }
103 }
104 }
105}
106
Sina Firoozabadi95383c42023-06-23 22:41:51107void MidiPermissionContext::UpdateTabContext(const PermissionRequestID& id,
108 const GURL& requesting_frame,
109 bool allowed) {
110 if (base::FeatureList::IsEnabled(
111 permissions::features::kBlockMidiByDefault)) {
112 content_settings::PageSpecificContentSettings* content_settings =
113 content_settings::PageSpecificContentSettings::GetForFrame(
114 id.global_render_frame_host_id());
115 if (!content_settings) {
116 return;
117 }
118
119 if (allowed) {
120 content_settings->OnContentAllowed(ContentSettingsType::MIDI);
121
122 content::ChildProcessSecurityPolicy::GetInstance()->GrantSendMidiMessage(
123 id.global_render_frame_host_id().child_id);
124 } else {
125 content_settings->OnContentBlocked(ContentSettingsType::MIDI);
126 }
127 }
128}
129
Richard Knoll933131062021-04-13 12:49:13130} // namespace permissions