Michael van Ouwerkerk | 18fd8d1 | 2021-05-12 16:31:43 | [diff] [blame^] | 1 | // Copyright 2021 The Chromium Authors. All rights reserved. |
| 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 | |
| 7 | #include "base/strings/utf_string_conversions.h" |
| 8 | #include "components/url_formatter/elide_url.h" |
| 9 | #include "content/public/browser/render_frame_host.h" |
| 10 | #include "content/public/browser/web_contents.h" |
| 11 | #include "extensions/buildflags/buildflags.h" |
| 12 | #include "ui/base/l10n/l10n_util.h" |
| 13 | #include "url/origin.h" |
| 14 | |
| 15 | #if BUILDFLAG(ENABLE_EXTENSIONS) |
| 16 | #include "extensions/browser/extension_registry.h" |
| 17 | #include "extensions/common/constants.h" |
| 18 | #endif |
| 19 | |
| 20 | std::u16string CreateChooserTitle(content::RenderFrameHost* render_frame_host, |
| 21 | int title_string_id_origin, |
| 22 | int title_string_id_extension) { |
| 23 | if (!render_frame_host) |
| 24 | return u""; |
| 25 | |
| 26 | url::Origin origin = render_frame_host->GetLastCommittedOrigin(); |
| 27 | |
| 28 | #if BUILDFLAG(ENABLE_EXTENSIONS) |
| 29 | if (origin.scheme() == extensions::kExtensionScheme) { |
| 30 | content::WebContents* web_contents = |
| 31 | content::WebContents::FromRenderFrameHost(render_frame_host); |
| 32 | content::BrowserContext* browser_context = |
| 33 | web_contents->GetBrowserContext(); |
| 34 | extensions::ExtensionRegistry* extension_registry = |
| 35 | extensions::ExtensionRegistry::Get(browser_context); |
| 36 | if (extension_registry) { |
| 37 | const extensions::Extension* extension = |
| 38 | extension_registry->enabled_extensions().GetByID(origin.host()); |
| 39 | if (extension) { |
| 40 | return l10n_util::GetStringFUTF16(title_string_id_extension, |
| 41 | base::UTF8ToUTF16(extension->name())); |
| 42 | } |
| 43 | } |
| 44 | } |
| 45 | #endif |
| 46 | |
| 47 | return l10n_util::GetStringFUTF16( |
| 48 | title_string_id_origin, |
| 49 | url_formatter::FormatOriginForSecurityDisplay( |
| 50 | origin, url_formatter::SchemeDisplay::OMIT_CRYPTOGRAPHIC)); |
| 51 | } |