| 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 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 82 content::WebContents* GetWebContentsFromJavaTab( | 82 content::WebContents* GetWebContentsFromJavaTab( |
| 83 const ScopedJavaGlobalRef<jobject>& j_tab_ref) { | 83 const ScopedJavaGlobalRef<jobject>& j_tab_ref) { |
| 84 JNIEnv* env = AttachCurrentThread(); | 84 JNIEnv* env = AttachCurrentThread(); |
| 85 TabAndroid* tab = TabAndroid::GetNativeTab(env, j_tab_ref); | 85 TabAndroid* tab = TabAndroid::GetNativeTab(env, j_tab_ref); |
| 86 if (!tab) | 86 if (!tab) |
| 87 return nullptr; | 87 return nullptr; |
| 88 | 88 |
| 89 return tab->web_contents(); | 89 return tab->web_contents(); |
| 90 } | 90 } |
| 91 | 91 |
| 92 void SavePageIfNotNavigatedAway(const GURL& original_url, | 92 void SavePageIfNotNavigatedAway(const GURL& url, |
| 93 const GURL& original_url, |
| 93 const ScopedJavaGlobalRef<jobject>& j_tab_ref) { | 94 const ScopedJavaGlobalRef<jobject>& j_tab_ref) { |
| 94 content::WebContents* web_contents = GetWebContentsFromJavaTab(j_tab_ref); | 95 content::WebContents* web_contents = GetWebContentsFromJavaTab(j_tab_ref); |
| 95 if (!web_contents) | 96 if (!web_contents) |
| 96 return; | 97 return; |
| 97 | 98 |
| 98 // This ignores fragment differences in URLs, bails out only if tab has | 99 // This ignores fragment differences in URLs, bails out only if tab has |
| 99 // navigated away and not just scrolled to a fragment. | 100 // navigated away and not just scrolled to a fragment. |
| 100 GURL url = web_contents->GetLastCommittedURL(); | 101 GURL current_url = web_contents->GetLastCommittedURL(); |
| 101 if (!OfflinePageUtils::EqualsIgnoringFragment(url, original_url)) | 102 if (!OfflinePageUtils::EqualsIgnoringFragment(current_url, url)) |
| 102 return; | 103 return; |
| 103 | 104 |
| 104 offline_pages::ClientId client_id; | 105 offline_pages::ClientId client_id; |
| 105 client_id.name_space = offline_pages::kDownloadNamespace; | 106 client_id.name_space = offline_pages::kDownloadNamespace; |
| 106 client_id.id = base::GenerateGUID(); | 107 client_id.id = base::GenerateGUID(); |
| 107 int64_t request_id = OfflinePageModel::kInvalidOfflineId; | 108 int64_t request_id = OfflinePageModel::kInvalidOfflineId; |
| 108 | 109 |
| 109 if (offline_pages::IsBackgroundLoaderForDownloadsEnabled()) { | 110 if (offline_pages::IsBackgroundLoaderForDownloadsEnabled()) { |
| 110 // Post disabled request before passing the download task to the tab helper. | 111 // Post disabled request before passing the download task to the tab helper. |
| 111 // This will keep the request persisted in case Chrome is evicted from RAM | 112 // This will keep the request persisted in case Chrome is evicted from RAM |
| 112 // or closed by the user. | 113 // or closed by the user. |
| 113 // Note: the 'disabled' status is not persisted (stored in memory) so it | 114 // Note: the 'disabled' status is not persisted (stored in memory) so it |
| 114 // automatically resets if Chrome is re-started. | 115 // automatically resets if Chrome is re-started. |
| 115 offline_pages::RequestCoordinator* request_coordinator = | 116 offline_pages::RequestCoordinator* request_coordinator = |
| 116 offline_pages::RequestCoordinatorFactory::GetForBrowserContext( | 117 offline_pages::RequestCoordinatorFactory::GetForBrowserContext( |
| 117 web_contents->GetBrowserContext()); | 118 web_contents->GetBrowserContext()); |
| 118 if (request_coordinator) { | 119 if (request_coordinator) { |
| 119 request_id = request_coordinator->SavePageLater( | 120 offline_pages::RequestCoordinator::SavePageLaterParams params; |
| 120 url, client_id, true, | 121 params.url = current_url; |
| 121 RequestCoordinator::RequestAvailability::DISABLED_FOR_OFFLINER); | 122 params.client_id = client_id; |
| 123 params.availability = |
| 124 RequestCoordinator::RequestAvailability::DISABLED_FOR_OFFLINER; |
| 125 params.original_url = original_url; |
| 126 request_id = request_coordinator->SavePageLater(params); |
| 122 } else { | 127 } else { |
| 123 DVLOG(1) << "SavePageIfNotNavigatedAway has no valid coordinator."; | 128 DVLOG(1) << "SavePageIfNotNavigatedAway has no valid coordinator."; |
| 124 } | 129 } |
| 125 } | 130 } |
| 126 | 131 |
| 127 // Pass request_id to the current tab's helper to attempt download right from | 132 // Pass request_id to the current tab's helper to attempt download right from |
| 128 // the tab. If unsuccessful, it'll enable the already-queued request for | 133 // the tab. If unsuccessful, it'll enable the already-queued request for |
| 129 // background offliner. Same will happen if Chrome is terminated since | 134 // background offliner. Same will happen if Chrome is terminated since |
| 130 // 'disabled' status of the request is RAM-stored info. | 135 // 'disabled' status of the request is RAM-stored info. |
| 131 offline_pages::RecentTabHelper* tab_helper = | 136 offline_pages::RecentTabHelper* tab_helper = |
| (...skipping 10 matching lines...) Expand all Loading... |
| 142 } | 147 } |
| 143 return; | 148 return; |
| 144 } | 149 } |
| 145 tab_helper->ObserveAndDownloadCurrentPage(client_id, request_id); | 150 tab_helper->ObserveAndDownloadCurrentPage(client_id, request_id); |
| 146 | 151 |
| 147 OfflinePageNotificationBridge notification_bridge; | 152 OfflinePageNotificationBridge notification_bridge; |
| 148 notification_bridge.ShowDownloadingToast(); | 153 notification_bridge.ShowDownloadingToast(); |
| 149 } | 154 } |
| 150 | 155 |
| 151 void RequestQueueDuplicateCheckDone( | 156 void RequestQueueDuplicateCheckDone( |
| 157 const GURL& url, |
| 152 const GURL& original_url, | 158 const GURL& original_url, |
| 153 const ScopedJavaGlobalRef<jobject>& j_tab_ref, | 159 const ScopedJavaGlobalRef<jobject>& j_tab_ref, |
| 154 bool has_duplicates, | 160 bool has_duplicates, |
| 155 const base::Time& latest_request_time) { | 161 const base::Time& latest_request_time) { |
| 156 if (has_duplicates) { | 162 if (has_duplicates) { |
| 157 base::TimeDelta time_since_most_recent_duplicate = | 163 base::TimeDelta time_since_most_recent_duplicate = |
| 158 base::Time::Now() - latest_request_time; | 164 base::Time::Now() - latest_request_time; |
| 159 // Using CUSTOM_COUNTS instead of time-oriented histogram to record | 165 // Using CUSTOM_COUNTS instead of time-oriented histogram to record |
| 160 // samples in seconds rather than milliseconds. | 166 // samples in seconds rather than milliseconds. |
| 161 UMA_HISTOGRAM_CUSTOM_COUNTS( | 167 UMA_HISTOGRAM_CUSTOM_COUNTS( |
| 162 "OfflinePages.DownloadRequestTimeSinceDuplicateRequested", | 168 "OfflinePages.DownloadRequestTimeSinceDuplicateRequested", |
| 163 time_since_most_recent_duplicate.InSeconds(), | 169 time_since_most_recent_duplicate.InSeconds(), |
| 164 base::TimeDelta::FromSeconds(1).InSeconds(), | 170 base::TimeDelta::FromSeconds(1).InSeconds(), |
| 165 base::TimeDelta::FromDays(7).InSeconds(), 50); | 171 base::TimeDelta::FromDays(7).InSeconds(), 50); |
| 166 | 172 |
| 167 // TODO(fgorski): Additionally we could update existing request's expiration | 173 // TODO(fgorski): Additionally we could update existing request's expiration |
| 168 // period, as it is still important. Alternative would be to actually take a | 174 // period, as it is still important. Alternative would be to actually take a |
| 169 // snapshot on the spot, but that would only work if the page is loaded | 175 // snapshot on the spot, but that would only work if the page is loaded |
| 170 // enough. | 176 // enough. |
| 171 // This simply toasts that the item is downloading. | 177 // This simply toasts that the item is downloading. |
| 172 OfflinePageNotificationBridge notification_bridge; | 178 OfflinePageNotificationBridge notification_bridge; |
| 173 notification_bridge.ShowDownloadingToast(); | 179 notification_bridge.ShowDownloadingToast(); |
| 174 return; | 180 return; |
| 175 } | 181 } |
| 176 | 182 |
| 177 SavePageIfNotNavigatedAway(original_url, j_tab_ref); | 183 SavePageIfNotNavigatedAway(url, original_url, j_tab_ref); |
| 178 } | 184 } |
| 179 | 185 |
| 180 void ModelDuplicateCheckDone(const GURL& original_url, | 186 void ModelDuplicateCheckDone(const GURL& url, |
| 187 const GURL& original_url, |
| 181 const ScopedJavaGlobalRef<jobject>& j_tab_ref, | 188 const ScopedJavaGlobalRef<jobject>& j_tab_ref, |
| 182 bool has_duplicates, | 189 bool has_duplicates, |
| 183 const base::Time& latest_saved_time) { | 190 const base::Time& latest_saved_time) { |
| 184 content::WebContents* web_contents = GetWebContentsFromJavaTab(j_tab_ref); | 191 content::WebContents* web_contents = GetWebContentsFromJavaTab(j_tab_ref); |
| 185 if (!web_contents) | 192 if (!web_contents) |
| 186 return; | 193 return; |
| 187 | 194 |
| 188 if (has_duplicates) { | 195 if (has_duplicates) { |
| 189 base::TimeDelta time_since_most_recent_duplicate = | 196 base::TimeDelta time_since_most_recent_duplicate = |
| 190 base::Time::Now() - latest_saved_time; | 197 base::Time::Now() - latest_saved_time; |
| 191 // Using CUSTOM_COUNTS instead of time-oriented histogram to record | 198 // Using CUSTOM_COUNTS instead of time-oriented histogram to record |
| 192 // samples in seconds rather than milliseconds. | 199 // samples in seconds rather than milliseconds. |
| 193 UMA_HISTOGRAM_CUSTOM_COUNTS( | 200 UMA_HISTOGRAM_CUSTOM_COUNTS( |
| 194 "OfflinePages.DownloadRequestTimeSinceDuplicateSaved", | 201 "OfflinePages.DownloadRequestTimeSinceDuplicateSaved", |
| 195 time_since_most_recent_duplicate.InSeconds(), | 202 time_since_most_recent_duplicate.InSeconds(), |
| 196 base::TimeDelta::FromSeconds(1).InSeconds(), | 203 base::TimeDelta::FromSeconds(1).InSeconds(), |
| 197 base::TimeDelta::FromDays(7).InSeconds(), 50); | 204 base::TimeDelta::FromDays(7).InSeconds(), 50); |
| 198 | 205 |
| 199 OfflinePageInfoBarDelegate::Create( | 206 OfflinePageInfoBarDelegate::Create( |
| 200 base::Bind(&SavePageIfNotNavigatedAway, original_url, j_tab_ref), | 207 base::Bind(&SavePageIfNotNavigatedAway, url, original_url, j_tab_ref), |
| 201 original_url, web_contents); | 208 url, web_contents); |
| 202 return; | 209 return; |
| 203 } | 210 } |
| 204 | 211 |
| 205 OfflinePageUtils::CheckExistenceOfRequestsWithURL( | 212 OfflinePageUtils::CheckExistenceOfRequestsWithURL( |
| 206 Profile::FromBrowserContext(web_contents->GetBrowserContext()) | 213 Profile::FromBrowserContext(web_contents->GetBrowserContext()) |
| 207 ->GetOriginalProfile(), | 214 ->GetOriginalProfile(), |
| 208 kDownloadNamespace, original_url, | 215 kDownloadNamespace, url, base::Bind(&RequestQueueDuplicateCheckDone, url, |
| 209 base::Bind(&RequestQueueDuplicateCheckDone, original_url, j_tab_ref)); | 216 original_url, j_tab_ref)); |
| 210 } | 217 } |
| 211 | 218 |
| 212 void ToJavaOfflinePageDownloadItemList( | 219 void ToJavaOfflinePageDownloadItemList( |
| 213 JNIEnv* env, | 220 JNIEnv* env, |
| 214 jobject j_result_obj, | 221 jobject j_result_obj, |
| 215 const std::vector<const DownloadUIItem*>& items) { | 222 const std::vector<const DownloadUIItem*>& items) { |
| 216 for (const auto item : items) { | 223 for (const auto item : items) { |
| 217 Java_OfflinePageDownloadBridge_createDownloadItemAndAddToList( | 224 Java_OfflinePageDownloadBridge_createDownloadItemAndAddToList( |
| 218 env, j_result_obj, ConvertUTF8ToJavaString(env, item->guid), | 225 env, j_result_obj, ConvertUTF8ToJavaString(env, item->guid), |
| 219 ConvertUTF8ToJavaString(env, item->url.spec()), item->download_state, | 226 ConvertUTF8ToJavaString(env, item->url.spec()), item->download_state, |
| (...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 368 const JavaParamRef<jobject>& j_tab) { | 375 const JavaParamRef<jobject>& j_tab) { |
| 369 TabAndroid* tab = TabAndroid::GetNativeTab(env, j_tab); | 376 TabAndroid* tab = TabAndroid::GetNativeTab(env, j_tab); |
| 370 if (!tab) | 377 if (!tab) |
| 371 return; | 378 return; |
| 372 | 379 |
| 373 content::WebContents* web_contents = tab->web_contents(); | 380 content::WebContents* web_contents = tab->web_contents(); |
| 374 if (!web_contents) | 381 if (!web_contents) |
| 375 return; | 382 return; |
| 376 | 383 |
| 377 GURL url = web_contents->GetLastCommittedURL(); | 384 GURL url = web_contents->GetLastCommittedURL(); |
| 385 GURL original_url = |
| 386 offline_pages::OfflinePageUtils::GetOriginalURLFromWebContents( |
| 387 web_contents); |
| 378 | 388 |
| 379 ScopedJavaGlobalRef<jobject> j_tab_ref(env, j_tab); | 389 ScopedJavaGlobalRef<jobject> j_tab_ref(env, j_tab); |
| 380 | 390 |
| 381 OfflinePageUtils::CheckExistenceOfPagesWithURL( | 391 OfflinePageUtils::CheckExistenceOfPagesWithURL( |
| 382 tab->GetProfile()->GetOriginalProfile(), kDownloadNamespace, url, | 392 tab->GetProfile()->GetOriginalProfile(), kDownloadNamespace, url, |
| 383 base::Bind(&ModelDuplicateCheckDone, url, j_tab_ref)); | 393 base::Bind(&ModelDuplicateCheckDone, url, original_url, j_tab_ref)); |
| 384 } | 394 } |
| 385 | 395 |
| 386 void OfflinePageDownloadBridge::CancelDownload( | 396 void OfflinePageDownloadBridge::CancelDownload( |
| 387 JNIEnv* env, | 397 JNIEnv* env, |
| 388 const JavaParamRef<jobject>& obj, | 398 const JavaParamRef<jobject>& obj, |
| 389 const JavaParamRef<jstring>& j_guid) { | 399 const JavaParamRef<jstring>& j_guid) { |
| 390 std::string guid = ConvertJavaStringToUTF8(env, j_guid); | 400 std::string guid = ConvertJavaStringToUTF8(env, j_guid); |
| 391 RequestCoordinator* coordinator = | 401 RequestCoordinator* coordinator = |
| 392 RequestCoordinatorFactory::GetForBrowserContext(browser_context_); | 402 RequestCoordinatorFactory::GetForBrowserContext(browser_context_); |
| 393 | 403 |
| (...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 488 base::MakeUnique<DownloadUIAdapterDelegate>(offline_page_model)); | 498 base::MakeUnique<DownloadUIAdapterDelegate>(offline_page_model)); |
| 489 offline_page_model->SetUserData(kDownloadUIAdapterKey, adapter); | 499 offline_page_model->SetUserData(kDownloadUIAdapterKey, adapter); |
| 490 } | 500 } |
| 491 | 501 |
| 492 return reinterpret_cast<jlong>( | 502 return reinterpret_cast<jlong>( |
| 493 new OfflinePageDownloadBridge(env, obj, adapter, browser_context)); | 503 new OfflinePageDownloadBridge(env, obj, adapter, browser_context)); |
| 494 } | 504 } |
| 495 | 505 |
| 496 } // namespace android | 506 } // namespace android |
| 497 } // namespace offline_pages | 507 } // namespace offline_pages |
| OLD | NEW |