Avi Drissman | 4a8573c | 2022-09-09 19:35:54 | [diff] [blame] | 1 | // Copyright 2014 The Chromium Authors |
[email protected] | 87ac156 | 2014-02-26 17:50:46 | [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 | |
| 5 | #include "chrome/browser/download/download_permission_request.h" |
| 6 | |
Florian Jacky | 7f3e3ca1b | 2025-05-05 16:19:20 | [diff] [blame] | 7 | #include <memory> |
| 8 | |
Florian Jacky | 11f9810 | 2025-05-20 15:57:18 | [diff] [blame] | 9 | #include "base/functional/callback_helpers.h" |
Bret Sepulveda | 5327d8b5 | 2021-07-21 17:44:23 | [diff] [blame] | 10 | #include "build/build_config.h" |
[email protected] | af39f00 | 2014-08-22 10:18:18 | [diff] [blame] | 11 | #include "chrome/grit/generated_resources.h" |
Florian Jacky | 7f3e3ca1b | 2025-05-05 16:19:20 | [diff] [blame] | 12 | #include "components/content_settings/core/common/content_settings_types.h" |
| 13 | #include "components/permissions/permission_request_data.h" |
Bret Sepulveda | 362cce4 | 2021-01-13 18:47:54 | [diff] [blame] | 14 | #include "components/permissions/request_type.h" |
Florian Jacky | 7f3e3ca1b | 2025-05-05 16:19:20 | [diff] [blame] | 15 | #include "components/permissions/resolvers/content_setting_permission_resolver.h" |
[email protected] | 87ac156 | 2014-02-26 17:50:46 | [diff] [blame] | 16 | #include "ui/base/l10n/l10n_util.h" |
| 17 | |
Xiaohan Wang | ab5fb91 | 2022-01-12 19:21:48 | [diff] [blame] | 18 | #if BUILDFLAG(IS_ANDROID) |
timloh | 178801f | 2017-06-13 04:17:58 | [diff] [blame] | 19 | #include "chrome/browser/android/android_theme_resources.h" |
Mugdha Lakhani | 6b39f5fd | 2018-11-23 11:19:01 | [diff] [blame] | 20 | #include "components/url_formatter/elide_url.h" |
| 21 | #include "url/origin.h" |
timloh | 178801f | 2017-06-13 04:17:58 | [diff] [blame] | 22 | #else |
Mugdha Lakhani | b676216 | 2020-03-30 10:23:25 | [diff] [blame] | 23 | #include "components/vector_icons/vector_icons.h" |
timloh | 178801f | 2017-06-13 04:17:58 | [diff] [blame] | 24 | #endif |
| 25 | |
[email protected] | 87ac156 | 2014-02-26 17:50:46 | [diff] [blame] | 26 | DownloadPermissionRequest::DownloadPermissionRequest( |
Min Qin | 357184a | 2019-06-25 16:31:44 | [diff] [blame] | 27 | base::WeakPtr<DownloadRequestLimiter::TabDownloadState> host, |
Bret Sepulveda | 5327d8b5 | 2021-07-21 17:44:23 | [diff] [blame] | 28 | const url::Origin& requesting_origin) |
| 29 | : PermissionRequest( |
Florian Jacky | 7f3e3ca1b | 2025-05-05 16:19:20 | [diff] [blame] | 30 | std::make_unique<permissions::PermissionRequestData>( |
| 31 | std::make_unique<permissions::ContentSettingPermissionResolver>( |
| 32 | permissions::RequestType::kMultipleDownloads), |
| 33 | /*user_gesture=*/false, |
| 34 | requesting_origin.GetURL()), |
Illia Klimov | e406ecc1 | 2022-11-22 15:53:29 | [diff] [blame] | 35 | base::BindRepeating(&DownloadPermissionRequest::PermissionDecided, |
Florian Jacky | 11f9810 | 2025-05-20 15:57:18 | [diff] [blame] | 36 | base::Unretained(this))), |
Bret Sepulveda | 5327d8b5 | 2021-07-21 17:44:23 | [diff] [blame] | 37 | host_(host), |
| 38 | requesting_origin_(requesting_origin) {} |
[email protected] | 87ac156 | 2014-02-26 17:50:46 | [diff] [blame] | 39 | |
Sorin Jianu | 057cba6a | 2024-11-29 05:49:34 | [diff] [blame] | 40 | DownloadPermissionRequest::~DownloadPermissionRequest() = default; |
[email protected] | 87ac156 | 2014-02-26 17:50:46 | [diff] [blame] | 41 | |
Florian Jacky | 7f3e3ca1b | 2025-05-05 16:19:20 | [diff] [blame] | 42 | void DownloadPermissionRequest::PermissionDecided( |
| 43 | ContentSetting result, |
| 44 | bool is_one_time, |
| 45 | bool is_final_decision, |
Chris Fredrickson | 173060c86 | 2025-05-14 14:23:01 | [diff] [blame] | 46 | const permissions::PermissionRequestData& request_data) { |
Ravjit Singh Uppal | c73b5a6 | 2020-11-13 01:38:52 | [diff] [blame] | 47 | DCHECK(!is_one_time); |
Illia Klimov | e406ecc1 | 2022-11-22 15:53:29 | [diff] [blame] | 48 | DCHECK(is_final_decision); |
Bret Sepulveda | 5327d8b5 | 2021-07-21 17:44:23 | [diff] [blame] | 49 | if (!host_) |
| 50 | return; |
| 51 | |
| 52 | // This may invalidate |host_|. |
| 53 | if (result == ContentSetting::CONTENT_SETTING_ALLOW) { |
| 54 | host_->Accept(requesting_origin_); |
| 55 | } else if (result == ContentSetting::CONTENT_SETTING_BLOCK) { |
| 56 | host_->Cancel(requesting_origin_); |
| 57 | } else { |
| 58 | DCHECK_EQ(CONTENT_SETTING_DEFAULT, result); |
| 59 | host_->CancelOnce(requesting_origin_); |
[email protected] | 87ac156 | 2014-02-26 17:50:46 | [diff] [blame] | 60 | } |
| 61 | } |