// Copyright (c) 2023 The Brave Authors. All rights reserved. // This Source Code Form is subject to the terms of the Mozilla Public // License, v. 2.0. If a copy of the MPL was not distributed with this file, // You can obtain one at https://mozilla.org/MPL/2.0/. #include "brave/app/command_utils.h" #include #include "base/containers/fixed_flat_map.h" #include "base/containers/span.h" #include "base/feature_list.h" #include "brave/app/brave_command_ids.h" #include "brave/components/commander/common/buildflags/buildflags.h" #include "brave/components/commands/common/features.h" #include "base/no_destructor.h" #include "base/strings/string_util.h" #include "chrome/app/chrome_command_ids.h" #include "chrome/grit/branded_strings.h" #include "chrome/grit/generated_resources.h" #include "components/grit/brave_components_strings.h" #include "components/strings/grit/components_strings.h" #include "third_party/re2/src/re2/re2.h" #include "third_party/re2/src/re2/stringpiece.h" #include "ui/base/l10n/l10n_util.h" #include "ui/strings/grit/ui_strings.h" #if BUILDFLAG(ENABLE_COMMANDER) #include "brave/components/commander/common/features.h" #endif namespace commands { namespace { constexpr auto kCommandTranslationIds = base::MakeFixedFlatMap({ COMMAND_NAMES }); constexpr int kCommandIds[] = { COMMAND_IDS }; } // namespace base::span GetCommands() { #if BUILDFLAG(ENABLE_COMMANDER) DCHECK(base::FeatureList::IsEnabled(features::kBraveCommands) || base::FeatureList::IsEnabled(::features::kBraveCommander)) #else DCHECK(base::FeatureList::IsEnabled(features::kBraveCommands)) #endif << "This should only be used when |kBraveCommands| is enabled."; return kCommandIds; } std::string GetCommandName(int command_id) { #if BUILDFLAG(ENABLE_COMMANDER) DCHECK(base::FeatureList::IsEnabled(features::kBraveCommands) || base::FeatureList::IsEnabled(::features::kBraveCommander)) #else DCHECK(base::FeatureList::IsEnabled(features::kBraveCommands)) #endif << "This should only be used when |kBraveCommands| is enabled."; auto it = kCommandTranslationIds.find(command_id); CHECK(it != kCommandTranslationIds.end()) << "Unknown command " << command_id << ". This function should only be used for known " "commands (i.e. commands in |GetCommands()|). " "This command should probably be added."; // Matches & and (&) static const base::NoDestructor kAmpersandPattern("(\\(?&[^\\s]\\)?)"); // Remove and '&' characters which don't have a space before and after. auto translation = l10n_util::GetStringUTF8(it->second); std::string match; if (re2::RE2::PartialMatch(translation, *kAmpersandPattern.get(), &match)) { // There are two cases here: // 1. The & refers to a character in the string - in this case, we just // remove the '&'. For example "Install &PWA". // 2. The & is in brackets, and defines a shortcut key not in the string, in // which case we remove the whole match. For example "Install (&K)PWA". auto remove = match.starts_with("(") ? match : "&"; base::ReplaceFirstSubstringAfterOffset(&translation, 0, remove, ""); } return translation; } } // namespace commands