Chromium Code Reviews| 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 #ifndef CHROME_BROWSER_ANDROID_OFFLINE_PAGES_RECENT_TAB_HELPER_H_ | 5 #ifndef CHROME_BROWSER_ANDROID_OFFLINE_PAGES_RECENT_TAB_HELPER_H_ |
| 6 #define CHROME_BROWSER_ANDROID_OFFLINE_PAGES_RECENT_TAB_HELPER_H_ | 6 #define CHROME_BROWSER_ANDROID_OFFLINE_PAGES_RECENT_TAB_HELPER_H_ |
| 7 | 7 |
| 8 #include <memory> | 8 #include <memory> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 50 public: | 50 public: |
| 51 virtual ~Delegate() {} | 51 virtual ~Delegate() {} |
| 52 virtual std::unique_ptr<OfflinePageArchiver> CreatePageArchiver( | 52 virtual std::unique_ptr<OfflinePageArchiver> CreatePageArchiver( |
| 53 content::WebContents* web_contents) = 0; | 53 content::WebContents* web_contents) = 0; |
| 54 virtual scoped_refptr<base::SingleThreadTaskRunner> GetTaskRunner() = 0; | 54 virtual scoped_refptr<base::SingleThreadTaskRunner> GetTaskRunner() = 0; |
| 55 // There is no expectations that tab_id is always present. | 55 // There is no expectations that tab_id is always present. |
| 56 virtual bool GetTabId(content::WebContents* web_contents, int* tab_id) = 0; | 56 virtual bool GetTabId(content::WebContents* web_contents, int* tab_id) = 0; |
| 57 }; | 57 }; |
| 58 void SetDelegate(std::unique_ptr<RecentTabHelper::Delegate> delegate); | 58 void SetDelegate(std::unique_ptr<RecentTabHelper::Delegate> delegate); |
| 59 | 59 |
| 60 // Support for Download Page feature. The Download Page button does this: | 60 // Creates a request to download the current page with a properly filled |
| 61 // 1. Creates suspended request for Background Offliner. | 61 // |client_id| and valid |request_id| issued by RequestCoordinator from a |
| 62 // 2. Calls this method with properly filled ClientId. | 62 // suspended request. This method might be called multiple times for the same |
| 63 // 3. This tab helper observes the page load and captures the page | 63 // page at any point after its navigation commits. There are some important |
| 64 // if the load progresses far enough, as indicated by SnapshotController. | 64 // points about how requests are handled: |
| 65 // 4. If capture is successful, this tab helper removes the suspended request. | 65 // a) While there is an ongoing request, new requests are ignored (no |
| 66 // Otherwise (navigation to other page, close tab) tab helper un-suspends | 66 // overlapping snapshots). |
| 67 // the request so the Background Offliner starts working on it. | 67 // b) The page has to be sufficiently loaded to be considerer of minimum |
| 68 // 5. If Chrome is killed at any point, next time Background Offliner loads | 68 // quality for the request to be started immediately. |
| 69 // it converts all suspended requests from last session into active. | 69 // c) Any calls made before the page is considered to have minimal quality |
| 70 // will be scheduled to be executed once that happens. The scheduled | |
| 71 // request is considered "ongoing" for a) purposes. | |
| 72 // d) If the save operation is successful the dormant request with | |
| 73 // RequestCoordinator is canceled; otherwise it is resumed. This logic is | |
| 74 // robust to crashes. | |
| 75 // e) At the moment the page reaches high quality, if there was a successful | |
| 76 // snapshot saved at a lower quality then a new snapshot is automatically | |
| 77 // requested to replace it. | |
| 78 // Note #1: Page quality is determined by SnapshotController and is based on | |
| 79 // its assessment of "how much loaded" it is. | |
| 80 // Note #2: Currently this method only accepts download requests from the | |
| 81 // downloads namespace. Multiple clients are not supported. | |
| 70 void ObserveAndDownloadCurrentPage(const ClientId& client_id, | 82 void ObserveAndDownloadCurrentPage(const ClientId& client_id, |
| 71 int64_t request_id); | 83 int64_t request_id); |
| 72 | 84 |
| 73 private: | 85 private: |
| 74 // Keeps client_id/request_id that will be used for the offline snapshot. | 86 struct SnapshotProgressInfo; |
| 75 struct SnapshotProgressInfo { | |
| 76 public: | |
| 77 // For a downloads snapshot request, where the |request_id| is defined. | |
| 78 SnapshotProgressInfo(const ClientId& client_id, int64_t request_id) | |
| 79 : client_id(client_id), request_id(request_id) {} | |
| 80 | |
| 81 // For a last_n snapshot request. | |
| 82 explicit SnapshotProgressInfo(const ClientId& client_id) | |
| 83 : client_id(client_id) {} | |
| 84 | |
| 85 bool IsForLastN(); | |
| 86 | |
| 87 // The ClientID to go with the offline page. | |
| 88 ClientId client_id; | |
| 89 | |
| 90 // Id of the suspended request in Background Offliner. Used to un-suspend | |
| 91 // the request if the capture of the current page was not possible (e.g. | |
| 92 // the user navigated to another page before current one was loaded). | |
| 93 // 0 if this is a "last_n" info. | |
| 94 int64_t request_id = OfflinePageModel::kInvalidOfflineId; | |
| 95 | |
| 96 // True if there was at least one snapshot successfully completed. | |
| 97 bool page_snapshot_completed = false; | |
| 98 | |
| 99 // Expected snapshot quality should the saving succeed. This value is only | |
| 100 // valid if |page_snapshot_completed| is true. | |
| 101 SnapshotController::PageQuality expected_page_quality = | |
| 102 SnapshotController::PageQuality::POOR; | |
| 103 }; | |
| 104 | 87 |
| 105 explicit RecentTabHelper(content::WebContents* web_contents); | 88 explicit RecentTabHelper(content::WebContents* web_contents); |
| 106 friend class content::WebContentsUserData<RecentTabHelper>; | 89 friend class content::WebContentsUserData<RecentTabHelper>; |
| 107 | 90 |
| 108 bool EnsureInitialized(); | 91 bool EnsureInitialized(); |
| 109 void ContinueSnapshotWithIdsToPurge(SnapshotProgressInfo* snapshot_info, | 92 void ContinueSnapshotWithIdsToPurge(SnapshotProgressInfo* snapshot_info, |
| 110 const std::vector<int64_t>& page_ids); | 93 const std::vector<int64_t>& page_ids); |
| 111 void ContinueSnapshotAfterPurge(SnapshotProgressInfo* snapshot_info, | 94 void ContinueSnapshotAfterPurge(SnapshotProgressInfo* snapshot_info, |
| 112 OfflinePageModel::DeletePageResult result); | 95 OfflinePageModel::DeletePageResult result); |
| 113 void SavePageCallback(SnapshotProgressInfo* snapshot_info, | 96 void SavePageCallback(SnapshotProgressInfo* snapshot_info, |
| 114 OfflinePageModel::SavePageResult result, | 97 OfflinePageModel::SavePageResult result, |
| 115 int64_t offline_id); | 98 int64_t offline_id); |
| 116 void ReportSnapshotCompleted(SnapshotProgressInfo* snapshot_info); | 99 void ReportSnapshotCompleted(SnapshotProgressInfo* snapshot_info, |
| 100 bool success); | |
| 117 void ReportDownloadStatusToRequestCoordinator( | 101 void ReportDownloadStatusToRequestCoordinator( |
| 118 SnapshotProgressInfo* snapshot_info); | 102 SnapshotProgressInfo* snapshot_info, |
| 103 bool cancel_background_request); | |
| 119 ClientId GetRecentPagesClientId() const; | 104 ClientId GetRecentPagesClientId() const; |
| 120 void SaveSnapshotForDownloads(); | 105 void SaveSnapshotForDownloads(bool replace_latest); |
| 121 | 106 |
| 122 // Page model is a service, no ownership. Can be null - for example, in | 107 // Page model is a service, no ownership. Can be null - for example, in |
| 123 // case when tab is in incognito profile. | 108 // case when tab is in incognito profile. |
| 124 OfflinePageModel* page_model_ = nullptr; | 109 OfflinePageModel* page_model_ = nullptr; |
| 125 | 110 |
| 126 // If false, never make snapshots off the attached WebContents. | 111 // If false, never make snapshots off the attached WebContents. |
| 127 // Not page-specific. | 112 // Not page-specific. |
| 128 bool snapshots_enabled_ = false; | 113 bool snapshots_enabled_ = false; |
| 129 | 114 |
| 130 // Snapshot progress information for a downloads triggered request. Null if | 115 // Snapshot progress information for an ongoing snapshot requested by |
| 131 // downloads is not capturing or hasn't captured the current page. | 116 // downloads. Null if there's no ongoing request. |
| 132 std::unique_ptr<SnapshotProgressInfo> downloads_latest_snapshot_info_; | 117 std::unique_ptr<SnapshotProgressInfo> downloads_ongoing_snapshot_info_; |
| 118 | |
| 119 // This is set to true if the ongoing snapshot for downloads is waiting on the | |
| 120 // page to reach a minimal quality level to start. | |
| 121 bool downloads_snapshot_on_hold_ = false; | |
| 122 | |
| 123 // Snapshot information for the last successful snapshot requested by | |
| 124 // downloads. Null no successful one has ever completed. | |
|
Dmitry Titov
2017/01/27 04:18:22
nit: "Null no" -> "Null if no"
carlosk
2017/01/27 18:47:14
Done.
| |
| 125 std::unique_ptr<SnapshotProgressInfo> downloads_latest_saved_snapshot_info_; | |
| 133 | 126 |
| 134 // Snapshot progress information for a last_n triggered request. Null if | 127 // Snapshot progress information for a last_n triggered request. Null if |
| 135 // last_n is not currently capturing the current page. | 128 // last_n is not currently capturing the current page. |
| 136 std::unique_ptr<SnapshotProgressInfo> last_n_ongoing_snapshot_info_; | 129 std::unique_ptr<SnapshotProgressInfo> last_n_ongoing_snapshot_info_; |
| 137 | 130 |
| 138 // If empty, the tab does not have AndroidId and can not capture pages. | 131 // If empty, the tab does not have AndroidId and can not capture pages. |
| 139 std::string tab_id_; | 132 std::string tab_id_; |
| 140 | 133 |
| 141 // The URL of the page that is currently being snapshotted. Used to check, | 134 // The URL of the page that is currently being snapshotted. Used to check, |
| 142 // during async operations, that WebContents still contains the same page. | 135 // during async operations, that WebContents still contains the same page. |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 159 SnapshotController::PageQuality::POOR; | 152 SnapshotController::PageQuality::POOR; |
| 160 | 153 |
| 161 base::WeakPtrFactory<RecentTabHelper> weak_ptr_factory_; | 154 base::WeakPtrFactory<RecentTabHelper> weak_ptr_factory_; |
| 162 | 155 |
| 163 DISALLOW_COPY_AND_ASSIGN(RecentTabHelper); | 156 DISALLOW_COPY_AND_ASSIGN(RecentTabHelper); |
| 164 }; | 157 }; |
| 165 | 158 |
| 166 } // namespace offline_pages | 159 } // namespace offline_pages |
| 167 | 160 |
| 168 #endif // CHROME_BROWSER_ANDROID_OFFLINE_PAGES_RECENT_TAB_HELPER_H_ | 161 #endif // CHROME_BROWSER_ANDROID_OFFLINE_PAGES_RECENT_TAB_HELPER_H_ |
| OLD | NEW |