| 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 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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), | 59 page_model_(nullptr), |
| 60 snapshots_enabled_(false), | 60 snapshots_enabled_(false), |
| 61 is_page_ready_for_snapshot_(false), | 61 is_page_ready_for_snapshot_(false), |
| 62 previous_request_id_for_load_(OfflinePageModel::kInvalidOfflineId), |
| 62 delegate_(new DefaultDelegate()), | 63 delegate_(new DefaultDelegate()), |
| 63 weak_ptr_factory_(this) { | 64 weak_ptr_factory_(this) { |
| 64 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 65 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 65 } | 66 } |
| 66 | 67 |
| 67 RecentTabHelper::~RecentTabHelper() { | 68 RecentTabHelper::~RecentTabHelper() { |
| 68 } | 69 } |
| 69 | 70 |
| 70 void RecentTabHelper::SetDelegate( | 71 void RecentTabHelper::SetDelegate( |
| 71 std::unique_ptr<RecentTabHelper::Delegate> delegate) { | 72 std::unique_ptr<RecentTabHelper::Delegate> delegate) { |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 153 | 154 |
| 154 if (offline_pages::IsOffliningRecentPagesEnabled()) { | 155 if (offline_pages::IsOffliningRecentPagesEnabled()) { |
| 155 int64_t proposed_id = OfflinePageModel::kInvalidOfflineId; | 156 int64_t proposed_id = OfflinePageModel::kInvalidOfflineId; |
| 156 download_info_ = base::MakeUnique<DownloadPageInfo>( | 157 download_info_ = base::MakeUnique<DownloadPageInfo>( |
| 157 GetRecentPagesClientId(), proposed_id); | 158 GetRecentPagesClientId(), proposed_id); |
| 158 } else { | 159 } else { |
| 159 download_info_.reset(); | 160 download_info_.reset(); |
| 160 } | 161 } |
| 161 | 162 |
| 162 is_page_ready_for_snapshot_ = false; | 163 is_page_ready_for_snapshot_ = false; |
| 164 previous_request_id_for_load_ = OfflinePageModel::kInvalidOfflineId; |
| 163 | 165 |
| 164 // New navigation, new snapshot session. | 166 // New navigation, new snapshot session. |
| 165 snapshot_url_ = web_contents()->GetLastCommittedURL(); | 167 snapshot_url_ = web_contents()->GetLastCommittedURL(); |
| 166 | 168 |
| 167 // Check for conditions that would cause us not to snapshot. | 169 // Check for conditions that would cause us not to snapshot. |
| 168 bool can_save = !navigation_handle->IsErrorPage() && | 170 bool can_save = !navigation_handle->IsErrorPage() && |
| 169 OfflinePageModel::CanSaveURL(snapshot_url_) && | 171 OfflinePageModel::CanSaveURL(snapshot_url_) && |
| 170 OfflinePageUtils::GetOfflinePageFromWebContents( | 172 OfflinePageUtils::GetOfflinePageFromWebContents( |
| 171 web_contents()) == nullptr; | 173 web_contents()) == nullptr; |
| 172 | 174 |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 218 GetRecentPagesClientId(), | 220 GetRecentPagesClientId(), |
| 219 base::Bind(&RecentTabHelper::ContinueSnapshotWithIdsToPurge, | 221 base::Bind(&RecentTabHelper::ContinueSnapshotWithIdsToPurge, |
| 220 weak_ptr_factory_.GetWeakPtr())); | 222 weak_ptr_factory_.GetWeakPtr())); |
| 221 } | 223 } |
| 222 | 224 |
| 223 void RecentTabHelper::ContinueSnapshotWithIdsToPurge( | 225 void RecentTabHelper::ContinueSnapshotWithIdsToPurge( |
| 224 const std::vector<int64_t>& page_ids) { | 226 const std::vector<int64_t>& page_ids) { |
| 225 if (!download_info_) | 227 if (!download_info_) |
| 226 return; | 228 return; |
| 227 | 229 |
| 230 std::vector<int64_t> ids(page_ids); |
| 228 // Also remove the download page if this is not a first snapshot. | 231 // Also remove the download page if this is not a first snapshot. |
| 229 std::vector<int64_t> ids(page_ids); | |
| 230 ids.push_back(download_info_->request_id_); | 232 ids.push_back(download_info_->request_id_); |
| 233 // Do not delete a previous save from the same page load right now as it's |
| 234 // unknown at this point in time if this snapshot will succeed. |
| 235 for (auto it = ids.begin(); it != ids.end(); ++it) { |
| 236 if (*it == previous_request_id_for_load_) { |
| 237 ids.erase(it); |
| 238 break; |
| 239 } |
| 240 } |
| 231 | 241 |
| 232 page_model_->DeletePagesByOfflineId( | 242 page_model_->DeletePagesByOfflineId( |
| 233 ids, base::Bind(&RecentTabHelper::ContinueSnapshotAfterPurge, | 243 ids, base::Bind(&RecentTabHelper::ContinueSnapshotAfterPurge, |
| 234 weak_ptr_factory_.GetWeakPtr())); | 244 weak_ptr_factory_.GetWeakPtr())); |
| 235 } | 245 } |
| 236 | 246 |
| 237 void RecentTabHelper::ContinueSnapshotAfterPurge( | 247 void RecentTabHelper::ContinueSnapshotAfterPurge( |
| 238 OfflinePageModel::DeletePageResult result) { | 248 OfflinePageModel::DeletePageResult result) { |
| 239 if (!download_info_ || | 249 if (!download_info_ || |
| 240 result != OfflinePageModel::DeletePageResult::SUCCESS || | 250 result != OfflinePageModel::DeletePageResult::SUCCESS || |
| (...skipping 11 matching lines...) Expand all Loading... |
| 252 base::Bind(&RecentTabHelper::SavePageCallback, | 262 base::Bind(&RecentTabHelper::SavePageCallback, |
| 253 weak_ptr_factory_.GetWeakPtr())); | 263 weak_ptr_factory_.GetWeakPtr())); |
| 254 } | 264 } |
| 255 | 265 |
| 256 void RecentTabHelper::SavePageCallback(OfflinePageModel::SavePageResult result, | 266 void RecentTabHelper::SavePageCallback(OfflinePageModel::SavePageResult result, |
| 257 int64_t offline_id) { | 267 int64_t offline_id) { |
| 258 if (!download_info_) | 268 if (!download_info_) |
| 259 return; | 269 return; |
| 260 download_info_->page_snapshot_completed_ = | 270 download_info_->page_snapshot_completed_ = |
| 261 (result == SavePageResult::SUCCESS); | 271 (result == SavePageResult::SUCCESS); |
| 272 |
| 273 if (download_info_->page_snapshot_completed_) { |
| 274 int64_t previous_request_id = previous_request_id_for_load_; |
| 275 previous_request_id_for_load_ = offline_id; |
| 276 |
| 277 // If there is a previous snapshot saved during the same page load it must |
| 278 // be deleted before finishing the snapshot process. |
| 279 if (previous_request_id != OfflinePageModel::kInvalidOfflineId) { |
| 280 std::vector<int64_t> page_id = {previous_request_id}; |
| 281 page_model_->DeletePagesByOfflineId( |
| 282 page_id, base::Bind(&RecentTabHelper::PreviousSaveWasPurged, |
| 283 weak_ptr_factory_.GetWeakPtr())); |
| 284 return; |
| 285 } |
| 286 } |
| 287 |
| 262 ReportSnapshotCompleted(); | 288 ReportSnapshotCompleted(); |
| 263 } | 289 } |
| 264 | 290 |
| 291 void RecentTabHelper::PreviousSaveWasPurged( |
| 292 OfflinePageModel::DeletePageResult result) { |
| 293 if (!download_info_) |
| 294 return; |
| 295 |
| 296 ReportSnapshotCompleted(); |
| 297 } |
| 298 |
| 265 void RecentTabHelper::ReportSnapshotCompleted() { | 299 void RecentTabHelper::ReportSnapshotCompleted() { |
| 266 snapshot_controller_->PendingSnapshotCompleted(); | 300 snapshot_controller_->PendingSnapshotCompleted(); |
| 267 // Tell RequestCoordinator how the request should be processed further. | 301 // Tell RequestCoordinator how the request should be processed further. |
| 268 ReportDownloadStatusToRequestCoordinator(); | 302 ReportDownloadStatusToRequestCoordinator(); |
| 269 } | 303 } |
| 270 | 304 |
| 271 void RecentTabHelper::ReportDownloadStatusToRequestCoordinator() { | 305 void RecentTabHelper::ReportDownloadStatusToRequestCoordinator() { |
| 272 if (!download_info_) | 306 if (!download_info_) |
| 273 return; | 307 return; |
| 274 | 308 |
| (...skipping 19 matching lines...) Expand all Loading... |
| 294 bool RecentTabHelper::IsSamePage() const { | 328 bool RecentTabHelper::IsSamePage() const { |
| 295 return web_contents() && | 329 return web_contents() && |
| 296 (web_contents()->GetLastCommittedURL() == snapshot_url_); | 330 (web_contents()->GetLastCommittedURL() == snapshot_url_); |
| 297 } | 331 } |
| 298 | 332 |
| 299 ClientId RecentTabHelper::GetRecentPagesClientId() const { | 333 ClientId RecentTabHelper::GetRecentPagesClientId() const { |
| 300 return ClientId(kLastNNamespace, tab_id_); | 334 return ClientId(kLastNNamespace, tab_id_); |
| 301 } | 335 } |
| 302 | 336 |
| 303 } // namespace offline_pages | 337 } // namespace offline_pages |
| OLD | NEW |