chromium/chrome/browser/paint_preview/services/paint_preview_tab_service.cc

// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/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  // BUILDFLAG(IS_ANDROID)

namespace paint_preview {

namespace {

// The maximum X and Y dimension in pixels.
// TODO(crbug.com/40193795): Tune this value.
constexpr int kMaxCaptureSizePixels =;

constexpr size_t kMaxPerCaptureSizeBytes =;       // 8 MB.
constexpr uint64_t kMaxDecodedImageSizeBytes =;  // 10 MB.

#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  // BUILDFLAG(IS_ANDROID)

// Safe since Tab ID are just converted to strings to be directory keys.
int TabIdFromDirectoryKey(const DirectoryKey& key) {}

}  // namespace

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  // BUILDFLAG(IS_ANDROID)

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) {}

}  // namespace paint_preview