Avi Drissman | 8ba1bad | 2022-09-13 19:22:36 | [diff] [blame] | 1 | // Copyright 2013 The Chromium Authors |
[email protected] | 44ecb83 | 2013-08-02 09:06: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 | |
Richard Knoll | 93313106 | 2021-04-13 12:49:13 | [diff] [blame] | 5 | #include "components/permissions/contexts/midi_permission_context.h" |
| 6 | |
Sina Firoozabadi | 95383c4 | 2023-06-23 22:41:51 | [diff] [blame] | 7 | #include "components/content_settings/browser/page_specific_content_settings.h" |
Richard Knoll | 93313106 | 2021-04-13 12:49:13 | [diff] [blame] | 8 | #include "components/content_settings/core/common/content_settings.h" |
| 9 | #include "components/content_settings/core/common/content_settings_types.h" |
Sina Firoozabadi | 95383c4 | 2023-06-23 22:41:51 | [diff] [blame] | 10 | #include "components/permissions/features.h" |
| 11 | #include "components/permissions/permission_request_id.h" |
Michael Wilson | abf3a60 | 2023-07-14 20:08:30 | [diff] [blame^] | 12 | #include "components/permissions/permissions_client.h" |
Sina Firoozabadi | 95383c4 | 2023-06-23 22:41:51 | [diff] [blame] | 13 | #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] | 44ecb83 | 2013-08-02 09:06:45 | [diff] [blame] | 16 | |
Richard Knoll | 93313106 | 2021-04-13 12:49:13 | [diff] [blame] | 17 | namespace permissions { |
| 18 | |
Clark DuVall | a11361ad3 | 2020-02-20 22:14:27 | [diff] [blame] | 19 | MidiPermissionContext::MidiPermissionContext( |
| 20 | content::BrowserContext* browser_context) |
Charlie Hu | 1b9da2c | 2021-03-04 18:51:30 | [diff] [blame] | 21 | : PermissionContextBase( |
| 22 | browser_context, |
| 23 | ContentSettingsType::MIDI, |
Michael Wilson | abf3a60 | 2023-07-14 20:08:30 | [diff] [blame^] | 24 | 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] | 44ecb83 | 2013-08-02 09:06:45 | [diff] [blame] | 34 | |
Michael Wilson | abf3a60 | 2023-07-14 20:08:30 | [diff] [blame^] | 35 | MidiPermissionContext::~MidiPermissionContext() { |
| 36 | if (base::FeatureList::IsEnabled( |
| 37 | permissions::features::kBlockMidiByDefault)) { |
| 38 | host_content_settings_map_->RemoveObserver(this); |
| 39 | } |
| 40 | } |
[email protected] | 44ecb83 | 2013-08-02 09:06:45 | [diff] [blame] | 41 | |
raymes | c2cf640f | 2017-05-29 01:35:27 | [diff] [blame] | 42 | ContentSetting MidiPermissionContext::GetPermissionStatusInternal( |
| 43 | content::RenderFrameHost* render_frame_host, |
| 44 | const GURL& requesting_origin, |
| 45 | const GURL& embedding_origin) const { |
Sina Firoozabadi | 95383c4 | 2023-06-23 22:41:51 | [diff] [blame] | 46 | if (base::FeatureList::IsEnabled(features::kBlockMidiByDefault)) { |
| 47 | return PermissionContextBase::GetPermissionStatusInternal( |
| 48 | render_frame_host, requesting_origin, embedding_origin); |
| 49 | } |
raymes | c2cf640f | 2017-05-29 01:35:27 | [diff] [blame] | 50 | return CONTENT_SETTING_ALLOW; |
[email protected] | 00880a0 | 2013-10-30 03:18:30 | [diff] [blame] | 51 | } |
mlamouri | a31c6ff1 | 2015-06-01 15:40:52 | [diff] [blame] | 52 | |
Michael Wilson | abf3a60 | 2023-07-14 20:08:30 | [diff] [blame^] | 53 | void 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 Firoozabadi | 95383c4 | 2023-06-23 22:41:51 | [diff] [blame] | 107 | void 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 Knoll | 93313106 | 2021-04-13 12:49:13 | [diff] [blame] | 130 | } // namespace permissions |