| 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/downloads/offline_page_download_b
ridge.h" | 5 #include "chrome/browser/android/offline_pages/downloads/offline_page_download_b
ridge.h" |
| 6 | 6 |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "base/android/jni_string.h" | 9 #include "base/android/jni_string.h" |
| 10 #include "base/guid.h" | 10 #include "base/guid.h" |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 60 ConvertUTF8ToJavaString(env, item.target_path.value()), | 60 ConvertUTF8ToJavaString(env, item.target_path.value()), |
| 61 item.start_time.ToJavaTime(), item.total_bytes); | 61 item.start_time.ToJavaTime(), item.total_bytes); |
| 62 } | 62 } |
| 63 } // namespace | 63 } // namespace |
| 64 | 64 |
| 65 OfflinePageDownloadBridge::OfflinePageDownloadBridge( | 65 OfflinePageDownloadBridge::OfflinePageDownloadBridge( |
| 66 JNIEnv* env, | 66 JNIEnv* env, |
| 67 const JavaParamRef<jobject>& obj, | 67 const JavaParamRef<jobject>& obj, |
| 68 DownloadUIAdapter* download_ui_adapter) | 68 DownloadUIAdapter* download_ui_adapter) |
| 69 : weak_java_ref_(env, obj), | 69 : weak_java_ref_(env, obj), |
| 70 download_ui_adapter_(download_ui_adapter), | 70 download_ui_adapter_(download_ui_adapter) { |
| 71 weak_ptr_factory_(this) { | |
| 72 DCHECK(download_ui_adapter_); | 71 DCHECK(download_ui_adapter_); |
| 73 download_ui_adapter_->AddObserver(this); | 72 download_ui_adapter_->AddObserver(this); |
| 74 } | 73 } |
| 75 | 74 |
| 76 OfflinePageDownloadBridge::~OfflinePageDownloadBridge() {} | 75 OfflinePageDownloadBridge::~OfflinePageDownloadBridge() {} |
| 77 | 76 |
| 78 // static | 77 // static |
| 79 bool OfflinePageDownloadBridge::Register(JNIEnv* env) { | 78 bool OfflinePageDownloadBridge::Register(JNIEnv* env) { |
| 80 return RegisterNativesImpl(env); | 79 return RegisterNativesImpl(env); |
| 81 } | 80 } |
| 82 | 81 |
| 82 // static |
| 83 void OfflinePageDownloadBridge::SavePageCallback( |
| 84 const DownloadUIItem& item, |
| 85 OfflinePageModel::SavePageResult result, |
| 86 int64_t offline_id) { |
| 87 OfflinePageNotificationBridge notification_bridge; |
| 88 if (result == SavePageResult::SUCCESS) |
| 89 notification_bridge.NotifyDownloadSuccessful(item); |
| 90 else |
| 91 notification_bridge.NotifyDownloadFailed(item); |
| 92 } |
| 93 |
| 83 void OfflinePageDownloadBridge::Destroy(JNIEnv* env, | 94 void OfflinePageDownloadBridge::Destroy(JNIEnv* env, |
| 84 const JavaParamRef<jobject>&) { | 95 const JavaParamRef<jobject>&) { |
| 85 download_ui_adapter_->RemoveObserver(this); | 96 download_ui_adapter_->RemoveObserver(this); |
| 86 delete this; | 97 delete this; |
| 87 } | 98 } |
| 88 | 99 |
| 89 void OfflinePageDownloadBridge::GetAllItems( | 100 void OfflinePageDownloadBridge::GetAllItems( |
| 90 JNIEnv* env, | 101 JNIEnv* env, |
| 91 const JavaParamRef<jobject>& obj, | 102 const JavaParamRef<jobject>& obj, |
| 92 const JavaParamRef<jobject>& j_result_obj) { | 103 const JavaParamRef<jobject>& j_result_obj) { |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 153 | 164 |
| 154 DownloadUIItem item; | 165 DownloadUIItem item; |
| 155 item.guid = client_id.id; | 166 item.guid = client_id.id; |
| 156 item.url = url; | 167 item.url = url; |
| 157 | 168 |
| 158 OfflinePageNotificationBridge bridge; | 169 OfflinePageNotificationBridge bridge; |
| 159 bridge.NotifyDownloadProgress(item); | 170 bridge.NotifyDownloadProgress(item); |
| 160 | 171 |
| 161 offline_page_model->SavePage( | 172 offline_page_model->SavePage( |
| 162 url, client_id, 0ul, std::move(archiver), | 173 url, client_id, 0ul, std::move(archiver), |
| 163 base::Bind(&OfflinePageDownloadBridge::SavePageCallback, | 174 base::Bind(&OfflinePageDownloadBridge::SavePageCallback, item)); |
| 164 weak_ptr_factory_.GetWeakPtr(), item)); | |
| 165 } | 175 } |
| 166 | 176 |
| 167 void OfflinePageDownloadBridge::ItemsLoaded() { | 177 void OfflinePageDownloadBridge::ItemsLoaded() { |
| 168 JNIEnv* env = AttachCurrentThread(); | 178 JNIEnv* env = AttachCurrentThread(); |
| 169 ScopedJavaLocalRef<jobject> obj = weak_java_ref_.get(env); | 179 ScopedJavaLocalRef<jobject> obj = weak_java_ref_.get(env); |
| 170 if (obj.is_null()) | 180 if (obj.is_null()) |
| 171 return; | 181 return; |
| 172 Java_OfflinePageDownloadBridge_downloadItemsLoaded(env, obj); | 182 Java_OfflinePageDownloadBridge_downloadItemsLoaded(env, obj); |
| 173 } | 183 } |
| 174 | 184 |
| (...skipping 17 matching lines...) Expand all Loading... |
| 192 | 202 |
| 193 void OfflinePageDownloadBridge::ItemUpdated(const DownloadUIItem& item) { | 203 void OfflinePageDownloadBridge::ItemUpdated(const DownloadUIItem& item) { |
| 194 JNIEnv* env = AttachCurrentThread(); | 204 JNIEnv* env = AttachCurrentThread(); |
| 195 ScopedJavaLocalRef<jobject> obj = weak_java_ref_.get(env); | 205 ScopedJavaLocalRef<jobject> obj = weak_java_ref_.get(env); |
| 196 if (obj.is_null()) | 206 if (obj.is_null()) |
| 197 return; | 207 return; |
| 198 Java_OfflinePageDownloadBridge_downloadItemUpdated( | 208 Java_OfflinePageDownloadBridge_downloadItemUpdated( |
| 199 env, obj, ToJavaOfflinePageDownloadItem(env, item)); | 209 env, obj, ToJavaOfflinePageDownloadItem(env, item)); |
| 200 } | 210 } |
| 201 | 211 |
| 202 void OfflinePageDownloadBridge::SavePageCallback( | |
| 203 const DownloadUIItem& item, | |
| 204 OfflinePageModel::SavePageResult result, | |
| 205 int64_t offline_id) { | |
| 206 OfflinePageNotificationBridge notification_bridge; | |
| 207 if (result == SavePageResult::SUCCESS) | |
| 208 notification_bridge.NotifyDownloadSuccessful(item); | |
| 209 else | |
| 210 notification_bridge.NotifyDownloadFailed(item); | |
| 211 } | |
| 212 | |
| 213 static jlong Init(JNIEnv* env, | 212 static jlong Init(JNIEnv* env, |
| 214 const JavaParamRef<jobject>& obj, | 213 const JavaParamRef<jobject>& obj, |
| 215 const JavaParamRef<jobject>& j_profile) { | 214 const JavaParamRef<jobject>& j_profile) { |
| 216 Profile* profile = ProfileAndroid::FromProfileAndroid(j_profile); | 215 Profile* profile = ProfileAndroid::FromProfileAndroid(j_profile); |
| 217 OfflinePageModel* offline_page_model = | 216 OfflinePageModel* offline_page_model = |
| 218 OfflinePageModelFactory::GetForBrowserContext(profile); | 217 OfflinePageModelFactory::GetForBrowserContext(profile); |
| 219 | 218 |
| 220 DownloadUIAdapter* adapter = | 219 DownloadUIAdapter* adapter = |
| 221 DownloadUIAdapter::FromOfflinePageModel(offline_page_model); | 220 DownloadUIAdapter::FromOfflinePageModel(offline_page_model); |
| 222 | 221 |
| 223 return reinterpret_cast<jlong>( | 222 return reinterpret_cast<jlong>( |
| 224 new OfflinePageDownloadBridge(env, obj, adapter)); | 223 new OfflinePageDownloadBridge(env, obj, adapter)); |
| 225 } | 224 } |
| 226 | 225 |
| 227 } // namespace android | 226 } // namespace android |
| 228 } // namespace offline_pages | 227 } // namespace offline_pages |
| OLD | NEW |