Commit 119be2cb authored by Mehran Mahmoudi's avatar Mehran Mahmoudi Committed by Commit Bot

[Paint Preview] Hook up player with paint_preview_base_service

This connects adds a paint_preview_base_service dependency on the
player.

Bug: 1019885
Change-Id: Ia1055d83591e828c3a945b0ddc948ec5496b2edf
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1955819Reviewed-by: default avatarMichael Thiessen <mthiesse@chromium.org>
Reviewed-by: default avatarCalder Kitagawa <ckitagawa@chromium.org>
Commit-Queue: Mehran Mahmoudi <mahmoudi@chromium.org>
Cr-Commit-Position: refs/heads/master@{#729957}
parent ef2c4415
...@@ -317,6 +317,7 @@ test("components_unittests") { ...@@ -317,6 +317,7 @@ test("components_unittests") {
"//components/gcm_driver/instance_id/android:instance_id_driver_test_support_java", "//components/gcm_driver/instance_id/android:instance_id_driver_test_support_java",
"//components/invalidation/impl", "//components/invalidation/impl",
"//components/invalidation/impl:java", "//components/invalidation/impl:java",
"//components/paint_preview/browser/android:java",
"//components/paint_preview/player/android:unit_tests", "//components/paint_preview/player/android:unit_tests",
"//components/policy/android:policy_java", "//components/policy/android:policy_java",
"//components/signin/core/browser", "//components/signin/core/browser",
......
...@@ -41,6 +41,10 @@ source_set("browser") { ...@@ -41,6 +41,10 @@ source_set("browser") {
"//url", "//url",
] ]
if (is_android) {
deps += [ ":jni_headers" ]
}
public_deps = [ public_deps = [
"//components/paint_preview/common", "//components/paint_preview/common",
"//components/paint_preview/common/mojom", "//components/paint_preview/common/mojom",
...@@ -50,6 +54,14 @@ source_set("browser") { ...@@ -50,6 +54,14 @@ source_set("browser") {
] ]
} }
if (is_android) {
generate_jni("jni_headers") {
sources = [
"android/java/src/org/chromium/components/paintpreview/browser/PaintPreviewBaseService.java",
]
}
}
source_set("test_support") { source_set("test_support") {
testonly = true testonly = true
sources = [ sources = [
......
...@@ -16,6 +16,7 @@ android_library("java") { ...@@ -16,6 +16,7 @@ android_library("java") {
annotation_processor_deps = [ "//base/android/jni_generator:jni_processor" ] annotation_processor_deps = [ "//base/android/jni_generator:jni_processor" ]
sources = [ sources = [
"java/src/org/chromium/components/paintpreview/browser/PaintPreviewBaseService.java",
"java/src/org/chromium/components/paintpreview/browser/PaintPreviewUtils.java", "java/src/org/chromium/components/paintpreview/browser/PaintPreviewUtils.java",
] ]
......
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package org.chromium.components.paintpreview.browser;
import org.chromium.base.annotations.CalledByNative;
/**
* The Java-side implementation of paint_preview_base_service.cc. This class is owned and managed by
* its C++ counterpart.
*/
public class PaintPreviewBaseService {
private long mNativePaintPreviewBaseService;
@CalledByNative
public PaintPreviewBaseService(long nativePaintPreviewBaseService) {
mNativePaintPreviewBaseService = nativePaintPreviewBaseService;
}
@CalledByNative
public void onDestroy() {
mNativePaintPreviewBaseService = 0;
}
public long getNativePaintPreviewBaseService() {
return mNativePaintPreviewBaseService;
}
}
\ No newline at end of file
...@@ -10,6 +10,8 @@ ...@@ -10,6 +10,8 @@
#include "base/callback.h" #include "base/callback.h"
#include "base/files/file_path.h" #include "base/files/file_path.h"
#include "base/logging.h" #include "base/logging.h"
#include "base/task/post_task.h"
#include "build/build_config.h"
#include "components/keyed_service/core/keyed_service.h" #include "components/keyed_service/core/keyed_service.h"
#include "components/paint_preview/browser/compositor_utils.h" #include "components/paint_preview/browser/compositor_utils.h"
#include "components/paint_preview/browser/file_manager.h" #include "components/paint_preview/browser/file_manager.h"
...@@ -19,6 +21,12 @@ ...@@ -19,6 +21,12 @@
#include "content/public/browser/web_contents.h" #include "content/public/browser/web_contents.h"
#include "ui/gfx/geometry/rect.h" #include "ui/gfx/geometry/rect.h"
#if defined(OS_ANDROID)
#include "base/android/jni_android.h"
#include "base/android/scoped_java_ref.h"
#include "components/paint_preview/browser/jni_headers/PaintPreviewBaseService_jni.h"
#endif // defined(OS_ANDROID)
namespace paint_preview { namespace paint_preview {
namespace { namespace {
...@@ -35,9 +43,27 @@ PaintPreviewBaseService::PaintPreviewBaseService( ...@@ -35,9 +43,27 @@ PaintPreviewBaseService::PaintPreviewBaseService(
: policy_(std::move(policy)), : policy_(std::move(policy)),
file_manager_( file_manager_(
path.AppendASCII(kPaintPreviewDir).AppendASCII(ascii_feature_name)), path.AppendASCII(kPaintPreviewDir).AppendASCII(ascii_feature_name)),
is_off_the_record_(is_off_the_record) {} is_off_the_record_(is_off_the_record) {
#if defined(OS_ANDROID)
JNIEnv* env = base::android::AttachCurrentThread();
base::android::ScopedJavaLocalRef<jobject> java_ref =
Java_PaintPreviewBaseService_Constructor(
env, reinterpret_cast<intptr_t>(this));
java_ref_.Reset(java_ref);
#endif // defined(OS_ANDROID)
}
PaintPreviewBaseService::~PaintPreviewBaseService() = default; PaintPreviewBaseService::~PaintPreviewBaseService() {
#if defined(OS_ANDROID)
JNIEnv* env = base::android::AttachCurrentThread();
Java_PaintPreviewBaseService_onDestroy(env, java_ref_);
#endif // defined(OS_ANDROID)
}
base::Optional<PaintPreviewProto>
PaintPreviewBaseService::GetCapturedPaintPreviewProto(const GURL& url) {
return base::nullopt;
}
void PaintPreviewBaseService::CapturePaintPreview( void PaintPreviewBaseService::CapturePaintPreview(
content::WebContents* web_contents, content::WebContents* web_contents,
......
...@@ -9,8 +9,11 @@ ...@@ -9,8 +9,11 @@
#include "base/callback.h" #include "base/callback.h"
#include "base/files/file_path.h" #include "base/files/file_path.h"
#include "base/macros.h"
#include "base/memory/weak_ptr.h" #include "base/memory/weak_ptr.h"
#include "base/optional.h"
#include "base/unguessable_token.h" #include "base/unguessable_token.h"
#include "build/build_config.h"
#include "components/keyed_service/core/keyed_service.h" #include "components/keyed_service/core/keyed_service.h"
#include "components/paint_preview/browser/file_manager.h" #include "components/paint_preview/browser/file_manager.h"
#include "components/paint_preview/browser/paint_preview_policy.h" #include "components/paint_preview/browser/paint_preview_policy.h"
...@@ -18,6 +21,10 @@ ...@@ -18,6 +21,10 @@
#include "components/paint_preview/common/proto/paint_preview.pb.h" #include "components/paint_preview/common/proto/paint_preview.pb.h"
#include "components/paint_preview/public/paint_preview_compositor_service.h" #include "components/paint_preview/public/paint_preview_compositor_service.h"
#include "content/public/browser/web_contents.h" #include "content/public/browser/web_contents.h"
#if defined(OS_ANDROID)
#include "base/android/jni_android.h"
#include "base/android/scoped_java_ref.h"
#endif // defined(OS_ANDROID)
namespace paint_preview { namespace paint_preview {
...@@ -63,6 +70,12 @@ class PaintPreviewBaseService : public KeyedService { ...@@ -63,6 +70,12 @@ class PaintPreviewBaseService : public KeyedService {
// Returns whether the created service is off the record. // Returns whether the created service is off the record.
bool IsOffTheRecord() const { return is_off_the_record_; } bool IsOffTheRecord() const { return is_off_the_record_; }
// Returns the PaintPreviewProto that is associated with |url|. Implementers
// of this class should override this function as it returns base::nullopt by
// default.
virtual base::Optional<PaintPreviewProto> GetCapturedPaintPreviewProto(
const GURL& url);
// The following methods both capture a Paint Preview; however, their behavior // The following methods both capture a Paint Preview; however, their behavior
// and intended use is different. The first method is intended for capturing // and intended use is different. The first method is intended for capturing
// full page contents. Generally, this is what you should be using for most // full page contents. Generally, this is what you should be using for most
...@@ -76,7 +89,6 @@ class PaintPreviewBaseService : public KeyedService { ...@@ -76,7 +89,6 @@ class PaintPreviewBaseService : public KeyedService {
// if a capture fails the service implementation is responsible for // if a capture fails the service implementation is responsible for
// implementing this management and tracking the directories in existence. // implementing this management and tracking the directories in existence.
// Data in a directory will contain: // Data in a directory will contain:
// - paint_preview.pb (the metadata proto)
// - a number of SKPs listed as <guid>.skp (one per frame) // - a number of SKPs listed as <guid>.skp (one per frame)
// //
// Captures the main frame of |web_contents| (an observer for capturing Paint // Captures the main frame of |web_contents| (an observer for capturing Paint
...@@ -100,6 +112,12 @@ class PaintPreviewBaseService : public KeyedService { ...@@ -100,6 +112,12 @@ class PaintPreviewBaseService : public KeyedService {
std::unique_ptr<PaintPreviewCompositorService> StartCompositorService( std::unique_ptr<PaintPreviewCompositorService> StartCompositorService(
base::OnceClosure disconnect_handler); base::OnceClosure disconnect_handler);
#if defined(OS_ANDROID)
base::android::ScopedJavaGlobalRef<jobject> GetJavaObject() {
return java_ref_;
}
#endif // defined(OS_ANDROID)
private: private:
void OnCaptured(OnCapturedCallback callback, void OnCaptured(OnCapturedCallback callback,
base::UnguessableToken guid, base::UnguessableToken guid,
...@@ -110,6 +128,11 @@ class PaintPreviewBaseService : public KeyedService { ...@@ -110,6 +128,11 @@ class PaintPreviewBaseService : public KeyedService {
FileManager file_manager_; FileManager file_manager_;
bool is_off_the_record_; bool is_off_the_record_;
#if defined(OS_ANDROID)
// Points to the Java reference.
base::android::ScopedJavaGlobalRef<jobject> java_ref_;
#endif // defined(OS_ANDROID)
base::WeakPtrFactory<PaintPreviewBaseService> weak_ptr_factory_{this}; base::WeakPtrFactory<PaintPreviewBaseService> weak_ptr_factory_{this};
PaintPreviewBaseService(const PaintPreviewBaseService&) = delete; PaintPreviewBaseService(const PaintPreviewBaseService&) = delete;
......
...@@ -13,7 +13,11 @@ source_set("player") { ...@@ -13,7 +13,11 @@ source_set("player") {
deps = [ deps = [
"//base", "//base",
"//components/paint_preview/browser", "//components/paint_preview/browser",
"//components/paint_preview/common",
"//components/paint_preview/common/proto",
"//components/paint_preview/public",
"//components/services/paint_preview_compositor", "//components/services/paint_preview_compositor",
"//mojo/public/cpp/bindings",
"//ui/gfx/geometry", "//ui/gfx/geometry",
"//url", "//url",
] ]
......
...@@ -34,6 +34,9 @@ source_set("unit_tests") { ...@@ -34,6 +34,9 @@ source_set("unit_tests") {
":player_android", ":player_android",
"//base", "//base",
"//base/test:test_support", "//base/test:test_support",
"//components/paint_preview/browser",
"//components/paint_preview/player",
"//components/services/paint_preview_compositor/public/mojom",
"//skia", "//skia",
"//testing/gmock", "//testing/gmock",
"//testing/gtest", "//testing/gtest",
...@@ -67,6 +70,7 @@ android_library("java") { ...@@ -67,6 +70,7 @@ android_library("java") {
deps = [ deps = [
"//base:base_java", "//base:base_java",
"//base:jni_java", "//base:jni_java",
"//components/paint_preview/browser/android:java",
"//ui/android:ui_java", "//ui/android:ui_java",
] ]
} }
......
...@@ -12,6 +12,7 @@ import org.chromium.base.Callback; ...@@ -12,6 +12,7 @@ import org.chromium.base.Callback;
import org.chromium.base.annotations.CalledByNative; import org.chromium.base.annotations.CalledByNative;
import org.chromium.base.annotations.JNINamespace; import org.chromium.base.annotations.JNINamespace;
import org.chromium.base.annotations.NativeMethods; import org.chromium.base.annotations.NativeMethods;
import org.chromium.components.paintpreview.browser.PaintPreviewBaseService;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
...@@ -27,16 +28,22 @@ class PlayerCompositorDelegateImpl implements PlayerCompositorDelegate { ...@@ -27,16 +28,22 @@ class PlayerCompositorDelegateImpl implements PlayerCompositorDelegate {
} }
private CompositorListener mCompositorListener; private CompositorListener mCompositorListener;
private long mNativePaintPreviewPlayerMediator; private long mNativePlayerCompositorDelegate;
PlayerCompositorDelegateImpl(String url, @Nonnull CompositorListener compositorListener) { PlayerCompositorDelegateImpl(PaintPreviewBaseService service, String url,
@Nonnull CompositorListener compositorListener) {
mCompositorListener = compositorListener; mCompositorListener = compositorListener;
mNativePaintPreviewPlayerMediator = if (service != null && service.getNativePaintPreviewBaseService() != 0) {
PlayerCompositorDelegateImplJni.get().initialize(this, url); mNativePlayerCompositorDelegate = PlayerCompositorDelegateImplJni.get().initialize(
this, service.getNativePaintPreviewBaseService(), url);
}
// TODO(crbug.com/1021590): Handle initialization errors when
// mNativePlayerCompositorDelegate == 0.
} }
/** /**
* Called by native when the Paint Preview compositor is ready. * Called by native when the Paint Preview compositor is ready.
*
* @param rootFrameGuid The GUID for the root frame. * @param rootFrameGuid The GUID for the root frame.
* @param frameGuids Contains all frame GUIDs that are in this hierarchy. * @param frameGuids Contains all frame GUIDs that are in this hierarchy.
* @param frameContentSize Contains the content size for each frame. In native, this is called * @param frameContentSize Contains the content size for each frame. In native, this is called
...@@ -68,31 +75,32 @@ class PlayerCompositorDelegateImpl implements PlayerCompositorDelegate { ...@@ -68,31 +75,32 @@ class PlayerCompositorDelegateImpl implements PlayerCompositorDelegate {
@Override @Override
public void requestBitmap(long frameGuid, Rect clipRect, float scaleFactor, public void requestBitmap(long frameGuid, Rect clipRect, float scaleFactor,
Callback<Bitmap> bitmapCallback, Runnable errorCallback) { Callback<Bitmap> bitmapCallback, Runnable errorCallback) {
if (mNativePaintPreviewPlayerMediator == 0) return; if (mNativePlayerCompositorDelegate == 0) return;
PlayerCompositorDelegateImplJni.get().requestBitmap(mNativePaintPreviewPlayerMediator, PlayerCompositorDelegateImplJni.get().requestBitmap(mNativePlayerCompositorDelegate,
frameGuid, bitmapCallback, errorCallback, scaleFactor, clipRect.left, clipRect.top, frameGuid, bitmapCallback, errorCallback, scaleFactor, clipRect.left, clipRect.top,
clipRect.width(), clipRect.height()); clipRect.width(), clipRect.height());
} }
@Override @Override
public void onClick(long frameGuid, Point point) { public void onClick(long frameGuid, Point point) {
if (mNativePaintPreviewPlayerMediator == 0) return; if (mNativePlayerCompositorDelegate == 0) return;
PlayerCompositorDelegateImplJni.get().onClick( PlayerCompositorDelegateImplJni.get().onClick(
mNativePaintPreviewPlayerMediator, frameGuid, point.x, point.y); mNativePlayerCompositorDelegate, frameGuid, point.x, point.y);
} }
void destroy() { void destroy() {
if (mNativePaintPreviewPlayerMediator == 0) return; if (mNativePlayerCompositorDelegate == 0) return;
PlayerCompositorDelegateImplJni.get().destroy(mNativePaintPreviewPlayerMediator); PlayerCompositorDelegateImplJni.get().destroy(mNativePlayerCompositorDelegate);
mNativePaintPreviewPlayerMediator = 0; mNativePlayerCompositorDelegate = 0;
} }
@NativeMethods @NativeMethods
interface Natives { interface Natives {
long initialize(PlayerCompositorDelegateImpl caller, String url); long initialize(PlayerCompositorDelegateImpl caller, long nativePaintPreviewBaseService,
String url);
void destroy(long nativePlayerCompositorDelegateAndroid); void destroy(long nativePlayerCompositorDelegateAndroid);
void requestBitmap(long nativePlayerCompositorDelegateAndroid, long frameGuid, void requestBitmap(long nativePlayerCompositorDelegateAndroid, long frameGuid,
Callback<Bitmap> bitmapCallback, Runnable errorCallback, float scaleFactor, Callback<Bitmap> bitmapCallback, Runnable errorCallback, float scaleFactor,
......
...@@ -12,6 +12,7 @@ import android.widget.FrameLayout; ...@@ -12,6 +12,7 @@ import android.widget.FrameLayout;
import androidx.annotation.VisibleForTesting; import androidx.annotation.VisibleForTesting;
import org.chromium.components.paintpreview.browser.PaintPreviewBaseService;
import org.chromium.components.paintpreview.player.frame.PlayerFrameCoordinator; import org.chromium.components.paintpreview.player.frame.PlayerFrameCoordinator;
import java.util.HashMap; import java.util.HashMap;
...@@ -27,9 +28,9 @@ public class PlayerManager { ...@@ -27,9 +28,9 @@ public class PlayerManager {
private PlayerFrameCoordinator mRootFrameCoordinator; private PlayerFrameCoordinator mRootFrameCoordinator;
private FrameLayout mHostView; private FrameLayout mHostView;
public PlayerManager(Context context, String url) { public PlayerManager(Context context, PaintPreviewBaseService service, String url) {
mContext = context; mContext = context;
mDelegate = new PlayerCompositorDelegateImpl(url, this::onCompositorReady); mDelegate = new PlayerCompositorDelegateImpl(service, url, this::onCompositorReady);
mHostView = new FrameLayout(mContext); mHostView = new FrameLayout(mContext);
} }
...@@ -55,6 +56,7 @@ public class PlayerManager { ...@@ -55,6 +56,7 @@ public class PlayerManager {
* This method builds a hierarchy of {@link PaintPreviewFrame}s from primitive variables * This method builds a hierarchy of {@link PaintPreviewFrame}s from primitive variables
* that originate from native. Detailed explanation of the parameters can be found in * that originate from native. Detailed explanation of the parameters can be found in
* {@link PlayerCompositorDelegateImpl#onCompositorReady}. * {@link PlayerCompositorDelegateImpl#onCompositorReady}.
*
* @return The root {@link PaintPreviewFrame} * @return The root {@link PaintPreviewFrame}
*/ */
@VisibleForTesting @VisibleForTesting
......
...@@ -10,6 +10,7 @@ ...@@ -10,6 +10,7 @@
#include "base/android/jni_array.h" #include "base/android/jni_array.h"
#include "base/android/jni_string.h" #include "base/android/jni_string.h"
#include "base/bind.h" #include "base/bind.h"
#include "components/paint_preview/browser/paint_preview_base_service.h"
#include "components/paint_preview/player/android/jni_headers/PlayerCompositorDelegateImpl_jni.h" #include "components/paint_preview/player/android/jni_headers/PlayerCompositorDelegateImpl_jni.h"
#include "components/services/paint_preview_compositor/public/mojom/paint_preview_compositor.mojom.h" #include "components/services/paint_preview_compositor/public/mojom/paint_preview_compositor.mojom.h"
#include "third_party/skia/include/core/SkBitmap.h" #include "third_party/skia/include/core/SkBitmap.h"
...@@ -26,23 +27,35 @@ namespace paint_preview { ...@@ -26,23 +27,35 @@ namespace paint_preview {
jlong JNI_PlayerCompositorDelegateImpl_Initialize( jlong JNI_PlayerCompositorDelegateImpl_Initialize(
JNIEnv* env, JNIEnv* env,
const JavaParamRef<jobject>& j_object, const JavaParamRef<jobject>& j_object,
jlong paint_preview_service,
const JavaParamRef<jstring>& j_string_url) { const JavaParamRef<jstring>& j_string_url) {
PlayerCompositorDelegateAndroid* mediator = PlayerCompositorDelegateAndroid* delegate =
new PlayerCompositorDelegateAndroid(env, j_object, j_string_url); new PlayerCompositorDelegateAndroid(
return reinterpret_cast<intptr_t>(mediator); env, j_object,
reinterpret_cast<PaintPreviewBaseService*>(paint_preview_service),
j_string_url);
return reinterpret_cast<intptr_t>(delegate);
} }
PlayerCompositorDelegateAndroid::PlayerCompositorDelegateAndroid( PlayerCompositorDelegateAndroid::PlayerCompositorDelegateAndroid(
JNIEnv* env, JNIEnv* env,
const JavaParamRef<jobject>& j_object, const JavaParamRef<jobject>& j_object,
PaintPreviewBaseService* paint_preview_service,
const JavaParamRef<jstring>& j_string_url) const JavaParamRef<jstring>& j_string_url)
: PlayerCompositorDelegate( : PlayerCompositorDelegate(
paint_preview_service,
GURL(base::android::ConvertJavaStringToUTF16(env, j_string_url))) { GURL(base::android::ConvertJavaStringToUTF16(env, j_string_url))) {
java_ref_.Reset(env, j_object); java_ref_.Reset(env, j_object);
} }
void PlayerCompositorDelegateAndroid::OnCompositorReady( void PlayerCompositorDelegateAndroid::OnCompositorReady(
const mojom::PaintPreviewBeginCompositeResponse& composite_response) { mojom::PaintPreviewCompositor::Status status,
mojom::PaintPreviewBeginCompositeResponsePtr composite_response) {
if (status != mojom::PaintPreviewCompositor::Status::kSuccess) {
// TODO(crbug.com/1021590): Handle initialization errors.
return;
}
JNIEnv* env = base::android::AttachCurrentThread(); JNIEnv* env = base::android::AttachCurrentThread();
// We use int64_t instead of uint64_t because (i) there is no equivalent // We use int64_t instead of uint64_t because (i) there is no equivalent
...@@ -54,7 +67,7 @@ void PlayerCompositorDelegateAndroid::OnCompositorReady( ...@@ -54,7 +67,7 @@ void PlayerCompositorDelegateAndroid::OnCompositorReady(
std::vector<int64_t> subframe_ids; std::vector<int64_t> subframe_ids;
std::vector<int> subframe_rects; std::vector<int> subframe_rects;
CompositeResponseFramesToVectors(composite_response.frames, &all_guids, CompositeResponseFramesToVectors(composite_response->frames, &all_guids,
&scroll_extents, &subframe_count, &scroll_extents, &subframe_count,
&subframe_ids, &subframe_rects); &subframe_ids, &subframe_rects);
...@@ -70,7 +83,7 @@ void PlayerCompositorDelegateAndroid::OnCompositorReady( ...@@ -70,7 +83,7 @@ void PlayerCompositorDelegateAndroid::OnCompositorReady(
base::android::ToJavaIntArray(env, subframe_rects); base::android::ToJavaIntArray(env, subframe_rects);
Java_PlayerCompositorDelegateImpl_onCompositorReady( Java_PlayerCompositorDelegateImpl_onCompositorReady(
env, java_ref_, composite_response.root_frame_guid, j_all_guids, env, java_ref_, composite_response->root_frame_guid, j_all_guids,
j_scroll_extents, j_subframe_count, j_subframe_ids, j_subframe_rects); j_scroll_extents, j_subframe_count, j_subframe_ids, j_subframe_rects);
} }
......
...@@ -13,16 +13,19 @@ ...@@ -13,16 +13,19 @@
class SkBitmap; class SkBitmap;
namespace paint_preview { namespace paint_preview {
class PaintPreviewBaseService;
class PlayerCompositorDelegateAndroid : public PlayerCompositorDelegate { class PlayerCompositorDelegateAndroid : public PlayerCompositorDelegate {
public: public:
PlayerCompositorDelegateAndroid( PlayerCompositorDelegateAndroid(
JNIEnv* env, JNIEnv* env,
const base::android::JavaParamRef<jobject>& jobject, const base::android::JavaParamRef<jobject>& jobject,
PaintPreviewBaseService* paint_preview_service,
const base::android::JavaParamRef<jstring>& j_string_url); const base::android::JavaParamRef<jstring>& j_string_url);
void OnCompositorReady(const mojom::PaintPreviewBeginCompositeResponse& void OnCompositorReady(
composite_response) override; mojom::PaintPreviewCompositor::Status status,
mojom::PaintPreviewBeginCompositeResponsePtr composite_response) override;
// Called from Java when there is a request for a new bitmap. When the bitmap // Called from Java when there is a request for a new bitmap. When the bitmap
// is ready, it will be passed to j_bitmap_callback. In case of any failure, // is ready, it will be passed to j_bitmap_callback. In case of any failure,
......
...@@ -4,36 +4,134 @@ ...@@ -4,36 +4,134 @@
#include "components/paint_preview/player/player_compositor_delegate.h" #include "components/paint_preview/player/player_compositor_delegate.h"
#include <string>
#include <utility>
#include <vector> #include <vector>
#include "base/callback.h" #include "base/callback.h"
#include "base/containers/flat_map.h"
#include "base/files/file_path.h"
#include "base/memory/read_only_shared_memory_region.h"
#include "base/memory/weak_ptr.h"
#include "base/optional.h"
#include "base/strings/string_piece.h"
#include "components/paint_preview/browser/compositor_utils.h"
#include "components/paint_preview/browser/paint_preview_base_service.h"
#include "components/paint_preview/common/proto/paint_preview.pb.h"
#include "components/paint_preview/public/paint_preview_compositor_client.h"
#include "components/paint_preview/public/paint_preview_compositor_service.h"
#include "components/services/paint_preview_compositor/public/mojom/paint_preview_compositor.mojom.h" #include "components/services/paint_preview_compositor/public/mojom/paint_preview_compositor.mojom.h"
#include "mojo/public/cpp/bindings/remote.h"
#include "third_party/skia/include/core/SkBitmap.h" #include "third_party/skia/include/core/SkBitmap.h"
#include "ui/gfx/geometry/rect.h" #include "ui/gfx/geometry/rect.h"
#include "url/gurl.h" #include "url/gurl.h"
namespace paint_preview { namespace paint_preview {
namespace {
PlayerCompositorDelegate::PlayerCompositorDelegate(const GURL& url) { base::flat_map<uint64_t, base::File> CreateFileMapFromProto(
// TODO(crbug.com/1019885): Use url to get proto and file map. const paint_preview::PaintPreviewProto& proto) {
// TODO(crbug.com/1019885): Initialize the PaintPreviewCompositor class. std::vector<std::pair<uint64_t, base::File>> entries;
entries.reserve(1 + proto.subframes_size());
uint64_t root_frame_id = proto.root_frame().id();
base::BasicStringPiece<std::string> root_frame_file_path =
proto.root_frame().file_path();
entries.emplace_back(
root_frame_id, base::File(base::FilePath(root_frame_file_path),
base::File::FLAG_OPEN | base::File::FLAG_READ));
for (int i = 0; i < proto.subframes_size(); ++i) {
uint64_t frame_id = proto.subframes(i).id();
base::BasicStringPiece<std::string> frame_file_path =
proto.subframes(i).file_path();
entries.emplace_back(
frame_id, base::File(base::FilePath(frame_file_path),
base::File::FLAG_OPEN | base::File::FLAG_READ));
}
return base::flat_map<uint64_t, base::File>(std::move(entries));
}
base::Optional<base::ReadOnlySharedMemoryRegion> ToReadOnlySharedMemory(
const paint_preview::PaintPreviewProto& proto) {
auto region = base::WritableSharedMemoryRegion::Create(proto.ByteSizeLong());
if (!region.IsValid())
return base::nullopt;
auto mapping = region.Map();
if (!mapping.IsValid())
return base::nullopt;
proto.SerializeToArray(mapping.memory(), mapping.size());
return base::WritableSharedMemoryRegion::ConvertToReadOnly(std::move(region));
}
} // namespace
PlayerCompositorDelegate::PlayerCompositorDelegate(
PaintPreviewBaseService* paint_preview_service,
const GURL& url)
: paint_preview_service_(paint_preview_service) {
paint_preview_compositor_service_ =
paint_preview_service_->StartCompositorService(base::BindOnce(
&PlayerCompositorDelegate::OnCompositorServiceDisconnected,
weak_factory_.GetWeakPtr()));
paint_preview_compositor_client_ =
paint_preview_compositor_service_->CreateCompositor(
base::BindOnce(&PlayerCompositorDelegate::OnCompositorClientCreated,
weak_factory_.GetWeakPtr(), url));
paint_preview_compositor_client_->SetDisconnectHandler(
base::BindOnce(&PlayerCompositorDelegate::OnCompositorClientDisconnected,
weak_factory_.GetWeakPtr()));
}
void PlayerCompositorDelegate::OnCompositorServiceDisconnected() {
// TODO(crbug.com/1039699): Handle compositor service disconnect event.
}
void PlayerCompositorDelegate::OnCompositorClientCreated(const GURL& url) {
paint_preview_compositor_client_->SetRootFrameUrl(url);
base::Optional<PaintPreviewProto> proto =
paint_preview_service_->GetCapturedPaintPreviewProto(url);
if (!proto || !proto.value().IsInitialized()) {
// TODO(crbug.com/1021590): Handle initialization errors.
return;
}
// TODO(crbug.com/1034111): Investigate executing this in the background.
mojom::PaintPreviewBeginCompositeRequestPtr begin_composite_request =
mojom::PaintPreviewBeginCompositeRequest::New();
begin_composite_request->file_map = CreateFileMapFromProto(proto.value());
// TODO(crbug.com/1034111): Don't perform this on UI thread.
auto read_only_proto = ToReadOnlySharedMemory(proto.value());
if (!read_only_proto) {
// TODO(crbug.com/1021590): Handle initialization errors.
return;
}
begin_composite_request->proto = std::move(read_only_proto.value());
paint_preview_compositor_client_->BeginComposite(
std::move(begin_composite_request),
base::BindOnce(&PlayerCompositorDelegate::OnCompositorReady,
weak_factory_.GetWeakPtr()));
// TODO(crbug.com/1019883): Initialize the HitTester class. // TODO(crbug.com/1019883): Initialize the HitTester class.
} }
void PlayerCompositorDelegate::OnCompositorClientDisconnected() {
// TODO(crbug.com/1039699): Handle compositor client disconnect event.
}
void PlayerCompositorDelegate::RequestBitmap( void PlayerCompositorDelegate::RequestBitmap(
uint64_t frame_guid, uint64_t frame_guid,
const gfx::Rect& clip_rect, const gfx::Rect& clip_rect,
float scale_factor, float scale_factor,
base::OnceCallback<void(mojom::PaintPreviewCompositor::Status, base::OnceCallback<void(mojom::PaintPreviewCompositor::Status,
const SkBitmap&)> callback) { const SkBitmap&)> callback) {
if (!paint_preview_compositor_ || !paint_preview_compositor_.is_bound()) { if (!paint_preview_compositor_client_) {
std::move(callback).Run( std::move(callback).Run(
mojom::PaintPreviewCompositor::Status::kCompositingFailure, SkBitmap()); mojom::PaintPreviewCompositor::Status::kCompositingFailure, SkBitmap());
return; return;
} }
paint_preview_compositor_->BitmapForFrame(frame_guid, clip_rect, scale_factor, paint_preview_compositor_client_->BitmapForFrame(
std::move(callback)); frame_guid, clip_rect, scale_factor, std::move(callback));
} }
void PlayerCompositorDelegate::OnClick(uint64_t frame_guid, int x, int y) { void PlayerCompositorDelegate::OnClick(uint64_t frame_guid, int x, int y) {
......
...@@ -6,23 +6,32 @@ ...@@ -6,23 +6,32 @@
#define COMPONENTS_PAINT_PREVIEW_PLAYER_PLAYER_COMPOSITOR_DELEGATE_H_ #define COMPONENTS_PAINT_PREVIEW_PLAYER_PLAYER_COMPOSITOR_DELEGATE_H_
#include "base/callback_forward.h" #include "base/callback_forward.h"
#include "base/containers/flat_map.h"
#include "base/memory/weak_ptr.h"
#include "base/optional.h"
#include "components/paint_preview/browser/paint_preview_base_service.h"
#include "components/paint_preview/public/paint_preview_compositor_client.h"
#include "components/paint_preview/public/paint_preview_compositor_service.h"
#include "components/services/paint_preview_compositor/public/mojom/paint_preview_compositor.mojom.h" #include "components/services/paint_preview_compositor/public/mojom/paint_preview_compositor.mojom.h"
#include "mojo/public/cpp/bindings/remote.h" #include "mojo/public/cpp/bindings/remote.h"
namespace gfx { namespace gfx {
class Rect; class Rect;
} } // namespace gfx
class SkBitmap; class SkBitmap;
class GURL;
namespace paint_preview { namespace paint_preview {
class PlayerCompositorDelegate { class PlayerCompositorDelegate {
public: public:
PlayerCompositorDelegate(const GURL& url); PlayerCompositorDelegate(PaintPreviewBaseService* paint_preview_service,
const GURL& url);
virtual void OnCompositorReady( virtual void OnCompositorReady(
const mojom::PaintPreviewBeginCompositeResponse& composite_response) = 0; mojom::PaintPreviewCompositor::Status status,
mojom::PaintPreviewBeginCompositeResponsePtr composite_response) = 0;
// Called when there is a request for a new bitmap. When the bitmap // Called when there is a request for a new bitmap. When the bitmap
// is ready, it will be passed to callback. // is ready, it will be passed to callback.
...@@ -40,8 +49,18 @@ class PlayerCompositorDelegate { ...@@ -40,8 +49,18 @@ class PlayerCompositorDelegate {
virtual ~PlayerCompositorDelegate(); virtual ~PlayerCompositorDelegate();
private: private:
// The current instance of PaintPreviewCompositor. void OnCompositorServiceDisconnected();
mojo::Remote<mojom::PaintPreviewCompositor> paint_preview_compositor_;
void OnCompositorClientCreated(const GURL& url);
void OnCompositorClientDisconnected();
PaintPreviewBaseService* paint_preview_service_;
std::unique_ptr<PaintPreviewCompositorService>
paint_preview_compositor_service_;
std::unique_ptr<PaintPreviewCompositorClient>
paint_preview_compositor_client_;
base::WeakPtrFactory<PlayerCompositorDelegate> weak_factory_{this};
PlayerCompositorDelegate(const PlayerCompositorDelegate&) = delete; PlayerCompositorDelegate(const PlayerCompositorDelegate&) = delete;
PlayerCompositorDelegate& operator=(const PlayerCompositorDelegate&) = delete; PlayerCompositorDelegate& operator=(const PlayerCompositorDelegate&) = delete;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment