#include "chrome/browser/paint_preview/services/paint_preview_tab_service.h"
#include <algorithm>
#include <utility>
#include "base/functional/callback.h"
#include "base/memory/memory_pressure_listener.h"
#include "base/memory/memory_pressure_monitor.h"
#include "base/metrics/histogram_functions.h"
#include "base/strings/string_number_conversions.h"
#include "base/task/sequenced_task_runner.h"
#include "base/task/task_traits.h"
#include "build/build_config.h"
#include "chrome/browser/paint_preview/services/paint_preview_tab_service_file_mixin.h"
#include "components/paint_preview/browser/file_manager.h"
#include "components/paint_preview/browser/warm_compositor.h"
#include "content/public/browser/render_process_host.h"
#include "ui/accessibility/ax_mode.h"
#include "ui/gfx/geometry/rect.h"
#if BUILDFLAG(IS_ANDROID)
#include "base/android/callback_android.h"
#include "base/android/jni_android.h"
#include "base/android/jni_array.h"
#include "base/android/jni_string.h"
#include "base/logging.h"
#include "chrome/browser/paint_preview/android/jni_headers/PaintPreviewTabService_jni.h"
#endif
namespace paint_preview {
namespace {
constexpr int kMaxCaptureSizePixels = …;
constexpr size_t kMaxPerCaptureSizeBytes = …;
constexpr uint64_t kMaxDecodedImageSizeBytes = …;
#if BUILDFLAG(IS_ANDROID)
void JavaBooleanCallbackAdapter(base::OnceCallback<void(bool)> callback,
PaintPreviewTabService::Status status) {
DVLOG(1) << "Capture finished with status: " << status;
std::move(callback).Run(status == PaintPreviewTabService::Status::kOk);
}
#endif
int TabIdFromDirectoryKey(const DirectoryKey& key) { … }
}
PaintPreviewTabService::TabServiceTask::TabServiceTask(
int tab_id,
const DirectoryKey& key,
int frame_tree_node_id,
content::GlobalRenderFrameHostId frame_routing_id,
float page_scale_factor,
int scroll_offset_x,
int scroll_offset_y,
base::ScopedClosureRunner capture_handle)
: … { … }
PaintPreviewTabService::TabServiceTask::~TabServiceTask() = default;
PaintPreviewTabService::PaintPreviewTabService(
std::unique_ptr<PaintPreviewFileMixin> file_mixin,
std::unique_ptr<PaintPreviewPolicy> policy,
bool is_off_the_record)
: … { … }
PaintPreviewTabService::~PaintPreviewTabService() { … }
void PaintPreviewTabService::CaptureTab(int tab_id,
content::WebContents* contents,
bool accessibility_enabled,
float page_scale_factor,
int x,
int y,
FinishedCallback callback) { … }
void PaintPreviewTabService::TabClosed(int tab_id) { … }
bool PaintPreviewTabService::HasCaptureForTab(int tab_id) { … }
void PaintPreviewTabService::AuditArtifacts(
const std::vector<int>& active_tab_ids) { … }
#if BUILDFLAG(IS_ANDROID)
void PaintPreviewTabService::CaptureTabAndroid(
JNIEnv* env,
jint j_tab_id,
const base::android::JavaParamRef<jobject>& j_web_contents,
jboolean j_accessibility_enabled,
jfloat j_page_scale_factor,
jint j_x,
jint j_y,
const base::android::JavaParamRef<jobject>& j_callback) {
content::WebContents* web_contents =
content::WebContents::FromJavaWebContents(j_web_contents);
CaptureTab(static_cast<int>(j_tab_id), web_contents,
static_cast<bool>(j_accessibility_enabled),
static_cast<float>(j_page_scale_factor), static_cast<int>(j_x),
static_cast<int>(j_y),
base::BindOnce(
&JavaBooleanCallbackAdapter,
base::BindOnce(
&base::android::RunBooleanCallbackAndroid,
base::android::ScopedJavaGlobalRef<jobject>(j_callback))));
}
void PaintPreviewTabService::TabClosedAndroid(JNIEnv* env, jint j_tab_id) {
TabClosed(static_cast<int>(j_tab_id));
}
jboolean PaintPreviewTabService::HasCaptureForTabAndroid(JNIEnv* env,
jint j_tab_id) {
return static_cast<jboolean>(HasCaptureForTab(static_cast<int>(j_tab_id)));
}
void PaintPreviewTabService::AuditArtifactsAndroid(
JNIEnv* env,
const base::android::JavaParamRef<jintArray>& j_tab_ids) {
std::vector<int> tab_ids;
base::android::JavaIntArrayToIntVector(env, j_tab_ids, &tab_ids);
AuditArtifacts(tab_ids);
}
jboolean PaintPreviewTabService::IsCacheInitializedAndroid(JNIEnv* env) {
return static_cast<jboolean>(CacheInitialized());
}
base::android::ScopedJavaLocalRef<jstring>
PaintPreviewTabService::GetPathAndroid(JNIEnv* env) {
return base::android::ConvertUTF8ToJavaString(
env, GetFileMixin()->GetFileManager()->GetPath().AsUTF8Unsafe());
}
#endif
void PaintPreviewTabService::DeleteTask(int tab_id) { … }
void PaintPreviewTabService::InitializeCache(
const base::flat_set<DirectoryKey>& in_use_keys) { … }
void PaintPreviewTabService::CaptureTabInternal(
base::WeakPtr<TabServiceTask> task,
bool accessibility_enabled,
const std::optional<base::FilePath>& file_path) { … }
void PaintPreviewTabService::OnAXTreeWritten(base::WeakPtr<TabServiceTask> task,
bool result) { … }
void PaintPreviewTabService::OnCaptured(
base::WeakPtr<TabServiceTask> task,
PaintPreviewBaseService::CaptureStatus status,
std::unique_ptr<CaptureResult> result) { … }
void PaintPreviewTabService::OnFinished(base::WeakPtr<TabServiceTask> task,
bool success) { … }
void PaintPreviewTabService::RunAudit(
const std::vector<int>& active_tab_ids,
const base::flat_set<DirectoryKey>& in_use_keys) { … }
}