| 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 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 49 void SavePageCallback(const DownloadUIItem& item, | 49 void SavePageCallback(const DownloadUIItem& item, |
| 50 OfflinePageModel::SavePageResult result, | 50 OfflinePageModel::SavePageResult result, |
| 51 int64_t offline_id) { | 51 int64_t offline_id) { |
| 52 OfflinePageNotificationBridge notification_bridge; | 52 OfflinePageNotificationBridge notification_bridge; |
| 53 if (result == SavePageResult::SUCCESS) | 53 if (result == SavePageResult::SUCCESS) |
| 54 notification_bridge.NotifyDownloadSuccessful(item); | 54 notification_bridge.NotifyDownloadSuccessful(item); |
| 55 else | 55 else |
| 56 notification_bridge.NotifyDownloadFailed(item); | 56 notification_bridge.NotifyDownloadFailed(item); |
| 57 } | 57 } |
| 58 | 58 |
| 59 // TODO(dewittj): Move to Download UI Adapter. |
| 59 content::WebContents* GetWebContentsFromJavaTab( | 60 content::WebContents* GetWebContentsFromJavaTab( |
| 60 const ScopedJavaGlobalRef<jobject>& j_tab_ref) { | 61 const ScopedJavaGlobalRef<jobject>& j_tab_ref) { |
| 61 JNIEnv* env = AttachCurrentThread(); | 62 JNIEnv* env = AttachCurrentThread(); |
| 62 TabAndroid* tab = TabAndroid::GetNativeTab(env, j_tab_ref); | 63 TabAndroid* tab = TabAndroid::GetNativeTab(env, j_tab_ref); |
| 63 if (!tab) | 64 if (!tab) |
| 64 return nullptr; | 65 return nullptr; |
| 65 | 66 |
| 66 return tab->web_contents(); | 67 return tab->web_contents(); |
| 67 } | 68 } |
| 68 | 69 |
| 70 // TODO(dewittj): Move to Download UI Adapter. |
| 71 OfflinePageModel* GetOfflinePageModelFromJavaTab( |
| 72 const ScopedJavaGlobalRef<jobject>& j_tab_ref) { |
| 73 content::WebContents* web_contents = GetWebContentsFromJavaTab(j_tab_ref); |
| 74 if (!web_contents) |
| 75 return nullptr; |
| 76 |
| 77 Profile* profile = |
| 78 Profile::FromBrowserContext(web_contents->GetBrowserContext()) |
| 79 ->GetOriginalProfile(); |
| 80 |
| 81 return OfflinePageModelFactory::GetForBrowserContext(profile); |
| 82 } |
| 83 |
| 69 void SavePageIfNavigatedToURL(const GURL& query_url, | 84 void SavePageIfNavigatedToURL(const GURL& query_url, |
| 70 const ScopedJavaGlobalRef<jobject>& j_tab_ref) { | 85 const ScopedJavaGlobalRef<jobject>& j_tab_ref) { |
| 71 content::WebContents* web_contents = GetWebContentsFromJavaTab(j_tab_ref); | 86 content::WebContents* web_contents = GetWebContentsFromJavaTab(j_tab_ref); |
| 72 if (!web_contents) | 87 if (!web_contents) |
| 73 return; | 88 return; |
| 74 | 89 |
| 75 // This doesn't detect navigations to the same URL, only that we are looking | 90 // This doesn't detect navigations to the same URL, only that we are looking |
| 76 // at a completely different page. | 91 // at a completely different page. |
| 77 GURL url = web_contents->GetLastCommittedURL(); | 92 GURL url = web_contents->GetLastCommittedURL(); |
| 78 if (!OfflinePageUtils::EqualsIgnoringFragment(url, query_url)) | 93 if (!OfflinePageUtils::EqualsIgnoringFragment(url, query_url)) |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 113 if (!offline_page_model) | 128 if (!offline_page_model) |
| 114 return; | 129 return; |
| 115 | 130 |
| 116 auto archiver = | 131 auto archiver = |
| 117 base::MakeUnique<offline_pages::OfflinePageMHTMLArchiver>(web_contents); | 132 base::MakeUnique<offline_pages::OfflinePageMHTMLArchiver>(web_contents); |
| 118 | 133 |
| 119 DownloadUIItem item; | 134 DownloadUIItem item; |
| 120 item.guid = client_id.id; | 135 item.guid = client_id.id; |
| 121 item.url = url; | 136 item.url = url; |
| 122 | 137 |
| 123 OfflinePageNotificationBridge bridge; | 138 notification_bridge.NotifyDownloadProgress(item); |
| 124 bridge.NotifyDownloadProgress(item); | |
| 125 | 139 |
| 126 notification_bridge.ShowDownloadingToast(); | 140 notification_bridge.ShowDownloadingToast(); |
| 127 offline_page_model->SavePage(url, client_id, 0l, std::move(archiver), | 141 offline_page_model->SavePage(url, client_id, 0l, std::move(archiver), |
| 128 base::Bind(&SavePageCallback, item)); | 142 base::Bind(&SavePageCallback, item)); |
| 129 } | 143 } |
| 130 | 144 |
| 145 void OnDeletePagesForInfoBar(const GURL& query_url, |
| 146 const ScopedJavaGlobalRef<jobject>& j_tab_ref, |
| 147 DeletePageResult result) { |
| 148 SavePageIfNavigatedToURL(query_url, j_tab_ref); |
| 149 } |
| 150 |
| 151 void DeletePagesForOverwrite(const GURL& query_url, |
| 152 const ScopedJavaGlobalRef<jobject>& j_tab_ref, |
| 153 const MultipleOfflinePageItemResult& pages) { |
| 154 OfflinePageModel* model = GetOfflinePageModelFromJavaTab(j_tab_ref); |
| 155 if (!model) |
| 156 return; |
| 157 |
| 158 std::vector<int64_t> offline_ids; |
| 159 for (auto& page : pages) { |
| 160 if (page.client_id.name_space == kDownloadNamespace || |
| 161 page.client_id.name_space == kAsyncNamespace) { |
| 162 offline_ids.emplace_back(page.offline_id); |
| 163 } |
| 164 } |
| 165 |
| 166 model->DeletePagesByOfflineId( |
| 167 offline_ids, base::Bind(&OnDeletePagesForInfoBar, query_url, j_tab_ref)); |
| 168 } |
| 169 |
| 170 void OnInfoBarAction(const GURL& query_url, |
| 171 const ScopedJavaGlobalRef<jobject>& j_tab_ref, |
| 172 OfflinePageInfoBarDelegate::Action action) { |
| 173 switch (action) { |
| 174 case OfflinePageInfoBarDelegate::Action::CREATE_NEW: |
| 175 SavePageIfNavigatedToURL(query_url, j_tab_ref); |
| 176 break; |
| 177 case OfflinePageInfoBarDelegate::Action::OVERWRITE: |
| 178 OfflinePageModel* offline_page_model = |
| 179 GetOfflinePageModelFromJavaTab(j_tab_ref); |
| 180 if (!offline_page_model) |
| 181 return; |
| 182 |
| 183 offline_page_model->GetPagesByOnlineURL( |
| 184 query_url, |
| 185 base::Bind(&DeletePagesForOverwrite, query_url, j_tab_ref)); |
| 186 break; |
| 187 } |
| 188 } |
| 189 |
| 131 void RequestQueueDuplicateCheckDone( | 190 void RequestQueueDuplicateCheckDone( |
| 132 const GURL& query_url, | 191 const GURL& query_url, |
| 133 const ScopedJavaGlobalRef<jobject>& j_tab_ref, | 192 const ScopedJavaGlobalRef<jobject>& j_tab_ref, |
| 134 bool has_duplicates) { | 193 bool has_duplicates) { |
| 135 if (has_duplicates) { | 194 if (has_duplicates) { |
| 136 // TODO(fgorski): Additionally we could update existing request's expiration | 195 // TODO(fgorski): Additionally we could update existing request's expiration |
| 137 // period, as it is still important. Alternative would be to actually take a | 196 // period, as it is still important. Alternative would be to actually take a |
| 138 // snapshot on the spot, but that would only work if the page is loaded | 197 // snapshot on the spot, but that would only work if the page is loaded |
| 139 // enough. | 198 // enough. |
| 140 // This simply toasts that the item is downloading. | 199 // This simply toasts that the item is downloading. |
| 141 OfflinePageNotificationBridge notification_bridge; | 200 OfflinePageNotificationBridge notification_bridge; |
| 142 notification_bridge.ShowDownloadingToast(); | 201 notification_bridge.ShowDownloadingToast(); |
| 143 return; | 202 return; |
| 144 } | 203 } |
| 145 | 204 |
| 146 SavePageIfNavigatedToURL(query_url, j_tab_ref); | 205 SavePageIfNavigatedToURL(query_url, j_tab_ref); |
| 147 } | 206 } |
| 148 | 207 |
| 149 void ModelDuplicateCheckDone(const GURL& query_url, | 208 void ModelDuplicateCheckDone(const GURL& query_url, |
| 150 const ScopedJavaGlobalRef<jobject>& j_tab_ref, | 209 const ScopedJavaGlobalRef<jobject>& j_tab_ref, |
| 210 const std::string& downloads_label, |
| 151 bool has_duplicates) { | 211 bool has_duplicates) { |
| 152 content::WebContents* web_contents = GetWebContentsFromJavaTab(j_tab_ref); | 212 content::WebContents* web_contents = GetWebContentsFromJavaTab(j_tab_ref); |
| 153 if (!web_contents) | 213 if (!web_contents) |
| 154 return; | 214 return; |
| 155 | 215 |
| 156 if (has_duplicates) { | 216 if (has_duplicates) { |
| 157 OfflinePageInfoBarDelegate::Create( | 217 OfflinePageInfoBarDelegate::Create( |
| 158 base::Bind(&SavePageIfNavigatedToURL, query_url, j_tab_ref), | 218 base::Bind(&OnInfoBarAction, query_url, j_tab_ref), downloads_label, |
| 159 query_url.spec(), web_contents); | 219 query_url.spec(), web_contents); |
| 160 return; | 220 return; |
| 161 } | 221 } |
| 162 | 222 |
| 163 OfflinePageUtils::CheckExistenceOfRequestsWithURL( | 223 OfflinePageUtils::CheckExistenceOfRequestsWithURL( |
| 164 Profile::FromBrowserContext(web_contents->GetBrowserContext()) | 224 Profile::FromBrowserContext(web_contents->GetBrowserContext()) |
| 165 ->GetOriginalProfile(), | 225 ->GetOriginalProfile(), |
| 166 kDownloadNamespace, query_url, | 226 kDownloadNamespace, query_url, |
| 167 base::Bind(&RequestQueueDuplicateCheckDone, query_url, j_tab_ref)); | 227 base::Bind(&RequestQueueDuplicateCheckDone, query_url, j_tab_ref)); |
| 168 } | 228 } |
| (...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 314 JNIEnv* env, | 374 JNIEnv* env, |
| 315 const JavaParamRef<jobject>& obj, | 375 const JavaParamRef<jobject>& obj, |
| 316 const JavaParamRef<jstring>& j_guid) { | 376 const JavaParamRef<jstring>& j_guid) { |
| 317 std::string guid = ConvertJavaStringToUTF8(env, j_guid); | 377 std::string guid = ConvertJavaStringToUTF8(env, j_guid); |
| 318 return download_ui_adapter_->GetOfflineIdByGuid(guid); | 378 return download_ui_adapter_->GetOfflineIdByGuid(guid); |
| 319 } | 379 } |
| 320 | 380 |
| 321 void OfflinePageDownloadBridge::StartDownload( | 381 void OfflinePageDownloadBridge::StartDownload( |
| 322 JNIEnv* env, | 382 JNIEnv* env, |
| 323 const JavaParamRef<jobject>& obj, | 383 const JavaParamRef<jobject>& obj, |
| 324 const JavaParamRef<jobject>& j_tab) { | 384 const JavaParamRef<jobject>& j_tab, |
| 385 const JavaParamRef<jstring>& j_downloads_label) { |
| 386 std::string downloads_label = ConvertJavaStringToUTF8(env, j_downloads_label); |
| 325 TabAndroid* tab = TabAndroid::GetNativeTab(env, j_tab); | 387 TabAndroid* tab = TabAndroid::GetNativeTab(env, j_tab); |
| 326 if (!tab) | 388 if (!tab) |
| 327 return; | 389 return; |
| 328 | 390 |
| 329 content::WebContents* web_contents = tab->web_contents(); | 391 content::WebContents* web_contents = tab->web_contents(); |
| 330 if (!web_contents) | 392 if (!web_contents) |
| 331 return; | 393 return; |
| 332 | 394 |
| 333 GURL url = web_contents->GetLastCommittedURL(); | 395 GURL url = web_contents->GetLastCommittedURL(); |
| 334 | 396 |
| 335 ScopedJavaGlobalRef<jobject> j_tab_ref(env, j_tab); | 397 ScopedJavaGlobalRef<jobject> j_tab_ref(env, j_tab); |
| 336 | 398 |
| 337 OfflinePageUtils::CheckExistenceOfPagesWithURL( | 399 OfflinePageUtils::CheckExistenceOfPagesWithURL( |
| 338 tab->GetProfile()->GetOriginalProfile(), kDownloadNamespace, url, | 400 tab->GetProfile()->GetOriginalProfile(), kDownloadNamespace, url, |
| 339 base::Bind(&ModelDuplicateCheckDone, url, j_tab_ref)); | 401 base::Bind(&ModelDuplicateCheckDone, url, j_tab_ref, downloads_label)); |
| 340 } | 402 } |
| 341 | 403 |
| 342 void OfflinePageDownloadBridge::CancelDownload( | 404 void OfflinePageDownloadBridge::CancelDownload( |
| 343 JNIEnv* env, | 405 JNIEnv* env, |
| 344 const JavaParamRef<jobject>& obj, | 406 const JavaParamRef<jobject>& obj, |
| 345 const JavaParamRef<jstring>& j_guid) { | 407 const JavaParamRef<jstring>& j_guid) { |
| 346 std::string guid = ConvertJavaStringToUTF8(env, j_guid); | 408 std::string guid = ConvertJavaStringToUTF8(env, j_guid); |
| 347 RequestCoordinator* coordinator = | 409 RequestCoordinator* coordinator = |
| 348 RequestCoordinatorFactory::GetForBrowserContext(browser_context_); | 410 RequestCoordinatorFactory::GetForBrowserContext(browser_context_); |
| 349 | 411 |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 433 | 495 |
| 434 DownloadUIAdapter* adapter = | 496 DownloadUIAdapter* adapter = |
| 435 DownloadUIAdapter::FromOfflinePageModel(offline_page_model); | 497 DownloadUIAdapter::FromOfflinePageModel(offline_page_model); |
| 436 | 498 |
| 437 return reinterpret_cast<jlong>( | 499 return reinterpret_cast<jlong>( |
| 438 new OfflinePageDownloadBridge(env, obj, adapter, browser_context)); | 500 new OfflinePageDownloadBridge(env, obj, adapter, browser_context)); |
| 439 } | 501 } |
| 440 | 502 |
| 441 } // namespace android | 503 } // namespace android |
| 442 } // namespace offline_pages | 504 } // namespace offline_pages |
| OLD | NEW |