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