blob: f32f25522138f87ac6af3bf764ab9adddf7db802 [file]
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "extensions/browser/ui_util.h"
#if !BUILDFLAG(IS_ANDROID)
#include <optional>
#endif
#include "base/command_line.h"
#include "base/strings/utf_string_conversions.h"
#include "build/build_config.h"
#include "extensions/common/extension.h"
#include "extensions/common/switches.h"
#include "ui/gfx/text_constants.h"
#include "ui/gfx/text_elider.h"
#if !BUILDFLAG(IS_ANDROID)
#include "content/public/browser/web_contents.h"
#include "extensions/browser/extension_registry.h"
#include "extensions/browser/mime_handler/mime_handler_stream_manager.h"
#include "extensions/common/extension_id.h"
#include "extensions/common/manifest_handlers/mime_types_handler.h"
#endif
namespace extensions::ui_util {
#if !BUILDFLAG(IS_ANDROID)
const Extension* GetTopLevelMimeHandlerExtension(
content::WebContents& web_contents) {
auto* manager =
mime_handler::MimeHandlerStreamManager::FromWebContents(&web_contents);
if (!manager) {
return nullptr;
}
std::optional<ExtensionId> id = manager->GetTopLevelHandlerExtensionId();
if (!id) {
return nullptr;
}
auto* registry = ExtensionRegistry::Get(web_contents.GetBrowserContext());
CHECK(registry);
// A claimed `StreamInfo` exists only while the MIME-handler extension is
// actively rendering the stream, and extension disable tears the extension
// frame down (`MaybeDeleteStreamOnExtensionHostChanged()`), which clears
// the `StreamInfo`. So an ID returned by `GetTopLevelHandlerExtensionId()`
// must resolve to an enabled extension.
const Extension* extension = registry->enabled_extensions().GetByID(*id);
CHECK(extension);
// Allowlisted plugin extensions never relabel the chip. The allowlist
// contains built-in PDF and QuickOffice variants, so this single filter
// covers both categories (see `kMIMETypeHandlersAllowlist`).
const MimeTypesHandler* handler = MimeTypesHandler::Get(*extension);
if (handler && handler->IsPluginExtension()) {
return nullptr;
}
return extension;
}
#endif // !BUILDFLAG(IS_ANDROID)
bool ShouldDisplayInExtensionSettings(Manifest::Type type,
mojom::ManifestLocation location) {
// Don't show for themes since the settings UI isn't really useful for them.
if (type == Manifest::Type::kTheme) {
return false;
}
// Hide component extensions because they are only extensions as an
// implementation detail of Chrome.
if (Manifest::IsComponentLocation(location) &&
!base::CommandLine::ForCurrentProcess()->HasSwitch(
switches::kShowComponentExtensionOptions)) {
return false;
}
// Unless they are unpacked, never show hosted apps. Note: We intentionally
// show packaged apps and platform apps because there are some pieces of
// functionality that are only available in chrome://extensions/ but which
// are needed for packaged and platform apps. For example, inspecting
// background pages. See http://crbug.com/40162419.
if (!Manifest::IsUnpackedLocation(location) &&
type == Manifest::Type::kHostedApp) {
return false;
}
return true;
}
bool ShouldDisplayInExtensionSettings(const Extension& extension) {
return ShouldDisplayInExtensionSettings(extension.GetType(),
extension.location());
}
std::u16string GetFixupExtensionNameForUIDisplay(
const std::u16string& extension_name) {
const size_t extension_name_char_limit =
75; // Extension name char limit on CWS
gfx::BreakType break_type = gfx::BreakType::CHARACTER_BREAK;
std::u16string fixup_extension_name = gfx::TruncateString(
extension_name, extension_name_char_limit, break_type);
return fixup_extension_name;
}
std::u16string GetFixupExtensionNameForUIDisplay(
const std::string& extension_name) {
return GetFixupExtensionNameForUIDisplay(base::UTF8ToUTF16(extension_name));
}
} // namespace extensions::ui_util