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