|
| 1 | +/* Copyright (c) 2026 The Brave Authors. All rights reserved. |
| 2 | + * This Source Code Form is subject to the terms of the Mozilla Public |
| 3 | + * License, v. 2.0. If a copy of the MPL was not distributed with this file, |
| 4 | + * You can obtain one at https://mozilla.org/MPL/2.0/. */ |
| 5 | + |
| 6 | +#include "brave/browser/speech/brave_soda_installer.h" |
| 7 | + |
| 8 | +#include <string> |
| 9 | +#include <string_view> |
| 10 | +#include <vector> |
| 11 | + |
| 12 | +#include "base/files/file_path.h" |
| 13 | +#include "base/functional/bind.h" |
| 14 | +#include "brave/components/local_ai/core/on_device_speech_models_component_installer.h" |
| 15 | +#include "chrome/browser/browser_process.h" |
| 16 | +#include "components/component_updater/component_updater_service.h" |
| 17 | +#include "components/soda/constants.h" |
| 18 | +#include "components/update_client/crx_update_item.h" |
| 19 | +#include "components/update_client/update_client_errors.h" |
| 20 | + |
| 21 | +namespace speech { |
| 22 | + |
| 23 | +BraveSodaInstaller::BraveSodaInstaller() { |
| 24 | + local_ai::OnDeviceSpeechModelsState::GetInstance()->AddObserver(this); |
| 25 | +} |
| 26 | + |
| 27 | +BraveSodaInstaller::~BraveSodaInstaller() { |
| 28 | + local_ai::OnDeviceSpeechModelsState::GetInstance()->RemoveObserver(this); |
| 29 | +} |
| 30 | + |
| 31 | +void BraveSodaInstaller::Init(PrefService* profile_prefs, |
| 32 | + PrefService* global_prefs) {} |
| 33 | + |
| 34 | +void BraveSodaInstaller::InstallSoda(PrefService* global_prefs) {} |
| 35 | + |
| 36 | +void BraveSodaInstaller::UninstallSoda(PrefService* global_prefs) {} |
| 37 | + |
| 38 | +void BraveSodaInstaller::UninstallLanguage(std::string_view language, |
| 39 | + PrefService* global_prefs) {} |
| 40 | + |
| 41 | +void BraveSodaInstaller::RegisterLanguage(std::string_view language, |
| 42 | + PrefService* global_prefs) {} |
| 43 | + |
| 44 | +void BraveSodaInstaller::UnregisterLanguage(std::string_view language, |
| 45 | + PrefService* global_prefs) {} |
| 46 | + |
| 47 | +void BraveSodaInstaller::InstallLanguage(std::string_view language, |
| 48 | + PrefService* global_prefs) { |
| 49 | + auto* state = local_ai::OnDeviceSpeechModelsState::GetInstance(); |
| 50 | + if (state->IsModelInstalled()) { |
| 51 | + return; |
| 52 | + } |
| 53 | + |
| 54 | + auto* component_updater = g_browser_process->component_updater(); |
| 55 | + if (component_updater && |
| 56 | + !component_updater_observation_.IsObservingSource(component_updater)) { |
| 57 | + component_updater_observation_.Observe(component_updater); |
| 58 | + } |
| 59 | + |
| 60 | + local_ai::MaybeRegisterOnDeviceSpeechModelsComponent( |
| 61 | + component_updater, global_prefs, |
| 62 | + base::BindOnce(&BraveSodaInstaller::OnComponentUpdaterResult, |
| 63 | + weak_factory_.GetWeakPtr())); |
| 64 | +} |
| 65 | + |
| 66 | +void BraveSodaInstaller::OnEvent(const update_client::CrxUpdateItem& item) { |
| 67 | + if (item.id != local_ai::kOnDeviceSpeechModelsComponentId) { |
| 68 | + return; |
| 69 | + } |
| 70 | + |
| 71 | + // Only handle failure case here, success case is handled via |
| 72 | + // `ComponentReady`. In-progress events are ignored as we don't report |
| 73 | + // downloading status. |
| 74 | + if (item.state != update_client::ComponentState::kUpdateError) { |
| 75 | + return; |
| 76 | + } |
| 77 | + |
| 78 | + NotifyOnSodaInstallError(LanguageCode::kEnUs, ErrorCode::kUnspecifiedError); |
| 79 | +} |
| 80 | + |
| 81 | +void BraveSodaInstaller::OnComponentUpdaterResult(update_client::Error error) { |
| 82 | + // Success needs no action, because `ComponentReady` publishes the install dir |
| 83 | + // before this runs and that is what reports the model as installed. |
| 84 | + auto* state = local_ai::OnDeviceSpeechModelsState::GetInstance(); |
| 85 | + if (error == update_client::Error::NONE && state->IsModelInstalled()) { |
| 86 | + return; |
| 87 | + } |
| 88 | + |
| 89 | + // Not a failure. A download for the component is already running, so this |
| 90 | + // request was refused as a duplicate. That download reports its own outcome, |
| 91 | + // success through `ComponentReady` and failure through `OnEvent`. |
| 92 | + if (error == update_client::Error::UPDATE_IN_PROGRESS) { |
| 93 | + return; |
| 94 | + } |
| 95 | + |
| 96 | + NotifyOnSodaInstallError(LanguageCode::kEnUs, ErrorCode::kUnspecifiedError); |
| 97 | +} |
| 98 | + |
| 99 | +std::vector<std::string> BraveSodaInstaller::GetLiveCaptionEnabledLanguages() |
| 100 | + const { |
| 101 | + // Brave's model serves English only. `install()` rejects anything outside |
| 102 | + // this list before it reaches `InstallLanguage`, which is what keeps a |
| 103 | + // request for another language from installing an English model. |
| 104 | + return {GetLanguageName(LanguageCode::kEnUs)}; |
| 105 | +} |
| 106 | + |
| 107 | +std::vector<std::string> BraveSodaInstaller::GetAvailableLanguages() const { |
| 108 | + return GetLiveCaptionEnabledLanguages(); |
| 109 | +} |
| 110 | + |
| 111 | +base::FilePath BraveSodaInstaller::GetSodaBinaryPath() const { |
| 112 | + return base::FilePath(); |
| 113 | +} |
| 114 | + |
| 115 | +base::FilePath BraveSodaInstaller::GetLanguagePath( |
| 116 | + std::string_view language) const { |
| 117 | + return base::FilePath(); |
| 118 | +} |
| 119 | + |
| 120 | +void BraveSodaInstaller::OnSpeechModelInstalledChanged(bool installed) { |
| 121 | + if (!installed) { |
| 122 | + soda_binary_installed_ = false; |
| 123 | + installed_languages_.erase(LanguageCode::kEnUs); |
| 124 | + return; |
| 125 | + } |
| 126 | + |
| 127 | + // Brave's model covers what upstream splits into a binary and a language |
| 128 | + // pack, so both flip together. |
| 129 | + soda_binary_installed_ = true; |
| 130 | + installed_languages_.insert(LanguageCode::kEnUs); |
| 131 | + NotifyOnSodaInstalled(LanguageCode::kEnUs); |
| 132 | +} |
| 133 | + |
| 134 | +} // namespace speech |
0 commit comments