| 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/bind.h" |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 50 content::WebContents* GetWebContentsFromJavaTab( | 50 content::WebContents* GetWebContentsFromJavaTab( |
| 51 const ScopedJavaGlobalRef<jobject>& j_tab_ref) { | 51 const ScopedJavaGlobalRef<jobject>& j_tab_ref) { |
| 52 JNIEnv* env = AttachCurrentThread(); | 52 JNIEnv* env = AttachCurrentThread(); |
| 53 TabAndroid* tab = TabAndroid::GetNativeTab(env, j_tab_ref); | 53 TabAndroid* tab = TabAndroid::GetNativeTab(env, j_tab_ref); |
| 54 if (!tab) | 54 if (!tab) |
| 55 return nullptr; | 55 return nullptr; |
| 56 | 56 |
| 57 return tab->web_contents(); | 57 return tab->web_contents(); |
| 58 } | 58 } |
| 59 | 59 |
| 60 // TODO(dewittj): Move to Download UI Adapter. | |
| 61 OfflinePageModel* GetOfflinePageModelFromJavaTab( | |
| 62 const ScopedJavaGlobalRef<jobject>& j_tab_ref) { | |
| 63 content::WebContents* web_contents = GetWebContentsFromJavaTab(j_tab_ref); | |
| 64 if (!web_contents) | |
| 65 return nullptr; | |
| 66 | |
| 67 Profile* profile = | |
| 68 Profile::FromBrowserContext(web_contents->GetBrowserContext()) | |
| 69 ->GetOriginalProfile(); | |
| 70 | |
| 71 return OfflinePageModelFactory::GetForBrowserContext(profile); | |
| 72 } | |
| 73 | |
| 74 void SavePageIfNotNavigatedAway(const GURL& original_url, | 60 void SavePageIfNotNavigatedAway(const GURL& original_url, |
| 75 const ScopedJavaGlobalRef<jobject>& j_tab_ref) { | 61 const ScopedJavaGlobalRef<jobject>& j_tab_ref) { |
| 76 content::WebContents* web_contents = GetWebContentsFromJavaTab(j_tab_ref); | 62 content::WebContents* web_contents = GetWebContentsFromJavaTab(j_tab_ref); |
| 77 if (!web_contents) | 63 if (!web_contents) |
| 78 return; | 64 return; |
| 79 | 65 |
| 80 // This ignores fragment differences in URLs, bails out only if tab has | 66 // This ignores fragment differences in URLs, bails out only if tab has |
| 81 // navigated away and not just scrolled to a fragment. | 67 // navigated away and not just scrolled to a fragment. |
| 82 GURL url = web_contents->GetLastCommittedURL(); | 68 GURL url = web_contents->GetLastCommittedURL(); |
| 83 if (!OfflinePageUtils::EqualsIgnoringFragment(url, original_url)) | 69 if (!OfflinePageUtils::EqualsIgnoringFragment(url, original_url)) |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 116 request_coordinator->EnableForOffliner(request_id); | 102 request_coordinator->EnableForOffliner(request_id); |
| 117 } | 103 } |
| 118 return; | 104 return; |
| 119 } | 105 } |
| 120 tab_helper->ObserveAndDownloadCurrentPage(client_id, request_id); | 106 tab_helper->ObserveAndDownloadCurrentPage(client_id, request_id); |
| 121 | 107 |
| 122 OfflinePageNotificationBridge notification_bridge; | 108 OfflinePageNotificationBridge notification_bridge; |
| 123 notification_bridge.ShowDownloadingToast(); | 109 notification_bridge.ShowDownloadingToast(); |
| 124 } | 110 } |
| 125 | 111 |
| 126 void OnDeletePagesForInfoBar(const GURL& original_url, | |
| 127 const ScopedJavaGlobalRef<jobject>& j_tab_ref, | |
| 128 DeletePageResult result) { | |
| 129 SavePageIfNotNavigatedAway(original_url, j_tab_ref); | |
| 130 } | |
| 131 | |
| 132 void DeletePagesForOverwrite(const GURL& original_url, | |
| 133 const ScopedJavaGlobalRef<jobject>& j_tab_ref, | |
| 134 const MultipleOfflinePageItemResult& pages) { | |
| 135 OfflinePageModel* model = GetOfflinePageModelFromJavaTab(j_tab_ref); | |
| 136 if (!model) | |
| 137 return; | |
| 138 | |
| 139 std::vector<int64_t> offline_ids; | |
| 140 for (auto& page : pages) { | |
| 141 if (page.client_id.name_space == kDownloadNamespace || | |
| 142 page.client_id.name_space == kAsyncNamespace) { | |
| 143 offline_ids.emplace_back(page.offline_id); | |
| 144 } | |
| 145 } | |
| 146 | |
| 147 model->DeletePagesByOfflineId( | |
| 148 offline_ids, base::Bind( | |
| 149 &OnDeletePagesForInfoBar, original_url, j_tab_ref)); | |
| 150 } | |
| 151 | |
| 152 void OnInfoBarAction(const GURL& original_url, | |
| 153 const ScopedJavaGlobalRef<jobject>& j_tab_ref, | |
| 154 OfflinePageInfoBarDelegate::Action action) { | |
| 155 switch (action) { | |
| 156 case OfflinePageInfoBarDelegate::Action::CREATE_NEW: | |
| 157 SavePageIfNotNavigatedAway(original_url, j_tab_ref); | |
| 158 break; | |
| 159 case OfflinePageInfoBarDelegate::Action::OVERWRITE: | |
| 160 OfflinePageModel* offline_page_model = | |
| 161 GetOfflinePageModelFromJavaTab(j_tab_ref); | |
| 162 if (!offline_page_model) | |
| 163 return; | |
| 164 | |
| 165 offline_page_model->GetPagesByOnlineURL( | |
| 166 original_url, | |
| 167 base::Bind(&DeletePagesForOverwrite, original_url, j_tab_ref)); | |
| 168 break; | |
| 169 } | |
| 170 } | |
| 171 | |
| 172 void RequestQueueDuplicateCheckDone( | 112 void RequestQueueDuplicateCheckDone( |
| 173 const GURL& original_url, | 113 const GURL& original_url, |
| 174 const ScopedJavaGlobalRef<jobject>& j_tab_ref, | 114 const ScopedJavaGlobalRef<jobject>& j_tab_ref, |
| 175 bool has_duplicates) { | 115 bool has_duplicates) { |
| 176 if (has_duplicates) { | 116 if (has_duplicates) { |
| 177 // TODO(fgorski): Additionally we could update existing request's expiration | 117 // TODO(fgorski): Additionally we could update existing request's expiration |
| 178 // period, as it is still important. Alternative would be to actually take a | 118 // period, as it is still important. Alternative would be to actually take a |
| 179 // snapshot on the spot, but that would only work if the page is loaded | 119 // snapshot on the spot, but that would only work if the page is loaded |
| 180 // enough. | 120 // enough. |
| 181 // This simply toasts that the item is downloading. | 121 // This simply toasts that the item is downloading. |
| 182 OfflinePageNotificationBridge notification_bridge; | 122 OfflinePageNotificationBridge notification_bridge; |
| 183 notification_bridge.ShowDownloadingToast(); | 123 notification_bridge.ShowDownloadingToast(); |
| 184 return; | 124 return; |
| 185 } | 125 } |
| 186 | 126 |
| 187 SavePageIfNotNavigatedAway(original_url, j_tab_ref); | 127 SavePageIfNotNavigatedAway(original_url, j_tab_ref); |
| 188 } | 128 } |
| 189 | 129 |
| 190 void ModelDuplicateCheckDone(const GURL& original_url, | 130 void ModelDuplicateCheckDone(const GURL& original_url, |
| 191 const ScopedJavaGlobalRef<jobject>& j_tab_ref, | 131 const ScopedJavaGlobalRef<jobject>& j_tab_ref, |
| 192 const std::string& downloads_label, | |
| 193 bool has_duplicates) { | 132 bool has_duplicates) { |
| 194 content::WebContents* web_contents = GetWebContentsFromJavaTab(j_tab_ref); | 133 content::WebContents* web_contents = GetWebContentsFromJavaTab(j_tab_ref); |
| 195 if (!web_contents) | 134 if (!web_contents) |
| 196 return; | 135 return; |
| 197 | 136 |
| 198 if (has_duplicates) { | 137 if (has_duplicates) { |
| 199 OfflinePageInfoBarDelegate::Create( | 138 OfflinePageInfoBarDelegate::Create( |
| 200 base::Bind(&OnInfoBarAction, original_url, j_tab_ref), downloads_label, | 139 base::Bind(&SavePageIfNotNavigatedAway, original_url, j_tab_ref), |
| 201 original_url, web_contents); | 140 original_url, web_contents); |
| 202 return; | 141 return; |
| 203 } | 142 } |
| 204 | 143 |
| 205 OfflinePageUtils::CheckExistenceOfRequestsWithURL( | 144 OfflinePageUtils::CheckExistenceOfRequestsWithURL( |
| 206 Profile::FromBrowserContext(web_contents->GetBrowserContext()) | 145 Profile::FromBrowserContext(web_contents->GetBrowserContext()) |
| 207 ->GetOriginalProfile(), | 146 ->GetOriginalProfile(), |
| 208 kDownloadNamespace, original_url, | 147 kDownloadNamespace, original_url, |
| 209 base::Bind(&RequestQueueDuplicateCheckDone, original_url, j_tab_ref)); | 148 base::Bind(&RequestQueueDuplicateCheckDone, original_url, j_tab_ref)); |
| 210 } | 149 } |
| (...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 356 JNIEnv* env, | 295 JNIEnv* env, |
| 357 const JavaParamRef<jobject>& obj, | 296 const JavaParamRef<jobject>& obj, |
| 358 const JavaParamRef<jstring>& j_guid) { | 297 const JavaParamRef<jstring>& j_guid) { |
| 359 std::string guid = ConvertJavaStringToUTF8(env, j_guid); | 298 std::string guid = ConvertJavaStringToUTF8(env, j_guid); |
| 360 return download_ui_adapter_->GetOfflineIdByGuid(guid); | 299 return download_ui_adapter_->GetOfflineIdByGuid(guid); |
| 361 } | 300 } |
| 362 | 301 |
| 363 void OfflinePageDownloadBridge::StartDownload( | 302 void OfflinePageDownloadBridge::StartDownload( |
| 364 JNIEnv* env, | 303 JNIEnv* env, |
| 365 const JavaParamRef<jobject>& obj, | 304 const JavaParamRef<jobject>& obj, |
| 366 const JavaParamRef<jobject>& j_tab, | 305 const JavaParamRef<jobject>& j_tab) { |
| 367 const JavaParamRef<jstring>& j_downloads_label) { | |
| 368 std::string downloads_label = ConvertJavaStringToUTF8(env, j_downloads_label); | |
| 369 TabAndroid* tab = TabAndroid::GetNativeTab(env, j_tab); | 306 TabAndroid* tab = TabAndroid::GetNativeTab(env, j_tab); |
| 370 if (!tab) | 307 if (!tab) |
| 371 return; | 308 return; |
| 372 | 309 |
| 373 content::WebContents* web_contents = tab->web_contents(); | 310 content::WebContents* web_contents = tab->web_contents(); |
| 374 if (!web_contents) | 311 if (!web_contents) |
| 375 return; | 312 return; |
| 376 | 313 |
| 377 GURL url = web_contents->GetLastCommittedURL(); | 314 GURL url = web_contents->GetLastCommittedURL(); |
| 378 | 315 |
| 379 ScopedJavaGlobalRef<jobject> j_tab_ref(env, j_tab); | 316 ScopedJavaGlobalRef<jobject> j_tab_ref(env, j_tab); |
| 380 | 317 |
| 381 OfflinePageUtils::CheckExistenceOfPagesWithURL( | 318 OfflinePageUtils::CheckExistenceOfPagesWithURL( |
| 382 tab->GetProfile()->GetOriginalProfile(), kDownloadNamespace, url, | 319 tab->GetProfile()->GetOriginalProfile(), kDownloadNamespace, url, |
| 383 base::Bind(&ModelDuplicateCheckDone, url, j_tab_ref, downloads_label)); | 320 base::Bind(&ModelDuplicateCheckDone, url, j_tab_ref)); |
| 384 } | 321 } |
| 385 | 322 |
| 386 void OfflinePageDownloadBridge::CancelDownload( | 323 void OfflinePageDownloadBridge::CancelDownload( |
| 387 JNIEnv* env, | 324 JNIEnv* env, |
| 388 const JavaParamRef<jobject>& obj, | 325 const JavaParamRef<jobject>& obj, |
| 389 const JavaParamRef<jstring>& j_guid) { | 326 const JavaParamRef<jstring>& j_guid) { |
| 390 std::string guid = ConvertJavaStringToUTF8(env, j_guid); | 327 std::string guid = ConvertJavaStringToUTF8(env, j_guid); |
| 391 RequestCoordinator* coordinator = | 328 RequestCoordinator* coordinator = |
| 392 RequestCoordinatorFactory::GetForBrowserContext(browser_context_); | 329 RequestCoordinatorFactory::GetForBrowserContext(browser_context_); |
| 393 | 330 |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 477 | 414 |
| 478 DownloadUIAdapter* adapter = | 415 DownloadUIAdapter* adapter = |
| 479 DownloadUIAdapter::FromOfflinePageModel(offline_page_model); | 416 DownloadUIAdapter::FromOfflinePageModel(offline_page_model); |
| 480 | 417 |
| 481 return reinterpret_cast<jlong>( | 418 return reinterpret_cast<jlong>( |
| 482 new OfflinePageDownloadBridge(env, obj, adapter, browser_context)); | 419 new OfflinePageDownloadBridge(env, obj, adapter, browser_context)); |
| 483 } | 420 } |
| 484 | 421 |
| 485 } // namespace android | 422 } // namespace android |
| 486 } // namespace offline_pages | 423 } // namespace offline_pages |
| OLD | NEW |