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

Unified Diff: chrome/browser/android/offline_pages/prefetch/prefetch_background_task.cc

Issue 2914703002: [Offline Prefetch] Backoff support for PrefetchBackgroundTask (Closed)
Patch Set: Fix trybots Created 3 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/prefetch/prefetch_background_task.cc
diff --git a/chrome/browser/android/offline_pages/prefetch/prefetch_background_task.cc b/chrome/browser/android/offline_pages/prefetch/prefetch_background_task.cc
index 17870fc266aa912de6ec434630e4ae561a8aa7a7..ac49aa3e66aa92c791be34fc44372cad5dd4b3cd 100644
--- a/chrome/browser/android/offline_pages/prefetch/prefetch_background_task.cc
+++ b/chrome/browser/android/offline_pages/prefetch/prefetch_background_task.cc
@@ -4,18 +4,49 @@
#include "chrome/browser/android/offline_pages/prefetch/prefetch_background_task.h"
+#include "base/time/time.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/profiles/profile_android.h"
+#include "chrome/common/pref_names.h"
#include "components/offline_pages/content/prefetch_service_factory.h"
#include "components/offline_pages/core/prefetch/prefetch_service.h"
+#include "components/prefs/pref_registry_simple.h"
+#include "components/prefs/pref_service.h"
#include "content/public/browser/browser_context.h"
#include "jni/PrefetchBackgroundTask_jni.h"
+#include "net/base/backoff_entry_serializer.h"
using base::android::JavaParamRef;
using base::android::ScopedJavaGlobalRef;
using base::android::ScopedJavaLocalRef;
namespace offline_pages {
+
+const net::BackoffEntry::Policy kBackoffPolicy = {
+ // Number of initial errors (in sequence) to ignore before applying
+ // exponential back-off rules.
+ 0,
+
+ // Initial delay for exponential back-off in ms.
+ 30 * 1000, // 30 seconds.
+
+ // Factor by which the waiting time will be multiplied.
+ 2,
+
+ // Fuzzing percentage.
+ // TODO(jianli): Figure out how to disable jitter for testing.
+ 0.33, // 33%.
+
+ // Maximum amount of time we are willing to delay our request in ms.
+ 24 * 3600 * 1000, // 1 day.
+
+ // Time to keep an entry from being discarded even when it
+ // has no significant state, -1 to never discard.
+ -1,
dewittj 2017/05/31 21:33:33 Is "discard" even relevant to us? Can you documen
jianli 2017/06/01 00:09:59 Done.
+
+ false, // Don't use initial delay unless the last request was an error.
+};
+
namespace prefetch {
// JNI call to start request processing in scheduled mode.
@@ -31,16 +62,22 @@ static jboolean StartPrefetchTask(JNIEnv* env,
return false;
prefetch_service->GetDispatcher()->BeginBackgroundTask(
- base::MakeUnique<PrefetchBackgroundTask>(env, jcaller, prefetch_service));
- return true;
+ base::MakeUnique<PrefetchBackgroundTask>(env, jcaller, prefetch_service,
+ profile));
+ return false; // true;
}
} // namespace prefetch
+void RegisterPrefetchBackgroundTaskPrefs(PrefRegistrySimple* registry) {
+ registry->RegisterListPref(prefs::kOfflinePrefetchBackoff);
+}
+
// static
-void PrefetchBackgroundTask::Schedule() {
+void PrefetchBackgroundTask::Schedule(int additional_delay_seconds) {
JNIEnv* env = base::android::AttachCurrentThread();
- return prefetch::Java_PrefetchBackgroundTask_scheduleTask(env);
+ return prefetch::Java_PrefetchBackgroundTask_scheduleTask(
+ env, additional_delay_seconds);
}
// static
@@ -52,29 +89,87 @@ void PrefetchBackgroundTask::Cancel() {
PrefetchBackgroundTask::PrefetchBackgroundTask(
JNIEnv* env,
const JavaParamRef<jobject>& java_prefetch_background_task,
- PrefetchService* service)
+ PrefetchService* service,
+ Profile* profile)
: java_prefetch_background_task_(java_prefetch_background_task),
- service_(service) {
+ service_(service),
+ profile_(profile) {
// Give the Java side a pointer to the new background task object.
prefetch::Java_PrefetchBackgroundTask_setNativeTask(
env, java_prefetch_background_task_, reinterpret_cast<jlong>(this));
+
+ SetupBackOff();
}
PrefetchBackgroundTask::~PrefetchBackgroundTask() {
JNIEnv* env = base::android::AttachCurrentThread();
prefetch::Java_PrefetchBackgroundTask_doneProcessing(
env, java_prefetch_background_task_.obj(), needs_reschedule_);
+
+ if (!task_killed_by_system_ && needs_reschedule_)
+ Schedule(GetAdditionalBackoffSeconds());
+}
+
+void PrefetchBackgroundTask::SetupBackOff() {
+ const base::ListValue* value =
+ profile_->GetPrefs()->GetList(prefs::kOfflinePrefetchBackoff);
+ if (value) {
+ backoff_ = net::BackoffEntrySerializer::DeserializeFromValue(
+ *value, &kBackoffPolicy, nullptr, base::Time::Now());
+ }
+
+ if (!backoff_)
+ backoff_ = base::MakeUnique<net::BackoffEntry>(&kBackoffPolicy);
}
bool PrefetchBackgroundTask::OnStopTask(JNIEnv* env,
const JavaParamRef<jobject>& jcaller) {
DCHECK(jcaller.obj() == java_prefetch_background_task_.obj());
- service_->GetDispatcher()->StopBackgroundTask(this);
- return needs_reschedule_;
+ task_killed_by_system_ = true;
+ service_->GetDispatcher()->StopBackgroundTask(/*signaled_by_system*/ true);
+
+ // Reschedules our task since the system decides to kill it before it is
+ // finished. Notes that we should reschedule without backoff even when it is
+ // in effect because we want to rerun the task asap.
+ if (GetAdditionalBackoffSeconds()) {
+ Schedule(0);
+ return false;
+ }
+ return true;
+}
+
+void PrefetchBackgroundTask::SetTaskRescheduling(
+ JNIEnv* env,
+ const base::android::JavaParamRef<jobject>& jcaller,
+ jboolean reschedule,
+ jboolean backoff) {
+ DCHECK(jcaller.obj() == java_prefetch_background_task_.obj());
+ SetNeedsReschedule(static_cast<bool>(reschedule), static_cast<bool>(backoff));
}
-void PrefetchBackgroundTask::SetNeedsReschedule(bool reschedule) {
+void PrefetchBackgroundTask::SignalTaskFinished(
+ JNIEnv* env,
+ const base::android::JavaParamRef<jobject>& jcaller) {
+ DCHECK(jcaller.obj() == java_prefetch_background_task_.obj());
+ service_->GetDispatcher()->StopBackgroundTask(/*signaled_by_system*/ false);
+}
+
+void PrefetchBackgroundTask::SetNeedsReschedule(bool reschedule, bool backoff) {
needs_reschedule_ = reschedule;
+
+ if (reschedule && backoff)
+ backoff_->InformOfRequest(false);
+ else
+ backoff_->Reset();
+
+ std::unique_ptr<base::Value> value =
+ net::BackoffEntrySerializer::SerializeToValue(*backoff_,
+ base::Time::Now());
+ profile_->GetPrefs()->Set(prefs::kOfflinePrefetchBackoff, *value);
+}
+
+int PrefetchBackgroundTask::GetAdditionalBackoffSeconds() const {
+ return static_cast<int>(backoff_->GetTimeUntilRelease().InSeconds());
}
bool RegisterPrefetchBackgroundTask(JNIEnv* env) {

Powered by Google App Engine
This is Rietveld 408576698