Avi Drissman | 8ba1bad | 2022-09-13 19:22:36 | [diff] [blame] | 1 | // Copyright 2022 The Chromium Authors |
Ramin Halavati | 26dcce2 | 2022-02-23 13:11:14 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Abigail Klein | 7a63c57 | 2024-02-28 20:45:09 | [diff] [blame] | 5 | #include "services/screen_ai/screen_ai_service_impl.h" |
Ramin Halavati | 26dcce2 | 2022-02-23 13:11:14 | [diff] [blame] | 6 | |
Ramin Halavati | 2667959 | 2025-03-13 07:24:57 | [diff] [blame] | 7 | #include <algorithm> |
Ramin Halavati | 55fdf4d | 2022-11-07 06:35:18 | [diff] [blame] | 8 | #include <memory> |
Ramin Halavati | 63d9c25 | 2024-07-29 06:59:17 | [diff] [blame] | 9 | #include <optional> |
Ramin Halavati | fd712d8e | 2022-10-19 05:11:27 | [diff] [blame] | 10 | #include <utility> |
Ramin Halavati | b044504d | 2022-10-24 06:36:34 | [diff] [blame] | 11 | #include <vector> |
Ramin Halavati | fd712d8e | 2022-10-19 05:11:27 | [diff] [blame] | 12 | |
| 13 | #include "base/check.h" |
Ramin Halavati | ad969ef | 2024-05-21 06:59:47 | [diff] [blame] | 14 | #include "base/check_is_test.h" |
Tom Sepez | e9b4dd55 | 2024-08-14 22:44:29 | [diff] [blame] | 15 | #include "base/compiler_specific.h" |
Ramin Halavati | 65f96fb | 2025-02-24 16:45:18 | [diff] [blame] | 16 | #include "base/cpu.h" |
Ramin Halavati | 55fdf4d | 2022-11-07 06:35:18 | [diff] [blame] | 17 | #include "base/functional/bind.h" |
| 18 | #include "base/location.h" |
Ramin Halavati | fafff15 | 2022-12-05 09:39:38 | [diff] [blame] | 19 | #include "base/logging.h" |
Kyungjun Lee | 9d755a0 | 2022-11-08 17:57:11 | [diff] [blame] | 20 | #include "base/metrics/histogram_functions.h" |
Ramin Halavati | da3ec44 | 2025-05-06 04:14:03 | [diff] [blame] | 21 | #include "base/numerics/safe_conversions.h" |
Ramin Halavati | 5ff855c | 2022-04-27 16:30:48 | [diff] [blame] | 22 | #include "base/process/process.h" |
Ramin Halavati | 65f96fb | 2025-02-24 16:45:18 | [diff] [blame] | 23 | #include "base/strings/stringprintf.h" |
Ramin Halavati | 2667959 | 2025-03-13 07:24:57 | [diff] [blame] | 24 | #include "base/system/sys_info.h" |
Sean Maher | 5b9af51f | 2022-11-21 15:32:47 | [diff] [blame] | 25 | #include "base/task/single_thread_task_runner.h" |
Ramin Halavati | 55fdf4d | 2022-11-07 06:35:18 | [diff] [blame] | 26 | #include "base/task/thread_pool.h" |
Ramin Halavati | aa67ee5 | 2025-04-25 05:15:53 | [diff] [blame] | 27 | #include "base/threading/sequence_bound.h" |
Ramin Halavati | 65f96fb | 2025-02-24 16:45:18 | [diff] [blame] | 28 | #include "components/crash/core/common/crash_key.h" |
Abigail Klein | 7a63c57 | 2024-02-28 20:45:09 | [diff] [blame] | 29 | #include "services/screen_ai/buildflags/buildflags.h" |
Ramin Halavati | 4c47eb8 | 2024-12-03 19:06:42 | [diff] [blame] | 30 | #include "services/screen_ai/proto/chrome_screen_ai.pb.h" |
Abigail Klein | 7a63c57 | 2024-02-28 20:45:09 | [diff] [blame] | 31 | #include "services/screen_ai/proto/main_content_extractor_proto_convertor.h" |
| 32 | #include "services/screen_ai/proto/visual_annotator_proto_convertor.h" |
Ramin Halavati | 4c47eb8 | 2024-12-03 19:06:42 | [diff] [blame] | 33 | #include "services/screen_ai/public/cpp/metrics.h" |
Abigail Klein | 7a63c57 | 2024-02-28 20:45:09 | [diff] [blame] | 34 | #include "services/screen_ai/public/cpp/utilities.h" |
Ramin Halavati | eddadb6 | 2022-05-04 17:29:49 | [diff] [blame] | 35 | #include "ui/accessibility/accessibility_features.h" |
Ramin Halavati | 4f904d0a | 2024-06-25 15:32:32 | [diff] [blame] | 36 | #include "ui/accessibility/ax_node.h" |
| 37 | #include "ui/accessibility/ax_tree.h" |
Ramin Halavati | 8423107 | 2022-08-17 08:02:20 | [diff] [blame] | 38 | #include "ui/accessibility/ax_tree_id.h" |
Nektarios Paisios | f73d697 | 2022-06-04 11:32:25 | [diff] [blame] | 39 | #include "ui/gfx/geometry/rect_f.h" |
Ramin Halavati | 367352a | 2022-04-14 06:00:23 | [diff] [blame] | 40 | |
Ramin Halavati | 361d0e4 | 2025-06-03 05:58:12 | [diff] [blame] | 41 | #if BUILDFLAG(IS_LINUX) |
| 42 | #include "partition_alloc/buildflags.h" |
| 43 | |
| 44 | #if PA_BUILDFLAG( \ |
| 45 | ENABLE_ALLOCATOR_SHIM_PARTITION_ALLOC_DISPATCH_WITH_ADVANCED_CHECKS_SUPPORT) |
| 46 | #include "base/allocator/partition_allocator/src/partition_alloc/shim/allocator_shim_default_dispatch_to_partition_alloc_with_advanced_checks.h" |
| 47 | #endif |
| 48 | #endif |
| 49 | |
Ramin Halavati | a9b50a10 | 2024-02-07 18:37:23 | [diff] [blame] | 50 | #if BUILDFLAG(USE_FAKE_SCREEN_AI) |
Abigail Klein | 7a63c57 | 2024-02-28 20:45:09 | [diff] [blame] | 51 | #include "services/screen_ai/screen_ai_library_wrapper_fake.h" |
Ramin Halavati | 767b8fc | 2024-02-02 06:23:08 | [diff] [blame] | 52 | #else |
Abigail Klein | 7a63c57 | 2024-02-28 20:45:09 | [diff] [blame] | 53 | #include "services/screen_ai/screen_ai_library_wrapper_impl.h" |
Ramin Halavati | 767b8fc | 2024-02-02 06:23:08 | [diff] [blame] | 54 | #endif |
| 55 | |
Ramin Halavati | 69bf775 | 2022-04-04 09:58:15 | [diff] [blame] | 56 | namespace screen_ai { |
Ramin Halavati | 26dcce2 | 2022-02-23 13:11:14 | [diff] [blame] | 57 | |
Ramin Halavati | 6d4fc2b | 2022-06-16 15:33:35 | [diff] [blame] | 58 | namespace { |
| 59 | |
Ramin Halavati | 2acf236 | 2024-08-02 14:22:53 | [diff] [blame] | 60 | // How often it would be checked that the service is idle and can be shutdown. |
Ramin Halavati | be45dde | 2025-05-01 04:19:10 | [diff] [blame] | 61 | // LINT.IfChange(kIdleCheckingDelay) |
Ramin Halavati | 1520b70 | 2025-04-23 15:24:18 | [diff] [blame] | 62 | constexpr base::TimeDelta kIdleCheckingDelay = base::Seconds(3); |
Ramin Halavati | be45dde | 2025-05-01 04:19:10 | [diff] [blame] | 63 | // LINT.ThenChange(//chrome/browser/screen_ai/optical_character_recognizer_browsertest.cc:kServiceIdleCheckingDelay) |
Ramin Halavati | 6ff5641 | 2024-08-13 07:15:54 | [diff] [blame] | 64 | |
Ramin Halavati | 4825419e | 2025-04-02 05:49:46 | [diff] [blame] | 65 | // How long to wait for a request to the library be responded, before assuming |
| 66 | // that the library is not responsive. |
Ramin Halavati | aa67ee5 | 2025-04-25 05:15:53 | [diff] [blame] | 67 | constexpr base::TimeDelta kMaxWaitForResponseTime = base::Seconds(10); |
Ramin Halavati | 4825419e | 2025-04-02 05:49:46 | [diff] [blame] | 68 | |
Ramin Halavati | ad969ef | 2024-05-21 06:59:47 | [diff] [blame] | 69 | // These values are persisted to logs. Entries should not be renumbered and |
| 70 | // numeric values should never be reused. |
Ramin Halavati | 29cc115 | 2024-10-27 17:30:28 | [diff] [blame] | 71 | // See `screen_ai_service.mojom` for more info. |
Ramin Halavati | b3fe2fe | 2025-03-20 16:53:03 | [diff] [blame] | 72 | // LINT.IfChange(OcrClientType) |
Ramin Halavati | ad969ef | 2024-05-21 06:59:47 | [diff] [blame] | 73 | enum class OcrClientTypeForMetrics { |
| 74 | kTest = 0, |
| 75 | kPdfViewer = 1, |
| 76 | kLocalSearch = 2, |
| 77 | kCameraApp = 3, |
Ramin Halavati | 29cc115 | 2024-10-27 17:30:28 | [diff] [blame] | 78 | kNotUsed = 4, // Can be used for a new client. |
Ramin Halavati | ad969ef | 2024-05-21 06:59:47 | [diff] [blame] | 79 | kMediaApp = 5, |
Michelle Chen | 5192d52 | 2024-10-22 23:38:22 | [diff] [blame] | 80 | kScreenshotTextDetection, |
| 81 | kMaxValue = kScreenshotTextDetection |
Ramin Halavati | ad969ef | 2024-05-21 06:59:47 | [diff] [blame] | 82 | }; |
Ramin Halavati | b3fe2fe | 2025-03-20 16:53:03 | [diff] [blame] | 83 | // LINT.ThenChange(//tools/metrics/histograms/metadata/accessibility/enums.xml:OcrClientType) |
| 84 | |
| 85 | // These values are persisted to logs. Entries should not be renumbered and |
| 86 | // numeric values should never be reused. |
| 87 | // See `screen_ai_service.mojom` for more info. |
| 88 | // LINT.IfChange(MainContentExtractionClientType) |
| 89 | enum class MainContentExtractionClientTypeForMetrics { |
| 90 | kTest = 0, |
| 91 | kReadingMode = 1, |
| 92 | kMainNode = 2, |
| 93 | kMahi = 3, |
| 94 | kMaxValue = kMahi |
| 95 | }; |
| 96 | // LINT.ThenChange(//tools/metrics/histograms/metadata/accessibility/enums.xml:MainContentExtractionClientType) |
Ramin Halavati | ad969ef | 2024-05-21 06:59:47 | [diff] [blame] | 97 | |
| 98 | OcrClientTypeForMetrics GetClientType(mojom::OcrClientType client_type) { |
| 99 | switch (client_type) { |
| 100 | case mojom::OcrClientType::kTest: |
| 101 | CHECK_IS_TEST(); |
| 102 | return OcrClientTypeForMetrics::kTest; |
| 103 | case mojom::OcrClientType::kPdfViewer: |
| 104 | return OcrClientTypeForMetrics::kPdfViewer; |
| 105 | case mojom::OcrClientType::kLocalSearch: |
| 106 | return OcrClientTypeForMetrics::kLocalSearch; |
| 107 | case mojom::OcrClientType::kCameraApp: |
| 108 | return OcrClientTypeForMetrics::kCameraApp; |
Ramin Halavati | ad969ef | 2024-05-21 06:59:47 | [diff] [blame] | 109 | case mojom::OcrClientType::kMediaApp: |
| 110 | return OcrClientTypeForMetrics::kMediaApp; |
Michelle Chen | 5192d52 | 2024-10-22 23:38:22 | [diff] [blame] | 111 | case mojom::OcrClientType::kScreenshotTextDetection: |
| 112 | return OcrClientTypeForMetrics::kScreenshotTextDetection; |
Ramin Halavati | ad969ef | 2024-05-21 06:59:47 | [diff] [blame] | 113 | } |
| 114 | } |
| 115 | |
Ramin Halavati | b3fe2fe | 2025-03-20 16:53:03 | [diff] [blame] | 116 | MainContentExtractionClientTypeForMetrics GetClientType( |
| 117 | mojom::MceClientType client_type) { |
| 118 | switch (client_type) { |
| 119 | case mojom::MceClientType::kTest: |
| 120 | CHECK_IS_TEST(); |
| 121 | return MainContentExtractionClientTypeForMetrics::kTest; |
| 122 | case mojom::MceClientType::kReadingMode: |
| 123 | return MainContentExtractionClientTypeForMetrics::kReadingMode; |
| 124 | case mojom::MceClientType::kMainNode: |
| 125 | return MainContentExtractionClientTypeForMetrics::kMainNode; |
| 126 | case mojom::MceClientType::kMahi: |
| 127 | return MainContentExtractionClientTypeForMetrics::kMahi; |
| 128 | } |
| 129 | } |
| 130 | |
Ramin Halavati | 87cfa8e6 | 2025-06-02 06:48:10 | [diff] [blame] | 131 | #if BUILDFLAG(IS_CHROMEOS) |
Ramin Halavati | 89a36c9 | 2023-05-05 15:01:03 | [diff] [blame] | 132 | ui::AXTreeUpdate ConvertVisualAnnotationToTreeUpdate( |
Ramin Halavati | dcb09a17 | 2024-07-15 08:28:55 | [diff] [blame] | 133 | std::optional<chrome_screen_ai::VisualAnnotation>& annotation_proto, |
Ramin Halavati | 89a36c9 | 2023-05-05 15:01:03 | [diff] [blame] | 134 | const gfx::Rect& image_rect) { |
| 135 | if (!annotation_proto) { |
| 136 | VLOG(0) << "Screen AI library could not process snapshot or no OCR data."; |
| 137 | return ui::AXTreeUpdate(); |
| 138 | } |
| 139 | |
| 140 | return VisualAnnotationToAXTreeUpdate(*annotation_proto, image_rect); |
| 141 | } |
Ramin Halavati | 87cfa8e6 | 2025-06-02 06:48:10 | [diff] [blame] | 142 | #endif // BUILDFLAG(IS_CHROMEOS) |
Ramin Halavati | 89a36c9 | 2023-05-05 15:01:03 | [diff] [blame] | 143 | |
Abigail Klein | 1867370 | 2024-03-05 20:59:06 | [diff] [blame] | 144 | ui::AXNodeID ComputeMainNode( |
| 145 | const ui::AXTree* tree, |
| 146 | const std::vector<ui::AXNodeID>& content_node_ids) { |
| 147 | ui::AXNode* front = tree->GetFromId(content_node_ids.front()); |
| 148 | ui::AXNode* back = tree->GetFromId(content_node_ids.back()); |
| 149 | ui::AXNode* main = front->GetLowestCommonAncestor(*back); |
| 150 | return main->id(); |
| 151 | } |
| 152 | |
Ramin Halavati | 65f96fb | 2025-02-24 16:45:18 | [diff] [blame] | 153 | #if !BUILDFLAG(USE_FAKE_SCREEN_AI) |
| 154 | void SetCPUInstructionSetCrashKey() { |
| 155 | #if defined(ARCH_CPU_X86_FAMILY) |
| 156 | base::CPU(); |
| 157 | // Report cpu micro architecture in case of crash. |
| 158 | static crash_reporter::CrashKeyString<3> cpu_info("intel_micro_architecture"); |
| 159 | cpu_info.Set( |
| 160 | base::StringPrintf("%i", base::CPU().GetIntelMicroArchitecture())); |
| 161 | #endif |
| 162 | } |
| 163 | #endif |
| 164 | |
Ramin Halavati | 2667959 | 2025-03-13 07:24:57 | [diff] [blame] | 165 | // Return a maximum 11 character string with the signature of available and |
| 166 | // total memory, both in MB and capped to 99999. |
| 167 | std::string GetMemoryStatusForCrashKey() { |
| 168 | int total_memory = base::SysInfo::AmountOfPhysicalMemoryMB(); |
| 169 | int available_memory = static_cast<int>( |
| 170 | base::SysInfo::AmountOfAvailablePhysicalMemory() / (1024 * 1024)); |
| 171 | |
| 172 | // Cap the number of digits for crash report. |
| 173 | total_memory = std::min(total_memory, 99999); |
| 174 | available_memory = std::min(available_memory, 99999); |
| 175 | return base::StringPrintf("%i,%i", available_memory, total_memory); |
| 176 | } |
| 177 | |
Ramin Halavati | aa67ee5 | 2025-04-25 05:15:53 | [diff] [blame] | 178 | class HangTimer : public base::OneShotTimer { |
| 179 | public: |
| 180 | explicit HangTimer(bool is_ocr) : is_ocr_(is_ocr) {} |
| 181 | |
| 182 | void StartTimer() { |
| 183 | Start(FROM_HERE, kMaxWaitForResponseTime, |
| 184 | base::BindOnce( |
| 185 | [](bool request_is_ocr) { |
| 186 | base::UmaHistogramBoolean( |
Ramin Halavati | b5db8847 | 2025-05-23 14:34:07 | [diff] [blame] | 187 | "Accessibility.ScreenAI.Service.NotResponsive.IsOCR", |
Ramin Halavati | aa67ee5 | 2025-04-25 05:15:53 | [diff] [blame] | 188 | request_is_ocr); |
| 189 | base::Process::TerminateCurrentProcessImmediately(0); |
| 190 | }, |
| 191 | is_ocr_)); |
| 192 | } |
| 193 | |
| 194 | private: |
| 195 | bool is_ocr_; |
| 196 | }; |
| 197 | |
Ramin Halavati | 26ae6b7 | 2022-11-10 06:45:05 | [diff] [blame] | 198 | } // namespace |
| 199 | |
Ramin Halavati | 39bab0d | 2024-01-30 06:05:01 | [diff] [blame] | 200 | // The library accepts simple pointers to model data retrieval functions, hence |
| 201 | // callback functions with linked object are not safe to pass. |
Ramin Halavati | 63d9c25 | 2024-07-29 06:59:17 | [diff] [blame] | 202 | // This global variable keeps the pointer the only instance of this class. |
| 203 | ModelDataHolder* g_model_data_holder_instance = nullptr; |
Ramin Halavati | 8ced639 | 2023-09-25 07:27:50 | [diff] [blame] | 204 | |
Ramin Halavati | 63d9c25 | 2024-07-29 06:59:17 | [diff] [blame] | 205 | // Keeps the handles of model files, and replies to calls for copying their |
| 206 | // content. |
| 207 | class ModelDataHolder { |
Ramin Halavati | 8ced639 | 2023-09-25 07:27:50 | [diff] [blame] | 208 | public: |
Ramin Halavati | 63d9c25 | 2024-07-29 06:59:17 | [diff] [blame] | 209 | ModelDataHolder() { |
| 210 | CHECK(!g_model_data_holder_instance); |
| 211 | g_model_data_holder_instance = this; |
| 212 | } |
Ramin Halavati | 8ced639 | 2023-09-25 07:27:50 | [diff] [blame] | 213 | |
Ramin Halavati | 63d9c25 | 2024-07-29 06:59:17 | [diff] [blame] | 214 | ModelDataHolder(const ModelDataHolder&) = delete; |
| 215 | ModelDataHolder& operator=(const ModelDataHolder&) = delete; |
| 216 | |
| 217 | ~ModelDataHolder() { |
| 218 | CHECK_EQ(g_model_data_holder_instance, this); |
| 219 | g_model_data_holder_instance = nullptr; |
Ramin Halavati | 8ced639 | 2023-09-25 07:27:50 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | // Returns 0 if file is not found. |
| 223 | static uint32_t GetDataSize(const char* relative_file_path) { |
Ramin Halavati | 63d9c25 | 2024-07-29 06:59:17 | [diff] [blame] | 224 | CHECK(g_model_data_holder_instance); |
| 225 | base::File* model_file = |
| 226 | g_model_data_holder_instance->GetModelFile(relative_file_path); |
| 227 | return model_file ? model_file->GetLength() : 0; |
Ramin Halavati | 8ced639 | 2023-09-25 07:27:50 | [diff] [blame] | 228 | } |
| 229 | |
Ramin Halavati | 63d9c25 | 2024-07-29 06:59:17 | [diff] [blame] | 230 | // Copies content of the file in `relative_file_path` to `buffer`. Expects |
| 231 | // that `buffer_size` would be enough for the entire file content. |
Ramin Halavati | 8ced639 | 2023-09-25 07:27:50 | [diff] [blame] | 232 | static void CopyData(const char* relative_file_path, |
| 233 | uint32_t buffer_size, |
| 234 | char* buffer) { |
Ramin Halavati | 63d9c25 | 2024-07-29 06:59:17 | [diff] [blame] | 235 | CHECK(g_model_data_holder_instance); |
| 236 | base::File* model_file = |
| 237 | g_model_data_holder_instance->GetModelFile(relative_file_path); |
| 238 | CHECK(model_file); |
| 239 | |
| 240 | int64_t length = model_file->GetLength(); |
| 241 | CHECK_GE(buffer_size, length); |
Tom Sepez | e9b4dd55 | 2024-08-14 22:44:29 | [diff] [blame] | 242 | CHECK_EQ(UNSAFE_TODO(model_file->Read(0, buffer, length)), length); |
Ramin Halavati | 8ced639 | 2023-09-25 07:27:50 | [diff] [blame] | 243 | } |
| 244 | |
Ramin Halavati | 63d9c25 | 2024-07-29 06:59:17 | [diff] [blame] | 245 | void AddModelFiles(base::flat_map<base::FilePath, base::File> model_files) { |
| 246 | for (auto& model_file : model_files) { |
| 247 | model_files_[model_file.first.MaybeAsASCII()] = |
| 248 | std::move(model_file.second); |
Ramin Halavati | 39bab0d | 2024-01-30 06:05:01 | [diff] [blame] | 249 | } |
| 250 | } |
Ramin Halavati | 8ced639 | 2023-09-25 07:27:50 | [diff] [blame] | 251 | |
Ramin Halavati | 63d9c25 | 2024-07-29 06:59:17 | [diff] [blame] | 252 | // Returns the file handle for `relative_file_path` if it exists. |
| 253 | base::File* GetModelFile(const char* relative_file_path) { |
| 254 | if (!base::Contains(model_files_, relative_file_path)) { |
| 255 | return nullptr; |
| 256 | } |
| 257 | return &model_files_[relative_file_path]; |
| 258 | } |
| 259 | |
Ramin Halavati | 8ced639 | 2023-09-25 07:27:50 | [diff] [blame] | 260 | private: |
Ramin Halavati | 63d9c25 | 2024-07-29 06:59:17 | [diff] [blame] | 261 | std::map<std::string, base::File> model_files_; |
Ramin Halavati | 8ced639 | 2023-09-25 07:27:50 | [diff] [blame] | 262 | }; |
| 263 | |
Ramin Halavati | 26ae6b7 | 2022-11-10 06:45:05 | [diff] [blame] | 264 | ScreenAIService::ScreenAIService( |
Ramin Halavati | c1e4fa9 | 2023-05-17 17:22:06 | [diff] [blame] | 265 | mojo::PendingReceiver<mojom::ScreenAIServiceFactory> receiver) |
| 266 | : factory_receiver_(this, std::move(receiver)), |
| 267 | ocr_receiver_(this), |
Ramin Halavati | ad969ef | 2024-05-21 06:59:47 | [diff] [blame] | 268 | main_content_extraction_receiver_(this) { |
Ramin Halavati | 361d0e4 | 2025-06-03 05:58:12 | [diff] [blame] | 269 | #if BUILDFLAG(IS_LINUX) && \ |
| 270 | PA_BUILDFLAG( \ |
| 271 | ENABLE_ALLOCATOR_SHIM_PARTITION_ALLOC_DISPATCH_WITH_ADVANCED_CHECKS_SUPPORT) |
| 272 | // TODO(crbug.com/418199684): Remove when the bug is fixed. |
| 273 | if (base::FeatureList::IsEnabled( |
| 274 | ::features::kScreenAIPartitionAllocAdvancedChecksEnabled)) { |
| 275 | allocator_shim::InstallCustomDispatchForPartitionAllocWithAdvancedChecks(); |
| 276 | } |
| 277 | #endif |
| 278 | |
Ramin Halavati | 6ff5641 | 2024-08-13 07:15:54 | [diff] [blame] | 279 | screen2x_main_content_extractors_.set_disconnect_handler( |
Ramin Halavati | 1520b70 | 2025-04-23 15:24:18 | [diff] [blame] | 280 | base::BindRepeating(&ScreenAIService::MceReceiverDisconnected, |
Ramin Halavati | 6ff5641 | 2024-08-13 07:15:54 | [diff] [blame] | 281 | weak_ptr_factory_.GetWeakPtr())); |
| 282 | screen_ai_annotators_.set_disconnect_handler( |
| 283 | base::BindRepeating(&ScreenAIService::OcrReceiverDisconnected, |
| 284 | weak_ptr_factory_.GetWeakPtr())); |
Ramin Halavati | 63d9c25 | 2024-07-29 06:59:17 | [diff] [blame] | 285 | model_data_holder_ = std::make_unique<ModelDataHolder>(); |
Ramin Halavati | aa67ee5 | 2025-04-25 05:15:53 | [diff] [blame] | 286 | |
| 287 | background_task_runner_ = base::ThreadPool::CreateSequencedTaskRunner( |
| 288 | {base::TaskPriority::BEST_EFFORT, |
| 289 | base::TaskShutdownBehavior::SKIP_ON_SHUTDOWN}); |
Ramin Halavati | ad969ef | 2024-05-21 06:59:47 | [diff] [blame] | 290 | } |
Ramin Halavati | 26ae6b7 | 2022-11-10 06:45:05 | [diff] [blame] | 291 | |
| 292 | ScreenAIService::~ScreenAIService() = default; |
| 293 | |
Ramin Halavati | 8ced639 | 2023-09-25 07:27:50 | [diff] [blame] | 294 | void ScreenAIService::LoadLibrary(const base::FilePath& library_path) { |
Abigail Klein | 7a63c57 | 2024-02-28 20:45:09 | [diff] [blame] | 295 | // The ScopedBlockingCall in LoadLibrary guarantees that this is not run on |
| 296 | // the UI thread. |
Ramin Halavati | a9b50a10 | 2024-02-07 18:37:23 | [diff] [blame] | 297 | #if BUILDFLAG(USE_FAKE_SCREEN_AI) |
Ramin Halavati | 767b8fc | 2024-02-02 06:23:08 | [diff] [blame] | 298 | library_ = std::make_unique<ScreenAILibraryWrapperFake>(); |
| 299 | #else |
| 300 | library_ = std::make_unique<ScreenAILibraryWrapperImpl>(); |
Ramin Halavati | 65f96fb | 2025-02-24 16:45:18 | [diff] [blame] | 301 | |
| 302 | // TODO(crbug.com/381256355): Remove when the library is SSE3 compatible. |
| 303 | SetCPUInstructionSetCrashKey(); |
Ramin Halavati | 767b8fc | 2024-02-02 06:23:08 | [diff] [blame] | 304 | #endif |
Ramin Halavati | 8ced639 | 2023-09-25 07:27:50 | [diff] [blame] | 305 | |
| 306 | bool load_sucessful = library_->Load(library_path); |
| 307 | base::UmaHistogramBoolean("Accessibility.ScreenAI.Library.Initialized", |
| 308 | load_sucessful); |
| 309 | |
| 310 | if (!load_sucessful) { |
| 311 | library_.reset(); |
| 312 | return; |
| 313 | } |
| 314 | |
| 315 | uint32_t version_major; |
| 316 | uint32_t version_minor; |
| 317 | library_->GetLibraryVersion(version_major, version_minor); |
| 318 | VLOG(2) << "Screen AI library version: " << version_major << "." |
| 319 | << version_minor; |
| 320 | |
Georg Neis | 06e38777 | 2024-12-25 07:32:22 | [diff] [blame] | 321 | #if BUILDFLAG(IS_CHROMEOS) |
Ramin Halavati | 8ced639 | 2023-09-25 07:27:50 | [diff] [blame] | 322 | library_->SetLogger(); |
| 323 | #endif |
| 324 | |
| 325 | if (features::IsScreenAIDebugModeEnabled()) { |
| 326 | library_->EnableDebugMode(); |
| 327 | } |
| 328 | |
Ramin Halavati | 63d9c25 | 2024-07-29 06:59:17 | [diff] [blame] | 329 | library_->SetFileContentFunctions(&ModelDataHolder::GetDataSize, |
| 330 | &ModelDataHolder::CopyData); |
Ramin Halavati | 8ced639 | 2023-09-25 07:27:50 | [diff] [blame] | 331 | } |
| 332 | |
Ramin Halavati | c1e4fa9 | 2023-05-17 17:22:06 | [diff] [blame] | 333 | void ScreenAIService::InitializeMainContentExtraction( |
Ramin Halavati | d139a0f | 2023-02-28 13:25:28 | [diff] [blame] | 334 | const base::FilePath& library_path, |
Ramin Halavati | fbc6202 | 2023-11-10 11:05:07 | [diff] [blame] | 335 | base::flat_map<base::FilePath, base::File> model_files, |
Ramin Halavati | c1e4fa9 | 2023-05-17 17:22:06 | [diff] [blame] | 336 | mojo::PendingReceiver<mojom::MainContentExtractionService> |
| 337 | main_content_extractor_service_receiver, |
| 338 | InitializeMainContentExtractionCallback callback) { |
| 339 | if (!library_) { |
Ramin Halavati | 8ced639 | 2023-09-25 07:27:50 | [diff] [blame] | 340 | LoadLibrary(library_path); |
Ramin Halavati | c1e4fa9 | 2023-05-17 17:22:06 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | if (!library_) { |
| 344 | std::move(callback).Run(false); |
| 345 | base::Process::TerminateCurrentProcessImmediately(-1); |
| 346 | } |
| 347 | |
Ramin Halavati | 63d9c25 | 2024-07-29 06:59:17 | [diff] [blame] | 348 | model_data_holder_->AddModelFiles(std::move(model_files)); |
Ramin Halavati | b044504d | 2022-10-24 06:36:34 | [diff] [blame] | 349 | |
Ramin Halavati | 8ced639 | 2023-09-25 07:27:50 | [diff] [blame] | 350 | bool init_successful = library_->InitMainContentExtraction(); |
Ramin Halavati | 1d57c2d | 2023-05-24 05:18:24 | [diff] [blame] | 351 | base::UmaHistogramBoolean( |
| 352 | "Accessibility.ScreenAI.MainContentExtraction.Initialized", |
| 353 | init_successful); |
| 354 | if (!init_successful) { |
Ramin Halavati | c1e4fa9 | 2023-05-17 17:22:06 | [diff] [blame] | 355 | std::move(callback).Run(false); |
Ramin Halavati | 89a36c9 | 2023-05-05 15:01:03 | [diff] [blame] | 356 | return; |
Ramin Halavati | d139a0f | 2023-02-28 13:25:28 | [diff] [blame] | 357 | } |
Ramin Halavati | 89a36c9 | 2023-05-05 15:01:03 | [diff] [blame] | 358 | |
Ramin Halavati | c1e4fa9 | 2023-05-17 17:22:06 | [diff] [blame] | 359 | // This interface should be created only once. |
| 360 | CHECK(!main_content_extraction_receiver_.is_bound()); |
| 361 | |
| 362 | main_content_extraction_receiver_.Bind( |
| 363 | std::move(main_content_extractor_service_receiver)); |
| 364 | |
| 365 | std::move(callback).Run(true); |
Ramin Halavati | 1520b70 | 2025-04-23 15:24:18 | [diff] [blame] | 366 | mce_last_used_ = base::TimeTicks::Now(); |
Ramin Halavati | cf15501 | 2025-05-05 15:50:01 | [diff] [blame] | 367 | StartShutDownOnIdleTimer(); |
Ramin Halavati | c1e4fa9 | 2023-05-17 17:22:06 | [diff] [blame] | 368 | } |
| 369 | |
| 370 | void ScreenAIService::InitializeOCR( |
| 371 | const base::FilePath& library_path, |
Ramin Halavati | fbc6202 | 2023-11-10 11:05:07 | [diff] [blame] | 372 | base::flat_map<base::FilePath, base::File> model_files, |
Ramin Halavati | c1e4fa9 | 2023-05-17 17:22:06 | [diff] [blame] | 373 | mojo::PendingReceiver<mojom::OCRService> ocr_service_receiver, |
| 374 | InitializeOCRCallback callback) { |
Ramin Halavati | 2667959 | 2025-03-13 07:24:57 | [diff] [blame] | 375 | static crash_reporter::CrashKeyString<12> memory_ocr_init( |
| 376 | "screen_ai_mem_ocr_init"); |
| 377 | memory_ocr_init.Set(GetMemoryStatusForCrashKey()); |
Ramin Halavati | c1e4fa9 | 2023-05-17 17:22:06 | [diff] [blame] | 378 | if (!library_) { |
Ramin Halavati | 8ced639 | 2023-09-25 07:27:50 | [diff] [blame] | 379 | LoadLibrary(library_path); |
Ramin Halavati | c1e4fa9 | 2023-05-17 17:22:06 | [diff] [blame] | 380 | } |
| 381 | |
| 382 | if (!library_) { |
| 383 | std::move(callback).Run(false); |
| 384 | base::Process::TerminateCurrentProcessImmediately(-1); |
| 385 | } |
| 386 | |
Ramin Halavati | 63d9c25 | 2024-07-29 06:59:17 | [diff] [blame] | 387 | model_data_holder_->AddModelFiles(std::move(model_files)); |
| 388 | |
Ramin Halavati | 0e2ae574 | 2023-11-10 05:57:54 | [diff] [blame] | 389 | bool init_successful = library_->InitOCR(); |
Ramin Halavati | 35d129f | 2023-06-22 16:37:09 | [diff] [blame] | 390 | base::UmaHistogramBoolean("Accessibility.ScreenAI.OCR.Initialized", |
Ramin Halavati | 1d57c2d | 2023-05-24 05:18:24 | [diff] [blame] | 391 | init_successful); |
Ramin Halavati | 48058433 | 2023-08-02 06:38:40 | [diff] [blame] | 392 | |
Ramin Halavati | 1d57c2d | 2023-05-24 05:18:24 | [diff] [blame] | 393 | if (!init_successful) { |
Ramin Halavati | c1e4fa9 | 2023-05-17 17:22:06 | [diff] [blame] | 394 | std::move(callback).Run(false); |
| 395 | return; |
| 396 | } |
| 397 | |
Ramin Halavati | da3ec44 | 2025-05-06 04:14:03 | [diff] [blame] | 398 | max_ocr_dimension_ = library_->GetMaxImageDimension(); |
| 399 | CHECK(max_ocr_dimension_); |
| 400 | |
Ramin Halavati | c1e4fa9 | 2023-05-17 17:22:06 | [diff] [blame] | 401 | // This interface should be created only once. |
| 402 | CHECK(!ocr_receiver_.is_bound()); |
| 403 | |
| 404 | ocr_receiver_.Bind(std::move(ocr_service_receiver)); |
| 405 | |
| 406 | std::move(callback).Run(true); |
Ramin Halavati | 2acf236 | 2024-08-02 14:22:53 | [diff] [blame] | 407 | ocr_last_used_ = base::TimeTicks::Now(); |
Ramin Halavati | cf15501 | 2025-05-05 15:50:01 | [diff] [blame] | 408 | StartShutDownOnIdleTimer(); |
Ramin Halavati | b044504d | 2022-10-24 06:36:34 | [diff] [blame] | 409 | } |
Ramin Halavati | 26dcce2 | 2022-02-23 13:11:14 | [diff] [blame] | 410 | |
Ramin Halavati | f26d7d4 | 2025-03-12 16:04:46 | [diff] [blame] | 411 | void ScreenAIService::BindShutdownHandler( |
| 412 | mojo::PendingRemote<mojom::ScreenAIServiceShutdownHandler> |
| 413 | shutdown_handler) { |
| 414 | DCHECK(!screen_ai_shutdown_handler_.is_bound()); |
| 415 | screen_ai_shutdown_handler_.Bind(std::move(shutdown_handler)); |
| 416 | } |
| 417 | |
Ramin Halavati | 26dcce2 | 2022-02-23 13:11:14 | [diff] [blame] | 418 | void ScreenAIService::BindAnnotator( |
| 419 | mojo::PendingReceiver<mojom::ScreenAIAnnotator> annotator) { |
| 420 | screen_ai_annotators_.Add(this, std::move(annotator)); |
| 421 | } |
| 422 | |
Ramin Halavati | eddadb6 | 2022-05-04 17:29:49 | [diff] [blame] | 423 | void ScreenAIService::BindMainContentExtractor( |
| 424 | mojo::PendingReceiver<mojom::Screen2xMainContentExtractor> |
| 425 | main_content_extractor) { |
Ramin Halavati | 6ff5641 | 2024-08-13 07:15:54 | [diff] [blame] | 426 | screen2x_main_content_extractors_.Add(this, |
| 427 | std::move(main_content_extractor)); |
Ramin Halavati | eddadb6 | 2022-05-04 17:29:49 | [diff] [blame] | 428 | } |
| 429 | |
Arthur Sonzogni | c571efb | 2024-01-26 20:26:18 | [diff] [blame] | 430 | std::optional<chrome_screen_ai::VisualAnnotation> |
Ramin Halavati | 7716b03 | 2024-08-19 14:56:20 | [diff] [blame] | 431 | ScreenAIService::PerformOcrAndRecordMetrics(const SkBitmap& image) { |
Ramin Halavati | 2667959 | 2025-03-13 07:24:57 | [diff] [blame] | 432 | static crash_reporter::CrashKeyString<12> memory_perform_ocr( |
| 433 | "screen_ai_mem_ocr_perform"); |
| 434 | memory_perform_ocr.Set(GetMemoryStatusForCrashKey()); |
| 435 | |
Ramin Halavati | 7716b03 | 2024-08-19 14:56:20 | [diff] [blame] | 436 | CHECK(base::Contains(ocr_client_types_, |
| 437 | screen_ai_annotators_.current_receiver())); |
Ramin Halavati | 4805c88 | 2025-03-03 18:28:48 | [diff] [blame] | 438 | OcrClientTypeForMetrics client_type = GetClientType( |
| 439 | ocr_client_types_.find(screen_ai_annotators_.current_receiver())->second); |
Ramin Halavati | ad969ef | 2024-05-21 06:59:47 | [diff] [blame] | 440 | base::UmaHistogramEnumeration("Accessibility.ScreenAI.OCR.ClientType", |
Ramin Halavati | 4805c88 | 2025-03-03 18:28:48 | [diff] [blame] | 441 | client_type); |
Ramin Halavati | ad969ef | 2024-05-21 06:59:47 | [diff] [blame] | 442 | |
Ramin Halavati | f4b48e0 | 2025-07-02 12:49:48 | [diff] [blame] | 443 | bool light_client = base::Contains(light_ocr_clients_, |
| 444 | screen_ai_annotators_.current_receiver()); |
| 445 | if (light_client != last_ocr_light_) { |
| 446 | library_->SetOCRLightMode(light_client); |
| 447 | last_ocr_light_ = light_client; |
| 448 | ocr_mode_switch_count_++; |
| 449 | } |
| 450 | |
Ramin Halavati | ac1ee292 | 2025-05-21 17:17:17 | [diff] [blame] | 451 | base::TimeTicks start_time = base::TimeTicks::Now(); |
Ramin Halavati | aa67ee5 | 2025-04-25 05:15:53 | [diff] [blame] | 452 | base::SequenceBound<HangTimer> hang_timer(background_task_runner_, |
| 453 | /*is_ocr=*/true); |
| 454 | hang_timer.AsyncCall(&HangTimer::StartTimer); |
Ramin Halavati | 7252cd0 | 2023-05-10 07:21:33 | [diff] [blame] | 455 | auto result = library_->PerformOcr(image); |
Ramin Halavati | aa67ee5 | 2025-04-25 05:15:53 | [diff] [blame] | 456 | hang_timer.AsyncCall(&base::OneShotTimer::Stop); |
Ramin Halavati | ac1ee292 | 2025-05-21 17:17:17 | [diff] [blame] | 457 | base::TimeDelta elapsed_time = base::TimeTicks::Now() - start_time; |
Ramin Halavati | 4825419e | 2025-04-02 05:49:46 | [diff] [blame] | 458 | |
Ramin Halavati | 2650ebe | 2023-11-27 19:56:35 | [diff] [blame] | 459 | int lines_count = result ? result->lines_size() : 0; |
| 460 | VLOG(1) << "OCR returned " << lines_count << " lines in " << elapsed_time; |
Ramin Halavati | 7252cd0 | 2023-05-10 07:21:33 | [diff] [blame] | 461 | |
Ramin Halavati | 18aba87 | 2024-08-23 14:33:05 | [diff] [blame] | 462 | if (!result) { |
| 463 | base::UmaHistogramEnumeration( |
Ramin Halavati | 4805c88 | 2025-03-03 18:28:48 | [diff] [blame] | 464 | "Accessibility.ScreenAI.OCR.Failed.ClientType", client_type); |
Ramin Halavati | 18aba87 | 2024-08-23 14:33:05 | [diff] [blame] | 465 | } |
Ramin Halavati | 49c2b5c | 2025-04-25 05:43:01 | [diff] [blame] | 466 | |
Ramin Halavati | da3ec44 | 2025-05-06 04:14:03 | [diff] [blame] | 467 | int max_dimension = base::checked_cast<int>(max_ocr_dimension_); |
| 468 | if (image.width() > max_dimension || image.height() > max_dimension) { |
Ramin Halavati | 4805c88 | 2025-03-03 18:28:48 | [diff] [blame] | 469 | base::UmaHistogramEnumeration( |
Ramin Halavati | 49c2b5c | 2025-04-25 05:43:01 | [diff] [blame] | 470 | "Accessibility.ScreenAI.OCR.Downsampled.ClientType", client_type); |
Ramin Halavati | 4805c88 | 2025-03-03 18:28:48 | [diff] [blame] | 471 | } |
| 472 | |
Ramin Halavati | 81b6d92 | 2024-06-07 14:20:34 | [diff] [blame] | 473 | base::UmaHistogramBoolean("Accessibility.ScreenAI.OCR.Successful", |
| 474 | result.has_value()); |
Ramin Halavati | 2650ebe | 2023-11-27 19:56:35 | [diff] [blame] | 475 | base::UmaHistogramCounts100("Accessibility.ScreenAI.OCR.LinesCount", |
| 476 | lines_count); |
Ramin Halavati | da3ec44 | 2025-05-06 04:14:03 | [diff] [blame] | 477 | if (image.width() < max_dimension && image.height() < max_dimension) { |
Ramin Halavati | 49c2b5c | 2025-04-25 05:43:01 | [diff] [blame] | 478 | base::UmaHistogramTimes("Accessibility.ScreenAI.OCR.Latency.NotDownsampled", |
Ramin Halavati | 3ae33ca | 2024-01-01 12:58:39 | [diff] [blame] | 479 | elapsed_time); |
| 480 | } else { |
Ramin Halavati | 49c2b5c | 2025-04-25 05:43:01 | [diff] [blame] | 481 | base::UmaHistogramTimes("Accessibility.ScreenAI.OCR.Latency.Downsampled", |
Ramin Halavati | 3ae33ca | 2024-01-01 12:58:39 | [diff] [blame] | 482 | elapsed_time); |
| 483 | } |
Ramin Halavati | 2650ebe | 2023-11-27 19:56:35 | [diff] [blame] | 484 | |
Ramin Halavati | 7716b03 | 2024-08-19 14:56:20 | [diff] [blame] | 485 | // MediaApp provides OCR for ChromeOS PDF viewer. |
Ramin Halavati | 4805c88 | 2025-03-03 18:28:48 | [diff] [blame] | 486 | if (client_type == OcrClientTypeForMetrics::kPdfViewer || |
| 487 | client_type == OcrClientTypeForMetrics::kMediaApp) { |
Ramin Halavati | 2650ebe | 2023-11-27 19:56:35 | [diff] [blame] | 488 | base::UmaHistogramCounts100("Accessibility.ScreenAI.OCR.LinesCount.PDF", |
| 489 | lines_count); |
| 490 | base::UmaHistogramTimes("Accessibility.ScreenAI.OCR.Time.PDF", |
| 491 | elapsed_time); |
Ramin Halavati | 696b8d5c | 2025-02-28 16:41:58 | [diff] [blame] | 492 | base::UmaHistogramCounts10M( |
| 493 | lines_count ? "Accessibility.ScreenAI.OCR.ImageSize.PDF.WithText" |
| 494 | : "Accessibility.ScreenAI.OCR.ImageSize.PDF.NoText", |
Ramin Halavati | 49c2b5c | 2025-04-25 05:43:01 | [diff] [blame] | 495 | image.width() * image.height()); |
Ramin Halavati | 4c47eb8 | 2024-12-03 19:06:42 | [diff] [blame] | 496 | |
Ramin Halavati | 01267d1 | 2025-01-21 18:11:30 | [diff] [blame] | 497 | if (result.has_value()) { |
| 498 | std::optional<uint64_t> most_detected_language = |
| 499 | GetMostDetectedLanguageInOcrData(*result); |
| 500 | if (most_detected_language.has_value()) { |
| 501 | base::UmaHistogramSparse( |
| 502 | "Accessibility.ScreenAI.OCR.MostDetectedLanguage.PDF", |
| 503 | most_detected_language.value()); |
| 504 | } |
Ramin Halavati | 4c47eb8 | 2024-12-03 19:06:42 | [diff] [blame] | 505 | } |
Ramin Halavati | 2650ebe | 2023-11-27 19:56:35 | [diff] [blame] | 506 | } |
| 507 | |
Ramin Halavati | ac1ee292 | 2025-05-21 17:17:17 | [diff] [blame] | 508 | ocr_last_used_ = base::TimeTicks::Now(); |
Ramin Halavati | 7252cd0 | 2023-05-10 07:21:33 | [diff] [blame] | 509 | return result; |
| 510 | } |
| 511 | |
Ramin Halavati | ad969ef | 2024-05-21 06:59:47 | [diff] [blame] | 512 | void ScreenAIService::SetClientType(mojom::OcrClientType client_type) { |
| 513 | ocr_client_types_[screen_ai_annotators_.current_receiver()] = client_type; |
| 514 | } |
| 515 | |
Ramin Halavati | d0d6231b | 2025-03-20 05:36:43 | [diff] [blame] | 516 | void ScreenAIService::SetClientType(mojom::MceClientType client_type) { |
| 517 | mce_client_types_[screen2x_main_content_extractors_.current_receiver()] = |
| 518 | client_type; |
| 519 | } |
| 520 | |
Ramin Halavati | 1520b70 | 2025-04-23 15:24:18 | [diff] [blame] | 521 | void ScreenAIService::OcrReceiverDisconnected() { |
| 522 | auto entry = ocr_client_types_.find(screen_ai_annotators_.current_receiver()); |
| 523 | if (entry != ocr_client_types_.end()) { |
| 524 | ocr_client_types_.erase(entry); |
| 525 | } |
| 526 | // Modify last used time to ensure the service does not shutdown while a |
| 527 | // client is disconnecting. |
| 528 | ocr_last_used_ = base::TimeTicks::Now(); |
| 529 | } |
| 530 | |
| 531 | void ScreenAIService::MceReceiverDisconnected() { |
| 532 | auto entry = mce_client_types_.find( |
| 533 | screen2x_main_content_extractors_.current_receiver()); |
| 534 | if (entry != mce_client_types_.end()) { |
| 535 | mce_client_types_.erase(entry); |
| 536 | } |
| 537 | // Modify last used time to ensure the service does not shutdown while a |
| 538 | // client is disconnecting. |
| 539 | mce_last_used_ = base::TimeTicks::Now(); |
| 540 | } |
| 541 | |
Ramin Halavati | 062042f | 2025-05-02 05:47:57 | [diff] [blame] | 542 | void ScreenAIService::GetMaxImageDimension( |
| 543 | GetMaxImageDimensionCallback callback) { |
Ramin Halavati | da3ec44 | 2025-05-06 04:14:03 | [diff] [blame] | 544 | CHECK(max_ocr_dimension_); |
| 545 | std::move(callback).Run(max_ocr_dimension_); |
Ramin Halavati | 062042f | 2025-05-02 05:47:57 | [diff] [blame] | 546 | } |
| 547 | |
Ramin Halavati | f4b48e0 | 2025-07-02 12:49:48 | [diff] [blame] | 548 | void ScreenAIService::SetOCRLightMode(bool enabled) { |
| 549 | const auto client = screen_ai_annotators_.current_receiver(); |
| 550 | if (enabled) { |
| 551 | light_ocr_clients_.insert(client); |
| 552 | } else { |
| 553 | light_ocr_clients_.erase(client); |
| 554 | } |
| 555 | } |
| 556 | |
| 557 | void ScreenAIService::IsOCRBusy(IsOCRBusyCallback callback) { |
| 558 | std::move(callback).Run(screen_ai_annotators_.size() > 1); |
| 559 | } |
| 560 | |
Dmitry Grebenyuk | c49b95ee | 2023-05-02 05:52:07 | [diff] [blame] | 561 | void ScreenAIService::PerformOcrAndReturnAnnotation( |
| 562 | const SkBitmap& image, |
| 563 | PerformOcrAndReturnAnnotationCallback callback) { |
Arthur Sonzogni | c571efb | 2024-01-26 20:26:18 | [diff] [blame] | 564 | std::optional<chrome_screen_ai::VisualAnnotation> annotation_proto = |
Ramin Halavati | 7716b03 | 2024-08-19 14:56:20 | [diff] [blame] | 565 | PerformOcrAndRecordMetrics(image); |
Dmitry Grebenyuk | c49b95ee | 2023-05-02 05:52:07 | [diff] [blame] | 566 | |
Ramin Halavati | 89a36c9 | 2023-05-05 15:01:03 | [diff] [blame] | 567 | if (annotation_proto) { |
| 568 | std::move(callback).Run(ConvertProtoToVisualAnnotation(*annotation_proto)); |
| 569 | return; |
Dmitry Grebenyuk | c49b95ee | 2023-05-02 05:52:07 | [diff] [blame] | 570 | } |
Ramin Halavati | 89a36c9 | 2023-05-05 15:01:03 | [diff] [blame] | 571 | |
| 572 | std::move(callback).Run(mojom::VisualAnnotation::New()); |
Dmitry Grebenyuk | c49b95ee | 2023-05-02 05:52:07 | [diff] [blame] | 573 | } |
| 574 | |
Ramin Halavati | 87cfa8e6 | 2025-06-02 06:48:10 | [diff] [blame] | 575 | #if BUILDFLAG(IS_CHROMEOS) |
Kyungjun Lee | 9046152 | 2023-04-25 06:50:09 | [diff] [blame] | 576 | void ScreenAIService::PerformOcrAndReturnAXTreeUpdate( |
Ramin Halavati | b446c02 | 2023-03-29 15:36:36 | [diff] [blame] | 577 | const SkBitmap& image, |
Kyungjun Lee | 9046152 | 2023-04-25 06:50:09 | [diff] [blame] | 578 | PerformOcrAndReturnAXTreeUpdateCallback callback) { |
Arthur Sonzogni | c571efb | 2024-01-26 20:26:18 | [diff] [blame] | 579 | std::optional<chrome_screen_ai::VisualAnnotation> annotation_proto = |
Ramin Halavati | 7716b03 | 2024-08-19 14:56:20 | [diff] [blame] | 580 | PerformOcrAndRecordMetrics(image); |
Ramin Halavati | 89a36c9 | 2023-05-05 15:01:03 | [diff] [blame] | 581 | ui::AXTreeUpdate update = ConvertVisualAnnotationToTreeUpdate( |
| 582 | annotation_proto, gfx::Rect(image.width(), image.height())); |
Kyungjun Lee | 9046152 | 2023-04-25 06:50:09 | [diff] [blame] | 583 | |
Ramin Halavati | 89a36c9 | 2023-05-05 15:01:03 | [diff] [blame] | 584 | // The original caller is always replied to, and an empty AXTreeUpdate tells |
| 585 | // that the annotation function was not successful. |
| 586 | std::move(callback).Run(update); |
Ramin Halavati | b446c02 | 2023-03-29 15:36:36 | [diff] [blame] | 587 | } |
Ramin Halavati | 87cfa8e6 | 2025-06-02 06:48:10 | [diff] [blame] | 588 | #endif // BUILDFLAG(IS_CHROMEOS) |
Ramin Halavati | b446c02 | 2023-03-29 15:36:36 | [diff] [blame] | 589 | |
| 590 | void ScreenAIService::ExtractMainContent(const ui::AXTreeUpdate& snapshot, |
Ramin Halavati | b446c02 | 2023-03-29 15:36:36 | [diff] [blame] | 591 | ExtractMainContentCallback callback) { |
Abigail Klein | 1867370 | 2024-03-05 20:59:06 | [diff] [blame] | 592 | ui::AXTree tree; |
| 593 | std::optional<std::vector<int32_t>> content_node_ids; |
Ramin Halavati | b3fe2fe | 2025-03-20 16:53:03 | [diff] [blame] | 594 | bool success = ExtractMainContentInternalAndRecordMetrics(snapshot, tree, |
| 595 | content_node_ids); |
Abigail Klein | 1867370 | 2024-03-05 20:59:06 | [diff] [blame] | 596 | |
| 597 | if (success) { |
| 598 | std::move(callback).Run(*content_node_ids); |
| 599 | } else { |
| 600 | std::move(callback).Run(std::vector<int32_t>()); |
| 601 | } |
| 602 | } |
| 603 | |
| 604 | void ScreenAIService::ExtractMainNode(const ui::AXTreeUpdate& snapshot, |
| 605 | ExtractMainNodeCallback callback) { |
| 606 | ui::AXTree tree; |
| 607 | std::optional<std::vector<int32_t>> content_node_ids; |
Ramin Halavati | b3fe2fe | 2025-03-20 16:53:03 | [diff] [blame] | 608 | bool success = ExtractMainContentInternalAndRecordMetrics(snapshot, tree, |
| 609 | content_node_ids); |
Abigail Klein | 1867370 | 2024-03-05 20:59:06 | [diff] [blame] | 610 | |
| 611 | if (success) { |
| 612 | ui::AXNodeID main_node_id = ComputeMainNode(&tree, *content_node_ids); |
| 613 | std::move(callback).Run(main_node_id); |
| 614 | } else { |
| 615 | std::move(callback).Run(ui::kInvalidAXNodeID); |
| 616 | } |
| 617 | } |
| 618 | |
Mark Schillaci | 79c8281 | 2025-03-07 21:46:37 | [diff] [blame] | 619 | void ScreenAIService::IdentifyMainNode(const ui::AXTreeUpdate& snapshot, |
| 620 | IdentifyMainNodeCallback callback) { |
| 621 | ui::AXTree tree; |
| 622 | std::optional<std::vector<int32_t>> content_node_ids; |
Ramin Halavati | b3fe2fe | 2025-03-20 16:53:03 | [diff] [blame] | 623 | bool success = ExtractMainContentInternalAndRecordMetrics(snapshot, tree, |
| 624 | content_node_ids); |
Mark Schillaci | 79c8281 | 2025-03-07 21:46:37 | [diff] [blame] | 625 | |
| 626 | if (success) { |
| 627 | ui::AXNodeID main_node_id = ComputeMainNode(&tree, *content_node_ids); |
| 628 | std::move(callback).Run(tree.GetAXTreeID(), main_node_id); |
| 629 | } else { |
| 630 | std::move(callback).Run(ui::AXTreeIDUnknown(), ui::kInvalidAXNodeID); |
| 631 | } |
| 632 | } |
| 633 | |
Ramin Halavati | b3fe2fe | 2025-03-20 16:53:03 | [diff] [blame] | 634 | bool ScreenAIService::ExtractMainContentInternalAndRecordMetrics( |
Abigail Klein | 1867370 | 2024-03-05 20:59:06 | [diff] [blame] | 635 | const ui::AXTreeUpdate& snapshot, |
| 636 | ui::AXTree& tree, |
| 637 | std::optional<std::vector<int32_t>>& content_node_ids) { |
Ramin Halavati | d0d6231b | 2025-03-20 05:36:43 | [diff] [blame] | 638 | CHECK(base::Contains(mce_client_types_, |
| 639 | screen2x_main_content_extractors_.current_receiver())); |
Ramin Halavati | 1520b70 | 2025-04-23 15:24:18 | [diff] [blame] | 640 | mce_last_used_ = base::TimeTicks::Now(); |
Ramin Halavati | b3fe2fe | 2025-03-20 16:53:03 | [diff] [blame] | 641 | MainContentExtractionClientTypeForMetrics client_type = GetClientType( |
| 642 | mce_client_types_[screen2x_main_content_extractors_.current_receiver()]); |
| 643 | |
| 644 | static crash_reporter::CrashKeyString<2> cpu_info( |
| 645 | "main_content_extraction_client"); |
| 646 | cpu_info.Set(base::StringPrintf("%i", static_cast<int>(client_type))); |
| 647 | |
Ramin Halavati | b446c02 | 2023-03-29 15:36:36 | [diff] [blame] | 648 | // Early return if input is empty. |
| 649 | if (snapshot.nodes.empty()) { |
Ramin Halavati | b3fe2fe | 2025-03-20 16:53:03 | [diff] [blame] | 650 | base::UmaHistogramEnumeration( |
| 651 | "Accessibility.ScreenAI.MainContentExtraction.Error.SnapshotEmpty", |
| 652 | client_type); |
Abigail Klein | 1867370 | 2024-03-05 20:59:06 | [diff] [blame] | 653 | return false; |
Ramin Halavati | b33fc0c | 2022-05-06 09:32:22 | [diff] [blame] | 654 | } |
Ramin Halavati | e2549a9 | 2022-08-02 07:43:19 | [diff] [blame] | 655 | |
Abigail Klein | 1867370 | 2024-03-05 20:59:06 | [diff] [blame] | 656 | // Deserialize the snapshot and reserialize it to a view hierarchy proto. |
Ramin Halavati | b3fe2fe | 2025-03-20 16:53:03 | [diff] [blame] | 657 | if (!tree.Unserialize(snapshot)) { |
| 658 | base::UmaHistogramEnumeration( |
| 659 | "Accessibility.ScreenAI.MainContentExtraction.Error." |
| 660 | "SnapshotUnserialize", |
| 661 | client_type); |
Ramin Halavati | 62e034d | 2024-08-02 08:58:22 | [diff] [blame] | 662 | return false; |
| 663 | } |
| 664 | |
Ramin Halavati | b3fe2fe | 2025-03-20 16:53:03 | [diff] [blame] | 665 | std::optional<ViewHierarchyAndTreeSize> converted_snapshot = |
| 666 | SnapshotToViewHierarchy(tree); |
| 667 | if (!converted_snapshot) { |
| 668 | base::UmaHistogramEnumeration( |
| 669 | "Accessibility.ScreenAI.MainContentExtraction.Error.SnapshotProto", |
| 670 | client_type); |
| 671 | return false; |
| 672 | } |
| 673 | |
| 674 | base::TimeTicks start_time = base::TimeTicks::Now(); |
Ramin Halavati | aa67ee5 | 2025-04-25 05:15:53 | [diff] [blame] | 675 | base::SequenceBound<HangTimer> hang_timer(background_task_runner_, |
| 676 | /*is_ocr=*/false); |
| 677 | hang_timer.AsyncCall(&HangTimer::StartTimer); |
Ramin Halavati | 62e034d | 2024-08-02 08:58:22 | [diff] [blame] | 678 | content_node_ids = |
| 679 | library_->ExtractMainContent(converted_snapshot->serialized_proto); |
Ramin Halavati | aa67ee5 | 2025-04-25 05:15:53 | [diff] [blame] | 680 | hang_timer.AsyncCall(&HangTimer::Stop); |
Ramin Halavati | b3fe2fe | 2025-03-20 16:53:03 | [diff] [blame] | 681 | base::TimeDelta elapsed_time = base::TimeTicks::Now() - start_time; |
| 682 | |
| 683 | bool successful = |
| 684 | content_node_ids.has_value() && content_node_ids->size() > 0; |
Ramin Halavati | 81b6d92 | 2024-06-07 14:20:34 | [diff] [blame] | 685 | base::UmaHistogramBoolean( |
Ramin Halavati | b3fe2fe | 2025-03-20 16:53:03 | [diff] [blame] | 686 | "Accessibility.ScreenAI.MainContentExtraction.Successful2", successful); |
| 687 | |
| 688 | if (!content_node_ids.has_value()) { |
| 689 | base::UmaHistogramEnumeration( |
| 690 | "Accessibility.ScreenAI.MainContentExtraction.Error.ResultNull", |
| 691 | client_type); |
| 692 | } else if (content_node_ids->empty()) { |
| 693 | base::UmaHistogramEnumeration( |
| 694 | "Accessibility.ScreenAI.MainContentExtraction.Error.ResultEmpty", |
| 695 | client_type); |
| 696 | } |
| 697 | |
Ramin Halavati | ac1ee292 | 2025-05-21 17:17:17 | [diff] [blame] | 698 | mce_last_used_ = base::TimeTicks::Now(); |
Ramin Halavati | b3fe2fe | 2025-03-20 16:53:03 | [diff] [blame] | 699 | if (successful) { |
| 700 | base::UmaHistogramTimes( |
| 701 | "Accessibility.ScreenAI.MainContentExtraction.Latency.Success", |
| 702 | elapsed_time); |
Ramin Halavati | 5aad701d | 2023-05-09 17:23:29 | [diff] [blame] | 703 | VLOG(2) << "Screen2x returned " << content_node_ids->size() << " node ids."; |
Abigail Klein | 1867370 | 2024-03-05 20:59:06 | [diff] [blame] | 704 | return true; |
| 705 | } else { |
Ramin Halavati | b3fe2fe | 2025-03-20 16:53:03 | [diff] [blame] | 706 | base::UmaHistogramTimes( |
| 707 | "Accessibility.ScreenAI.MainContentExtraction.Latency.Failure", |
| 708 | elapsed_time); |
Abigail Klein | 1867370 | 2024-03-05 20:59:06 | [diff] [blame] | 709 | VLOG(0) << "Screen2x returned no results."; |
| 710 | return false; |
Ramin Halavati | 5aad701d | 2023-05-09 17:23:29 | [diff] [blame] | 711 | } |
Abigail Klein | 1867370 | 2024-03-05 20:59:06 | [diff] [blame] | 712 | } |
Ramin Halavati | 89a36c9 | 2023-05-05 15:01:03 | [diff] [blame] | 713 | |
Abigail Klein | 1867370 | 2024-03-05 20:59:06 | [diff] [blame] | 714 | ui::AXNodeID ScreenAIService::ComputeMainNodeForTesting( |
| 715 | const ui::AXTree* tree, |
| 716 | const std::vector<ui::AXNodeID>& content_node_ids) { |
| 717 | return ComputeMainNode(tree, content_node_ids); |
Abigail Klein | dfdde35 | 2023-01-27 21:03:10 | [diff] [blame] | 718 | } |
| 719 | |
Ramin Halavati | cf15501 | 2025-05-05 15:50:01 | [diff] [blame] | 720 | void ScreenAIService::StartShutDownOnIdleTimer() { |
| 721 | if (!idle_checking_timer_) { |
| 722 | idle_checking_timer_ = std::make_unique<base::RepeatingTimer>(); |
| 723 | idle_checking_timer_->Start(FROM_HERE, kIdleCheckingDelay, this, |
| 724 | &ScreenAIService::ShutDownOnIdle); |
| 725 | } |
| 726 | } |
| 727 | |
Ramin Halavati | be45dde | 2025-05-01 04:19:10 | [diff] [blame] | 728 | void ScreenAIService::ShutDownOnIdle() { |
Ramin Halavati | 2acf236 | 2024-08-02 14:22:53 | [diff] [blame] | 729 | const base::TimeTicks kIdlenessThreshold = |
| 730 | base::TimeTicks::Now() - kIdleCheckingDelay; |
Ramin Halavati | be45dde | 2025-05-01 04:19:10 | [diff] [blame] | 731 | if (ocr_last_used_ < kIdlenessThreshold && |
| 732 | mce_last_used_ < kIdlenessThreshold) { |
Ramin Halavati | f26d7d4 | 2025-03-12 16:04:46 | [diff] [blame] | 733 | screen_ai_shutdown_handler_->ShuttingDownOnIdle(); |
Ramin Halavati | f4b48e0 | 2025-07-02 12:49:48 | [diff] [blame] | 734 | |
| 735 | // If OCR was used, record the number of times it's mode was switched. |
| 736 | if (ocr_last_used_ != base::TimeTicks()) { |
| 737 | base::UmaHistogramCounts100("Accessibility.ScreenAI.OCR.ModeSwitch", |
| 738 | ocr_mode_switch_count_); |
| 739 | } |
| 740 | |
Ramin Halavati | dc0b2e51 | 2024-08-16 17:39:09 | [diff] [blame] | 741 | base::Process::TerminateCurrentProcessImmediately(0); |
Ramin Halavati | 2acf236 | 2024-08-02 14:22:53 | [diff] [blame] | 742 | } |
Ramin Halavati | 3842e7e | 2024-07-31 04:39:21 | [diff] [blame] | 743 | } |
| 744 | |
Ramin Halavati | 26dcce2 | 2022-02-23 13:11:14 | [diff] [blame] | 745 | } // namespace screen_ai |