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

Side by Side 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, 6 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 unified diff | Download patch
OLDNEW
1 // Copyright 2017 The Chromium Authors. All rights reserved. 1 // Copyright 2017 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/android/offline_pages/prefetch/prefetch_background_task .h" 5 #include "chrome/browser/android/offline_pages/prefetch/prefetch_background_task .h"
6 6
7 #include "base/time/time.h"
7 #include "chrome/browser/profiles/profile.h" 8 #include "chrome/browser/profiles/profile.h"
8 #include "chrome/browser/profiles/profile_android.h" 9 #include "chrome/browser/profiles/profile_android.h"
10 #include "chrome/common/pref_names.h"
9 #include "components/offline_pages/content/prefetch_service_factory.h" 11 #include "components/offline_pages/content/prefetch_service_factory.h"
10 #include "components/offline_pages/core/prefetch/prefetch_service.h" 12 #include "components/offline_pages/core/prefetch/prefetch_service.h"
13 #include "components/prefs/pref_registry_simple.h"
14 #include "components/prefs/pref_service.h"
11 #include "content/public/browser/browser_context.h" 15 #include "content/public/browser/browser_context.h"
12 #include "jni/PrefetchBackgroundTask_jni.h" 16 #include "jni/PrefetchBackgroundTask_jni.h"
17 #include "net/base/backoff_entry_serializer.h"
13 18
14 using base::android::JavaParamRef; 19 using base::android::JavaParamRef;
15 using base::android::ScopedJavaGlobalRef; 20 using base::android::ScopedJavaGlobalRef;
16 using base::android::ScopedJavaLocalRef; 21 using base::android::ScopedJavaLocalRef;
17 22
18 namespace offline_pages { 23 namespace offline_pages {
24
25 const net::BackoffEntry::Policy kBackoffPolicy = {
26 // Number of initial errors (in sequence) to ignore before applying
27 // exponential back-off rules.
28 0,
29
30 // Initial delay for exponential back-off in ms.
31 30 * 1000, // 30 seconds.
32
33 // Factor by which the waiting time will be multiplied.
34 2,
35
36 // Fuzzing percentage.
37 // TODO(jianli): Figure out how to disable jitter for testing.
38 0.33, // 33%.
39
40 // Maximum amount of time we are willing to delay our request in ms.
41 24 * 3600 * 1000, // 1 day.
42
43 // Time to keep an entry from being discarded even when it
44 // has no significant state, -1 to never discard.
45 -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.
46
47 false, // Don't use initial delay unless the last request was an error.
48 };
49
19 namespace prefetch { 50 namespace prefetch {
20 51
21 // JNI call to start request processing in scheduled mode. 52 // JNI call to start request processing in scheduled mode.
22 static jboolean StartPrefetchTask(JNIEnv* env, 53 static jboolean StartPrefetchTask(JNIEnv* env,
23 const JavaParamRef<jobject>& jcaller, 54 const JavaParamRef<jobject>& jcaller,
24 const JavaParamRef<jobject>& jprofile) { 55 const JavaParamRef<jobject>& jprofile) {
25 Profile* profile = ProfileAndroid::FromProfileAndroid(jprofile); 56 Profile* profile = ProfileAndroid::FromProfileAndroid(jprofile);
26 DCHECK(profile); 57 DCHECK(profile);
27 58
28 PrefetchService* prefetch_service = 59 PrefetchService* prefetch_service =
29 PrefetchServiceFactory::GetForBrowserContext(profile); 60 PrefetchServiceFactory::GetForBrowserContext(profile);
30 if (!prefetch_service) 61 if (!prefetch_service)
31 return false; 62 return false;
32 63
33 prefetch_service->GetDispatcher()->BeginBackgroundTask( 64 prefetch_service->GetDispatcher()->BeginBackgroundTask(
34 base::MakeUnique<PrefetchBackgroundTask>(env, jcaller, prefetch_service)); 65 base::MakeUnique<PrefetchBackgroundTask>(env, jcaller, prefetch_service,
35 return true; 66 profile));
67 return false; // true;
36 } 68 }
37 69
38 } // namespace prefetch 70 } // namespace prefetch
39 71
40 // static 72 void RegisterPrefetchBackgroundTaskPrefs(PrefRegistrySimple* registry) {
41 void PrefetchBackgroundTask::Schedule() { 73 registry->RegisterListPref(prefs::kOfflinePrefetchBackoff);
42 JNIEnv* env = base::android::AttachCurrentThread();
43 return prefetch::Java_PrefetchBackgroundTask_scheduleTask(env);
44 } 74 }
45 75
46 // static 76 // static
77 void PrefetchBackgroundTask::Schedule(int additional_delay_seconds) {
78 JNIEnv* env = base::android::AttachCurrentThread();
79 return prefetch::Java_PrefetchBackgroundTask_scheduleTask(
80 env, additional_delay_seconds);
81 }
82
83 // static
47 void PrefetchBackgroundTask::Cancel() { 84 void PrefetchBackgroundTask::Cancel() {
48 JNIEnv* env = base::android::AttachCurrentThread(); 85 JNIEnv* env = base::android::AttachCurrentThread();
49 return prefetch::Java_PrefetchBackgroundTask_cancelTask(env); 86 return prefetch::Java_PrefetchBackgroundTask_cancelTask(env);
50 } 87 }
51 88
52 PrefetchBackgroundTask::PrefetchBackgroundTask( 89 PrefetchBackgroundTask::PrefetchBackgroundTask(
53 JNIEnv* env, 90 JNIEnv* env,
54 const JavaParamRef<jobject>& java_prefetch_background_task, 91 const JavaParamRef<jobject>& java_prefetch_background_task,
55 PrefetchService* service) 92 PrefetchService* service,
93 Profile* profile)
56 : java_prefetch_background_task_(java_prefetch_background_task), 94 : java_prefetch_background_task_(java_prefetch_background_task),
57 service_(service) { 95 service_(service),
96 profile_(profile) {
58 // Give the Java side a pointer to the new background task object. 97 // Give the Java side a pointer to the new background task object.
59 prefetch::Java_PrefetchBackgroundTask_setNativeTask( 98 prefetch::Java_PrefetchBackgroundTask_setNativeTask(
60 env, java_prefetch_background_task_, reinterpret_cast<jlong>(this)); 99 env, java_prefetch_background_task_, reinterpret_cast<jlong>(this));
100
101 SetupBackOff();
61 } 102 }
62 103
63 PrefetchBackgroundTask::~PrefetchBackgroundTask() { 104 PrefetchBackgroundTask::~PrefetchBackgroundTask() {
64 JNIEnv* env = base::android::AttachCurrentThread(); 105 JNIEnv* env = base::android::AttachCurrentThread();
65 prefetch::Java_PrefetchBackgroundTask_doneProcessing( 106 prefetch::Java_PrefetchBackgroundTask_doneProcessing(
66 env, java_prefetch_background_task_.obj(), needs_reschedule_); 107 env, java_prefetch_background_task_.obj(), needs_reschedule_);
108
109 if (!task_killed_by_system_ && needs_reschedule_)
110 Schedule(GetAdditionalBackoffSeconds());
111 }
112
113 void PrefetchBackgroundTask::SetupBackOff() {
114 const base::ListValue* value =
115 profile_->GetPrefs()->GetList(prefs::kOfflinePrefetchBackoff);
116 if (value) {
117 backoff_ = net::BackoffEntrySerializer::DeserializeFromValue(
118 *value, &kBackoffPolicy, nullptr, base::Time::Now());
119 }
120
121 if (!backoff_)
122 backoff_ = base::MakeUnique<net::BackoffEntry>(&kBackoffPolicy);
67 } 123 }
68 124
69 bool PrefetchBackgroundTask::OnStopTask(JNIEnv* env, 125 bool PrefetchBackgroundTask::OnStopTask(JNIEnv* env,
70 const JavaParamRef<jobject>& jcaller) { 126 const JavaParamRef<jobject>& jcaller) {
71 DCHECK(jcaller.obj() == java_prefetch_background_task_.obj()); 127 DCHECK(jcaller.obj() == java_prefetch_background_task_.obj());
72 service_->GetDispatcher()->StopBackgroundTask(this); 128 task_killed_by_system_ = true;
73 return needs_reschedule_; 129 service_->GetDispatcher()->StopBackgroundTask(/*signaled_by_system*/ true);
130
131 // Reschedules our task since the system decides to kill it before it is
132 // finished. Notes that we should reschedule without backoff even when it is
133 // in effect because we want to rerun the task asap.
134 if (GetAdditionalBackoffSeconds()) {
135 Schedule(0);
136 return false;
137 }
138 return true;
74 } 139 }
75 140
76 void PrefetchBackgroundTask::SetNeedsReschedule(bool reschedule) { 141 void PrefetchBackgroundTask::SetTaskRescheduling(
142 JNIEnv* env,
143 const base::android::JavaParamRef<jobject>& jcaller,
144 jboolean reschedule,
145 jboolean backoff) {
146 DCHECK(jcaller.obj() == java_prefetch_background_task_.obj());
147 SetNeedsReschedule(static_cast<bool>(reschedule), static_cast<bool>(backoff));
148 }
149
150 void PrefetchBackgroundTask::SignalTaskFinished(
151 JNIEnv* env,
152 const base::android::JavaParamRef<jobject>& jcaller) {
153 DCHECK(jcaller.obj() == java_prefetch_background_task_.obj());
154 service_->GetDispatcher()->StopBackgroundTask(/*signaled_by_system*/ false);
155 }
156
157 void PrefetchBackgroundTask::SetNeedsReschedule(bool reschedule, bool backoff) {
77 needs_reschedule_ = reschedule; 158 needs_reschedule_ = reschedule;
159
160 if (reschedule && backoff)
161 backoff_->InformOfRequest(false);
162 else
163 backoff_->Reset();
164
165 std::unique_ptr<base::Value> value =
166 net::BackoffEntrySerializer::SerializeToValue(*backoff_,
167 base::Time::Now());
168 profile_->GetPrefs()->Set(prefs::kOfflinePrefetchBackoff, *value);
169 }
170
171 int PrefetchBackgroundTask::GetAdditionalBackoffSeconds() const {
172 return static_cast<int>(backoff_->GetTimeUntilRelease().InSeconds());
78 } 173 }
79 174
80 bool RegisterPrefetchBackgroundTask(JNIEnv* env) { 175 bool RegisterPrefetchBackgroundTask(JNIEnv* env) {
81 return prefetch::RegisterNativesImpl(env); 176 return prefetch::RegisterNativesImpl(env);
82 } 177 }
83 178
84 } // namespace offline_pages 179 } // namespace offline_pages
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698