| 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/offline_pages/prefetch/prefetch_service_factory.h" | 8 #include "chrome/browser/offline_pages/prefetch/prefetch_service_factory.h" |
| 8 #include "chrome/browser/profiles/profile.h" | 9 #include "chrome/browser/profiles/profile.h" |
| 9 #include "chrome/browser/profiles/profile_android.h" | 10 #include "chrome/browser/profiles/profile_android.h" |
| 11 #include "chrome/common/pref_names.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->GetPrefetchDispatcher()->BeginBackgroundTask( | 49 prefetch_service->GetPrefetchDispatcher()->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 (needs_reschedule_) { |
| 95 // If the task is killed due to the system, it should be rescheduled without |
| 96 // backoff even when it is in effect because we want to rerun the task asap. |
| 97 Schedule(task_killed_by_system_ ? 0 : GetAdditionalBackoffSeconds()); |
| 98 } |
| 99 } |
| 100 |
| 101 void PrefetchBackgroundTask::SetupBackOff() { |
| 102 const base::ListValue* value = |
| 103 profile_->GetPrefs()->GetList(prefs::kOfflinePrefetchBackoff); |
| 104 if (value) { |
| 105 backoff_ = net::BackoffEntrySerializer::DeserializeFromValue( |
| 106 *value, &kBackoffPolicy, nullptr, base::Time::Now()); |
| 107 } |
| 108 |
| 109 if (!backoff_) |
| 110 backoff_ = base::MakeUnique<net::BackoffEntry>(&kBackoffPolicy); |
| 67 } | 111 } |
| 68 | 112 |
| 69 bool PrefetchBackgroundTask::OnStopTask(JNIEnv* env, | 113 bool PrefetchBackgroundTask::OnStopTask(JNIEnv* env, |
| 70 const JavaParamRef<jobject>& jcaller) { | 114 const JavaParamRef<jobject>& jcaller) { |
| 71 DCHECK(jcaller.obj() == java_prefetch_background_task_.obj()); | 115 task_killed_by_system_ = true; |
| 72 service_->GetPrefetchDispatcher()->StopBackgroundTask(this); | 116 needs_reschedule_ = true; |
| 73 return needs_reschedule_; | 117 service_->GetPrefetchDispatcher()->StopBackgroundTask(); |
| 118 return false; |
| 74 } | 119 } |
| 75 | 120 |
| 76 void PrefetchBackgroundTask::SetNeedsReschedule(bool reschedule) { | 121 void PrefetchBackgroundTask::SetTaskReschedulingForTesting( |
| 122 JNIEnv* env, |
| 123 const base::android::JavaParamRef<jobject>& jcaller, |
| 124 jboolean reschedule, |
| 125 jboolean backoff) { |
| 126 SetNeedsReschedule(static_cast<bool>(reschedule), static_cast<bool>(backoff)); |
| 127 } |
| 128 |
| 129 void PrefetchBackgroundTask::SignalTaskFinishedForTesting( |
| 130 JNIEnv* env, |
| 131 const base::android::JavaParamRef<jobject>& jcaller) { |
| 132 service_->GetPrefetchDispatcher()->RequestFinishBackgroundTaskForTest(); |
| 133 } |
| 134 |
| 135 void PrefetchBackgroundTask::SetNeedsReschedule(bool reschedule, bool backoff) { |
| 77 needs_reschedule_ = reschedule; | 136 needs_reschedule_ = reschedule; |
| 137 |
| 138 if (reschedule && backoff) |
| 139 backoff_->InformOfRequest(false); |
| 140 else |
| 141 backoff_->Reset(); |
| 142 |
| 143 std::unique_ptr<base::Value> value = |
| 144 net::BackoffEntrySerializer::SerializeToValue(*backoff_, |
| 145 base::Time::Now()); |
| 146 profile_->GetPrefs()->Set(prefs::kOfflinePrefetchBackoff, *value); |
| 147 } |
| 148 |
| 149 int PrefetchBackgroundTask::GetAdditionalBackoffSeconds() const { |
| 150 return static_cast<int>(backoff_->GetTimeUntilRelease().InSeconds()); |
| 78 } | 151 } |
| 79 | 152 |
| 80 bool RegisterPrefetchBackgroundTask(JNIEnv* env) { | 153 bool RegisterPrefetchBackgroundTask(JNIEnv* env) { |
| 81 return prefetch::RegisterNativesImpl(env); | 154 return prefetch::RegisterNativesImpl(env); |
| 82 } | 155 } |
| 83 | 156 |
| 84 } // namespace offline_pages | 157 } // namespace offline_pages |
| OLD | NEW |