#include "services/screen_ai/screen_ai_service_impl.h"
#include <memory>
#include <optional>
#include <utility>
#include <vector>
#include "base/check.h"
#include "base/check_is_test.h"
#include "base/compiler_specific.h"
#include "base/functional/bind.h"
#include "base/location.h"
#include "base/logging.h"
#include "base/metrics/histogram_functions.h"
#include "base/process/process.h"
#include "base/strings/stringprintf.h"
#include "base/task/single_thread_task_runner.h"
#include "base/task/thread_pool.h"
#include "components/crash/core/common/crash_key.h"
#include "services/metrics/public/cpp/ukm_builders.h"
#include "services/metrics/public/cpp/ukm_recorder.h"
#include "services/screen_ai/buildflags/buildflags.h"
#include "services/screen_ai/proto/main_content_extractor_proto_convertor.h"
#include "services/screen_ai/proto/visual_annotator_proto_convertor.h"
#include "services/screen_ai/public/cpp/utilities.h"
#include "ui/accessibility/accessibility_features.h"
#include "ui/accessibility/ax_node.h"
#include "ui/accessibility/ax_tree.h"
#include "ui/accessibility/ax_tree_id.h"
#include "ui/gfx/geometry/rect_f.h"
#if BUILDFLAG(USE_FAKE_SCREEN_AI)
#include "services/screen_ai/screen_ai_library_wrapper_fake.h"
#else
#include "services/screen_ai/screen_ai_library_wrapper_impl.h"
#endif
namespace screen_ai {
namespace {
constexpr base::TimeDelta kIdleCheckingDelay = …;
constexpr base::TimeDelta kCoolDownTime = …;
enum class OcrClientTypeForMetrics { … };
OcrClientTypeForMetrics GetClientType(mojom::OcrClientType client_type) { … }
ui::AXTreeUpdate ConvertVisualAnnotationToTreeUpdate(
std::optional<chrome_screen_ai::VisualAnnotation>& annotation_proto,
const gfx::Rect& image_rect) { … }
ui::AXNodeID ComputeMainNode(
const ui::AXTree* tree,
const std::vector<ui::AXNodeID>& content_node_ids) { … }
}
ModelDataHolder* g_model_data_holder_instance = …;
class ModelDataHolder { … };
ScreenAIService::ScreenAIService(
mojo::PendingReceiver<mojom::ScreenAIServiceFactory> receiver)
: … { … }
ScreenAIService::~ScreenAIService() = default;
void ScreenAIService::LoadLibrary(const base::FilePath& library_path) { … }
void ScreenAIService::InitializeMainContentExtraction(
const base::FilePath& library_path,
base::flat_map<base::FilePath, base::File> model_files,
mojo::PendingReceiver<mojom::MainContentExtractionService>
main_content_extractor_service_receiver,
InitializeMainContentExtractionCallback callback) { … }
void ScreenAIService::InitializeOCR(
const base::FilePath& library_path,
base::flat_map<base::FilePath, base::File> model_files,
mojo::PendingReceiver<mojom::OCRService> ocr_service_receiver,
InitializeOCRCallback callback) { … }
void ScreenAIService::BindAnnotator(
mojo::PendingReceiver<mojom::ScreenAIAnnotator> annotator) { … }
void ScreenAIService::BindMainContentExtractor(
mojo::PendingReceiver<mojom::Screen2xMainContentExtractor>
main_content_extractor) { … }
std::optional<chrome_screen_ai::VisualAnnotation>
ScreenAIService::PerformOcrAndRecordMetrics(const SkBitmap& image) { … }
void ScreenAIService::SetClientType(mojom::OcrClientType client_type) { … }
void ScreenAIService::PerformOcrAndReturnAnnotation(
const SkBitmap& image,
PerformOcrAndReturnAnnotationCallback callback) { … }
void ScreenAIService::PerformOcrAndReturnAXTreeUpdate(
const SkBitmap& image,
PerformOcrAndReturnAXTreeUpdateCallback callback) { … }
void ScreenAIService::ExtractMainContent(const ui::AXTreeUpdate& snapshot,
ukm::SourceId ukm_source_id,
ExtractMainContentCallback callback) { … }
void ScreenAIService::ExtractMainNode(const ui::AXTreeUpdate& snapshot,
ExtractMainNodeCallback callback) { … }
bool ScreenAIService::ExtractMainContentInternal(
const ui::AXTreeUpdate& snapshot,
ui::AXTree& tree,
std::optional<std::vector<int32_t>>& content_node_ids) { … }
ui::AXNodeID ScreenAIService::ComputeMainNodeForTesting(
const ui::AXTree* tree,
const std::vector<ui::AXNodeID>& content_node_ids) { … }
void ScreenAIService::RecordMetrics(ukm::SourceId ukm_source_id,
ukm::UkmRecorder* ukm_recorder,
base::TimeDelta elapsed_time,
bool success) { … }
void ScreenAIService::OcrReceiverDisconnected() { … }
void ScreenAIService::CheckIdleStateAfterDelay() { … }
void ScreenAIService::ShutDownIfNoClients() { … }
}