| 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 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 61 | 61 |
| 62 RecentTabHelper::~RecentTabHelper() { | 62 RecentTabHelper::~RecentTabHelper() { |
| 63 } | 63 } |
| 64 | 64 |
| 65 void RecentTabHelper::DidFinishNavigation( | 65 void RecentTabHelper::DidFinishNavigation( |
| 66 content::NavigationHandle* navigation_handle) { | 66 content::NavigationHandle* navigation_handle) { |
| 67 if (navigation_handle->IsInMainFrame() && | 67 if (navigation_handle->IsInMainFrame() && |
| 68 navigation_handle->HasCommitted()) { | 68 navigation_handle->HasCommitted()) { |
| 69 // Cancel tasks in flight that relate to the previous page. | 69 // Cancel tasks in flight that relate to the previous page. |
| 70 weak_ptr_factory_.InvalidateWeakPtrs(); | 70 weak_ptr_factory_.InvalidateWeakPtrs(); |
| 71 |
| 71 // New navigation, new snapshot session. | 72 // New navigation, new snapshot session. |
| 73 snapshot_url_ = GURL(); |
| 74 GURL last_committed_url = web_contents()->GetLastCommittedURL(); |
| 75 |
| 76 // Check for conditions that would cause us not to snapshot. |
| 77 bool can_save = !navigation_handle->IsErrorPage() && |
| 78 OfflinePageModel::CanSaveURL(last_committed_url); |
| 79 |
| 80 // We only want to record UMA if the page is not Off The Record. |
| 81 if (!never_do_snapshots_) |
| 82 UMA_HISTOGRAM_BOOLEAN("OfflinePages.CanSaveRecentPage", can_save); |
| 83 |
| 84 // Always reset so that posted tasks get cancelled. |
| 72 snapshot_controller_->Reset(); | 85 snapshot_controller_->Reset(); |
| 73 snapshot_url_ = GURL::EmptyGURL(); | 86 |
| 87 if (never_do_snapshots_ || !can_save) |
| 88 snapshot_controller_->Stop(); |
| 89 else |
| 90 snapshot_url_ = last_committed_url; |
| 74 } | 91 } |
| 75 } | 92 } |
| 76 | 93 |
| 77 void RecentTabHelper::DocumentAvailableInMainFrame() { | 94 void RecentTabHelper::DocumentAvailableInMainFrame() { |
| 78 snapshot_controller_->DocumentAvailableInMainFrame(); | 95 snapshot_controller_->DocumentAvailableInMainFrame(); |
| 79 } | 96 } |
| 80 | 97 |
| 81 void RecentTabHelper::DocumentOnLoadCompletedInMainFrame() { | 98 void RecentTabHelper::DocumentOnLoadCompletedInMainFrame() { |
| 82 snapshot_controller_->DocumentOnLoadCompletedInMainFrame(); | 99 snapshot_controller_->DocumentOnLoadCompletedInMainFrame(); |
| 83 } | 100 } |
| 84 | 101 |
| 85 // This starts a sequence of async operations chained through callbacks: | 102 // This starts a sequence of async operations chained through callbacks: |
| 86 // - compute the set of old 'last_n' pages that have to be purged | 103 // - compute the set of old 'last_n' pages that have to be purged |
| 87 // - delete the pages found in the previous step | 104 // - delete the pages found in the previous step |
| 88 // - snapshot the current web contents | 105 // - snapshot the current web contents |
| 89 // Along the chain, the original URL is passed and compared, to detect | 106 // Along the chain, the original URL is passed and compared, to detect |
| 90 // possible navigation and cancel snapshot in that case. | 107 // possible navigation and cancel snapshot in that case. |
| 91 void RecentTabHelper::StartSnapshot() { | 108 void RecentTabHelper::StartSnapshot() { |
| 92 if (never_do_snapshots_) | |
| 93 return; | |
| 94 | |
| 95 // Ignores any non-normal pages, like error pages. | |
| 96 content::NavigationEntry* entry = | |
| 97 web_contents()->GetController().GetLastCommittedEntry(); | |
| 98 if (!entry || entry->GetPageType() != content::PAGE_TYPE_NORMAL) | |
| 99 return; | |
| 100 | |
| 101 GURL url = web_contents()->GetLastCommittedURL(); | |
| 102 bool can_save = OfflinePageModel::CanSaveURL(url); | |
| 103 UMA_HISTOGRAM_BOOLEAN("OfflinePages.CanSaveRecentPage", can_save); | |
| 104 if (!can_save) | |
| 105 return; | |
| 106 | |
| 107 snapshot_url_ = url; | |
| 108 | |
| 109 // TODO(dimich): Implement automatic cleanup as per design doc, based on | 109 // TODO(dimich): Implement automatic cleanup as per design doc, based on |
| 110 // storage limits and page age. | 110 // storage limits and page age. |
| 111 // This algorithm (remove pages before making sure the save was successful) | 111 // This algorithm (remove pages before making sure the save was successful) |
| 112 // prefers keeping the storage use low and under control by potentially | 112 // prefers keeping the storage use low and under control by potentially |
| 113 // sacrificing the current snapshot. | 113 // sacrificing the current snapshot. |
| 114 page_model_->GetOfflineIdsForClientId( | 114 page_model_->GetOfflineIdsForClientId( |
| 115 client_id(), | 115 client_id(), |
| 116 base::Bind(&RecentTabHelper::ContinueSnapshotWithIdsToPurge, | 116 base::Bind(&RecentTabHelper::ContinueSnapshotWithIdsToPurge, |
| 117 weak_ptr_factory_.GetWeakPtr())); | 117 weak_ptr_factory_.GetWeakPtr())); |
| 118 } | 118 } |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 205 std::unique_ptr<TestArchiveFactory> test_archive_factory) { | 205 std::unique_ptr<TestArchiveFactory> test_archive_factory) { |
| 206 test_archive_factory_ = std::move(test_archive_factory); | 206 test_archive_factory_ = std::move(test_archive_factory); |
| 207 } | 207 } |
| 208 | 208 |
| 209 void RecentTabHelper::SetTaskRunnerForTest( | 209 void RecentTabHelper::SetTaskRunnerForTest( |
| 210 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner) { | 210 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner) { |
| 211 snapshot_controller_.reset(new SnapshotController(task_runner, this)); | 211 snapshot_controller_.reset(new SnapshotController(task_runner, this)); |
| 212 } | 212 } |
| 213 | 213 |
| 214 } // namespace offline_pages | 214 } // namespace offline_pages |
| OLD | NEW |