Commit c05be04a authored by Shakti Sahu's avatar Shakti Sahu Committed by Commit Bot

Video Tutorials : Added Factory and JNI bridge for VideoTutorialService

Bug: 1114883
Change-Id: I4b067fd6fa31feb0673186170f116134619f6e69
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2347915
Commit-Queue: Shakti Sahu <shaktisahu@chromium.org>
Reviewed-by: default avatarDavid Trainor <dtrainor@chromium.org>
Reviewed-by: default avatarHesen Zhang <hesen@chromium.org>
Cr-Commit-Position: refs/heads/master@{#800285}
parent 74b1ba65
...@@ -3171,6 +3171,7 @@ generate_jni("chrome_jni_headers") { ...@@ -3171,6 +3171,7 @@ generate_jni("chrome_jni_headers") {
"java/src/org/chromium/chrome/browser/translate/TranslateBridge.java", "java/src/org/chromium/chrome/browser/translate/TranslateBridge.java",
"java/src/org/chromium/chrome/browser/usage_stats/NotificationSuspender.java", "java/src/org/chromium/chrome/browser/usage_stats/NotificationSuspender.java",
"java/src/org/chromium/chrome/browser/usage_stats/UsageStatsBridge.java", "java/src/org/chromium/chrome/browser/usage_stats/UsageStatsBridge.java",
"java/src/org/chromium/chrome/browser/video_tutorials/VideoTutorialServiceFactory.java",
"java/src/org/chromium/chrome/browser/webapps/WebApkHandlerDelegate.java", "java/src/org/chromium/chrome/browser/webapps/WebApkHandlerDelegate.java",
"java/src/org/chromium/chrome/browser/webapps/WebApkInstallService.java", "java/src/org/chromium/chrome/browser/webapps/WebApkInstallService.java",
"java/src/org/chromium/chrome/browser/webapps/WebApkInstaller.java", "java/src/org/chromium/chrome/browser/webapps/WebApkInstaller.java",
......
...@@ -1683,6 +1683,7 @@ chrome_java_sources = [ ...@@ -1683,6 +1683,7 @@ chrome_java_sources = [
"java/src/org/chromium/chrome/browser/user_education/IPHCommand.java", "java/src/org/chromium/chrome/browser/user_education/IPHCommand.java",
"java/src/org/chromium/chrome/browser/user_education/IPHCommandBuilder.java", "java/src/org/chromium/chrome/browser/user_education/IPHCommandBuilder.java",
"java/src/org/chromium/chrome/browser/user_education/UserEducationHelper.java", "java/src/org/chromium/chrome/browser/user_education/UserEducationHelper.java",
"java/src/org/chromium/chrome/browser/video_tutorials/VideoTutorialServiceFactory.java",
"java/src/org/chromium/chrome/browser/vr/ArDelegate.java", "java/src/org/chromium/chrome/browser/vr/ArDelegate.java",
"java/src/org/chromium/chrome/browser/vr/ArDelegateProvider.java", "java/src/org/chromium/chrome/browser/vr/ArDelegateProvider.java",
"java/src/org/chromium/chrome/browser/webapps/ActivateWebApkActivity.java", "java/src/org/chromium/chrome/browser/webapps/ActivateWebApkActivity.java",
......
file://chrome/browser/video_tutorials/OWNERS
# TEAM: chrome-upboarding@chromium.org
# COMPONENT: Upboarding>VideoTutorials
# OS: Android
// Copyright 2020 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.chrome.browser.video_tutorials;
import org.chromium.base.annotations.NativeMethods;
import org.chromium.chrome.browser.profiles.Profile;
/**
* Basic factory that creates and returns an {@link VideoTutorialService} that is attached
* natively to the given {@link Profile}.
*/
public class VideoTutorialServiceFactory {
private static VideoTutorialService sVideoTutorialServiceForTesting;
/**
* Used to get access to the video tutorials backend.
* @return An {@link VideoTutorialService} instance.
*/
public static VideoTutorialService getForProfile(Profile profile) {
if (sVideoTutorialServiceForTesting != null) return sVideoTutorialServiceForTesting;
return VideoTutorialServiceFactoryJni.get().getForProfile(profile);
}
/** For testing only. */
public static void setVideoTutorialServiceForTesting(VideoTutorialService provider) {
sVideoTutorialServiceForTesting = provider;
}
@NativeMethods
interface Natives {
VideoTutorialService getForProfile(Profile profile);
}
}
\ No newline at end of file
...@@ -2950,6 +2950,7 @@ static_library("browser") { ...@@ -2950,6 +2950,7 @@ static_library("browser") {
"updates/update_notification_client.h", "updates/update_notification_client.h",
"updates/update_notification_service_bridge_android.cc", "updates/update_notification_service_bridge_android.cc",
"updates/update_notification_service_bridge_android.h", "updates/update_notification_service_bridge_android.h",
"video_tutorials/android/video_tutorial_service_bridge_factory.cc",
"webauthn/android/cable_module_android.cc", "webauthn/android/cable_module_android.cc",
] ]
public_deps += [ public_deps += [
......
...@@ -17,8 +17,6 @@ group("video_tutorials") { ...@@ -17,8 +17,6 @@ group("video_tutorials") {
public_deps = [ public_deps = [
":factory", ":factory",
":public", ":public",
"//base",
"//url:url",
] ]
deps = [ "internal" ] deps = [ "internal" ]
...@@ -33,11 +31,22 @@ source_set("public") { ...@@ -33,11 +31,22 @@ source_set("public") {
"video_tutorial_service.h", "video_tutorial_service.h",
] ]
deps = []
public_deps = [ public_deps = [
"//base", "//base",
"//components/keyed_service/core", "//components/keyed_service/core",
"//url:url", "//url:url",
] ]
if (is_android) {
sources += [
"android/video_tutorial_service_bridge.cc",
"android/video_tutorial_service_bridge.h",
]
deps += [ ":jni_headers" ]
}
} }
source_set("factory") { source_set("factory") {
...@@ -59,13 +68,23 @@ if (is_android) { ...@@ -59,13 +68,23 @@ if (is_android) {
sources = [ sources = [
"android/java/src/org/chromium/chrome/browser/video_tutorials/Tutorial.java", "android/java/src/org/chromium/chrome/browser/video_tutorials/Tutorial.java",
"android/java/src/org/chromium/chrome/browser/video_tutorials/VideoTutorialService.java", "android/java/src/org/chromium/chrome/browser/video_tutorials/VideoTutorialService.java",
"android/java/src/org/chromium/chrome/browser/video_tutorials/bridges/VideoTutorialServiceBridge.java",
] ]
deps = [ deps = [
"//base:base_java", "//base:base_java",
"//base:jni_java",
"//components/browser_ui/widget/android:java", "//components/browser_ui/widget/android:java",
"//third_party/android_deps:androidx_annotation_annotation_java", "//third_party/android_deps:androidx_annotation_annotation_java",
] ]
annotation_processor_deps = [ "//base/android/jni_generator:jni_processor" ]
}
generate_jni("jni_headers") {
visibility = [ ":*" ]
sources = [ "android/java/src/org/chromium/chrome/browser/video_tutorials/bridges/VideoTutorialServiceBridge.java" ]
} }
} }
......
// Copyright 2020 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.chrome.browser.video_tutorials.bridges;
import org.chromium.base.Callback;
import org.chromium.base.annotations.CalledByNative;
import org.chromium.base.annotations.JNINamespace;
import org.chromium.chrome.browser.video_tutorials.Tutorial;
import org.chromium.chrome.browser.video_tutorials.VideoTutorialService;
import java.util.List;
/**
* Bridge to the native video tutorial service for the given {@link Profile}.
*/
@JNINamespace("video_tutorials")
public class VideoTutorialServiceBridge implements VideoTutorialService {
private long mNativeVideoTutorialServiceBridge;
@CalledByNative
private static VideoTutorialServiceBridge create(long nativePtr) {
return new VideoTutorialServiceBridge(nativePtr);
}
private VideoTutorialServiceBridge(long nativePtr) {
mNativeVideoTutorialServiceBridge = nativePtr;
}
@Override
public void getTutorials(Callback<List<Tutorial>> callback) {
callback.onResult(null);
}
@CalledByNative
private void clearNativePtr() {
mNativeVideoTutorialServiceBridge = 0;
}
}
// Copyright 2020 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.
#include "chrome/browser/video_tutorials/android/video_tutorial_service_bridge.h"
#include <memory>
#include <vector>
#include "base/android/callback_android.h"
#include "base/android/jni_string.h"
#include "base/bind.h"
#include "chrome/browser/video_tutorials/jni_headers/VideoTutorialServiceBridge_jni.h"
using base::android::AttachCurrentThread;
namespace video_tutorials {
namespace {
const char kVideoTutorialServiceBridgeKey[] = "video_tutorial_service_bridge";
} // namespace
// static
ScopedJavaLocalRef<jobject>
VideoTutorialServiceBridge::GetBridgeForVideoTutorialService(
VideoTutorialService* video_tutorial_service) {
if (!video_tutorial_service->GetUserData(kVideoTutorialServiceBridgeKey)) {
video_tutorial_service->SetUserData(
kVideoTutorialServiceBridgeKey,
std::make_unique<VideoTutorialServiceBridge>(video_tutorial_service));
}
VideoTutorialServiceBridge* bridge = static_cast<VideoTutorialServiceBridge*>(
video_tutorial_service->GetUserData(kVideoTutorialServiceBridgeKey));
return ScopedJavaLocalRef<jobject>(bridge->java_obj_);
}
VideoTutorialServiceBridge::VideoTutorialServiceBridge(
VideoTutorialService* video_tutorial_service)
: video_tutorial_service_(video_tutorial_service) {
DCHECK(video_tutorial_service_);
JNIEnv* env = base::android::AttachCurrentThread();
java_obj_.Reset(env, Java_VideoTutorialServiceBridge_create(
env, reinterpret_cast<int64_t>(this))
.obj());
}
VideoTutorialServiceBridge::~VideoTutorialServiceBridge() {
JNIEnv* env = base::android::AttachCurrentThread();
Java_VideoTutorialServiceBridge_clearNativePtr(env, java_obj_);
}
} // namespace video_tutorials
// Copyright 2020 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.
#ifndef CHROME_BROWSER_VIDEO_TUTORIALS_ANDROID_VIDEO_TUTORIAL_SERVICE_BRIDGE_H_
#define CHROME_BROWSER_VIDEO_TUTORIALS_ANDROID_VIDEO_TUTORIAL_SERVICE_BRIDGE_H_
#include "base/android/jni_android.h"
#include "base/supports_user_data.h"
#include "chrome/browser/video_tutorials/video_tutorial_service.h"
using base::android::JavaParamRef;
using base::android::JavaRef;
using base::android::ScopedJavaGlobalRef;
using base::android::ScopedJavaLocalRef;
namespace video_tutorials {
// Helper class responsible for bridging the VideoTutorialService between C++
// and Java.
class VideoTutorialServiceBridge : public base::SupportsUserData::Data {
public:
// Returns a Java VideoTutorialServiceBridge for |video_tutorial_service|.
// There will be only one bridge per VideoTutorialServiceBridge.
static ScopedJavaLocalRef<jobject> GetBridgeForVideoTutorialService(
VideoTutorialService* video_tutorial_service);
explicit VideoTutorialServiceBridge(
VideoTutorialService* video_tutorial_service);
~VideoTutorialServiceBridge() override;
private:
// A reference to the Java counterpart of this class. See
// VideoTutorialServiceBridge.java.
ScopedJavaGlobalRef<jobject> java_obj_;
// Not owned.
VideoTutorialService* video_tutorial_service_;
DISALLOW_COPY_AND_ASSIGN(VideoTutorialServiceBridge);
};
} // namespace video_tutorials
#endif // CHROME_BROWSER_VIDEO_TUTORIALS_ANDROID_VIDEO_TUTORIAL_SERVICE_BRIDGE_H_
// Copyright 2020 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.
#include "base/android/jni_android.h"
#include "chrome/android/chrome_jni_headers/VideoTutorialServiceFactory_jni.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/profiles/profile_android.h"
#include "chrome/browser/profiles/profile_key.h"
#include "chrome/browser/video_tutorials/android/video_tutorial_service_bridge.h"
#include "chrome/browser/video_tutorials/video_tutorial_service_factory.h"
// Takes a Java Profile and returns a Java VideoTutorialService.
static base::android::ScopedJavaLocalRef<jobject>
JNI_VideoTutorialServiceFactory_GetForProfile(
JNIEnv* env,
const base::android::JavaParamRef<jobject>& j_profile) {
Profile* profile = ProfileAndroid::FromProfileAndroid(j_profile);
ProfileKey* profile_key = profile->GetProfileKey();
// Return null if there is no reasonable context for the provided Java
// profile.
if (profile_key == nullptr)
return base::android::ScopedJavaLocalRef<jobject>();
video_tutorials::VideoTutorialService* tile_service =
video_tutorials::VideoTutorialServiceFactory::GetInstance()->GetForKey(
profile_key);
return video_tutorials::VideoTutorialServiceBridge::
GetBridgeForVideoTutorialService(tile_service);
}
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