Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/android/offline_pages/evaluation/offline_page_evaluatio n_bridge.h" | |
| 6 | |
| 7 #include "base/android/callback_android.h" | |
| 8 #include "base/android/jni_android.h" | |
| 9 #include "base/android/jni_string.h" | |
| 10 #include "chrome/browser/android/offline_pages/offline_page_model_factory.h" | |
| 11 #include "chrome/browser/android/offline_pages/request_coordinator_factory.h" | |
| 12 #include "chrome/browser/profiles/profile.h" | |
| 13 #include "chrome/browser/profiles/profile_android.h" | |
| 14 #include "components/offline_pages/background/request_coordinator.h" | |
| 15 #include "components/offline_pages/background/request_notifier.h" | |
| 16 #include "components/offline_pages/background/save_page_request.h" | |
| 17 #include "components/offline_pages/offline_page_item.h" | |
| 18 #include "components/offline_pages/offline_page_model.h" | |
| 19 #include "content/public/browser/browser_context.h" | |
| 20 #include "jni/OfflinePageEvaluationBridge_jni.h" | |
| 21 #include "jni/SavePageRequest_jni.h" | |
| 22 | |
| 23 using base::android::ConvertJavaStringToUTF8; | |
| 24 using base::android::ConvertUTF8ToJavaString; | |
| 25 using base::android::JavaParamRef; | |
| 26 using base::android::ScopedJavaGlobalRef; | |
| 27 using base::android::ScopedJavaLocalRef; | |
| 28 | |
| 29 namespace offline_pages { | |
| 30 namespace android { | |
| 31 | |
| 32 namespace { | |
| 33 | |
| 34 void ToJavaOfflinePageList(JNIEnv* env, | |
| 35 jobject j_result_obj, | |
| 36 const std::vector<OfflinePageItem>& offline_pages) { | |
| 37 for (const OfflinePageItem& offline_page : offline_pages) { | |
| 38 Java_OfflinePageEvaluationBridge_createOfflinePageAndAddToList( | |
| 39 env, j_result_obj, | |
| 40 ConvertUTF8ToJavaString(env, offline_page.url.spec()), | |
| 41 offline_page.offline_id, | |
| 42 ConvertUTF8ToJavaString(env, offline_page.client_id.name_space), | |
| 43 ConvertUTF8ToJavaString(env, offline_page.client_id.id), | |
| 44 ConvertUTF8ToJavaString(env, offline_page.GetOfflineURL().spec()), | |
| 45 ConvertUTF8ToJavaString(env, offline_page.file_path.value()), | |
| 46 offline_page.file_size, offline_page.creation_time.ToJavaTime(), | |
| 47 offline_page.access_count, offline_page.last_access_time.ToJavaTime()); | |
| 48 } | |
| 49 } | |
| 50 | |
| 51 ScopedJavaLocalRef<jobject> ToJavaSavePageRequest( | |
| 52 JNIEnv* env, | |
| 53 const SavePageRequest& request) { | |
| 54 return Java_SavePageRequest_create( | |
| 55 env, static_cast<int>(request.request_state()), request.request_id(), | |
| 56 ConvertUTF8ToJavaString(env, request.url().spec()), | |
| 57 ConvertUTF8ToJavaString(env, request.client_id().name_space), | |
| 58 ConvertUTF8ToJavaString(env, request.client_id().id)); | |
| 59 } | |
| 60 | |
| 61 void GetAllPagesCallback( | |
| 62 const ScopedJavaGlobalRef<jobject>& j_result_obj, | |
| 63 const ScopedJavaGlobalRef<jobject>& j_callback_obj, | |
| 64 const OfflinePageModel::MultipleOfflinePageItemResult& result) { | |
| 65 JNIEnv* env = base::android::AttachCurrentThread(); | |
| 66 ToJavaOfflinePageList(env, j_result_obj.obj(), result); | |
| 67 base::android::RunCallbackAndroid(j_callback_obj, j_result_obj); | |
| 68 } | |
| 69 | |
| 70 void OnPushRequestsDone(const ScopedJavaGlobalRef<jobject>& j_callback_obj, | |
| 71 bool result) { | |
| 72 base::android::RunCallbackAndroid(j_callback_obj, result); | |
| 73 } | |
| 74 | |
| 75 } // namespace | |
| 76 | |
| 77 static ScopedJavaLocalRef<jobject> GetBridgeForProfile( | |
| 78 JNIEnv* env, | |
| 79 const JavaParamRef<jclass>& jcaller, | |
| 80 const JavaParamRef<jobject>& j_profile) { | |
| 81 Profile* profile = ProfileAndroid::FromProfileAndroid(j_profile); | |
| 82 | |
| 83 OfflinePageModel* offline_page_model = | |
| 84 OfflinePageModelFactory::GetForBrowserContext(profile); | |
| 85 | |
| 86 RequestCoordinator* request_coordinator = | |
| 87 RequestCoordinatorFactory::GetForBrowserContext(profile); | |
| 88 | |
| 89 if (offline_page_model == nullptr || request_coordinator == nullptr) | |
| 90 return ScopedJavaLocalRef<jobject>(); | |
| 91 | |
| 92 OfflinePageEvaluationBridge* bridge = new OfflinePageEvaluationBridge( | |
| 93 env, profile, offline_page_model, request_coordinator); | |
| 94 | |
| 95 return ScopedJavaLocalRef<jobject>(bridge->java_ref()); | |
| 96 } | |
| 97 | |
| 98 // static | |
| 99 bool OfflinePageEvaluationBridge::Register(JNIEnv* env) { | |
| 100 return RegisterNativesImpl(env); | |
| 101 } | |
| 102 | |
| 103 OfflinePageEvaluationBridge::OfflinePageEvaluationBridge( | |
| 104 JNIEnv* env, | |
| 105 content::BrowserContext* browser_context, | |
| 106 OfflinePageModel* offline_page_model, | |
| 107 RequestCoordinator* request_coordinator) | |
| 108 : browser_context_(browser_context), | |
| 109 offline_page_model_(offline_page_model), | |
| 110 request_coordinator_(request_coordinator) { | |
| 111 ScopedJavaLocalRef<jobject> j_offline_page_evaluation_bridge = | |
| 112 Java_OfflinePageEvaluationBridge_create(env, | |
| 113 reinterpret_cast<jlong>(this)); | |
| 114 java_ref_.Reset(j_offline_page_evaluation_bridge); | |
| 115 | |
| 116 NotifyIfDoneLoading(); | |
| 117 offline_page_model_->AddObserver(this); | |
| 118 request_coordinator_->AddObserver(this); | |
| 119 } | |
| 120 | |
| 121 OfflinePageEvaluationBridge::~OfflinePageEvaluationBridge() { | |
| 122 JNIEnv* env = base::android::AttachCurrentThread(); | |
| 123 Java_OfflinePageEvaluationBridge_offlinePageEvaluationBridgeDestroyed( | |
| 124 env, java_ref_); | |
| 125 } | |
| 126 | |
| 127 // Implement OfflinePageModel::Observer | |
| 128 void OfflinePageEvaluationBridge::OfflinePageModelLoaded( | |
| 129 OfflinePageModel* model) { | |
| 130 DCHECK_EQ(offline_page_model_, model); | |
| 131 NotifyIfDoneLoading(); | |
| 132 } | |
| 133 | |
| 134 void OfflinePageEvaluationBridge::OfflinePageModelChanged( | |
| 135 OfflinePageModel* model) {} | |
| 136 | |
| 137 void OfflinePageEvaluationBridge::OfflinePageDeleted( | |
| 138 int64_t offline_id, | |
| 139 const ClientId& client_id) {} | |
| 140 | |
| 141 // Implement RequestCoordinator::Observer | |
| 142 void OfflinePageEvaluationBridge::OnAdded(const SavePageRequest& request) { | |
| 143 JNIEnv* env = base::android::AttachCurrentThread(); | |
| 144 Java_OfflinePageEvaluationBridge_savePageRequestAdded( | |
| 145 env, java_ref_, ToJavaSavePageRequest(env, request)); | |
| 146 } | |
| 147 | |
| 148 void OfflinePageEvaluationBridge::OnCompleted( | |
| 149 const SavePageRequest& request, | |
| 150 RequestNotifier::BackgroundSavePageResult status) { | |
| 151 JNIEnv* env = base::android::AttachCurrentThread(); | |
| 152 Java_OfflinePageEvaluationBridge_savePageRequestCompleted( | |
| 153 env, java_ref_, ToJavaSavePageRequest(env, request), | |
| 154 static_cast<int>(status)); | |
| 155 } | |
| 156 | |
| 157 void OfflinePageEvaluationBridge::OnChanged(const SavePageRequest& request) { | |
| 158 JNIEnv* env = base::android::AttachCurrentThread(); | |
| 159 Java_OfflinePageEvaluationBridge_savePageRequestChanged( | |
| 160 env, java_ref_, ToJavaSavePageRequest(env, request)); | |
| 161 } | |
| 162 | |
| 163 void OfflinePageEvaluationBridge::GetAllPages( | |
| 164 JNIEnv* env, | |
| 165 const JavaParamRef<jobject>& obj, | |
| 166 const JavaParamRef<jobject>& j_result_obj, | |
| 167 const JavaParamRef<jobject>& j_callback_obj) { | |
| 168 DCHECK(j_result_obj); | |
| 169 DCHECK(j_callback_obj); | |
| 170 | |
| 171 ScopedJavaGlobalRef<jobject> j_result_ref; | |
| 172 j_result_ref.Reset(env, j_result_obj); | |
| 173 | |
| 174 ScopedJavaGlobalRef<jobject> j_callback_ref; | |
| 175 j_callback_ref.Reset(env, j_callback_obj); | |
| 176 | |
| 177 offline_page_model_->GetAllPages( | |
| 178 base::Bind(&GetAllPagesCallback, j_result_ref, j_callback_ref)); | |
| 179 } | |
| 180 | |
| 181 void OfflinePageEvaluationBridge::PushRequestProcessing( | |
| 182 JNIEnv* env, | |
| 183 const base::android::JavaParamRef<jobject>& obj, | |
| 184 const base::android::JavaParamRef<jobject>& j_callback_obj) { | |
| 185 ScopedJavaGlobalRef<jobject> j_callback_ref(j_callback_obj); | |
| 186 if (!request_coordinator_) { | |
|
Pete Williamson
2016/10/04 23:50:55
How could we ever end up with no request coordinat
romax
2016/10/11 21:46:30
Done.
| |
| 187 base::android::RunCallbackAndroid(j_callback_obj, false); | |
| 188 return; | |
| 189 } | |
| 190 net::NetworkChangeNotifier::ConnectionType connection = | |
| 191 net::NetworkChangeNotifier::GetConnectionType(); | |
| 192 DeviceConditions device_conditions(false, 0, connection); | |
| 193 request_coordinator_->StartProcessing( | |
| 194 device_conditions, base::Bind(&OnPushRequestsDone, j_callback_ref)); | |
| 195 } | |
| 196 | |
| 197 void OfflinePageEvaluationBridge::SavePageLater( | |
| 198 JNIEnv* env, | |
| 199 const JavaParamRef<jobject>& obj, | |
| 200 const JavaParamRef<jstring>& j_url, | |
| 201 const JavaParamRef<jstring>& j_namespace, | |
| 202 const JavaParamRef<jstring>& j_client_id, | |
| 203 jboolean user_requested) { | |
| 204 offline_pages::ClientId client_id; | |
| 205 client_id.name_space = ConvertJavaStringToUTF8(env, j_namespace); | |
| 206 client_id.id = ConvertJavaStringToUTF8(env, j_client_id); | |
| 207 | |
| 208 request_coordinator_->SavePageLater(GURL(ConvertJavaStringToUTF8(env, j_url)), | |
| 209 client_id, | |
| 210 static_cast<bool>(user_requested)); | |
| 211 } | |
| 212 | |
| 213 void OfflinePageEvaluationBridge::NotifyIfDoneLoading() const { | |
| 214 if (!offline_page_model_->is_loaded()) | |
| 215 return; | |
| 216 JNIEnv* env = base::android::AttachCurrentThread(); | |
| 217 Java_OfflinePageEvaluationBridge_offlinePageModelLoaded(env, java_ref_); | |
| 218 } | |
| 219 | |
| 220 } // namespace android | |
| 221 } // namespace offline_pages | |
| OLD | NEW |