blob: c4aee7cc972be7b5ab0e4b73c927468996f6965b [file]
// 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 COMPONENTS_ON_DEVICE_TRANSLATION_COMPONENT_MANAGER_H_
#define COMPONENTS_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/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 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();
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:
// Whether RegisterTranslateKitComponent() was called.
bool translate_kit_component_registered_ = false;
// The singleton instance of ComponentManager for testing.
static ComponentManager* component_manager_for_test_;
};
} // namespace on_device_translation
#endif // COMPONENTS_ON_DEVICE_TRANSLATION_COMPONENT_MANAGER_H_