Avi Drissman | 4a8573c | 2022-09-09 19:35:54 | [diff] [blame] | 1 | // Copyright 2021 The Chromium Authors |
Michael van Ouwerkerk | 18fd8d1 | 2021-05-12 16:31:43 | [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/chooser_controller/title_util.h" |
| 6 | |
Zelin Liu | 89707e4 | 2023-03-27 22:48:46 | [diff] [blame] | 7 | #include <string> |
| 8 | |
Michael van Ouwerkerk | 18fd8d1 | 2021-05-12 16:31:43 | [diff] [blame] | 9 | #include "base/strings/utf_string_conversions.h" |
Sonja Laurila | 0ef63917 | 2022-09-23 11:26:50 | [diff] [blame] | 10 | #include "chrome/browser/profiles/profile.h" |
Zelin Liu | 89707e4 | 2023-03-27 22:48:46 | [diff] [blame] | 11 | #include "chrome/browser/ui/url_identity.h" |
Michael van Ouwerkerk | 18fd8d1 | 2021-05-12 16:31:43 | [diff] [blame] | 12 | #include "content/public/browser/render_frame_host.h" |
Michael van Ouwerkerk | 18fd8d1 | 2021-05-12 16:31:43 | [diff] [blame] | 13 | #include "ui/base/l10n/l10n_util.h" |
Zelin Liu | 89707e4 | 2023-03-27 22:48:46 | [diff] [blame] | 14 | #include "url/gurl.h" |
Michael van Ouwerkerk | 18fd8d1 | 2021-05-12 16:31:43 | [diff] [blame] | 15 | |
Zelin Liu | 89707e4 | 2023-03-27 22:48:46 | [diff] [blame] | 16 | namespace { |
Michael van Ouwerkerk | 18fd8d1 | 2021-05-12 16:31:43 | [diff] [blame] | 17 | |
Zelin Liu | 89707e4 | 2023-03-27 22:48:46 | [diff] [blame] | 18 | constexpr UrlIdentity::TypeSet kUrlIdentityAllowedTypes = { |
| 19 | UrlIdentity::Type::kDefault, UrlIdentity::Type::kFile, |
| 20 | UrlIdentity::Type::kIsolatedWebApp, UrlIdentity::Type::kChromeExtension}; |
| 21 | constexpr UrlIdentity::FormatOptions kUrlIdentityOptions{ |
| 22 | .default_options = { |
| 23 | UrlIdentity::DefaultFormatOptions::kOmitCryptographicScheme}}; |
| 24 | |
| 25 | } // namespace |
| 26 | |
| 27 | std::u16string CreateChooserTitle(content::RenderFrameHost* render_frame_host, |
| 28 | int title_string_id) { |
| 29 | if (!render_frame_host) { |
Michael van Ouwerkerk | 18fd8d1 | 2021-05-12 16:31:43 | [diff] [blame] | 30 | return u""; |
Zelin Liu | 89707e4 | 2023-03-27 22:48:46 | [diff] [blame] | 31 | } |
Reilly Grant | b13ddd23 | 2022-01-25 20:43:44 | [diff] [blame] | 32 | // Ensure the permission request is attributed to the main frame. |
| 33 | render_frame_host = render_frame_host->GetMainFrame(); |
Michael van Ouwerkerk | 18fd8d1 | 2021-05-12 16:31:43 | [diff] [blame] | 34 | |
Alesandro Ortiz | 5fb01bb | 2023-07-19 22:40:10 | [diff] [blame] | 35 | const GURL& url = render_frame_host->GetLastCommittedOrigin().GetURL(); |
Sonja Laurila | 0ef63917 | 2022-09-23 11:26:50 | [diff] [blame] | 36 | Profile* profile = |
| 37 | Profile::FromBrowserContext(render_frame_host->GetBrowserContext()); |
| 38 | |
Zelin Liu | 89707e4 | 2023-03-27 22:48:46 | [diff] [blame] | 39 | UrlIdentity identity = UrlIdentity::CreateFromUrl( |
| 40 | profile, url, kUrlIdentityAllowedTypes, kUrlIdentityOptions); |
Sonja Laurila | 0ef63917 | 2022-09-23 11:26:50 | [diff] [blame] | 41 | |
Zelin Liu | 89707e4 | 2023-03-27 22:48:46 | [diff] [blame] | 42 | return l10n_util::GetStringFUTF16(title_string_id, identity.name); |
Michael van Ouwerkerk | 18fd8d1 | 2021-05-12 16:31:43 | [diff] [blame] | 43 | } |