Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2016 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 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/recent_tab_helper.h" | 5 #include "chrome/browser/android/offline_pages/recent_tab_helper.h" |
| 6 | 6 |
| 7 #include <queue> | 7 #include <queue> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 49 bool GetTabId(content::WebContents* web_contents, int* tab_id) override { | 49 bool GetTabId(content::WebContents* web_contents, int* tab_id) override { |
| 50 return offline_pages::OfflinePageUtils::GetTabId(web_contents, tab_id); | 50 return offline_pages::OfflinePageUtils::GetTabId(web_contents, tab_id); |
| 51 } | 51 } |
| 52 }; | 52 }; |
| 53 } // namespace | 53 } // namespace |
| 54 | 54 |
| 55 namespace offline_pages { | 55 namespace offline_pages { |
| 56 | 56 |
| 57 RecentTabHelper::RecentTabHelper(content::WebContents* web_contents) | 57 RecentTabHelper::RecentTabHelper(content::WebContents* web_contents) |
| 58 : content::WebContentsObserver(web_contents), | 58 : content::WebContentsObserver(web_contents), |
| 59 page_model_(nullptr), | |
| 60 snapshots_enabled_(false), | |
| 61 is_page_ready_for_snapshot_(false), | |
| 62 delegate_(new DefaultDelegate()), | 59 delegate_(new DefaultDelegate()), |
| 63 weak_ptr_factory_(this) { | 60 weak_ptr_factory_(this) { |
| 64 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 61 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 65 } | 62 } |
| 66 | 63 |
| 67 RecentTabHelper::~RecentTabHelper() { | 64 RecentTabHelper::~RecentTabHelper() { |
| 68 } | 65 } |
| 69 | 66 |
| 70 void RecentTabHelper::SetDelegate( | 67 void RecentTabHelper::SetDelegate( |
| 71 std::unique_ptr<RecentTabHelper::Delegate> delegate) { | 68 std::unique_ptr<RecentTabHelper::Delegate> delegate) { |
| 72 DCHECK(delegate); | 69 DCHECK(delegate); |
| 73 delegate_ = std::move(delegate); | 70 delegate_ = std::move(delegate); |
| 74 } | 71 } |
| 75 | 72 |
| 76 void RecentTabHelper::ObserveAndDownloadCurrentPage( | 73 void RecentTabHelper::ObserveAndDownloadCurrentPage( |
| 77 const ClientId& client_id, int64_t request_id) { | 74 const ClientId& client_id, int64_t request_id) { |
| 78 EnsureInitialized(); | 75 EnsureInitialized(); |
| 79 download_info_ = base::MakeUnique<DownloadPageInfo>(client_id, request_id); | 76 download_info_ = base::MakeUnique<DownloadPageInfo>(client_id, request_id); |
| 80 | 77 |
| 81 // If this tab helper is not enabled, immediately give the job back to | 78 // If this tab helper is not enabled, immediately give the job back to |
| 82 // RequestCoordinator. | 79 // RequestCoordinator. |
| 83 if (!snapshots_enabled_ || !page_model_) { | 80 if (!snapshots_enabled_ || !page_model_) { |
| 84 ReportDownloadStatusToRequestCoordinator(); | 81 ReportDownloadStatusToRequestCoordinator(); |
| 82 weak_ptr_factory_.InvalidateWeakPtrs(); | |
|
carlosk
2017/01/17 23:09:56
This change is what justifies the change of if-che
| |
| 85 download_info_.reset(); | 83 download_info_.reset(); |
| 86 return; | 84 return; |
| 87 } | 85 } |
| 88 | 86 |
| 89 // No snapshots yet happened on the current page - return and wait for some. | 87 // No snapshots yet happened on the current page - return and wait for some. |
| 90 if (!is_page_ready_for_snapshot_) | 88 if (!is_page_ready_for_snapshot_) |
| 91 return; | 89 return; |
| 92 | 90 |
| 93 // If snapshot already happened and we missed it, go ahead and snapshot now. | 91 // If snapshot already happened and we missed it, go ahead and snapshot now. |
| 94 OfflinePageModel::SavePageParams save_page_params; | 92 OfflinePageModel::SavePageParams save_page_params; |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 127 if (!snapshots_enabled_) | 125 if (!snapshots_enabled_) |
| 128 return; | 126 return; |
| 129 | 127 |
| 130 page_model_ = OfflinePageModelFactory::GetForBrowserContext( | 128 page_model_ = OfflinePageModelFactory::GetForBrowserContext( |
| 131 web_contents()->GetBrowserContext()); | 129 web_contents()->GetBrowserContext()); |
| 132 } | 130 } |
| 133 | 131 |
| 134 void RecentTabHelper::DidFinishNavigation( | 132 void RecentTabHelper::DidFinishNavigation( |
| 135 content::NavigationHandle* navigation_handle) { | 133 content::NavigationHandle* navigation_handle) { |
| 136 if (!navigation_handle->IsInMainFrame() || | 134 if (!navigation_handle->IsInMainFrame() || |
| 137 !navigation_handle->HasCommitted()) { | 135 navigation_handle->IsExternalProtocol() || |
| 136 navigation_handle->IsSamePage() || !navigation_handle->HasCommitted()) { | |
|
carlosk
2017/01/17 23:09:56
IsExternalProtocol: if a navigation is handled ext
carlosk
2017/01/18 03:06:10
I had to remove the IsExternalProtocol check becau
| |
| 138 return; | 137 return; |
| 139 } | 138 } |
| 140 | 139 |
| 141 // Cancel tasks in flight that relate to the previous page. | |
| 142 weak_ptr_factory_.InvalidateWeakPtrs(); | |
| 143 | |
| 144 EnsureInitialized(); | 140 EnsureInitialized(); |
| 145 if (!snapshots_enabled_) | 141 if (!snapshots_enabled_) |
| 146 return; | 142 return; |
| 147 | 143 |
| 144 // Cancel tasks in flight that relate to the previous page. | |
| 145 weak_ptr_factory_.InvalidateWeakPtrs(); | |
| 146 | |
| 148 // We navigated to a different page, lets report progress to Background | 147 // We navigated to a different page, lets report progress to Background |
| 149 // Offliner. | 148 // Offliner. |
| 150 if (download_info_ && !navigation_handle->IsSamePage()) { | 149 ReportDownloadStatusToRequestCoordinator(); |
|
carlosk
2017/01/17 23:09:57
The same page check now happens earlier and the do
dewittj
2017/01/17 23:21:52
Perhaps a minor rename to reflect the download_inf
carlosk
2017/01/18 03:06:10
Keeping as is as discussed offline.
| |
| 151 ReportDownloadStatusToRequestCoordinator(); | |
| 152 } | |
| 153 | 150 |
| 154 if (offline_pages::IsOffliningRecentPagesEnabled()) { | 151 if (offline_pages::IsOffliningRecentPagesEnabled()) { |
| 155 download_info_ = base::MakeUnique<DownloadPageInfo>( | 152 download_info_ = base::MakeUnique<DownloadPageInfo>( |
| 156 GetRecentPagesClientId(), OfflinePageModel::kInvalidOfflineId); | 153 GetRecentPagesClientId(), OfflinePageModel::kInvalidOfflineId); |
| 157 } else { | 154 } else { |
| 158 download_info_.reset(); | 155 download_info_.reset(); |
| 159 } | 156 } |
| 160 | 157 |
| 161 is_page_ready_for_snapshot_ = false; | 158 is_page_ready_for_snapshot_ = false; |
| 162 | 159 |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 214 | 211 |
| 215 // Remove previously captured pages for this tab. | 212 // Remove previously captured pages for this tab. |
| 216 page_model_->GetOfflineIdsForClientId( | 213 page_model_->GetOfflineIdsForClientId( |
| 217 GetRecentPagesClientId(), | 214 GetRecentPagesClientId(), |
| 218 base::Bind(&RecentTabHelper::ContinueSnapshotWithIdsToPurge, | 215 base::Bind(&RecentTabHelper::ContinueSnapshotWithIdsToPurge, |
| 219 weak_ptr_factory_.GetWeakPtr())); | 216 weak_ptr_factory_.GetWeakPtr())); |
| 220 } | 217 } |
| 221 | 218 |
| 222 void RecentTabHelper::ContinueSnapshotWithIdsToPurge( | 219 void RecentTabHelper::ContinueSnapshotWithIdsToPurge( |
| 223 const std::vector<int64_t>& page_ids) { | 220 const std::vector<int64_t>& page_ids) { |
| 224 if (!download_info_) | 221 DCHECK(download_info_); |
| 225 return; | |
| 226 | 222 |
| 227 // Also remove the download page if this is not a first snapshot. | 223 // Also remove the download page if this is not a first snapshot. |
| 228 std::vector<int64_t> ids(page_ids); | 224 std::vector<int64_t> ids(page_ids); |
| 229 ids.push_back(download_info_->request_id_); | 225 ids.push_back(download_info_->request_id_); |
| 230 | 226 |
| 231 page_model_->DeletePagesByOfflineId( | 227 page_model_->DeletePagesByOfflineId( |
| 232 ids, base::Bind(&RecentTabHelper::ContinueSnapshotAfterPurge, | 228 ids, base::Bind(&RecentTabHelper::ContinueSnapshotAfterPurge, |
| 233 weak_ptr_factory_.GetWeakPtr())); | 229 weak_ptr_factory_.GetWeakPtr())); |
| 234 } | 230 } |
| 235 | 231 |
| 236 void RecentTabHelper::ContinueSnapshotAfterPurge( | 232 void RecentTabHelper::ContinueSnapshotAfterPurge( |
| 237 OfflinePageModel::DeletePageResult result) { | 233 OfflinePageModel::DeletePageResult result) { |
| 238 if (!download_info_ || | 234 DCHECK(download_info_); |
| 239 result != OfflinePageModel::DeletePageResult::SUCCESS || | 235 DCHECK_EQ(snapshot_url_, web_contents()->GetLastCommittedURL()); |
| 240 !IsSamePage()) { | 236 if (result != OfflinePageModel::DeletePageResult::SUCCESS) { |
|
carlosk
2017/01/17 23:09:57
This IsSamePage check is not needed: if anything h
| |
| 241 ReportSnapshotCompleted(); | 237 ReportSnapshotCompleted(); |
| 242 return; | 238 return; |
| 243 } | 239 } |
| 244 | 240 |
| 245 OfflinePageModel::SavePageParams save_page_params; | 241 OfflinePageModel::SavePageParams save_page_params; |
| 246 save_page_params.url = snapshot_url_; | 242 save_page_params.url = snapshot_url_; |
| 247 save_page_params.client_id = download_info_->client_id_; | 243 save_page_params.client_id = download_info_->client_id_; |
| 248 save_page_params.proposed_offline_id = download_info_->request_id_; | 244 save_page_params.proposed_offline_id = download_info_->request_id_; |
| 249 page_model_->SavePage(save_page_params, | 245 page_model_->SavePage(save_page_params, |
| 250 delegate_->CreatePageArchiver(web_contents()), | 246 delegate_->CreatePageArchiver(web_contents()), |
| 251 base::Bind(&RecentTabHelper::SavePageCallback, | 247 base::Bind(&RecentTabHelper::SavePageCallback, |
| 252 weak_ptr_factory_.GetWeakPtr())); | 248 weak_ptr_factory_.GetWeakPtr())); |
| 253 } | 249 } |
| 254 | 250 |
| 255 void RecentTabHelper::SavePageCallback(OfflinePageModel::SavePageResult result, | 251 void RecentTabHelper::SavePageCallback(OfflinePageModel::SavePageResult result, |
| 256 int64_t offline_id) { | 252 int64_t offline_id) { |
| 257 if (!download_info_) | 253 DCHECK(download_info_); |
| 258 return; | |
| 259 download_info_->page_snapshot_completed_ = | 254 download_info_->page_snapshot_completed_ = |
| 260 (result == SavePageResult::SUCCESS); | 255 (result == SavePageResult::SUCCESS); |
| 261 ReportSnapshotCompleted(); | 256 ReportSnapshotCompleted(); |
| 262 } | 257 } |
| 263 | 258 |
| 264 void RecentTabHelper::ReportSnapshotCompleted() { | 259 void RecentTabHelper::ReportSnapshotCompleted() { |
| 265 snapshot_controller_->PendingSnapshotCompleted(); | 260 snapshot_controller_->PendingSnapshotCompleted(); |
| 266 // Tell RequestCoordinator how the request should be processed further. | 261 // Tell RequestCoordinator how the request should be processed further. |
| 267 ReportDownloadStatusToRequestCoordinator(); | 262 ReportDownloadStatusToRequestCoordinator(); |
| 268 } | 263 } |
| 269 | 264 |
| 270 void RecentTabHelper::ReportDownloadStatusToRequestCoordinator() { | 265 void RecentTabHelper::ReportDownloadStatusToRequestCoordinator() { |
| 271 if (!download_info_) | 266 if (!download_info_ || isSnapshottingForLastN()) |
|
dewittj
2017/01/17 23:21:52
!download_info_ is redundant here unless you take
carlosk
2017/01/18 03:06:10
It is not redundant: here the value is "or-ed" (||
| |
| 272 return; | |
| 273 | |
| 274 if (download_info_->request_id_ == OfflinePageModel::kInvalidOfflineId) | |
| 275 return; | 267 return; |
| 276 | 268 |
| 277 RequestCoordinator* request_coordinator = | 269 RequestCoordinator* request_coordinator = |
| 278 RequestCoordinatorFactory::GetForBrowserContext( | 270 RequestCoordinatorFactory::GetForBrowserContext( |
| 279 web_contents()->GetBrowserContext()); | 271 web_contents()->GetBrowserContext()); |
| 280 if (!request_coordinator) | 272 if (!request_coordinator) |
| 281 return; | 273 return; |
| 282 | 274 |
| 283 // It is OK to call these methods more then once, depending on | 275 // It is OK to call these methods more then once, depending on |
| 284 // number of snapshots attempted in this tab helper. If the request_id is not | 276 // number of snapshots attempted in this tab helper. If the request_id is not |
| 285 // in the list of RequestCoordinator, these calls have no effect. | 277 // in the list of RequestCoordinator, these calls have no effect. |
| 286 if (download_info_->page_snapshot_completed_) | 278 if (download_info_->page_snapshot_completed_) |
| 287 request_coordinator->MarkRequestCompleted(download_info_->request_id_); | 279 request_coordinator->MarkRequestCompleted(download_info_->request_id_); |
| 288 else | 280 else |
| 289 request_coordinator->EnableForOffliner(download_info_->request_id_, | 281 request_coordinator->EnableForOffliner(download_info_->request_id_, |
| 290 download_info_->client_id_); | 282 download_info_->client_id_); |
| 291 } | 283 } |
| 292 | 284 |
| 293 bool RecentTabHelper::IsSamePage() const { | |
| 294 return web_contents() && | |
| 295 (web_contents()->GetLastCommittedURL() == snapshot_url_); | |
| 296 } | |
| 297 | |
| 298 ClientId RecentTabHelper::GetRecentPagesClientId() const { | 285 ClientId RecentTabHelper::GetRecentPagesClientId() const { |
| 299 return ClientId(kLastNNamespace, tab_id_); | 286 return ClientId(kLastNNamespace, tab_id_); |
| 300 } | 287 } |
| 301 | 288 |
| 289 bool RecentTabHelper::isSnapshottingForLastN() const { | |
| 290 // For a snapshot to be executing for a last_n save download_info_ must be set | |
| 291 // and it must have an invalid request id. | |
| 292 // Note: at any point in time the download_info_ can be replaced with one | |
| 293 // created for a download snapshot request. | |
| 294 return download_info_ && | |
| 295 download_info_->request_id_ == OfflinePageModel::kInvalidOfflineId; | |
| 296 } | |
| 297 | |
| 302 } // namespace offline_pages | 298 } // namespace offline_pages |
| OLD | NEW |