Chromium Code Reviews| 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 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 59 content::WebContents* GetWebContentsFromJavaTab( | 59 content::WebContents* GetWebContentsFromJavaTab( |
| 60 const ScopedJavaGlobalRef<jobject>& j_tab_ref) { | 60 const ScopedJavaGlobalRef<jobject>& j_tab_ref) { |
| 61 JNIEnv* env = AttachCurrentThread(); | 61 JNIEnv* env = AttachCurrentThread(); |
| 62 TabAndroid* tab = TabAndroid::GetNativeTab(env, j_tab_ref); | 62 TabAndroid* tab = TabAndroid::GetNativeTab(env, j_tab_ref); |
| 63 if (!tab) | 63 if (!tab) |
| 64 return nullptr; | 64 return nullptr; |
| 65 | 65 |
| 66 return tab->web_contents(); | 66 return tab->web_contents(); |
| 67 } | 67 } |
| 68 | 68 |
| 69 void SavePageIfNavigatedToURL(const GURL& query_url, | 69 void SavePageIfStillOnSamePage(const GURL& original_url, |
|
Pete Williamson
2016/10/12 17:33:36
I thought our bridges were supposed to be thin pas
Dmitry Titov
2016/10/12 23:11:14
Indeed. Bridges ideally should be dumb translators
| |
| 70 const ScopedJavaGlobalRef<jobject>& j_tab_ref) { | 70 const ScopedJavaGlobalRef<jobject>& j_tab_ref) { |
| 71 content::WebContents* web_contents = GetWebContentsFromJavaTab(j_tab_ref); | 71 content::WebContents* web_contents = GetWebContentsFromJavaTab(j_tab_ref); |
| 72 if (!web_contents) | 72 if (!web_contents) |
| 73 return; | 73 return; |
| 74 | 74 |
| 75 // This doesn't detect navigations to the same URL, only that we are looking | 75 // This ignores fragment differences in URLs, bails out only if tab has |
| 76 // at a completely different page. | 76 // navigated to a completely different page. |
| 77 GURL url = web_contents->GetLastCommittedURL(); | 77 GURL url = web_contents->GetLastCommittedURL(); |
| 78 if (!OfflinePageUtils::EqualsIgnoringFragment(url, query_url)) | 78 if (!OfflinePageUtils::EqualsIgnoringFragment(url, original_url)) |
| 79 return; | 79 return; |
| 80 | 80 |
| 81 offline_pages::ClientId client_id; | 81 offline_pages::ClientId client_id; |
| 82 client_id.name_space = offline_pages::kDownloadNamespace; | 82 client_id.name_space = offline_pages::kDownloadNamespace; |
| 83 client_id.id = base::GenerateGUID(); | 83 client_id.id = base::GenerateGUID(); |
| 84 int64_t request_id = 0; | |
| 84 | 85 |
| 85 Profile* profile = | 86 if (offline_pages::IsBackgroundLoaderForDownloadsEnabled()) { |
| 86 Profile::FromBrowserContext(web_contents->GetBrowserContext()) | 87 // Post disabled request before passing the download task to the tab helper. |
| 87 ->GetOriginalProfile(); | 88 // This will keep the request persisted in case Chrome is evicted from RAM |
| 89 // or closed by the user. | |
| 90 offline_pages::RequestCoordinator* request_coordinator = | |
| 91 offline_pages::RequestCoordinatorFactory::GetForBrowserContext( | |
| 92 web_contents->GetBrowserContext()); | |
| 93 request_id = request_coordinator->SavePageLater( | |
| 94 url, client_id, true, | |
| 95 RequestCoordinator::RequestAvailability::DISABLED_FOR_OFFLINER); | |
| 96 } | |
| 97 | |
| 98 // Pass request_id to the current tab's helper to attempt download right from | |
| 99 // the tab. If unsuccessful, it'll enable the already-queued request for | |
| 100 // background offliner. Same will happen if Chrome is terminated since | |
| 101 // 'disabled' status of the request is RAM-stored info. | |
| 102 offline_pages::RecentTabHelper* tab_helper = | |
| 103 RecentTabHelper::FromWebContents(web_contents); | |
| 104 if (!tab_helper) | |
| 105 return; | |
| 106 tab_helper->ObserveAndDownloadCurrentPage(client_id, request_id); | |
| 88 | 107 |
| 89 OfflinePageNotificationBridge notification_bridge; | 108 OfflinePageNotificationBridge notification_bridge; |
| 90 | |
| 91 // If the page is not loaded enough to be captured, submit a background loader | |
| 92 // request instead. | |
| 93 offline_pages::RecentTabHelper* tab_helper = | |
| 94 RecentTabHelper::FromWebContents(web_contents); | |
| 95 if (tab_helper && !tab_helper->is_page_ready_for_snapshot() && | |
| 96 offline_pages::IsBackgroundLoaderForDownloadsEnabled()) { | |
| 97 // TODO(dimich): Improve this to wait for the page load if it is still going | |
| 98 // on. Pre-submit the request and if the load finishes and capture happens, | |
| 99 // remove request. | |
| 100 offline_pages::RequestCoordinator* request_coordinator = | |
| 101 offline_pages::RequestCoordinatorFactory::GetForBrowserContext(profile); | |
| 102 request_coordinator->SavePageLater( | |
| 103 url, client_id, true, | |
| 104 RequestCoordinator::RequestAvailability::ENABLED_FOR_OFFLINER); | |
| 105 | |
| 106 notification_bridge.ShowDownloadingToast(); | |
| 107 return; | |
| 108 } | |
| 109 | |
| 110 // Page is ready, capture it right from the tab. | |
| 111 offline_pages::OfflinePageModel* offline_page_model = | |
| 112 OfflinePageModelFactory::GetForBrowserContext(profile); | |
| 113 if (!offline_page_model) | |
| 114 return; | |
| 115 | |
| 116 auto archiver = | |
| 117 base::MakeUnique<offline_pages::OfflinePageMHTMLArchiver>(web_contents); | |
| 118 | |
| 119 DownloadUIItem item; | |
| 120 item.guid = client_id.id; | |
| 121 item.url = url; | |
| 122 | |
| 123 OfflinePageNotificationBridge bridge; | |
| 124 bridge.NotifyDownloadProgress(item); | |
| 125 | |
| 126 notification_bridge.ShowDownloadingToast(); | 109 notification_bridge.ShowDownloadingToast(); |
| 127 offline_page_model->SavePage(url, client_id, 0l, std::move(archiver), | |
| 128 base::Bind(&SavePageCallback, item)); | |
| 129 } | 110 } |
| 130 | 111 |
| 131 void RequestQueueDuplicateCheckDone( | 112 void RequestQueueDuplicateCheckDone( |
| 132 const GURL& query_url, | 113 const GURL& original_url, |
| 133 const ScopedJavaGlobalRef<jobject>& j_tab_ref, | 114 const ScopedJavaGlobalRef<jobject>& j_tab_ref, |
| 134 bool has_duplicates) { | 115 bool has_duplicates) { |
| 135 if (has_duplicates) { | 116 if (has_duplicates) { |
| 136 // TODO(fgorski): Additionally we could update existing request's expiration | 117 // TODO(fgorski): Additionally we could update existing request's expiration |
| 137 // 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 |
| 138 // 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 |
| 139 // enough. | 120 // enough. |
| 140 // This simply toasts that the item is downloading. | 121 // This simply toasts that the item is downloading. |
| 141 OfflinePageNotificationBridge notification_bridge; | 122 OfflinePageNotificationBridge notification_bridge; |
| 142 notification_bridge.ShowDownloadingToast(); | 123 notification_bridge.ShowDownloadingToast(); |
| 143 return; | 124 return; |
| 144 } | 125 } |
| 145 | 126 |
| 146 SavePageIfNavigatedToURL(query_url, j_tab_ref); | 127 SavePageIfStillOnSamePage(original_url, j_tab_ref); |
| 147 } | 128 } |
| 148 | 129 |
| 149 void ModelDuplicateCheckDone(const GURL& query_url, | 130 void ModelDuplicateCheckDone(const GURL& original_url, |
| 150 const ScopedJavaGlobalRef<jobject>& j_tab_ref, | 131 const ScopedJavaGlobalRef<jobject>& j_tab_ref, |
| 151 bool has_duplicates) { | 132 bool has_duplicates) { |
| 152 content::WebContents* web_contents = GetWebContentsFromJavaTab(j_tab_ref); | 133 content::WebContents* web_contents = GetWebContentsFromJavaTab(j_tab_ref); |
| 153 if (!web_contents) | 134 if (!web_contents) |
| 154 return; | 135 return; |
| 155 | 136 |
| 156 if (has_duplicates) { | 137 if (has_duplicates) { |
| 157 OfflinePageInfoBarDelegate::Create( | 138 OfflinePageInfoBarDelegate::Create( |
| 158 base::Bind(&SavePageIfNavigatedToURL, query_url, j_tab_ref), | 139 base::Bind(&SavePageIfStillOnSamePage, original_url, j_tab_ref), |
| 159 query_url.spec(), web_contents); | 140 original_url.spec(), web_contents); |
| 160 return; | 141 return; |
| 161 } | 142 } |
| 162 | 143 |
| 163 OfflinePageUtils::CheckExistenceOfRequestsWithURL( | 144 OfflinePageUtils::CheckExistenceOfRequestsWithURL( |
| 164 Profile::FromBrowserContext(web_contents->GetBrowserContext()) | 145 Profile::FromBrowserContext(web_contents->GetBrowserContext()) |
| 165 ->GetOriginalProfile(), | 146 ->GetOriginalProfile(), |
| 166 kDownloadNamespace, query_url, | 147 kDownloadNamespace, original_url, |
| 167 base::Bind(&RequestQueueDuplicateCheckDone, query_url, j_tab_ref)); | 148 base::Bind(&RequestQueueDuplicateCheckDone, original_url, j_tab_ref)); |
| 168 } | 149 } |
| 169 | 150 |
| 170 void ToJavaOfflinePageDownloadItemList( | 151 void ToJavaOfflinePageDownloadItemList( |
| 171 JNIEnv* env, | 152 JNIEnv* env, |
| 172 jobject j_result_obj, | 153 jobject j_result_obj, |
| 173 const std::vector<const DownloadUIItem*>& items) { | 154 const std::vector<const DownloadUIItem*>& items) { |
| 174 for (const auto item : items) { | 155 for (const auto item : items) { |
| 175 Java_OfflinePageDownloadBridge_createDownloadItemAndAddToList( | 156 Java_OfflinePageDownloadBridge_createDownloadItemAndAddToList( |
| 176 env, j_result_obj, ConvertUTF8ToJavaString(env, item->guid), | 157 env, j_result_obj, ConvertUTF8ToJavaString(env, item->guid), |
| 177 ConvertUTF8ToJavaString(env, item->url.spec()), | 158 ConvertUTF8ToJavaString(env, item->url.spec()), |
| (...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 324 const JavaParamRef<jobject>& j_tab) { | 305 const JavaParamRef<jobject>& j_tab) { |
| 325 TabAndroid* tab = TabAndroid::GetNativeTab(env, j_tab); | 306 TabAndroid* tab = TabAndroid::GetNativeTab(env, j_tab); |
| 326 if (!tab) | 307 if (!tab) |
| 327 return; | 308 return; |
| 328 | 309 |
| 329 content::WebContents* web_contents = tab->web_contents(); | 310 content::WebContents* web_contents = tab->web_contents(); |
| 330 if (!web_contents) | 311 if (!web_contents) |
| 331 return; | 312 return; |
| 332 | 313 |
| 333 GURL url = web_contents->GetLastCommittedURL(); | 314 GURL url = web_contents->GetLastCommittedURL(); |
| 334 | |
| 335 ScopedJavaGlobalRef<jobject> j_tab_ref(env, j_tab); | 315 ScopedJavaGlobalRef<jobject> j_tab_ref(env, j_tab); |
| 336 | 316 |
| 337 OfflinePageUtils::CheckExistenceOfPagesWithURL( | 317 OfflinePageUtils::CheckExistenceOfPagesWithURL( |
| 338 tab->GetProfile()->GetOriginalProfile(), kDownloadNamespace, url, | 318 tab->GetProfile()->GetOriginalProfile(), kDownloadNamespace, url, |
| 339 base::Bind(&ModelDuplicateCheckDone, url, j_tab_ref)); | 319 base::Bind(&ModelDuplicateCheckDone, url, j_tab_ref)); |
| 340 } | 320 } |
| 341 | 321 |
| 342 void OfflinePageDownloadBridge::CancelDownload( | 322 void OfflinePageDownloadBridge::CancelDownload( |
| 343 JNIEnv* env, | 323 JNIEnv* env, |
| 344 const JavaParamRef<jobject>& obj, | 324 const JavaParamRef<jobject>& obj, |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 433 | 413 |
| 434 DownloadUIAdapter* adapter = | 414 DownloadUIAdapter* adapter = |
| 435 DownloadUIAdapter::FromOfflinePageModel(offline_page_model); | 415 DownloadUIAdapter::FromOfflinePageModel(offline_page_model); |
| 436 | 416 |
| 437 return reinterpret_cast<jlong>( | 417 return reinterpret_cast<jlong>( |
| 438 new OfflinePageDownloadBridge(env, obj, adapter, browser_context)); | 418 new OfflinePageDownloadBridge(env, obj, adapter, browser_context)); |
| 439 } | 419 } |
| 440 | 420 |
| 441 } // namespace android | 421 } // namespace android |
| 442 } // namespace offline_pages | 422 } // namespace offline_pages |
| OLD | NEW |