| 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/bind.h" |
| 10 #include "base/guid.h" | 11 #include "base/guid.h" |
| 12 #include "base/logging.h" |
| 11 #include "base/memory/ptr_util.h" | 13 #include "base/memory/ptr_util.h" |
| 12 #include "chrome/browser/android/offline_pages/downloads/offline_page_notificati
on_bridge.h" | 14 #include "chrome/browser/android/offline_pages/downloads/offline_page_notificati
on_bridge.h" |
| 13 #include "chrome/browser/android/offline_pages/offline_page_mhtml_archiver.h" | 15 #include "chrome/browser/android/offline_pages/offline_page_mhtml_archiver.h" |
| 14 #include "chrome/browser/android/offline_pages/offline_page_model_factory.h" | 16 #include "chrome/browser/android/offline_pages/offline_page_model_factory.h" |
| 15 #include "chrome/browser/android/offline_pages/recent_tab_helper.h" | 17 #include "chrome/browser/android/offline_pages/recent_tab_helper.h" |
| 16 #include "chrome/browser/android/offline_pages/request_coordinator_factory.h" | 18 #include "chrome/browser/android/offline_pages/request_coordinator_factory.h" |
| 17 #include "chrome/browser/android/tab_android.h" | 19 #include "chrome/browser/android/tab_android.h" |
| 18 #include "chrome/browser/profiles/profile.h" | 20 #include "chrome/browser/profiles/profile.h" |
| 19 #include "chrome/browser/profiles/profile_android.h" | 21 #include "chrome/browser/profiles/profile_android.h" |
| 20 #include "components/offline_pages/background/request_coordinator.h" | 22 #include "components/offline_pages/background/request_coordinator.h" |
| 21 #include "components/offline_pages/client_namespace_constants.h" | 23 #include "components/offline_pages/client_namespace_constants.h" |
| 22 #include "components/offline_pages/downloads/download_ui_item.h" | 24 #include "components/offline_pages/downloads/download_ui_item.h" |
| 23 #include "components/offline_pages/offline_page_feature.h" | 25 #include "components/offline_pages/offline_page_feature.h" |
| 24 #include "components/offline_pages/offline_page_model.h" | 26 #include "components/offline_pages/offline_page_model.h" |
| 27 #include "content/public/browser/browser_context.h" |
| 25 #include "content/public/browser/web_contents.h" | 28 #include "content/public/browser/web_contents.h" |
| 26 #include "jni/OfflinePageDownloadBridge_jni.h" | 29 #include "jni/OfflinePageDownloadBridge_jni.h" |
| 27 #include "net/base/filename_util.h" | 30 #include "net/base/filename_util.h" |
| 28 #include "url/gurl.h" | 31 #include "url/gurl.h" |
| 29 | 32 |
| 30 using base::android::AttachCurrentThread; | 33 using base::android::AttachCurrentThread; |
| 31 using base::android::ConvertJavaStringToUTF8; | 34 using base::android::ConvertJavaStringToUTF8; |
| 32 using base::android::ConvertUTF8ToJavaString; | 35 using base::android::ConvertUTF8ToJavaString; |
| 33 using base::android::ConvertUTF16ToJavaString; | 36 using base::android::ConvertUTF16ToJavaString; |
| 34 using base::android::JavaParamRef; | 37 using base::android::JavaParamRef; |
| (...skipping 22 matching lines...) Expand all Loading... |
| 57 ScopedJavaLocalRef<jobject> ToJavaOfflinePageDownloadItem( | 60 ScopedJavaLocalRef<jobject> ToJavaOfflinePageDownloadItem( |
| 58 JNIEnv* env, | 61 JNIEnv* env, |
| 59 const DownloadUIItem& item) { | 62 const DownloadUIItem& item) { |
| 60 return Java_OfflinePageDownloadBridge_createDownloadItem( | 63 return Java_OfflinePageDownloadBridge_createDownloadItem( |
| 61 env, ConvertUTF8ToJavaString(env, item.guid), | 64 env, ConvertUTF8ToJavaString(env, item.guid), |
| 62 ConvertUTF8ToJavaString(env, item.url.spec()), | 65 ConvertUTF8ToJavaString(env, item.url.spec()), |
| 63 ConvertUTF16ToJavaString(env, item.title), | 66 ConvertUTF16ToJavaString(env, item.title), |
| 64 ConvertUTF8ToJavaString(env, item.target_path.value()), | 67 ConvertUTF8ToJavaString(env, item.target_path.value()), |
| 65 item.start_time.ToJavaTime(), item.total_bytes); | 68 item.start_time.ToJavaTime(), item.total_bytes); |
| 66 } | 69 } |
| 70 |
| 71 std::vector<int64_t> FilterRequestsByGuid( |
| 72 const std::vector<SavePageRequest>& requests, |
| 73 const std::string& guid) { |
| 74 std::vector<int64_t> request_ids; |
| 75 for (const SavePageRequest& request : requests) { |
| 76 if (request.client_id().id == guid && |
| 77 (request.client_id().name_space == kDownloadNamespace || |
| 78 request.client_id().name_space == kAsyncNamespace)) { |
| 79 request_ids.push_back(request.request_id()); |
| 80 } |
| 81 } |
| 82 return request_ids; |
| 83 } |
| 84 |
| 85 void CancelRequestCallback(const RequestQueue::UpdateMultipleRequestResults&) { |
| 86 // Results ignored here, as UI uses observer to update itself. |
| 87 } |
| 88 |
| 89 void CancelRequestsContinuation(content::BrowserContext* browser_context, |
| 90 const std::string& guid, |
| 91 const std::vector<SavePageRequest>& requests) { |
| 92 RequestCoordinator* coordinator = |
| 93 RequestCoordinatorFactory::GetForBrowserContext(browser_context); |
| 94 if (coordinator) { |
| 95 std::vector<int64_t> request_ids = FilterRequestsByGuid(requests, guid); |
| 96 coordinator->RemoveRequests(request_ids, |
| 97 base::Bind(&CancelRequestCallback)); |
| 98 } else { |
| 99 LOG(WARNING) << "CancelRequestsContinuation has no valid coordinator."; |
| 100 } |
| 101 } |
| 102 |
| 103 void PauseRequestsContinuation(content::BrowserContext* browser_context, |
| 104 const std::string& guid, |
| 105 const std::vector<SavePageRequest>& requests) { |
| 106 RequestCoordinator* coordinator = |
| 107 RequestCoordinatorFactory::GetForBrowserContext(browser_context); |
| 108 if (coordinator) |
| 109 coordinator->PauseRequests(FilterRequestsByGuid(requests, guid)); |
| 110 else |
| 111 LOG(WARNING) << "PauseRequestsContinuation has no valid coordinator."; |
| 112 } |
| 113 |
| 114 void ResumeRequestsContinuation(content::BrowserContext* browser_context, |
| 115 const std::string& guid, |
| 116 const std::vector<SavePageRequest>& requests) { |
| 117 RequestCoordinator* coordinator = |
| 118 RequestCoordinatorFactory::GetForBrowserContext(browser_context); |
| 119 if (coordinator) |
| 120 coordinator->ResumeRequests(FilterRequestsByGuid(requests, guid)); |
| 121 else |
| 122 LOG(WARNING) << "ResumeRequestsContinuation has no valid coordinator."; |
| 123 } |
| 124 |
| 67 } // namespace | 125 } // namespace |
| 68 | 126 |
| 69 OfflinePageDownloadBridge::OfflinePageDownloadBridge( | 127 OfflinePageDownloadBridge::OfflinePageDownloadBridge( |
| 70 JNIEnv* env, | 128 JNIEnv* env, |
| 71 const JavaParamRef<jobject>& obj, | 129 const JavaParamRef<jobject>& obj, |
| 72 DownloadUIAdapter* download_ui_adapter) | 130 DownloadUIAdapter* download_ui_adapter, |
| 131 content::BrowserContext* browser_context) |
| 73 : weak_java_ref_(env, obj), | 132 : weak_java_ref_(env, obj), |
| 74 download_ui_adapter_(download_ui_adapter) { | 133 download_ui_adapter_(download_ui_adapter), |
| 134 browser_context_(browser_context) { |
| 75 DCHECK(download_ui_adapter_); | 135 DCHECK(download_ui_adapter_); |
| 76 download_ui_adapter_->AddObserver(this); | 136 download_ui_adapter_->AddObserver(this); |
| 77 } | 137 } |
| 78 | 138 |
| 79 OfflinePageDownloadBridge::~OfflinePageDownloadBridge() {} | 139 OfflinePageDownloadBridge::~OfflinePageDownloadBridge() {} |
| 80 | 140 |
| 81 // static | 141 // static |
| 82 bool OfflinePageDownloadBridge::Register(JNIEnv* env) { | 142 bool OfflinePageDownloadBridge::Register(JNIEnv* env) { |
| 83 return RegisterNativesImpl(env); | 143 return RegisterNativesImpl(env); |
| 84 } | 144 } |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 170 offline_pages::RequestCoordinator* request_coordinator = | 230 offline_pages::RequestCoordinator* request_coordinator = |
| 171 offline_pages::RequestCoordinatorFactory::GetForBrowserContext( | 231 offline_pages::RequestCoordinatorFactory::GetForBrowserContext( |
| 172 tab->GetProfile()->GetOriginalProfile()); | 232 tab->GetProfile()->GetOriginalProfile()); |
| 173 request_coordinator->SavePageLater(url, client_id, true); | 233 request_coordinator->SavePageLater(url, client_id, true); |
| 174 return; | 234 return; |
| 175 } | 235 } |
| 176 | 236 |
| 177 // Page is ready, capture it right from the tab. | 237 // Page is ready, capture it right from the tab. |
| 178 offline_pages::OfflinePageModel* offline_page_model = | 238 offline_pages::OfflinePageModel* offline_page_model = |
| 179 OfflinePageModelFactory::GetForBrowserContext( | 239 OfflinePageModelFactory::GetForBrowserContext( |
| 180 tab->GetProfile()->GetOriginalProfile()); | 240 tab->GetProfile()->GetOriginalProfile()); |
| 181 if (!offline_page_model) | 241 if (!offline_page_model) |
| 182 return; | 242 return; |
| 183 | 243 |
| 184 auto archiver = | 244 auto archiver = |
| 185 base::MakeUnique<offline_pages::OfflinePageMHTMLArchiver>(web_contents); | 245 base::MakeUnique<offline_pages::OfflinePageMHTMLArchiver>(web_contents); |
| 186 | 246 |
| 187 DownloadUIItem item; | 247 DownloadUIItem item; |
| 188 item.guid = client_id.id; | 248 item.guid = client_id.id; |
| 189 item.url = url; | 249 item.url = url; |
| 190 | 250 |
| 191 OfflinePageNotificationBridge bridge; | 251 OfflinePageNotificationBridge bridge; |
| 192 bridge.NotifyDownloadProgress(item); | 252 bridge.NotifyDownloadProgress(item); |
| 193 | 253 |
| 194 offline_page_model->SavePage( | 254 offline_page_model->SavePage( |
| 195 url, client_id, 0ul, std::move(archiver), | 255 url, client_id, 0ul, std::move(archiver), |
| 196 base::Bind(&OfflinePageDownloadBridge::SavePageCallback, item)); | 256 base::Bind(&OfflinePageDownloadBridge::SavePageCallback, item)); |
| 197 } | 257 } |
| 198 | 258 |
| 259 void OfflinePageDownloadBridge::CancelDownload( |
| 260 JNIEnv* env, |
| 261 const JavaParamRef<jobject>& obj, |
| 262 const JavaParamRef<jstring>& j_guid) { |
| 263 std::string guid = ConvertJavaStringToUTF8(env, j_guid); |
| 264 RequestCoordinator* coordinator = |
| 265 RequestCoordinatorFactory::GetForBrowserContext(browser_context_); |
| 266 |
| 267 if (coordinator) { |
| 268 coordinator->GetAllRequests( |
| 269 base::Bind(&CancelRequestsContinuation, browser_context_, guid)); |
| 270 } else { |
| 271 LOG(WARNING) << "CancelDownload has no valid coordinator."; |
| 272 } |
| 273 } |
| 274 |
| 275 void OfflinePageDownloadBridge::PauseDownload( |
| 276 JNIEnv* env, |
| 277 const JavaParamRef<jobject>& obj, |
| 278 const JavaParamRef<jstring>& j_guid) { |
| 279 std::string guid = ConvertJavaStringToUTF8(env, j_guid); |
| 280 RequestCoordinator* coordinator = |
| 281 RequestCoordinatorFactory::GetForBrowserContext(browser_context_); |
| 282 |
| 283 if (coordinator) { |
| 284 coordinator->GetAllRequests( |
| 285 base::Bind(&PauseRequestsContinuation, browser_context_, guid)); |
| 286 } else { |
| 287 LOG(WARNING) << "PauseDownload has no valid coordinator."; |
| 288 } |
| 289 } |
| 290 |
| 291 void OfflinePageDownloadBridge::ResumeDownload( |
| 292 JNIEnv* env, |
| 293 const JavaParamRef<jobject>& obj, |
| 294 const JavaParamRef<jstring>& j_guid) { |
| 295 std::string guid = ConvertJavaStringToUTF8(env, j_guid); |
| 296 RequestCoordinator* coordinator = |
| 297 RequestCoordinatorFactory::GetForBrowserContext(browser_context_); |
| 298 |
| 299 if (coordinator) { |
| 300 coordinator->GetAllRequests( |
| 301 base::Bind(&ResumeRequestsContinuation, browser_context_, guid)); |
| 302 } else { |
| 303 LOG(WARNING) << "ResumeDownload has no valid coordinator."; |
| 304 } |
| 305 } |
| 306 |
| 199 void OfflinePageDownloadBridge::ItemsLoaded() { | 307 void OfflinePageDownloadBridge::ItemsLoaded() { |
| 200 JNIEnv* env = AttachCurrentThread(); | 308 JNIEnv* env = AttachCurrentThread(); |
| 201 ScopedJavaLocalRef<jobject> obj = weak_java_ref_.get(env); | 309 ScopedJavaLocalRef<jobject> obj = weak_java_ref_.get(env); |
| 202 if (obj.is_null()) | 310 if (obj.is_null()) |
| 203 return; | 311 return; |
| 204 Java_OfflinePageDownloadBridge_downloadItemsLoaded(env, obj); | 312 Java_OfflinePageDownloadBridge_downloadItemsLoaded(env, obj); |
| 205 } | 313 } |
| 206 | 314 |
| 207 void OfflinePageDownloadBridge::ItemAdded(const DownloadUIItem& item) { | 315 void OfflinePageDownloadBridge::ItemAdded(const DownloadUIItem& item) { |
| 208 JNIEnv* env = AttachCurrentThread(); | 316 JNIEnv* env = AttachCurrentThread(); |
| (...skipping 18 matching lines...) Expand all Loading... |
| 227 ScopedJavaLocalRef<jobject> obj = weak_java_ref_.get(env); | 335 ScopedJavaLocalRef<jobject> obj = weak_java_ref_.get(env); |
| 228 if (obj.is_null()) | 336 if (obj.is_null()) |
| 229 return; | 337 return; |
| 230 Java_OfflinePageDownloadBridge_downloadItemUpdated( | 338 Java_OfflinePageDownloadBridge_downloadItemUpdated( |
| 231 env, obj, ToJavaOfflinePageDownloadItem(env, item)); | 339 env, obj, ToJavaOfflinePageDownloadItem(env, item)); |
| 232 } | 340 } |
| 233 | 341 |
| 234 static jlong Init(JNIEnv* env, | 342 static jlong Init(JNIEnv* env, |
| 235 const JavaParamRef<jobject>& obj, | 343 const JavaParamRef<jobject>& obj, |
| 236 const JavaParamRef<jobject>& j_profile) { | 344 const JavaParamRef<jobject>& j_profile) { |
| 237 Profile* profile = ProfileAndroid::FromProfileAndroid(j_profile); | 345 content::BrowserContext* browser_context = |
| 346 ProfileAndroid::FromProfileAndroid(j_profile); |
| 347 |
| 238 OfflinePageModel* offline_page_model = | 348 OfflinePageModel* offline_page_model = |
| 239 OfflinePageModelFactory::GetForBrowserContext(profile); | 349 OfflinePageModelFactory::GetForBrowserContext(browser_context); |
| 240 | 350 |
| 241 DownloadUIAdapter* adapter = | 351 DownloadUIAdapter* adapter = |
| 242 DownloadUIAdapter::FromOfflinePageModel(offline_page_model); | 352 DownloadUIAdapter::FromOfflinePageModel(offline_page_model); |
| 243 | 353 |
| 244 return reinterpret_cast<jlong>( | 354 return reinterpret_cast<jlong>( |
| 245 new OfflinePageDownloadBridge(env, obj, adapter)); | 355 new OfflinePageDownloadBridge(env, obj, adapter, browser_context)); |
| 246 } | 356 } |
| 247 | 357 |
| 248 } // namespace android | 358 } // namespace android |
| 249 } // namespace offline_pages | 359 } // namespace offline_pages |
| OLD | NEW |