Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 0, // Number of initial errors to ignore without backoff. | |
| 27 30 * 1000, // Initial delay for backoff in ms: 30 seconds. | |
| 28 2, // Factor to multiply for exponential backoff. | |
| 29 0.33, // Fuzzing percentage. | |
| 30 24 * 3600 * 1000, // Maximum time to delay requests in ms: 1 day. | |
| 31 -1, // Don't discard entry even if unused. | |
| 32 false // Don't use initial delay unless the last was an error. | |
| 33 }; | |
| 34 | |
| 19 namespace prefetch { | 35 namespace prefetch { |
| 20 | 36 |
| 21 // JNI call to start request processing in scheduled mode. | 37 // JNI call to start request processing in scheduled mode. |
| 22 static jboolean StartPrefetchTask(JNIEnv* env, | 38 static jboolean StartPrefetchTask(JNIEnv* env, |
| 23 const JavaParamRef<jobject>& jcaller, | 39 const JavaParamRef<jobject>& jcaller, |
| 24 const JavaParamRef<jobject>& jprofile) { | 40 const JavaParamRef<jobject>& jprofile) { |
| 25 Profile* profile = ProfileAndroid::FromProfileAndroid(jprofile); | 41 Profile* profile = ProfileAndroid::FromProfileAndroid(jprofile); |
| 26 DCHECK(profile); | 42 DCHECK(profile); |
| 27 | 43 |
| 28 PrefetchService* prefetch_service = | 44 PrefetchService* prefetch_service = |
| 29 PrefetchServiceFactory::GetForBrowserContext(profile); | 45 PrefetchServiceFactory::GetForBrowserContext(profile); |
| 30 if (!prefetch_service) | 46 if (!prefetch_service) |
| 31 return false; | 47 return false; |
| 32 | 48 |
| 33 prefetch_service->GetDispatcher()->BeginBackgroundTask( | 49 prefetch_service->GetDispatcher()->BeginBackgroundTask( |
| 34 base::MakeUnique<PrefetchBackgroundTask>(env, jcaller, prefetch_service)); | 50 base::MakeUnique<PrefetchBackgroundTask>(env, jcaller, prefetch_service, |
| 35 return true; | 51 profile)); |
| 52 return false; // true; | |
| 36 } | 53 } |
| 37 | 54 |
| 38 } // namespace prefetch | 55 } // namespace prefetch |
| 39 | 56 |
| 40 // static | 57 void RegisterPrefetchBackgroundTaskPrefs(PrefRegistrySimple* registry) { |
| 41 void PrefetchBackgroundTask::Schedule() { | 58 registry->RegisterListPref(prefs::kOfflinePrefetchBackoff); |
| 42 JNIEnv* env = base::android::AttachCurrentThread(); | |
| 43 return prefetch::Java_PrefetchBackgroundTask_scheduleTask(env); | |
| 44 } | 59 } |
| 45 | 60 |
| 46 // static | 61 // static |
| 62 void PrefetchBackgroundTask::Schedule(int additional_delay_seconds) { | |
| 63 JNIEnv* env = base::android::AttachCurrentThread(); | |
| 64 return prefetch::Java_PrefetchBackgroundTask_scheduleTask( | |
| 65 env, additional_delay_seconds); | |
| 66 } | |
| 67 | |
| 68 // static | |
| 47 void PrefetchBackgroundTask::Cancel() { | 69 void PrefetchBackgroundTask::Cancel() { |
| 48 JNIEnv* env = base::android::AttachCurrentThread(); | 70 JNIEnv* env = base::android::AttachCurrentThread(); |
| 49 return prefetch::Java_PrefetchBackgroundTask_cancelTask(env); | 71 return prefetch::Java_PrefetchBackgroundTask_cancelTask(env); |
| 50 } | 72 } |
| 51 | 73 |
| 52 PrefetchBackgroundTask::PrefetchBackgroundTask( | 74 PrefetchBackgroundTask::PrefetchBackgroundTask( |
| 53 JNIEnv* env, | 75 JNIEnv* env, |
| 54 const JavaParamRef<jobject>& java_prefetch_background_task, | 76 const JavaParamRef<jobject>& java_prefetch_background_task, |
| 55 PrefetchService* service) | 77 PrefetchService* service, |
| 78 Profile* profile) | |
| 56 : java_prefetch_background_task_(java_prefetch_background_task), | 79 : java_prefetch_background_task_(java_prefetch_background_task), |
| 57 service_(service) { | 80 service_(service), |
| 81 profile_(profile) { | |
| 58 // Give the Java side a pointer to the new background task object. | 82 // Give the Java side a pointer to the new background task object. |
| 59 prefetch::Java_PrefetchBackgroundTask_setNativeTask( | 83 prefetch::Java_PrefetchBackgroundTask_setNativeTask( |
| 60 env, java_prefetch_background_task_, reinterpret_cast<jlong>(this)); | 84 env, java_prefetch_background_task_, reinterpret_cast<jlong>(this)); |
| 85 | |
| 86 SetupBackOff(); | |
| 61 } | 87 } |
| 62 | 88 |
| 63 PrefetchBackgroundTask::~PrefetchBackgroundTask() { | 89 PrefetchBackgroundTask::~PrefetchBackgroundTask() { |
| 64 JNIEnv* env = base::android::AttachCurrentThread(); | 90 JNIEnv* env = base::android::AttachCurrentThread(); |
| 65 prefetch::Java_PrefetchBackgroundTask_doneProcessing( | 91 prefetch::Java_PrefetchBackgroundTask_doneProcessing( |
| 66 env, java_prefetch_background_task_.obj(), needs_reschedule_); | 92 env, java_prefetch_background_task_.obj(), needs_reschedule_); |
| 93 | |
| 94 if (!task_killed_by_system_ && needs_reschedule_) | |
| 95 Schedule(GetAdditionalBackoffSeconds()); | |
| 96 } | |
| 97 | |
| 98 void PrefetchBackgroundTask::SetupBackOff() { | |
| 99 const base::ListValue* value = | |
| 100 profile_->GetPrefs()->GetList(prefs::kOfflinePrefetchBackoff); | |
| 101 if (value) { | |
| 102 backoff_ = net::BackoffEntrySerializer::DeserializeFromValue( | |
| 103 *value, &kBackoffPolicy, nullptr, base::Time::Now()); | |
| 104 } | |
| 105 | |
| 106 if (!backoff_) | |
| 107 backoff_ = base::MakeUnique<net::BackoffEntry>(&kBackoffPolicy); | |
| 67 } | 108 } |
| 68 | 109 |
| 69 bool PrefetchBackgroundTask::OnStopTask(JNIEnv* env, | 110 bool PrefetchBackgroundTask::OnStopTask(JNIEnv* env, |
| 70 const JavaParamRef<jobject>& jcaller) { | 111 const JavaParamRef<jobject>& jcaller) { |
| 71 DCHECK(jcaller.obj() == java_prefetch_background_task_.obj()); | 112 DCHECK(jcaller.obj() == java_prefetch_background_task_.obj()); |
| 72 service_->GetDispatcher()->StopBackgroundTask(this); | 113 task_killed_by_system_ = true; |
| 73 return needs_reschedule_; | 114 service_->GetDispatcher()->StopBackgroundTask(); |
| 115 | |
| 116 // Reschedules our task since the system decides to kill it before it is | |
| 117 // finished. Notes that we should reschedule without backoff even when it is | |
| 118 // in effect because we want to rerun the task asap. | |
| 119 if (GetAdditionalBackoffSeconds()) { | |
| 120 Schedule(0); | |
|
dewittj
2017/06/01 00:22:27
Should we always Schedule(0) instead of sometimes
jianli
2017/06/02 00:48:37
Done.
| |
| 121 return false; | |
| 122 } | |
| 123 return true; | |
| 74 } | 124 } |
| 75 | 125 |
| 76 void PrefetchBackgroundTask::SetNeedsReschedule(bool reschedule) { | 126 void PrefetchBackgroundTask::SetTaskRescheduling( |
| 127 JNIEnv* env, | |
| 128 const base::android::JavaParamRef<jobject>& jcaller, | |
| 129 jboolean reschedule, | |
| 130 jboolean backoff) { | |
| 131 DCHECK(jcaller.obj() == java_prefetch_background_task_.obj()); | |
| 132 SetNeedsReschedule(static_cast<bool>(reschedule), static_cast<bool>(backoff)); | |
| 133 } | |
| 134 | |
| 135 void PrefetchBackgroundTask::SignalTaskFinishedForTesting( | |
| 136 JNIEnv* env, | |
| 137 const base::android::JavaParamRef<jobject>& jcaller) { | |
| 138 DCHECK(jcaller.obj() == java_prefetch_background_task_.obj()); | |
| 139 service_->GetDispatcher()->RequestFinishBackgroundTaskForTest(); | |
| 140 } | |
| 141 | |
| 142 void PrefetchBackgroundTask::SetNeedsReschedule(bool reschedule, bool backoff) { | |
| 77 needs_reschedule_ = reschedule; | 143 needs_reschedule_ = reschedule; |
| 144 | |
| 145 if (reschedule && backoff) | |
| 146 backoff_->InformOfRequest(false); | |
| 147 else | |
| 148 backoff_->Reset(); | |
| 149 | |
| 150 std::unique_ptr<base::Value> value = | |
| 151 net::BackoffEntrySerializer::SerializeToValue(*backoff_, | |
| 152 base::Time::Now()); | |
| 153 profile_->GetPrefs()->Set(prefs::kOfflinePrefetchBackoff, *value); | |
| 154 } | |
| 155 | |
| 156 int PrefetchBackgroundTask::GetAdditionalBackoffSeconds() const { | |
| 157 return static_cast<int>(backoff_->GetTimeUntilRelease().InSeconds()); | |
| 78 } | 158 } |
| 79 | 159 |
| 80 bool RegisterPrefetchBackgroundTask(JNIEnv* env) { | 160 bool RegisterPrefetchBackgroundTask(JNIEnv* env) { |
| 81 return prefetch::RegisterNativesImpl(env); | 161 return prefetch::RegisterNativesImpl(env); |
| 82 } | 162 } |
| 83 | 163 |
| 84 } // namespace offline_pages | 164 } // namespace offline_pages |
| OLD | NEW |