Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(160)

Unified Diff: chrome/browser/android/offline_pages/background_scheduler_bridge.cc

Issue 1985923002: Wireframe scheduler implementation (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Address BackgroundTask CR feedback from DougArnett Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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:30 please add a nullptr check since we don't support
Pete Williamson 2016/05/23 19:36:30 Done.
+
+ // 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(

Powered by Google App Engine
This is Rietveld 408576698