Commit 43680be2 authored by Mohamed Heikal's avatar Mohamed Heikal Committed by Commit Bot

[Android] Java code now passes profile key in JNI calls

Previously some JNI calls that used to pass Profile.java objects to
native, were switched to use ProfileKey. However, since there was no
implementation of ProfileKey.java, the JNI calls passed nothing from
Java's side and native called ProfileKey's equivalent of
ProfileManager::GetLastUsedProfile.

Now that ProfileKey.java exists, JNI calls should pass in
ProfileKey.java objects to native instead.

Change-Id: I90f83e4c837a6d12613354c9bca5a8d52bf01d31
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1657752
Commit-Queue: Mohamed Heikal <mheikal@chromium.org>
Reviewed-by: default avatarXi Han <hanxi@chromium.org>
Reviewed-by: default avatarDavid Trainor <dtrainor@chromium.org>
Reviewed-by: default avatarHenrique Nakashima <hnakashima@chromium.org>
Reviewed-by: default avatarMin Qin <qinmin@chromium.org>
Cr-Commit-Position: refs/heads/master@{#672153}
parent 55db827b
......@@ -10,6 +10,7 @@ import org.chromium.base.Callback;
import org.chromium.base.annotations.JNINamespace;
import org.chromium.base.library_loader.LibraryProcessType;
import org.chromium.chrome.browser.background_task_scheduler.NativeBackgroundTask;
import org.chromium.chrome.browser.profiles.ProfileKey;
import org.chromium.chrome.browser.util.FeatureUtilities;
import org.chromium.components.background_task_scheduler.TaskParameters;
import org.chromium.components.download.DownloadTaskType;
......@@ -58,8 +59,9 @@ public class DownloadBackgroundTask extends NativeBackgroundTask {
assert BrowserStartupController.get(LibraryProcessType.PROCESS_BROWSER)
.isStartupSuccessfullyCompleted()
|| mStartsServiceManagerOnly;
ProfileKey key = ProfileKey.getLastUsedProfileKey().getOriginalKey();
nativeStartBackgroundTask(
mCurrentTaskType, needsReschedule -> callback.taskFinished(needsReschedule));
key, mCurrentTaskType, needsReschedule -> callback.taskFinished(needsReschedule));
}
@Override
......@@ -76,8 +78,8 @@ public class DownloadBackgroundTask extends NativeBackgroundTask {
protected boolean onStopTaskWithNative(Context context, TaskParameters taskParameters) {
@DownloadTaskType
int taskType = taskParameters.getExtras().getInt(DownloadTaskScheduler.EXTRA_TASK_TYPE);
return nativeStopBackgroundTask(taskType);
ProfileKey key = ProfileKey.getLastUsedProfileKey().getOriginalKey();
return nativeStopBackgroundTask(key, taskType);
}
@Override
......@@ -85,6 +87,7 @@ public class DownloadBackgroundTask extends NativeBackgroundTask {
DownloadTaskScheduler.rescheduleAllTasks();
}
private native void nativeStartBackgroundTask(int taskType, Callback<Boolean> callback);
private native boolean nativeStopBackgroundTask(int taskType);
private native void nativeStartBackgroundTask(
ProfileKey key, int taskType, Callback<Boolean> callback);
private native boolean nativeStopBackgroundTask(ProfileKey key, int taskType);
}
......@@ -6,6 +6,7 @@ package org.chromium.chrome.browser.offlinepages.prefetch;
import org.chromium.base.annotations.JNINamespace;
import org.chromium.chrome.browser.ChromeFeatureList;
import org.chromium.chrome.browser.profiles.ProfileKey;
/**
* Allows the querying and setting of Offline Prefetch related configurations.
......@@ -26,14 +27,14 @@ public class PrefetchConfiguration {
* user setting to be true. If the current browser Profile is null this method returns false.
*/
public static boolean isPrefetchingEnabled() {
return nativeIsPrefetchingEnabled();
return nativeIsPrefetchingEnabled(ProfileKey.getLastUsedProfileKey());
}
/**
* Return the value of offline_pages.enabled_by_server pref.
*/
public static boolean isPrefetchingEnabledByServer() {
return nativeIsEnabledByServer();
return nativeIsEnabledByServer(ProfileKey.getLastUsedProfileKey());
}
/**
......@@ -42,14 +43,14 @@ public class PrefetchConfiguration {
* since the last check.
*/
public static boolean isForbiddenCheckDue() {
return nativeIsForbiddenCheckDue();
return nativeIsForbiddenCheckDue(ProfileKey.getLastUsedProfileKey());
}
/**
* Returns true if the GeneratePageBundle-forbidden check has never run and is due to run.
*/
public static boolean isEnabledByServerUnknown() {
return nativeIsEnabledByServerUnknown();
return nativeIsEnabledByServerUnknown(ProfileKey.getLastUsedProfileKey());
}
/**
......@@ -57,7 +58,7 @@ public class PrefetchConfiguration {
* enabled or disabled. If the current browser Profile is null the setting will not be changed.
*/
public static void setPrefetchingEnabledInSettings(boolean enabled) {
nativeSetPrefetchingEnabledInSettings(enabled);
nativeSetPrefetchingEnabledInSettings(ProfileKey.getLastUsedProfileKey(), enabled);
}
/**
......@@ -65,13 +66,14 @@ public class PrefetchConfiguration {
* enabled or disabled.
*/
public static boolean isPrefetchingEnabledInSettings() {
return nativeIsPrefetchingEnabledInSettings();
return nativeIsPrefetchingEnabledInSettings(ProfileKey.getLastUsedProfileKey());
}
private static native boolean nativeIsPrefetchingEnabled();
private static native boolean nativeIsEnabledByServer();
private static native boolean nativeIsForbiddenCheckDue();
private static native boolean nativeIsEnabledByServerUnknown();
private static native void nativeSetPrefetchingEnabledInSettings(boolean enabled);
private static native boolean nativeIsPrefetchingEnabledInSettings();
private static native boolean nativeIsPrefetchingEnabled(ProfileKey key);
private static native boolean nativeIsEnabledByServer(ProfileKey key);
private static native boolean nativeIsForbiddenCheckDue(ProfileKey key);
private static native boolean nativeIsEnabledByServerUnknown(ProfileKey key);
private static native void nativeSetPrefetchingEnabledInSettings(
ProfileKey key, boolean enabled);
private static native boolean nativeIsPrefetchingEnabledInSettings(ProfileKey key);
}
......@@ -7,9 +7,9 @@
#include "base/callback_forward.h"
#include "chrome/android/chrome_jni_headers/DownloadBackgroundTask_jni.h"
#include "chrome/browser/android/download/download_manager_service.h"
#include "chrome/browser/android/profile_key_util.h"
#include "chrome/browser/download/download_service_factory.h"
#include "chrome/browser/profiles/profile_key.h"
#include "chrome/browser/profiles/profile_key_android.h"
#include "components/download/public/background_service/download_service.h"
#include "components/download/public/common/auto_resumption_handler.h"
#include "content/public/browser/browser_context.h"
......@@ -19,10 +19,10 @@ using base::android::JavaParamRef;
namespace download {
namespace android {
DownloadService* GetDownloadService() {
ProfileKey* profile_key = ::android::GetMainProfileKey();
DCHECK(profile_key);
return DownloadServiceFactory::GetForKey(profile_key);
DownloadService* GetDownloadService(const JavaParamRef<jobject>& jkey) {
ProfileKey* key = ProfileKeyAndroid::FromProfileKeyAndroid(jkey);
DCHECK(key);
return DownloadServiceFactory::GetForKey(key);
}
AutoResumptionHandler* GetAutoResumptionHandler() {
......@@ -34,9 +34,10 @@ AutoResumptionHandler* GetAutoResumptionHandler() {
// static
void JNI_DownloadBackgroundTask_StartBackgroundTask(
JNIEnv* env,
const base::android::JavaParamRef<jobject>& jcaller,
const JavaParamRef<jobject>& jcaller,
const JavaParamRef<jobject>& jkey,
jint task_type,
const base::android::JavaParamRef<jobject>& jcallback) {
const JavaParamRef<jobject>& jcallback) {
TaskFinishedCallback finish_callback =
base::BindOnce(&base::android::RunBooleanCallbackAndroid,
base::android::ScopedJavaGlobalRef<jobject>(jcallback));
......@@ -50,7 +51,7 @@ void JNI_DownloadBackgroundTask_StartBackgroundTask(
case download::DownloadTaskType::DOWNLOAD_TASK:
FALLTHROUGH;
case download::DownloadTaskType::CLEANUP_TASK:
GetDownloadService()->OnStartScheduledTask(
GetDownloadService(jkey)->OnStartScheduledTask(
static_cast<DownloadTaskType>(task_type), std::move(finish_callback));
break;
}
......@@ -59,7 +60,8 @@ void JNI_DownloadBackgroundTask_StartBackgroundTask(
// static
jboolean JNI_DownloadBackgroundTask_StopBackgroundTask(
JNIEnv* env,
const base::android::JavaParamRef<jobject>& jcaller,
const JavaParamRef<jobject>& jcaller,
const JavaParamRef<jobject>& jkey,
jint task_type) {
switch (static_cast<DownloadTaskType>(task_type)) {
case download::DownloadTaskType::DOWNLOAD_AUTO_RESUMPTION_TASK: {
......@@ -69,7 +71,7 @@ jboolean JNI_DownloadBackgroundTask_StopBackgroundTask(
case download::DownloadTaskType::DOWNLOAD_TASK:
FALLTHROUGH;
case download::DownloadTaskType::CLEANUP_TASK:
return GetDownloadService()->OnStopScheduledTask(
return GetDownloadService(jkey)->OnStopScheduledTask(
static_cast<DownloadTaskType>(task_type));
}
return false;
......
......@@ -4,15 +4,10 @@
#include "base/android/jni_android.h"
#include "chrome/android/chrome_jni_headers/PrefetchConfiguration_jni.h"
#include "chrome/browser/android/profile_key_util.h"
#include "chrome/browser/offline_pages/prefetch/prefetch_service_factory.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/profiles/profile_android.h"
#include "chrome/browser/profiles/profile_key.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "chrome/browser/profiles/profile_key_android.h"
#include "components/offline_pages/core/offline_page_feature.h"
#include "components/offline_pages/core/prefetch/prefetch_prefs.h"
#include "components/offline_pages/core/prefetch/prefetch_service.h"
using base::android::JavaParamRef;
......@@ -22,39 +17,51 @@ using base::android::JavaParamRef;
namespace offline_pages {
namespace android {
JNI_EXPORT jboolean
JNI_PrefetchConfiguration_IsPrefetchingEnabled(JNIEnv* env) {
return static_cast<jboolean>(
prefetch_prefs::IsEnabled(::android::GetMainProfileKey()->GetPrefs()));
JNI_EXPORT jboolean JNI_PrefetchConfiguration_IsPrefetchingEnabled(
JNIEnv* env,
const JavaParamRef<jobject>& jkey) {
ProfileKey* key = ProfileKeyAndroid::FromProfileKeyAndroid(jkey);
return static_cast<jboolean>(prefetch_prefs::IsEnabled(key->GetPrefs()));
}
JNI_EXPORT jboolean JNI_PrefetchConfiguration_IsEnabledByServer(JNIEnv* env) {
return static_cast<jboolean>(prefetch_prefs::IsEnabledByServer(
::android::GetMainProfileKey()->GetPrefs()));
JNI_EXPORT jboolean
JNI_PrefetchConfiguration_IsEnabledByServer(JNIEnv* env,
const JavaParamRef<jobject>& jkey) {
ProfileKey* key = ProfileKeyAndroid::FromProfileKeyAndroid(jkey);
return static_cast<jboolean>(
prefetch_prefs::IsEnabledByServer(key->GetPrefs()));
}
JNI_EXPORT jboolean JNI_PrefetchConfiguration_IsForbiddenCheckDue(JNIEnv* env) {
return static_cast<jboolean>(prefetch_prefs::IsForbiddenCheckDue(
::android::GetMainProfileKey()->GetPrefs()));
JNI_EXPORT jboolean JNI_PrefetchConfiguration_IsForbiddenCheckDue(
JNIEnv* env,
const JavaParamRef<jobject>& jkey) {
ProfileKey* key = ProfileKeyAndroid::FromProfileKeyAndroid(jkey);
return static_cast<jboolean>(
prefetch_prefs::IsForbiddenCheckDue(key->GetPrefs()));
}
JNI_EXPORT jboolean
JNI_PrefetchConfiguration_IsEnabledByServerUnknown(JNIEnv* env) {
return static_cast<jboolean>(prefetch_prefs::IsEnabledByServerUnknown(
::android::GetMainProfileKey()->GetPrefs()));
JNI_EXPORT jboolean JNI_PrefetchConfiguration_IsEnabledByServerUnknown(
JNIEnv* env,
const JavaParamRef<jobject>& jkey) {
ProfileKey* key = ProfileKeyAndroid::FromProfileKeyAndroid(jkey);
return static_cast<jboolean>(
prefetch_prefs::IsEnabledByServerUnknown(key->GetPrefs()));
}
JNI_EXPORT void JNI_PrefetchConfiguration_SetPrefetchingEnabledInSettings(
JNIEnv* env,
const JavaParamRef<jobject>& jkey,
jboolean enabled) {
prefetch_prefs::SetPrefetchingEnabledInSettings(
::android::GetMainProfileKey()->GetPrefs(), enabled);
ProfileKey* key = ProfileKeyAndroid::FromProfileKeyAndroid(jkey);
prefetch_prefs::SetPrefetchingEnabledInSettings(key->GetPrefs(), enabled);
}
JNI_EXPORT jboolean
JNI_PrefetchConfiguration_IsPrefetchingEnabledInSettings(JNIEnv* env) {
return static_cast<jboolean>(prefetch_prefs::IsPrefetchingEnabledInSettings(
::android::GetMainProfileKey()->GetPrefs()));
JNI_EXPORT jboolean JNI_PrefetchConfiguration_IsPrefetchingEnabledInSettings(
JNIEnv* env,
const JavaParamRef<jobject>& jkey) {
ProfileKey* key = ProfileKeyAndroid::FromProfileKeyAndroid(jkey);
return static_cast<jboolean>(
prefetch_prefs::IsPrefetchingEnabledInSettings(key->GetPrefs()));
}
} // namespace android
......
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