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