Chromium Code Reviews| Index: chrome/browser/android/offline_pages/background_scheduler_bridge.cc |
| diff --git a/chrome/browser/android/offline_pages/background_scheduler_bridge.cc b/chrome/browser/android/offline_pages/background_scheduler_bridge.cc |
| index 455c8b9452490308926da9c91fd774a9eff55638..f2ada1b259d67246f8527afb72274543e119ac71 100644 |
| --- a/chrome/browser/android/offline_pages/background_scheduler_bridge.cc |
| +++ b/chrome/browser/android/offline_pages/background_scheduler_bridge.cc |
| @@ -4,8 +4,11 @@ |
| #include "base/android/scoped_java_ref.h" |
| #include "chrome/browser/android/offline_pages/background_scheduler_bridge.h" |
| +#include "chrome/browser/android/offline_pages/offline_page_model_factory.h" |
| +#include "chrome/browser/android/offline_pages/request_coordinator_factory.h" |
| #include "chrome/browser/profiles/profile.h" |
| #include "chrome/browser/profiles/profile_android.h" |
| +#include "components/offline_pages/background/request_coordinator.h" |
| #include "jni/BackgroundSchedulerBridge_jni.h" |
| using base::android::ScopedJavaGlobalRef; |
| @@ -25,11 +28,39 @@ void ProcessingDoneCallback( |
| } // namespace |
| +// This static function acts as a factory to create the scheduler bridge if it |
| +// does not already exist, and re-use it if it does. It also attaches the |
| +// Java side of the bridge to the java_ref_ variable, and returns the c++ side |
| +// pointer for the java side to attach to. It is called from Java. |
| +// TODO(petewil): Is it better to use ScopedJavaLocalRef or ScopedJavaGlobalRef? |
| +static ScopedJavaLocalRef<jobject> GetBackgroundSchedulerBridgeForProfile( |
| + JNIEnv* env, |
| + const JavaParamRef<jclass>& jcaller, |
| + const JavaParamRef<jobject>& j_profile) { |
| + Profile* profile = ProfileAndroid::FromProfileAndroid(j_profile); |
| + |
| + // The bridge pointer is owned by the request coordinator. |
| + RequestCoordinator* request_coordinator = |
| + RequestCoordinatorFactory::GetForBrowserContext(profile); |
|
dewittj
2016/05/20 21:52:31
needs a null check here, we don't support incognit
Pete Williamson
2016/05/23 19:36:31
Done. (but the check is further down, not here)
|
| + |
| + // We use a reinterpret cast here so that the type returned by the |
| + // GetScheduler call can remain an interface, which we can mock differently |
| + // for testing. |
| + BackgroundSchedulerBridge* bridge = |
| + reinterpret_cast<BackgroundSchedulerBridge*>( |
| + request_coordinator->GetScheduler()); |
| + if (!bridge) { |
| + bridge = new BackgroundSchedulerBridge(); |
| + } |
| + bridge->AttachBridgeToJavaSide(env); |
| + return ScopedJavaLocalRef<jobject>(bridge->java_ref()); |
| +} |
| + |
| // JNI call to start request processing. |
| static jboolean StartProcessing( |
| JNIEnv* env, |
| const JavaParamRef<jclass>& jcaller, |
| - const JavaParamRef<jobject>& j_profile, |
| + const JavaParamRef<jobject>& j_context, |
| const JavaParamRef<jobject>& j_callback_obj) { |
| ScopedJavaGlobalRef<jobject> j_callback_ref; |
| j_callback_ref.Reset(env, j_callback_obj); |
| @@ -39,10 +70,32 @@ static jboolean StartProcessing( |
| return false; |
| } |
| -BackgroundSchedulerBridge::BackgroundSchedulerBridge() { |
| -} |
| +// We split the construction into two steps, build and init. The Ctor will be |
| +// called from the RequestCoordinatorFactory, and the init function will be |
| +// called from the Java side when a BackgroundTask arrives. |
| +// TODO(petewil): We eventually need some piece of Java code to call |
| +// BackgroundSchedulerBridge.getForProfile(); so "schedule()" will work |
| +// if BackgroundTask.incomingTask has not been called this session. |
| +BackgroundSchedulerBridge::BackgroundSchedulerBridge() {} |
| BackgroundSchedulerBridge::~BackgroundSchedulerBridge() { |
| + JNIEnv* env = base::android::AttachCurrentThread(); |
| + |
| + // Native shutdown causes the destruction of |this|. |
| + if (java_ref_.obj() != nullptr) { |
| + Java_BackgroundSchedulerBridge_backgroundSchedulerBridgeDestroyed( |
| + env, java_ref_.obj()); |
| + } |
| +} |
| + |
| +// Attach to the Java side of the bridge if it is not already attached. |
| +void BackgroundSchedulerBridge::AttachBridgeToJavaSide(JNIEnv* env) { |
| +if (java_ref().is_null()) { |
| + ScopedJavaLocalRef<jobject> j_offline_page_bridge = |
| + Java_BackgroundSchedulerBridge_create( |
| + env, reinterpret_cast<jlong>(this)); |
| + java_ref_.Reset(j_offline_page_bridge); |
| + } |
| } |
| void BackgroundSchedulerBridge::Schedule( |