1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
|
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_BROWSER_ON_DEVICE_TRANSLATION_COMPONENT_MANAGER_H_
#define CHROME_BROWSER_ON_DEVICE_TRANSLATION_COMPONENT_MANAGER_H_
#include <optional>
#include <set>
#include <vector>
#include "base/auto_reset.h"
#include "base/files/file_path.h"
#include "components/services/on_device_translation/public/mojom/on_device_translation_service.mojom-forward.h"
namespace on_device_translation {
enum class LanguagePackKey;
// This class handles the TranslateKit component and the language pack
// components.
class ComponentManager {
public:
// Returns the singleton instance of ComponentManager.
static ComponentManager& GetInstance();
// Sets the singleton instance of ComponentManager for testing.
static base::AutoReset<ComponentManager*> SetForTesting(
ComponentManager* manager);
// Returns the path of the TranslateKit library. If the path is set by the
// command line, returns the path from the command line
// `--translate-kit-binary-path`. Otherwise, returns the path from the global
// prefs.
static base::FilePath GetTranslateKitLibraryPath();
// Returns true if the path of the TranslateKit library is set by the command
// line `--translate-kit-binary-path`.
static bool HasTranslateKitLibraryPathFromCommandLine();
// Returns the language packs that were registered.
static std::set<LanguagePackKey> GetRegisteredLanguagePacks();
// Returns the language packs that were installed and ready to use.
static std::set<LanguagePackKey> GetInstalledLanguagePacks();
ComponentManager();
virtual ~ComponentManager();
// Disallow copy and assign.
ComponentManager(const ComponentManager&) = delete;
ComponentManager& operator=(const ComponentManager&) = delete;
// Registers the TranslateKit component and returns true if this is called for
// the first time. Otherwise returns false.
bool RegisterTranslateKitComponent();
// Registers the language pack component.
virtual void RegisterTranslateKitLanguagePackComponent(
LanguagePackKey language_pack) = 0;
// Uninstalls the language pack component.
virtual void UninstallTranslateKitLanguagePackComponent(
LanguagePackKey language_pack) = 0;
// If the TranslateKit binary path is passed via the command line
// `--translate-kit-binary-path`, returns the binary path. Otherwise, returns
// the directory path of the installation location of the TranslateKit binary
// component.
// This is called from a launcher thread to allow reading the files under the
// returned path in the sandboxed process on macOS.
base::FilePath GetTranslateKitComponentPath();
// Get the language pack info. If the language pack info is set by the
// command line `--translate-kit-packages`, returns the language pack info
// from the command line. Otherwise, returns the language pack info from the
// global prefs.
void GetLanguagePackInfo(
std::vector<mojom::OnDeviceTranslationLanguagePackagePtr>& packages,
std::vector<base::FilePath>& package_pathes);
// Returns true if the language pack information is from the command line
// `--translate-kit-packages`.
bool HasLanguagePackInfoFromCommandLine() const {
return !!language_packs_from_command_line_;
}
protected:
// This is called when RegisterTranslateKitComponent() is called for the first
// time.
virtual void RegisterTranslateKitComponentImpl() = 0;
// This is called from GetTranslateKitComponentPath() when the TranslateKit
// binary path is not passed via the command line.
virtual base::FilePath GetTranslateKitComponentPathImpl() = 0;
private:
// The information of a language pack.
struct LanguagePackInfo {
std::string language1;
std::string language2;
base::FilePath package_path;
};
// Get a list of LanguagePackInfo from the command line flag
// `--translate-kit-packages`.
static std::optional<std::vector<LanguagePackInfo>>
GetLanguagePackInfoFromCommandLine();
// Whether RegisterTranslateKitComponent() was called.
bool translate_kit_component_registered_ = false;
// The LanguagePackInfo from the command line. This is nullopt if the command
// line flag `--translate-kit-packages` is not set.
const std::optional<std::vector<LanguagePackInfo>>
language_packs_from_command_line_;
// The singleton instance of ComponentManager for testing.
static ComponentManager* component_manager_for_test_;
};
} // namespace on_device_translation
#endif // CHROME_BROWSER_ON_DEVICE_TRANSLATION_COMPONENT_MANAGER_H_
|