blob: 07984caeb416727b9477cec24b00f56c4b6f9158 [file] [log] [blame]
Avi Drissman4a8573c2022-09-09 19:35:541// Copyright 2021 The Chromium Authors
Michael van Ouwerkerk18fd8d12021-05-12 16:31:432// 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 Liu89707e42023-03-27 22:48:467#include <string>
8
Michael van Ouwerkerk18fd8d12021-05-12 16:31:439#include "base/strings/utf_string_conversions.h"
Sonja Laurila0ef639172022-09-23 11:26:5010#include "chrome/browser/profiles/profile.h"
Zelin Liu89707e42023-03-27 22:48:4611#include "chrome/browser/ui/url_identity.h"
Michael van Ouwerkerk18fd8d12021-05-12 16:31:4312#include "content/public/browser/render_frame_host.h"
Michael van Ouwerkerk18fd8d12021-05-12 16:31:4313#include "ui/base/l10n/l10n_util.h"
Zelin Liu89707e42023-03-27 22:48:4614#include "url/gurl.h"
Michael van Ouwerkerk18fd8d12021-05-12 16:31:4315
Zelin Liu89707e42023-03-27 22:48:4616namespace {
Michael van Ouwerkerk18fd8d12021-05-12 16:31:4317
Zelin Liu89707e42023-03-27 22:48:4618constexpr UrlIdentity::TypeSet kUrlIdentityAllowedTypes = {
19 UrlIdentity::Type::kDefault, UrlIdentity::Type::kFile,
20 UrlIdentity::Type::kIsolatedWebApp, UrlIdentity::Type::kChromeExtension};
21constexpr UrlIdentity::FormatOptions kUrlIdentityOptions{
22 .default_options = {
23 UrlIdentity::DefaultFormatOptions::kOmitCryptographicScheme}};
24
25} // namespace
26
27std::u16string CreateChooserTitle(content::RenderFrameHost* render_frame_host,
28 int title_string_id) {
29 if (!render_frame_host) {
Michael van Ouwerkerk18fd8d12021-05-12 16:31:4330 return u"";
Zelin Liu89707e42023-03-27 22:48:4631 }
Reilly Grantb13ddd232022-01-25 20:43:4432 // Ensure the permission request is attributed to the main frame.
33 render_frame_host = render_frame_host->GetMainFrame();
Michael van Ouwerkerk18fd8d12021-05-12 16:31:4334
Alesandro Ortiz5fb01bb2023-07-19 22:40:1035 const GURL& url = render_frame_host->GetLastCommittedOrigin().GetURL();
Sonja Laurila0ef639172022-09-23 11:26:5036 Profile* profile =
37 Profile::FromBrowserContext(render_frame_host->GetBrowserContext());
38
Zelin Liu89707e42023-03-27 22:48:4639 UrlIdentity identity = UrlIdentity::CreateFromUrl(
40 profile, url, kUrlIdentityAllowedTypes, kUrlIdentityOptions);
Sonja Laurila0ef639172022-09-23 11:26:5041
Zelin Liu89707e42023-03-27 22:48:4642 return l10n_util::GetStringFUTF16(title_string_id, identity.name);
Michael van Ouwerkerk18fd8d12021-05-12 16:31:4343}