| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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/evaluation/offline_page_evaluatio
n_bridge.h" | 5 #include "chrome/browser/android/offline_pages/evaluation/offline_page_evaluatio
n_bridge.h" |
| 6 | 6 |
| 7 #include "base/android/callback_android.h" | 7 #include "base/android/callback_android.h" |
| 8 #include "base/android/jni_android.h" | 8 #include "base/android/jni_android.h" |
| 9 #include "base/android/jni_string.h" | 9 #include "base/android/jni_string.h" |
| 10 #include "base/memory/ptr_util.h" |
| 11 #include "base/sequenced_task_runner.h" |
| 12 #include "chrome/browser/android/offline_pages/background_scheduler_bridge.h" |
| 13 #include "chrome/browser/android/offline_pages/downloads/offline_page_notificati
on_bridge.h" |
| 14 #include "chrome/browser/android/offline_pages/evaluation/evaluation_test_schedu
ler.h" |
| 10 #include "chrome/browser/android/offline_pages/offline_page_model_factory.h" | 15 #include "chrome/browser/android/offline_pages/offline_page_model_factory.h" |
| 16 #include "chrome/browser/android/offline_pages/prerendering_offliner_factory.h" |
| 11 #include "chrome/browser/android/offline_pages/request_coordinator_factory.h" | 17 #include "chrome/browser/android/offline_pages/request_coordinator_factory.h" |
| 18 #include "chrome/browser/net/nqe/ui_network_quality_estimator_service.h" |
| 19 #include "chrome/browser/net/nqe/ui_network_quality_estimator_service_factory.h" |
| 12 #include "chrome/browser/profiles/profile.h" | 20 #include "chrome/browser/profiles/profile.h" |
| 13 #include "chrome/browser/profiles/profile_android.h" | 21 #include "chrome/browser/profiles/profile_android.h" |
| 22 #include "chrome/common/chrome_constants.h" |
| 23 #include "components/offline_pages/background/offliner_factory.h" |
| 24 #include "components/offline_pages/background/offliner_policy.h" |
| 14 #include "components/offline_pages/background/request_coordinator.h" | 25 #include "components/offline_pages/background/request_coordinator.h" |
| 15 #include "components/offline_pages/background/request_notifier.h" | 26 #include "components/offline_pages/background/request_notifier.h" |
| 27 #include "components/offline_pages/background/request_queue.h" |
| 28 #include "components/offline_pages/background/request_queue_store.h" |
| 29 #include "components/offline_pages/background/request_queue_store_sql.h" |
| 16 #include "components/offline_pages/background/save_page_request.h" | 30 #include "components/offline_pages/background/save_page_request.h" |
| 31 #include "components/offline_pages/downloads/download_notifying_observer.h" |
| 17 #include "components/offline_pages/offline_page_item.h" | 32 #include "components/offline_pages/offline_page_item.h" |
| 18 #include "components/offline_pages/offline_page_model.h" | 33 #include "components/offline_pages/offline_page_model.h" |
| 19 #include "content/public/browser/browser_context.h" | 34 #include "content/public/browser/browser_context.h" |
| 35 #include "content/public/browser/browser_thread.h" |
| 20 #include "jni/OfflinePageEvaluationBridge_jni.h" | 36 #include "jni/OfflinePageEvaluationBridge_jni.h" |
| 21 #include "jni/SavePageRequest_jni.h" | 37 #include "jni/SavePageRequest_jni.h" |
| 22 | 38 |
| 23 using base::android::ConvertJavaStringToUTF8; | 39 using base::android::ConvertJavaStringToUTF8; |
| 24 using base::android::ConvertUTF8ToJavaString; | 40 using base::android::ConvertUTF8ToJavaString; |
| 25 using base::android::JavaParamRef; | 41 using base::android::JavaParamRef; |
| 26 using base::android::ScopedJavaGlobalRef; | 42 using base::android::ScopedJavaGlobalRef; |
| 27 using base::android::ScopedJavaLocalRef; | 43 using base::android::ScopedJavaLocalRef; |
| 28 | 44 |
| 29 namespace offline_pages { | 45 namespace offline_pages { |
| 30 namespace android { | 46 namespace android { |
| 31 | 47 |
| 32 namespace { | 48 namespace { |
| 49 const bool kPreferUntriedRequest = false; |
| 50 const bool kPreferEarlierRequest = true; |
| 51 const bool kPreferRetryCountOverRecency = false; |
| 52 const int kMaxStartedTries = 4; |
| 53 const int kMaxCompletedTries = 1; |
| 54 const int kImmediateRequestExpirationTimeInSeconds = 3600; |
| 33 | 55 |
| 34 void ToJavaOfflinePageList(JNIEnv* env, | 56 void ToJavaOfflinePageList(JNIEnv* env, |
| 35 jobject j_result_obj, | 57 jobject j_result_obj, |
| 36 const std::vector<OfflinePageItem>& offline_pages) { | 58 const std::vector<OfflinePageItem>& offline_pages) { |
| 37 for (const OfflinePageItem& offline_page : offline_pages) { | 59 for (const OfflinePageItem& offline_page : offline_pages) { |
| 38 Java_OfflinePageEvaluationBridge_createOfflinePageAndAddToList( | 60 Java_OfflinePageEvaluationBridge_createOfflinePageAndAddToList( |
| 39 env, j_result_obj, | 61 env, j_result_obj, |
| 40 ConvertUTF8ToJavaString(env, offline_page.url.spec()), | 62 ConvertUTF8ToJavaString(env, offline_page.url.spec()), |
| 41 offline_page.offline_id, | 63 offline_page.offline_id, |
| 42 ConvertUTF8ToJavaString(env, offline_page.client_id.name_space), | 64 ConvertUTF8ToJavaString(env, offline_page.client_id.name_space), |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 81 Profile* profile = ProfileAndroid::FromProfileAndroid(j_profile); | 103 Profile* profile = ProfileAndroid::FromProfileAndroid(j_profile); |
| 82 | 104 |
| 83 OfflinePageModel* offline_page_model = | 105 OfflinePageModel* offline_page_model = |
| 84 OfflinePageModelFactory::GetForBrowserContext(profile); | 106 OfflinePageModelFactory::GetForBrowserContext(profile); |
| 85 | 107 |
| 86 RequestCoordinator* request_coordinator = nullptr; | 108 RequestCoordinator* request_coordinator = nullptr; |
| 87 | 109 |
| 88 if (j_use_evaluation_scheduler) { | 110 if (j_use_evaluation_scheduler) { |
| 89 request_coordinator = static_cast<RequestCoordinator*>( | 111 request_coordinator = static_cast<RequestCoordinator*>( |
| 90 RequestCoordinatorFactory::GetInstance()->SetTestingFactoryAndUse( | 112 RequestCoordinatorFactory::GetInstance()->SetTestingFactoryAndUse( |
| 91 profile, &RequestCoordinatorFactory::GetTestingFactory)); | 113 profile, |
| 114 &OfflinePageEvaluationBridge::GetTestingRequestCoordinator)); |
| 92 } else { | 115 } else { |
| 93 request_coordinator = | 116 request_coordinator = |
| 94 RequestCoordinatorFactory::GetForBrowserContext(profile); | 117 RequestCoordinatorFactory::GetForBrowserContext(profile); |
| 95 } | 118 } |
| 96 | 119 |
| 97 if (offline_page_model == nullptr || request_coordinator == nullptr) | 120 if (offline_page_model == nullptr || request_coordinator == nullptr) |
| 98 return ScopedJavaLocalRef<jobject>(); | 121 return ScopedJavaLocalRef<jobject>(); |
| 99 | 122 |
| 100 OfflinePageEvaluationBridge* bridge = new OfflinePageEvaluationBridge( | 123 OfflinePageEvaluationBridge* bridge = new OfflinePageEvaluationBridge( |
| 101 env, profile, offline_page_model, request_coordinator); | 124 env, profile, offline_page_model, request_coordinator); |
| 102 | 125 |
| 103 return ScopedJavaLocalRef<jobject>(bridge->java_ref()); | 126 return ScopedJavaLocalRef<jobject>(bridge->java_ref()); |
| 104 } | 127 } |
| 105 | 128 |
| 106 // static | 129 // static |
| 107 bool OfflinePageEvaluationBridge::Register(JNIEnv* env) { | 130 bool OfflinePageEvaluationBridge::Register(JNIEnv* env) { |
| 108 return RegisterNativesImpl(env); | 131 return RegisterNativesImpl(env); |
| 109 } | 132 } |
| 110 | 133 |
| 134 // static |
| 135 std::unique_ptr<KeyedService> |
| 136 OfflinePageEvaluationBridge::GetTestingRequestCoordinator( |
| 137 content::BrowserContext* context) { |
| 138 // Create a new OfflinerPolicy with a larger background processing |
| 139 // budget (3600 sec). Other values are the same with default ones. |
| 140 std::unique_ptr<OfflinerPolicy> policy(new OfflinerPolicy( |
| 141 kPreferUntriedRequest, kPreferEarlierRequest, |
| 142 kPreferRetryCountOverRecency, kMaxStartedTries, kMaxCompletedTries, |
| 143 kImmediateRequestExpirationTimeInSeconds)); |
| 144 std::unique_ptr<OfflinerFactory> prerenderer_offliner( |
| 145 new PrerenderingOfflinerFactory(context)); |
| 146 |
| 147 scoped_refptr<base::SequencedTaskRunner> background_task_runner = |
| 148 content::BrowserThread::GetBlockingPool()->GetSequencedTaskRunner( |
| 149 content::BrowserThread::GetBlockingPool()->GetSequenceToken()); |
| 150 Profile* profile = Profile::FromBrowserContext(context); |
| 151 base::FilePath queue_store_path = |
| 152 profile->GetPath().Append(chrome::kOfflinePageRequestQueueDirname); |
| 153 |
| 154 std::unique_ptr<RequestQueueStoreSQL> queue_store( |
| 155 new RequestQueueStoreSQL(background_task_runner, queue_store_path)); |
| 156 std::unique_ptr<RequestQueue> queue(new RequestQueue(std::move(queue_store))); |
| 157 std::unique_ptr<android::EvaluationTestScheduler> scheduler( |
| 158 new android::EvaluationTestScheduler()); |
| 159 net::NetworkQualityEstimator::NetworkQualityProvider* |
| 160 network_quality_estimator = |
| 161 UINetworkQualityEstimatorServiceFactory::GetForProfile(profile); |
| 162 // TODO(fgorski): Something needs to keep the handle to the Notification |
| 163 // dispatcher. |
| 164 std::unique_ptr<RequestCoordinator> request_coordinator = |
| 165 base::MakeUnique<RequestCoordinator>( |
| 166 std::move(policy), std::move(prerenderer_offliner), std::move(queue), |
| 167 std::move(scheduler), network_quality_estimator); |
| 168 request_coordinator->SetImmediateScheduleCallbackForTest( |
| 169 base::Bind(&android::EvaluationTestScheduler::ImmediateScheduleCallback, |
| 170 base::Unretained(scheduler.get()))); |
| 171 |
| 172 DownloadNotifyingObserver::CreateAndStartObserving( |
| 173 request_coordinator.get(), |
| 174 base::MakeUnique<android::OfflinePageNotificationBridge>()); |
| 175 |
| 176 return std::move(request_coordinator); |
| 177 } |
| 178 |
| 111 OfflinePageEvaluationBridge::OfflinePageEvaluationBridge( | 179 OfflinePageEvaluationBridge::OfflinePageEvaluationBridge( |
| 112 JNIEnv* env, | 180 JNIEnv* env, |
| 113 content::BrowserContext* browser_context, | 181 content::BrowserContext* browser_context, |
| 114 OfflinePageModel* offline_page_model, | 182 OfflinePageModel* offline_page_model, |
| 115 RequestCoordinator* request_coordinator) | 183 RequestCoordinator* request_coordinator) |
| 116 : browser_context_(browser_context), | 184 : browser_context_(browser_context), |
| 117 offline_page_model_(offline_page_model), | 185 offline_page_model_(offline_page_model), |
| 118 request_coordinator_(request_coordinator) { | 186 request_coordinator_(request_coordinator) { |
| 119 java_ref_.Reset(Java_OfflinePageEvaluationBridge_create( | 187 java_ref_.Reset(Java_OfflinePageEvaluationBridge_create( |
| 120 env, reinterpret_cast<jlong>(this))); | 188 env, reinterpret_cast<jlong>(this))); |
| (...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 215 | 283 |
| 216 void OfflinePageEvaluationBridge::NotifyIfDoneLoading() const { | 284 void OfflinePageEvaluationBridge::NotifyIfDoneLoading() const { |
| 217 if (!offline_page_model_->is_loaded()) | 285 if (!offline_page_model_->is_loaded()) |
| 218 return; | 286 return; |
| 219 JNIEnv* env = base::android::AttachCurrentThread(); | 287 JNIEnv* env = base::android::AttachCurrentThread(); |
| 220 Java_OfflinePageEvaluationBridge_offlinePageModelLoaded(env, java_ref_); | 288 Java_OfflinePageEvaluationBridge_offlinePageModelLoaded(env, java_ref_); |
| 221 } | 289 } |
| 222 | 290 |
| 223 } // namespace android | 291 } // namespace android |
| 224 } // namespace offline_pages | 292 } // namespace offline_pages |
| OLD | NEW |