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 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
|
// 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.
#include "chrome/browser/ui/views/side_panel/read_anything/read_anything_service.h"
#include "base/check_is_test.h"
#include "chrome/browser/accessibility/embedded_a11y_extension_loader.h"
#include "chrome/browser/extensions/extension_service.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_list.h"
#include "chrome/browser/ui/browser_window/public/browser_window_features.h"
#include "chrome/browser/ui/views/side_panel/read_anything/read_anything_service_factory.h"
#include "chrome/browser/ui/views/side_panel/side_panel_entry_id.h"
#include "chrome/browser/ui/views/side_panel/side_panel_ui.h"
#include "chrome/common/extensions/extension_constants.h"
#include "chrome/grit/browser_resources.h"
#include "extensions/browser/extension_system.h"
#include "ui/accessibility/accessibility_features.h"
#if !BUILDFLAG(IS_CHROMEOS)
#include "chrome/browser/component_updater/wasm_tts_engine_component_installer.h"
#include "chrome/browser/extensions/component_loader.h"
#endif // !BUILDFLAG(IS_CHROMEOS)
namespace {
// The number of seconds to wait before removing the extension. This avoids
// removing an extension only to add it back immediately.
constexpr int kRemoveExtensionDelaySeconds = 30;
} // namespace
#if !BUILDFLAG(IS_CHROMEOS)
const base::FilePath::CharType kManifestFileName[] =
FILE_PATH_LITERAL("wasm_tts_manifest.json");
#endif // !BUILDFLAG(IS_CHROMEOS)
ReadAnythingService::ReadAnythingService(Profile* profile) : profile_(profile) {
if (features::IsReadAnythingDocsIntegrationEnabled()) {
EmbeddedA11yExtensionLoader::GetInstance()->Init();
// The extension may still be installed from a previous session. Queue the
// timer to uninstall it.
// TODO(https://crbug.com/362787711): This logic also needs to run if the
// feature is disabled.
local_side_panel_switch_delay_timer_.Start(
FROM_HERE, base::Seconds(kRemoveExtensionDelaySeconds),
base::BindRepeating(
&ReadAnythingService::OnLocalSidePanelSwitchDelayTimeout,
weak_ptr_factory_.GetWeakPtr()));
}
if (features::IsDataCollectionModeForScreen2xEnabled()) {
browser_list_observer_.Observe(BrowserList::GetInstance());
}
}
// The service is shutting down which means the profile is destroying, at which
// point we should not be re-entrantly trying to modify the profile by removing
// the extension. Instead remove the extension at startup.
ReadAnythingService::~ReadAnythingService() = default;
// static
ReadAnythingService* ReadAnythingService::Get(Profile* profile) {
return ReadAnythingServiceFactory::GetInstance()->GetForBrowserContext(
profile);
}
void ReadAnythingService::OnReadAnythingSidePanelEntryShown() {
// The TTS download extension should only be installed on non-ChromeOS devices
// when the Read Aloud flag is enabled.
#if !BUILDFLAG(IS_CHROMEOS)
SetupDesktopEngine();
#endif // !BUILDFLAG(IS_CHROMEOS)
if (!features::IsReadAnythingDocsIntegrationEnabled()) {
return;
}
active_local_side_panel_count_++;
InstallGDocsHelperExtension();
}
#if !BUILDFLAG(IS_CHROMEOS)
void ReadAnythingService::SetupDesktopEngine() {
// If the WasmTtsComponentUpdater flag is disabled, install the Tts
// extension as a component extension.
if (features::IsReadAnythingReadAloudEnabled() &&
!features::IsWasmTtsComponentUpdaterEnabled() &&
!features::IsWasmTtsEngineAutoInstallDisabled()) {
InstallTtsDownloadExtension();
return;
}
// If the extension was previously installed but now the Read Aloud flag
// is disabled, or if the component updater flag is enabled, we should
// uninstall the component extension.
RemoveTtsDownloadExtension();
// Install the TTS extension via the component updater if the
// component updater flag is enabled.
if (features::IsReadAnythingReadAloudEnabled() &&
features::IsWasmTtsComponentUpdaterEnabled() &&
!features::IsWasmTtsEngineAutoInstallDisabled()) {
// Signal that the reading mode panel is opened and it's now safe to
// install the WasmTtsEngineComponent.
component_updater::WasmTtsEngineComponentInstallerPolicy::
GetWasmTTSEngineDirectory(base::BindOnce(InstallComponent));
}
}
#endif // !BUILDFLAG(IS_CHROMEOS)
void ReadAnythingService::OnReadAnythingSidePanelEntryHidden() {
if (!features::IsReadAnythingDocsIntegrationEnabled()) {
return;
}
active_local_side_panel_count_--;
local_side_panel_switch_delay_timer_.Reset();
}
void ReadAnythingService::InstallGDocsHelperExtension() {
#if BUILDFLAG(IS_CHROMEOS)
EmbeddedA11yExtensionLoader::GetInstance()->InstallExtensionWithId(
extension_misc::kReadingModeGDocsHelperExtensionId,
extension_misc::kReadingModeGDocsHelperExtensionPath,
extension_misc::kReadingModeGDocsHelperManifestFilename,
/*should_localize=*/false);
#else
auto* component_loader = extensions::ComponentLoader::Get(profile_);
if (!component_loader) {
// In tests, the loader might not be created.
CHECK_IS_TEST();
return;
}
if (!component_loader->Exists(
extension_misc::kReadingModeGDocsHelperExtensionId)) {
component_loader->Add(
IDR_READING_MODE_GDOCS_HELPER_MANIFEST,
base::FilePath(FILE_PATH_LITERAL("reading_mode_gdocs_helper")));
}
#endif // BUILDFLAG(IS_CHROMEOS)
}
void ReadAnythingService::RemoveGDocsHelperExtension() {
#if BUILDFLAG(IS_CHROMEOS)
EmbeddedA11yExtensionLoader::GetInstance()->RemoveExtensionWithId(
extension_misc::kReadingModeGDocsHelperExtensionId);
#else
auto* component_loader = extensions::ComponentLoader::Get(profile_);
if (!component_loader) {
// In tests, the loader might not be created.
CHECK_IS_TEST();
return;
}
component_loader->Remove(extension_misc::kReadingModeGDocsHelperExtensionId);
#endif // BUILDFLAG(IS_CHROMEOS)
}
void ReadAnythingService::OnLocalSidePanelSwitchDelayTimeout() {
if (active_local_side_panel_count_ > 0) {
return;
}
RemoveGDocsHelperExtension();
}
void ReadAnythingService::OnBrowserSetLastActive(Browser* browser) {
if (!features::IsDataCollectionModeForScreen2xEnabled() ||
browser->profile() != profile_) {
return;
}
// This code is called as part of a screen2x data generation workflow, where
// the browser is opened by a CLI and the read-anything side panel is
// automatically opened. Therefore we force the UI to show right away, as in
// tests.
// TODO(https://crbug.com/358191922): Remove this code.
auto* side_panel_ui = browser->GetFeatures().side_panel_ui();
if (side_panel_ui->GetCurrentEntryId() != SidePanelEntryId::kReadAnything) {
side_panel_ui->SetNoDelaysForTesting(true); // IN-TEST
side_panel_ui->Show(SidePanelEntryId::kReadAnything);
}
}
void ReadAnythingService::InstallTtsDownloadExtension() {
#if !BUILDFLAG(IS_CHROMEOS)
auto* component_loader = extensions::ComponentLoader::Get(profile_);
if (!component_loader) {
// In tests, the loader might not be created.
CHECK_IS_TEST();
return;
}
if (!component_loader->Exists(extension_misc::kTTSEngineExtensionId)) {
component_loader->Add(IDR_TTS_ENGINE_MANIFEST,
base::FilePath(FILE_PATH_LITERAL("tts_engine")));
}
#endif // BUILDFLAG(!IS_CHROMEOS)
}
void ReadAnythingService::RemoveTtsDownloadExtension() {
#if !BUILDFLAG(IS_CHROMEOS)
auto* component_loader = extensions::ComponentLoader::Get(profile_);
if (!component_loader) {
// In tests, the service might not be created.
CHECK_IS_TEST();
return;
}
component_loader->Remove(extension_misc::kTTSEngineExtensionId);
#endif // !BUILDFLAG(IS_CHROMEOS)
}
#if !BUILDFLAG(IS_CHROMEOS)
void ReadAnythingService::InstallComponent(const base::FilePath& new_dir) {
EmbeddedA11yExtensionLoader::GetInstance()->Init();
EmbeddedA11yExtensionLoader::GetInstance()->InstallExtensionWithIdAndPath(
extension_misc::kComponentUpdaterTTSEngineExtensionId, new_dir,
kManifestFileName,
/*should_localize=*/false);
}
#endif // !BUILDFLAG(IS_CHROMEOS)
|