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 | 55fdf4d | 2022-11-07 06:35:18 | [diff] [blame] | 7 | #include <memory> |
Ramin Halavati | 63d9c25 | 2024-07-29 06:59:17 | [diff] [blame] | 8 | #include <optional> |
Ramin Halavati | fd712d8e | 2022-10-19 05:11:27 | [diff] [blame] | 9 | #include <utility> |
Ramin Halavati | b044504d | 2022-10-24 06:36:34 | [diff] [blame] | 10 | #include <vector> |
Ramin Halavati | fd712d8e | 2022-10-19 05:11:27 | [diff] [blame] | 11 | |
| 12 | #include "base/check.h" |
Ramin Halavati | ad969ef | 2024-05-21 06:59:47 | [diff] [blame] | 13 | #include "base/check_is_test.h" |
Tom Sepez | e9b4dd55 | 2024-08-14 22:44:29 | [diff] [blame^] | 14 | #include "base/compiler_specific.h" |
Ramin Halavati | 55fdf4d | 2022-11-07 06:35:18 | [diff] [blame] | 15 | #include "base/functional/bind.h" |
| 16 | #include "base/location.h" |
Ramin Halavati | fafff15 | 2022-12-05 09:39:38 | [diff] [blame] | 17 | #include "base/logging.h" |
Kyungjun Lee | 9d755a0 | 2022-11-08 17:57:11 | [diff] [blame] | 18 | #include "base/metrics/histogram_functions.h" |
Ramin Halavati | 5ff855c | 2022-04-27 16:30:48 | [diff] [blame] | 19 | #include "base/process/process.h" |
Ramin Halavati | 62e034d | 2024-08-02 08:58:22 | [diff] [blame] | 20 | #include "base/strings/stringprintf.h" |
Sean Maher | 5b9af51f | 2022-11-21 15:32:47 | [diff] [blame] | 21 | #include "base/task/single_thread_task_runner.h" |
Ramin Halavati | 55fdf4d | 2022-11-07 06:35:18 | [diff] [blame] | 22 | #include "base/task/thread_pool.h" |
Ramin Halavati | 62e034d | 2024-08-02 08:58:22 | [diff] [blame] | 23 | #include "components/crash/core/common/crash_key.h" |
Abigail Klein | dfdde35 | 2023-01-27 21:03:10 | [diff] [blame] | 24 | #include "services/metrics/public/cpp/ukm_builders.h" |
| 25 | #include "services/metrics/public/cpp/ukm_recorder.h" |
Abigail Klein | 7a63c57 | 2024-02-28 20:45:09 | [diff] [blame] | 26 | #include "services/screen_ai/buildflags/buildflags.h" |
| 27 | #include "services/screen_ai/proto/main_content_extractor_proto_convertor.h" |
| 28 | #include "services/screen_ai/proto/visual_annotator_proto_convertor.h" |
| 29 | #include "services/screen_ai/public/cpp/utilities.h" |
Ramin Halavati | eddadb6 | 2022-05-04 17:29:49 | [diff] [blame] | 30 | #include "ui/accessibility/accessibility_features.h" |
Ramin Halavati | 4f904d0a | 2024-06-25 15:32:32 | [diff] [blame] | 31 | #include "ui/accessibility/ax_node.h" |
| 32 | #include "ui/accessibility/ax_tree.h" |
Ramin Halavati | 8423107 | 2022-08-17 08:02:20 | [diff] [blame] | 33 | #include "ui/accessibility/ax_tree_id.h" |
Nektarios Paisios | f73d697 | 2022-06-04 11:32:25 | [diff] [blame] | 34 | #include "ui/gfx/geometry/rect_f.h" |
Ramin Halavati | 367352a | 2022-04-14 06:00:23 | [diff] [blame] | 35 | |
Ramin Halavati | a9b50a10 | 2024-02-07 18:37:23 | [diff] [blame] | 36 | #if BUILDFLAG(USE_FAKE_SCREEN_AI) |
Abigail Klein | 7a63c57 | 2024-02-28 20:45:09 | [diff] [blame] | 37 | #include "services/screen_ai/screen_ai_library_wrapper_fake.h" |
Ramin Halavati | 767b8fc | 2024-02-02 06:23:08 | [diff] [blame] | 38 | #else |
Abigail Klein | 7a63c57 | 2024-02-28 20:45:09 | [diff] [blame] | 39 | #include "services/screen_ai/screen_ai_library_wrapper_impl.h" |
Ramin Halavati | 767b8fc | 2024-02-02 06:23:08 | [diff] [blame] | 40 | #endif |
| 41 | |
Ramin Halavati | 69bf775 | 2022-04-04 09:58:15 | [diff] [blame] | 42 | namespace screen_ai { |
Ramin Halavati | 26dcce2 | 2022-02-23 13:11:14 | [diff] [blame] | 43 | |
Ramin Halavati | 6d4fc2b | 2022-06-16 15:33:35 | [diff] [blame] | 44 | namespace { |
| 45 | |
Ramin Halavati | 2acf236 | 2024-08-02 14:22:53 | [diff] [blame] | 46 | // How often it would be checked that the service is idle and can be shutdown. |
| 47 | constexpr base::TimeDelta kIdleCheckingDelay = base::Minutes(10); |
| 48 | |
Ramin Halavati | 6ff5641 | 2024-08-13 07:15:54 | [diff] [blame] | 49 | // How long after all clients are disconnected, it is checked if service is |
| 50 | // idle. |
| 51 | constexpr base::TimeDelta kCoolDownTime = base::Seconds(1); |
| 52 | |
Ramin Halavati | ad969ef | 2024-05-21 06:59:47 | [diff] [blame] | 53 | // These values are persisted to logs. Entries should not be renumbered and |
| 54 | // numeric values should never be reused. |
| 55 | enum class OcrClientTypeForMetrics { |
| 56 | kTest = 0, |
| 57 | kPdfViewer = 1, |
| 58 | kLocalSearch = 2, |
| 59 | kCameraApp = 3, |
| 60 | kPdfSearchify = 4, |
| 61 | kMediaApp = 5, |
| 62 | kMaxValue = kMediaApp |
| 63 | }; |
| 64 | |
| 65 | OcrClientTypeForMetrics GetClientType(mojom::OcrClientType client_type) { |
| 66 | switch (client_type) { |
| 67 | case mojom::OcrClientType::kTest: |
| 68 | CHECK_IS_TEST(); |
| 69 | return OcrClientTypeForMetrics::kTest; |
| 70 | case mojom::OcrClientType::kPdfViewer: |
| 71 | return OcrClientTypeForMetrics::kPdfViewer; |
| 72 | case mojom::OcrClientType::kLocalSearch: |
| 73 | return OcrClientTypeForMetrics::kLocalSearch; |
| 74 | case mojom::OcrClientType::kCameraApp: |
| 75 | return OcrClientTypeForMetrics::kCameraApp; |
| 76 | case mojom::OcrClientType::kPdfSearchify: |
| 77 | return OcrClientTypeForMetrics::kPdfSearchify; |
| 78 | case mojom::OcrClientType::kMediaApp: |
| 79 | return OcrClientTypeForMetrics::kMediaApp; |
| 80 | } |
| 81 | } |
| 82 | |
Ramin Halavati | 89a36c9 | 2023-05-05 15:01:03 | [diff] [blame] | 83 | ui::AXTreeUpdate ConvertVisualAnnotationToTreeUpdate( |
Ramin Halavati | dcb09a17 | 2024-07-15 08:28:55 | [diff] [blame] | 84 | std::optional<chrome_screen_ai::VisualAnnotation>& annotation_proto, |
Ramin Halavati | 89a36c9 | 2023-05-05 15:01:03 | [diff] [blame] | 85 | const gfx::Rect& image_rect) { |
| 86 | if (!annotation_proto) { |
| 87 | VLOG(0) << "Screen AI library could not process snapshot or no OCR data."; |
| 88 | return ui::AXTreeUpdate(); |
| 89 | } |
| 90 | |
| 91 | return VisualAnnotationToAXTreeUpdate(*annotation_proto, image_rect); |
| 92 | } |
| 93 | |
Abigail Klein | 1867370 | 2024-03-05 20:59:06 | [diff] [blame] | 94 | ui::AXNodeID ComputeMainNode( |
| 95 | const ui::AXTree* tree, |
| 96 | const std::vector<ui::AXNodeID>& content_node_ids) { |
| 97 | ui::AXNode* front = tree->GetFromId(content_node_ids.front()); |
| 98 | ui::AXNode* back = tree->GetFromId(content_node_ids.back()); |
| 99 | ui::AXNode* main = front->GetLowestCommonAncestor(*back); |
| 100 | return main->id(); |
| 101 | } |
| 102 | |
Ramin Halavati | 26ae6b7 | 2022-11-10 06:45:05 | [diff] [blame] | 103 | } // namespace |
| 104 | |
Ramin Halavati | 39bab0d | 2024-01-30 06:05:01 | [diff] [blame] | 105 | // The library accepts simple pointers to model data retrieval functions, hence |
| 106 | // callback functions with linked object are not safe to pass. |
Ramin Halavati | 63d9c25 | 2024-07-29 06:59:17 | [diff] [blame] | 107 | // This global variable keeps the pointer the only instance of this class. |
| 108 | ModelDataHolder* g_model_data_holder_instance = nullptr; |
Ramin Halavati | 8ced639 | 2023-09-25 07:27:50 | [diff] [blame] | 109 | |
Ramin Halavati | 63d9c25 | 2024-07-29 06:59:17 | [diff] [blame] | 110 | // Keeps the handles of model files, and replies to calls for copying their |
| 111 | // content. |
| 112 | class ModelDataHolder { |
Ramin Halavati | 8ced639 | 2023-09-25 07:27:50 | [diff] [blame] | 113 | public: |
Ramin Halavati | 63d9c25 | 2024-07-29 06:59:17 | [diff] [blame] | 114 | ModelDataHolder() { |
| 115 | CHECK(!g_model_data_holder_instance); |
| 116 | g_model_data_holder_instance = this; |
| 117 | } |
Ramin Halavati | 8ced639 | 2023-09-25 07:27:50 | [diff] [blame] | 118 | |
Ramin Halavati | 63d9c25 | 2024-07-29 06:59:17 | [diff] [blame] | 119 | ModelDataHolder(const ModelDataHolder&) = delete; |
| 120 | ModelDataHolder& operator=(const ModelDataHolder&) = delete; |
| 121 | |
| 122 | ~ModelDataHolder() { |
| 123 | CHECK_EQ(g_model_data_holder_instance, this); |
| 124 | g_model_data_holder_instance = nullptr; |
Ramin Halavati | 8ced639 | 2023-09-25 07:27:50 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | // Returns 0 if file is not found. |
| 128 | static uint32_t GetDataSize(const char* relative_file_path) { |
Ramin Halavati | 63d9c25 | 2024-07-29 06:59:17 | [diff] [blame] | 129 | CHECK(g_model_data_holder_instance); |
| 130 | base::File* model_file = |
| 131 | g_model_data_holder_instance->GetModelFile(relative_file_path); |
| 132 | return model_file ? model_file->GetLength() : 0; |
Ramin Halavati | 8ced639 | 2023-09-25 07:27:50 | [diff] [blame] | 133 | } |
| 134 | |
Ramin Halavati | 63d9c25 | 2024-07-29 06:59:17 | [diff] [blame] | 135 | // Copies content of the file in `relative_file_path` to `buffer`. Expects |
| 136 | // that `buffer_size` would be enough for the entire file content. |
Ramin Halavati | 8ced639 | 2023-09-25 07:27:50 | [diff] [blame] | 137 | static void CopyData(const char* relative_file_path, |
| 138 | uint32_t buffer_size, |
| 139 | char* buffer) { |
Ramin Halavati | 63d9c25 | 2024-07-29 06:59:17 | [diff] [blame] | 140 | CHECK(g_model_data_holder_instance); |
| 141 | base::File* model_file = |
| 142 | g_model_data_holder_instance->GetModelFile(relative_file_path); |
| 143 | CHECK(model_file); |
| 144 | |
| 145 | int64_t length = model_file->GetLength(); |
| 146 | CHECK_GE(buffer_size, length); |
Tom Sepez | e9b4dd55 | 2024-08-14 22:44:29 | [diff] [blame^] | 147 | CHECK_EQ(UNSAFE_TODO(model_file->Read(0, buffer, length)), length); |
Ramin Halavati | 8ced639 | 2023-09-25 07:27:50 | [diff] [blame] | 148 | } |
| 149 | |
Ramin Halavati | 63d9c25 | 2024-07-29 06:59:17 | [diff] [blame] | 150 | void AddModelFiles(base::flat_map<base::FilePath, base::File> model_files) { |
| 151 | for (auto& model_file : model_files) { |
| 152 | model_files_[model_file.first.MaybeAsASCII()] = |
| 153 | std::move(model_file.second); |
Ramin Halavati | 39bab0d | 2024-01-30 06:05:01 | [diff] [blame] | 154 | } |
| 155 | } |
Ramin Halavati | 8ced639 | 2023-09-25 07:27:50 | [diff] [blame] | 156 | |
Ramin Halavati | 63d9c25 | 2024-07-29 06:59:17 | [diff] [blame] | 157 | // Returns the file handle for `relative_file_path` if it exists. |
| 158 | base::File* GetModelFile(const char* relative_file_path) { |
| 159 | if (!base::Contains(model_files_, relative_file_path)) { |
| 160 | return nullptr; |
| 161 | } |
| 162 | return &model_files_[relative_file_path]; |
| 163 | } |
| 164 | |
Ramin Halavati | 8ced639 | 2023-09-25 07:27:50 | [diff] [blame] | 165 | private: |
Ramin Halavati | 63d9c25 | 2024-07-29 06:59:17 | [diff] [blame] | 166 | std::map<std::string, base::File> model_files_; |
Ramin Halavati | 8ced639 | 2023-09-25 07:27:50 | [diff] [blame] | 167 | }; |
| 168 | |
Ramin Halavati | 26ae6b7 | 2022-11-10 06:45:05 | [diff] [blame] | 169 | ScreenAIService::ScreenAIService( |
Ramin Halavati | c1e4fa9 | 2023-05-17 17:22:06 | [diff] [blame] | 170 | mojo::PendingReceiver<mojom::ScreenAIServiceFactory> receiver) |
| 171 | : factory_receiver_(this, std::move(receiver)), |
| 172 | ocr_receiver_(this), |
Ramin Halavati | ad969ef | 2024-05-21 06:59:47 | [diff] [blame] | 173 | main_content_extraction_receiver_(this) { |
Ramin Halavati | 6ff5641 | 2024-08-13 07:15:54 | [diff] [blame] | 174 | screen2x_main_content_extractors_.set_disconnect_handler( |
| 175 | base::BindRepeating(&ScreenAIService::CheckIdleStateAfterDelay, |
| 176 | weak_ptr_factory_.GetWeakPtr())); |
| 177 | screen_ai_annotators_.set_disconnect_handler( |
| 178 | base::BindRepeating(&ScreenAIService::OcrReceiverDisconnected, |
| 179 | weak_ptr_factory_.GetWeakPtr())); |
Ramin Halavati | 63d9c25 | 2024-07-29 06:59:17 | [diff] [blame] | 180 | model_data_holder_ = std::make_unique<ModelDataHolder>(); |
Ramin Halavati | 2acf236 | 2024-08-02 14:22:53 | [diff] [blame] | 181 | idle_checking_timer_ = std::make_unique<base::RepeatingTimer>(); |
| 182 | idle_checking_timer_->Start(FROM_HERE, kIdleCheckingDelay, this, |
| 183 | &ScreenAIService::ShutDownIfNoClients); |
Ramin Halavati | ad969ef | 2024-05-21 06:59:47 | [diff] [blame] | 184 | } |
Ramin Halavati | 26ae6b7 | 2022-11-10 06:45:05 | [diff] [blame] | 185 | |
| 186 | ScreenAIService::~ScreenAIService() = default; |
| 187 | |
Ramin Halavati | 8ced639 | 2023-09-25 07:27:50 | [diff] [blame] | 188 | void ScreenAIService::LoadLibrary(const base::FilePath& library_path) { |
Abigail Klein | 7a63c57 | 2024-02-28 20:45:09 | [diff] [blame] | 189 | // The ScopedBlockingCall in LoadLibrary guarantees that this is not run on |
| 190 | // the UI thread. |
Ramin Halavati | a9b50a10 | 2024-02-07 18:37:23 | [diff] [blame] | 191 | #if BUILDFLAG(USE_FAKE_SCREEN_AI) |
Ramin Halavati | 767b8fc | 2024-02-02 06:23:08 | [diff] [blame] | 192 | library_ = std::make_unique<ScreenAILibraryWrapperFake>(); |
| 193 | #else |
| 194 | library_ = std::make_unique<ScreenAILibraryWrapperImpl>(); |
| 195 | #endif |
Ramin Halavati | 8ced639 | 2023-09-25 07:27:50 | [diff] [blame] | 196 | |
| 197 | bool load_sucessful = library_->Load(library_path); |
| 198 | base::UmaHistogramBoolean("Accessibility.ScreenAI.Library.Initialized", |
| 199 | load_sucessful); |
| 200 | |
| 201 | if (!load_sucessful) { |
| 202 | library_.reset(); |
| 203 | return; |
| 204 | } |
| 205 | |
| 206 | uint32_t version_major; |
| 207 | uint32_t version_minor; |
| 208 | library_->GetLibraryVersion(version_major, version_minor); |
| 209 | VLOG(2) << "Screen AI library version: " << version_major << "." |
| 210 | << version_minor; |
| 211 | |
| 212 | #if BUILDFLAG(IS_CHROMEOS_ASH) |
| 213 | library_->SetLogger(); |
| 214 | #endif |
| 215 | |
| 216 | if (features::IsScreenAIDebugModeEnabled()) { |
| 217 | library_->EnableDebugMode(); |
| 218 | } |
| 219 | |
Ramin Halavati | 63d9c25 | 2024-07-29 06:59:17 | [diff] [blame] | 220 | library_->SetFileContentFunctions(&ModelDataHolder::GetDataSize, |
| 221 | &ModelDataHolder::CopyData); |
Ramin Halavati | 8ced639 | 2023-09-25 07:27:50 | [diff] [blame] | 222 | } |
| 223 | |
Ramin Halavati | c1e4fa9 | 2023-05-17 17:22:06 | [diff] [blame] | 224 | void ScreenAIService::InitializeMainContentExtraction( |
Ramin Halavati | d139a0f | 2023-02-28 13:25:28 | [diff] [blame] | 225 | const base::FilePath& library_path, |
Ramin Halavati | fbc6202 | 2023-11-10 11:05:07 | [diff] [blame] | 226 | base::flat_map<base::FilePath, base::File> model_files, |
Ramin Halavati | c1e4fa9 | 2023-05-17 17:22:06 | [diff] [blame] | 227 | mojo::PendingReceiver<mojom::MainContentExtractionService> |
| 228 | main_content_extractor_service_receiver, |
| 229 | InitializeMainContentExtractionCallback callback) { |
| 230 | if (!library_) { |
Ramin Halavati | 8ced639 | 2023-09-25 07:27:50 | [diff] [blame] | 231 | LoadLibrary(library_path); |
Ramin Halavati | c1e4fa9 | 2023-05-17 17:22:06 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | if (!library_) { |
| 235 | std::move(callback).Run(false); |
| 236 | base::Process::TerminateCurrentProcessImmediately(-1); |
| 237 | } |
| 238 | |
Ramin Halavati | 63d9c25 | 2024-07-29 06:59:17 | [diff] [blame] | 239 | model_data_holder_->AddModelFiles(std::move(model_files)); |
Ramin Halavati | b044504d | 2022-10-24 06:36:34 | [diff] [blame] | 240 | |
Ramin Halavati | 8ced639 | 2023-09-25 07:27:50 | [diff] [blame] | 241 | bool init_successful = library_->InitMainContentExtraction(); |
Ramin Halavati | 1d57c2d | 2023-05-24 05:18:24 | [diff] [blame] | 242 | base::UmaHistogramBoolean( |
| 243 | "Accessibility.ScreenAI.MainContentExtraction.Initialized", |
| 244 | init_successful); |
| 245 | if (!init_successful) { |
Ramin Halavati | c1e4fa9 | 2023-05-17 17:22:06 | [diff] [blame] | 246 | std::move(callback).Run(false); |
Ramin Halavati | 89a36c9 | 2023-05-05 15:01:03 | [diff] [blame] | 247 | return; |
Ramin Halavati | d139a0f | 2023-02-28 13:25:28 | [diff] [blame] | 248 | } |
Ramin Halavati | 89a36c9 | 2023-05-05 15:01:03 | [diff] [blame] | 249 | |
Ramin Halavati | c1e4fa9 | 2023-05-17 17:22:06 | [diff] [blame] | 250 | // This interface should be created only once. |
| 251 | CHECK(!main_content_extraction_receiver_.is_bound()); |
| 252 | |
| 253 | main_content_extraction_receiver_.Bind( |
| 254 | std::move(main_content_extractor_service_receiver)); |
| 255 | |
| 256 | std::move(callback).Run(true); |
Ramin Halavati | 2acf236 | 2024-08-02 14:22:53 | [diff] [blame] | 257 | main_content_extraction_last_used_ = base::TimeTicks::Now(); |
Ramin Halavati | c1e4fa9 | 2023-05-17 17:22:06 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | void ScreenAIService::InitializeOCR( |
| 261 | const base::FilePath& library_path, |
Ramin Halavati | fbc6202 | 2023-11-10 11:05:07 | [diff] [blame] | 262 | base::flat_map<base::FilePath, base::File> model_files, |
Ramin Halavati | c1e4fa9 | 2023-05-17 17:22:06 | [diff] [blame] | 263 | mojo::PendingReceiver<mojom::OCRService> ocr_service_receiver, |
| 264 | InitializeOCRCallback callback) { |
| 265 | if (!library_) { |
Ramin Halavati | 8ced639 | 2023-09-25 07:27:50 | [diff] [blame] | 266 | LoadLibrary(library_path); |
Ramin Halavati | c1e4fa9 | 2023-05-17 17:22:06 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | if (!library_) { |
| 270 | std::move(callback).Run(false); |
| 271 | base::Process::TerminateCurrentProcessImmediately(-1); |
| 272 | } |
| 273 | |
Ramin Halavati | 63d9c25 | 2024-07-29 06:59:17 | [diff] [blame] | 274 | model_data_holder_->AddModelFiles(std::move(model_files)); |
| 275 | |
Ramin Halavati | 0e2ae574 | 2023-11-10 05:57:54 | [diff] [blame] | 276 | bool init_successful = library_->InitOCR(); |
Ramin Halavati | 35d129f | 2023-06-22 16:37:09 | [diff] [blame] | 277 | base::UmaHistogramBoolean("Accessibility.ScreenAI.OCR.Initialized", |
Ramin Halavati | 1d57c2d | 2023-05-24 05:18:24 | [diff] [blame] | 278 | init_successful); |
Ramin Halavati | 48058433 | 2023-08-02 06:38:40 | [diff] [blame] | 279 | |
Ramin Halavati | 1d57c2d | 2023-05-24 05:18:24 | [diff] [blame] | 280 | if (!init_successful) { |
Ramin Halavati | c1e4fa9 | 2023-05-17 17:22:06 | [diff] [blame] | 281 | std::move(callback).Run(false); |
| 282 | return; |
| 283 | } |
| 284 | |
| 285 | // This interface should be created only once. |
| 286 | CHECK(!ocr_receiver_.is_bound()); |
| 287 | |
| 288 | ocr_receiver_.Bind(std::move(ocr_service_receiver)); |
| 289 | |
| 290 | std::move(callback).Run(true); |
Ramin Halavati | 2acf236 | 2024-08-02 14:22:53 | [diff] [blame] | 291 | ocr_last_used_ = base::TimeTicks::Now(); |
Ramin Halavati | b044504d | 2022-10-24 06:36:34 | [diff] [blame] | 292 | } |
Ramin Halavati | 26dcce2 | 2022-02-23 13:11:14 | [diff] [blame] | 293 | |
| 294 | void ScreenAIService::BindAnnotator( |
| 295 | mojo::PendingReceiver<mojom::ScreenAIAnnotator> annotator) { |
| 296 | screen_ai_annotators_.Add(this, std::move(annotator)); |
| 297 | } |
| 298 | |
Ramin Halavati | eddadb6 | 2022-05-04 17:29:49 | [diff] [blame] | 299 | void ScreenAIService::BindMainContentExtractor( |
| 300 | mojo::PendingReceiver<mojom::Screen2xMainContentExtractor> |
| 301 | main_content_extractor) { |
Ramin Halavati | 6ff5641 | 2024-08-13 07:15:54 | [diff] [blame] | 302 | screen2x_main_content_extractors_.Add(this, |
| 303 | std::move(main_content_extractor)); |
Ramin Halavati | eddadb6 | 2022-05-04 17:29:49 | [diff] [blame] | 304 | } |
| 305 | |
Arthur Sonzogni | c571efb | 2024-01-26 20:26:18 | [diff] [blame] | 306 | std::optional<chrome_screen_ai::VisualAnnotation> |
Ramin Halavati | 2650ebe | 2023-11-27 19:56:35 | [diff] [blame] | 307 | ScreenAIService::PerformOcrAndRecordMetrics(const SkBitmap& image, |
| 308 | bool a11y_tree_request) { |
Ramin Halavati | ad969ef | 2024-05-21 06:59:47 | [diff] [blame] | 309 | auto entry = ocr_client_types_.find(screen_ai_annotators_.current_receiver()); |
| 310 | CHECK(entry != ocr_client_types_.end()) << "OCR client type is not set."; |
| 311 | base::UmaHistogramEnumeration("Accessibility.ScreenAI.OCR.ClientType", |
| 312 | GetClientType(entry->second)); |
| 313 | |
Ramin Halavati | 2acf236 | 2024-08-02 14:22:53 | [diff] [blame] | 314 | ocr_last_used_ = base::TimeTicks::Now(); |
Ramin Halavati | 7252cd0 | 2023-05-10 07:21:33 | [diff] [blame] | 315 | auto result = library_->PerformOcr(image); |
Ramin Halavati | 2acf236 | 2024-08-02 14:22:53 | [diff] [blame] | 316 | base::TimeDelta elapsed_time = base::TimeTicks::Now() - ocr_last_used_; |
Ramin Halavati | 2650ebe | 2023-11-27 19:56:35 | [diff] [blame] | 317 | int lines_count = result ? result->lines_size() : 0; |
Ramin Halavati | 3ae33ca | 2024-01-01 12:58:39 | [diff] [blame] | 318 | unsigned image_size = image.width() * image.height(); |
Ramin Halavati | 2650ebe | 2023-11-27 19:56:35 | [diff] [blame] | 319 | VLOG(1) << "OCR returned " << lines_count << " lines in " << elapsed_time; |
Ramin Halavati | 7252cd0 | 2023-05-10 07:21:33 | [diff] [blame] | 320 | |
Ramin Halavati | 81b6d92 | 2024-06-07 14:20:34 | [diff] [blame] | 321 | base::UmaHistogramBoolean("Accessibility.ScreenAI.OCR.Successful", |
| 322 | result.has_value()); |
Ramin Halavati | 2650ebe | 2023-11-27 19:56:35 | [diff] [blame] | 323 | base::UmaHistogramCounts100("Accessibility.ScreenAI.OCR.LinesCount", |
| 324 | lines_count); |
Ramin Halavati | a0548b26 | 2023-06-27 05:56:47 | [diff] [blame] | 325 | base::UmaHistogramCounts10M("Accessibility.ScreenAI.OCR.ImageSize10M", |
Ramin Halavati | 3ae33ca | 2024-01-01 12:58:39 | [diff] [blame] | 326 | image_size); |
| 327 | if (image_size < 500 * 500) { |
| 328 | base::UmaHistogramTimes("Accessibility.ScreenAI.OCR.Latency.Small", |
| 329 | elapsed_time); |
| 330 | } else if (image_size < 1000 * 1000) { |
| 331 | base::UmaHistogramTimes("Accessibility.ScreenAI.OCR.Latency.Medium", |
| 332 | elapsed_time); |
| 333 | } else if (image_size < 2000 * 2000) { |
| 334 | base::UmaHistogramTimes("Accessibility.ScreenAI.OCR.Latency.Large", |
| 335 | elapsed_time); |
| 336 | } else { |
| 337 | base::UmaHistogramTimes("Accessibility.ScreenAI.OCR.Latency.XLarge", |
| 338 | elapsed_time); |
| 339 | } |
Ramin Halavati | 2650ebe | 2023-11-27 19:56:35 | [diff] [blame] | 340 | |
| 341 | // If needed to extend to more clients, an identifier can be passed from the |
| 342 | // client to introduce itself and these metrics can be collected based on it. |
| 343 | if (a11y_tree_request) { |
| 344 | base::UmaHistogramCounts100("Accessibility.ScreenAI.OCR.LinesCount.PDF", |
| 345 | lines_count); |
| 346 | base::UmaHistogramTimes("Accessibility.ScreenAI.OCR.Time.PDF", |
| 347 | elapsed_time); |
| 348 | base::UmaHistogramCounts10M("Accessibility.ScreenAI.OCR.ImageSize.PDF", |
| 349 | image.width() * image.height()); |
| 350 | } |
| 351 | |
Ramin Halavati | 7252cd0 | 2023-05-10 07:21:33 | [diff] [blame] | 352 | return result; |
| 353 | } |
| 354 | |
Ramin Halavati | ad969ef | 2024-05-21 06:59:47 | [diff] [blame] | 355 | void ScreenAIService::SetClientType(mojom::OcrClientType client_type) { |
| 356 | ocr_client_types_[screen_ai_annotators_.current_receiver()] = client_type; |
| 357 | } |
| 358 | |
Dmitry Grebenyuk | c49b95ee | 2023-05-02 05:52:07 | [diff] [blame] | 359 | void ScreenAIService::PerformOcrAndReturnAnnotation( |
| 360 | const SkBitmap& image, |
| 361 | PerformOcrAndReturnAnnotationCallback callback) { |
Arthur Sonzogni | c571efb | 2024-01-26 20:26:18 | [diff] [blame] | 362 | std::optional<chrome_screen_ai::VisualAnnotation> annotation_proto = |
Ramin Halavati | 2650ebe | 2023-11-27 19:56:35 | [diff] [blame] | 363 | PerformOcrAndRecordMetrics(image, /*a11y_tree_request=*/false); |
Dmitry Grebenyuk | c49b95ee | 2023-05-02 05:52:07 | [diff] [blame] | 364 | |
Ramin Halavati | 89a36c9 | 2023-05-05 15:01:03 | [diff] [blame] | 365 | if (annotation_proto) { |
| 366 | std::move(callback).Run(ConvertProtoToVisualAnnotation(*annotation_proto)); |
| 367 | return; |
Dmitry Grebenyuk | c49b95ee | 2023-05-02 05:52:07 | [diff] [blame] | 368 | } |
Ramin Halavati | 89a36c9 | 2023-05-05 15:01:03 | [diff] [blame] | 369 | |
| 370 | std::move(callback).Run(mojom::VisualAnnotation::New()); |
Dmitry Grebenyuk | c49b95ee | 2023-05-02 05:52:07 | [diff] [blame] | 371 | } |
| 372 | |
Kyungjun Lee | 9046152 | 2023-04-25 06:50:09 | [diff] [blame] | 373 | void ScreenAIService::PerformOcrAndReturnAXTreeUpdate( |
Ramin Halavati | b446c02 | 2023-03-29 15:36:36 | [diff] [blame] | 374 | const SkBitmap& image, |
Kyungjun Lee | 9046152 | 2023-04-25 06:50:09 | [diff] [blame] | 375 | PerformOcrAndReturnAXTreeUpdateCallback callback) { |
Arthur Sonzogni | c571efb | 2024-01-26 20:26:18 | [diff] [blame] | 376 | std::optional<chrome_screen_ai::VisualAnnotation> annotation_proto = |
Ramin Halavati | 2650ebe | 2023-11-27 19:56:35 | [diff] [blame] | 377 | PerformOcrAndRecordMetrics(image, /*a11y_tree_request=*/true); |
Ramin Halavati | 89a36c9 | 2023-05-05 15:01:03 | [diff] [blame] | 378 | ui::AXTreeUpdate update = ConvertVisualAnnotationToTreeUpdate( |
| 379 | annotation_proto, gfx::Rect(image.width(), image.height())); |
Kyungjun Lee | 9046152 | 2023-04-25 06:50:09 | [diff] [blame] | 380 | |
Ramin Halavati | 89a36c9 | 2023-05-05 15:01:03 | [diff] [blame] | 381 | // The original caller is always replied to, and an empty AXTreeUpdate tells |
| 382 | // that the annotation function was not successful. |
| 383 | std::move(callback).Run(update); |
Ramin Halavati | b446c02 | 2023-03-29 15:36:36 | [diff] [blame] | 384 | } |
| 385 | |
| 386 | void ScreenAIService::ExtractMainContent(const ui::AXTreeUpdate& snapshot, |
| 387 | ukm::SourceId ukm_source_id, |
| 388 | ExtractMainContentCallback callback) { |
Ramin Halavati | 2acf236 | 2024-08-02 14:22:53 | [diff] [blame] | 389 | main_content_extraction_last_used_ = base::TimeTicks::Now(); |
Abigail Klein | 1867370 | 2024-03-05 20:59:06 | [diff] [blame] | 390 | ui::AXTree tree; |
| 391 | std::optional<std::vector<int32_t>> content_node_ids; |
| 392 | bool success = ExtractMainContentInternal(snapshot, tree, content_node_ids); |
Ramin Halavati | 2acf236 | 2024-08-02 14:22:53 | [diff] [blame] | 393 | base::TimeDelta elapsed_time = |
| 394 | base::TimeTicks::Now() - main_content_extraction_last_used_; |
Abigail Klein | 1867370 | 2024-03-05 20:59:06 | [diff] [blame] | 395 | RecordMetrics(ukm_source_id, ukm::UkmRecorder::Get(), elapsed_time, success); |
| 396 | |
| 397 | if (success) { |
| 398 | std::move(callback).Run(*content_node_ids); |
| 399 | } else { |
| 400 | std::move(callback).Run(std::vector<int32_t>()); |
| 401 | } |
| 402 | } |
| 403 | |
| 404 | void ScreenAIService::ExtractMainNode(const ui::AXTreeUpdate& snapshot, |
| 405 | ExtractMainNodeCallback callback) { |
| 406 | ui::AXTree tree; |
| 407 | std::optional<std::vector<int32_t>> content_node_ids; |
| 408 | bool success = ExtractMainContentInternal(snapshot, tree, content_node_ids); |
| 409 | |
| 410 | if (success) { |
| 411 | ui::AXNodeID main_node_id = ComputeMainNode(&tree, *content_node_ids); |
| 412 | std::move(callback).Run(main_node_id); |
| 413 | } else { |
| 414 | std::move(callback).Run(ui::kInvalidAXNodeID); |
| 415 | } |
| 416 | } |
| 417 | |
| 418 | bool ScreenAIService::ExtractMainContentInternal( |
| 419 | const ui::AXTreeUpdate& snapshot, |
| 420 | ui::AXTree& tree, |
| 421 | std::optional<std::vector<int32_t>>& content_node_ids) { |
Ramin Halavati | b446c02 | 2023-03-29 15:36:36 | [diff] [blame] | 422 | // Early return if input is empty. |
| 423 | if (snapshot.nodes.empty()) { |
Abigail Klein | 1867370 | 2024-03-05 20:59:06 | [diff] [blame] | 424 | return false; |
Ramin Halavati | b33fc0c | 2022-05-06 09:32:22 | [diff] [blame] | 425 | } |
Ramin Halavati | e2549a9 | 2022-08-02 07:43:19 | [diff] [blame] | 426 | |
Abigail Klein | 1867370 | 2024-03-05 20:59:06 | [diff] [blame] | 427 | // Deserialize the snapshot and reserialize it to a view hierarchy proto. |
| 428 | CHECK(tree.Unserialize(snapshot)); |
Ramin Halavati | 62e034d | 2024-08-02 08:58:22 | [diff] [blame] | 429 | std::optional<ViewHierarchyAndTreeSize> converted_snapshot = |
| 430 | SnapshotToViewHierarchy(tree); |
| 431 | if (!converted_snapshot) { |
| 432 | VLOG(0) << "Proto not generated."; |
| 433 | return false; |
| 434 | } |
| 435 | |
| 436 | // Report request specifications in case the call crashes. |
| 437 | static crash_reporter::CrashKeyString<95> crash_info( |
| 438 | "main_content_extraction_info"); |
| 439 | crash_info.Set(base::StringPrintf( |
| 440 | "TD:%i, TR:%i, SNC:%10zu, SBS:%10zu, TS:%10i, TW:%6i, TH:%6i, SS:%10zu", |
| 441 | snapshot.has_tree_data, snapshot.root_id != ui::kInvalidAXNodeID, |
| 442 | snapshot.nodes.size(), snapshot.ByteSize(), tree.size(), |
| 443 | static_cast<int>(converted_snapshot->tree_dimensions.width()), |
| 444 | static_cast<int>(converted_snapshot->tree_dimensions.height()), |
| 445 | converted_snapshot->serialized_proto.size())); |
| 446 | |
| 447 | content_node_ids = |
| 448 | library_->ExtractMainContent(converted_snapshot->serialized_proto); |
Ramin Halavati | 81b6d92 | 2024-06-07 14:20:34 | [diff] [blame] | 449 | base::UmaHistogramBoolean( |
| 450 | "Accessibility.ScreenAI.MainContentExtraction.Successful", |
| 451 | content_node_ids.has_value()); |
Abigail Klein | 1867370 | 2024-03-05 20:59:06 | [diff] [blame] | 452 | if (content_node_ids.has_value() && content_node_ids->size() > 0) { |
Ramin Halavati | 5aad701d | 2023-05-09 17:23:29 | [diff] [blame] | 453 | VLOG(2) << "Screen2x returned " << content_node_ids->size() << " node ids."; |
Abigail Klein | 1867370 | 2024-03-05 20:59:06 | [diff] [blame] | 454 | return true; |
| 455 | } else { |
| 456 | VLOG(0) << "Screen2x returned no results."; |
| 457 | return false; |
Ramin Halavati | 5aad701d | 2023-05-09 17:23:29 | [diff] [blame] | 458 | } |
Abigail Klein | 1867370 | 2024-03-05 20:59:06 | [diff] [blame] | 459 | } |
Ramin Halavati | 89a36c9 | 2023-05-05 15:01:03 | [diff] [blame] | 460 | |
Abigail Klein | 1867370 | 2024-03-05 20:59:06 | [diff] [blame] | 461 | ui::AXNodeID ScreenAIService::ComputeMainNodeForTesting( |
| 462 | const ui::AXTree* tree, |
| 463 | const std::vector<ui::AXNodeID>& content_node_ids) { |
| 464 | return ComputeMainNode(tree, content_node_ids); |
Abigail Klein | dfdde35 | 2023-01-27 21:03:10 | [diff] [blame] | 465 | } |
| 466 | |
| 467 | // static |
| 468 | void ScreenAIService::RecordMetrics(ukm::SourceId ukm_source_id, |
| 469 | ukm::UkmRecorder* ukm_recorder, |
| 470 | base::TimeDelta elapsed_time, |
| 471 | bool success) { |
| 472 | if (success) { |
| 473 | base::UmaHistogramTimes( |
| 474 | "Accessibility.ScreenAI.Screen2xDistillationTime.Success", |
| 475 | elapsed_time); |
| 476 | if (ukm_source_id != ukm::kInvalidSourceId) { |
| 477 | ukm::builders::Accessibility_ScreenAI(ukm_source_id) |
| 478 | .SetScreen2xDistillationTime_Success(elapsed_time.InMilliseconds()) |
| 479 | .Record(ukm_recorder); |
| 480 | } |
| 481 | } else { |
| 482 | base::UmaHistogramTimes( |
| 483 | "Accessibility.ScreenAI.Screen2xDistillationTime.Failure", |
| 484 | elapsed_time); |
| 485 | if (ukm_source_id != ukm::kInvalidSourceId) { |
| 486 | ukm::builders::Accessibility_ScreenAI(ukm_source_id) |
| 487 | .SetScreen2xDistillationTime_Failure(elapsed_time.InMilliseconds()) |
| 488 | .Record(ukm_recorder); |
| 489 | } |
| 490 | } |
Ramin Halavati | eddadb6 | 2022-05-04 17:29:49 | [diff] [blame] | 491 | } |
| 492 | |
Ramin Halavati | 6ff5641 | 2024-08-13 07:15:54 | [diff] [blame] | 493 | void ScreenAIService::OcrReceiverDisconnected() { |
Ramin Halavati | ad969ef | 2024-05-21 06:59:47 | [diff] [blame] | 494 | auto entry = ocr_client_types_.find(screen_ai_annotators_.current_receiver()); |
| 495 | if (entry != ocr_client_types_.end()) { |
| 496 | ocr_client_types_.erase(entry); |
| 497 | } |
Ramin Halavati | 6ff5641 | 2024-08-13 07:15:54 | [diff] [blame] | 498 | |
| 499 | CheckIdleStateAfterDelay(); |
| 500 | } |
| 501 | |
| 502 | void ScreenAIService::CheckIdleStateAfterDelay() { |
| 503 | // Check if service is idle, a little after the client disconnects. |
| 504 | base::SequencedTaskRunner::GetCurrentDefault()->PostDelayedTask( |
| 505 | FROM_HERE, |
| 506 | base::BindOnce(&ScreenAIService::ShutDownIfNoClients, |
| 507 | weak_ptr_factory_.GetWeakPtr()), |
| 508 | kCoolDownTime); |
Ramin Halavati | ad969ef | 2024-05-21 06:59:47 | [diff] [blame] | 509 | } |
| 510 | |
Ramin Halavati | 3842e7e | 2024-07-31 04:39:21 | [diff] [blame] | 511 | void ScreenAIService::ShutDownIfNoClients() { |
Ramin Halavati | 2acf236 | 2024-08-02 14:22:53 | [diff] [blame] | 512 | bool ocr_has_clients = screen_ai_annotators_.size(); |
| 513 | bool main_content_extraction_has_clients = |
Ramin Halavati | 6ff5641 | 2024-08-13 07:15:54 | [diff] [blame] | 514 | screen2x_main_content_extractors_.size(); |
Ramin Halavati | 2acf236 | 2024-08-02 14:22:53 | [diff] [blame] | 515 | if (!ocr_has_clients && !main_content_extraction_has_clients) { |
| 516 | VLOG(2) << "Shutting down since no client."; |
Ramin Halavati | 3842e7e | 2024-07-31 04:39:21 | [diff] [blame] | 517 | base::Process::TerminateCurrentProcessImmediately(0); |
| 518 | } |
Ramin Halavati | 2acf236 | 2024-08-02 14:22:53 | [diff] [blame] | 519 | |
| 520 | // Collect data on whether each functionality of the service is idle. |
| 521 | // This will be used to plan further to shut down the service when idle, or |
| 522 | // track features which keep the service in idle state. |
| 523 | // TODO(b/353718857): Shut down when both features are idle, after ensuring |
| 524 | // all clients support reconnecting. |
| 525 | const base::TimeTicks kIdlenessThreshold = |
| 526 | base::TimeTicks::Now() - kIdleCheckingDelay; |
| 527 | if (!ocr_idle_reported_ && !ocr_last_used_.is_null() && |
| 528 | ocr_last_used_ < kIdlenessThreshold) { |
| 529 | ocr_idle_reported_ = true; |
| 530 | base::UmaHistogramBoolean("Accessibility.ScreenAI.OCR.Idle.Connected", |
| 531 | ocr_has_clients); |
| 532 | } |
| 533 | if (!main_content_extraction_idle_reported_ && |
| 534 | !main_content_extraction_last_used_.is_null() && |
| 535 | main_content_extraction_last_used_ < kIdlenessThreshold) { |
| 536 | main_content_extraction_idle_reported_ = true; |
| 537 | base::UmaHistogramBoolean( |
| 538 | "Accessibility.ScreenAI.MainContentExtraction.Idle.Connected", |
| 539 | main_content_extraction_has_clients); |
| 540 | } |
Ramin Halavati | 3842e7e | 2024-07-31 04:39:21 | [diff] [blame] | 541 | } |
| 542 | |
Ramin Halavati | 26dcce2 | 2022-02-23 13:11:14 | [diff] [blame] | 543 | } // namespace screen_ai |