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 |
| 11 #include "base/memory/weak_ptr.h" | 11 #include "base/memory/weak_ptr.h" |
| 12 #include "components/offline_pages/core/downloads/download_ui_item.h" | 12 #include "components/offline_pages/core/downloads/download_ui_item.h" |
| 13 #include "components/offline_pages/core/offline_page_model.h" | 13 #include "components/offline_pages/core/offline_page_model.h" |
| 14 #include "components/offline_pages/core/snapshot_controller.h" | 14 #include "components/offline_pages/core/snapshot_controller.h" |
| 15 #include "content/public/browser/web_contents_observer.h" | 15 #include "content/public/browser/web_contents_observer.h" |
| 16 #include "content/public/browser/web_contents_user_data.h" | 16 #include "content/public/browser/web_contents_user_data.h" |
| 17 #include "url/gurl.h" | 17 #include "url/gurl.h" |
| 18 | 18 |
| 19 namespace content { | 19 namespace content { |
| 20 class NavigationHandle; | 20 class NavigationHandle; |
| 21 } | 21 } |
| 22 | 22 |
| 23 namespace offline_pages { | 23 namespace offline_pages { |
| 24 | 24 |
| 25 using PageQuality = SnapshotController::PageQuality; | |
|
dewittj
2017/01/20 22:41:26
Does this pollute the namespace? in other words, c
carlosk
2017/01/20 23:04:35
I'm unsure but I agree it's a problem. I'll move t
| |
| 26 | |
| 25 // Attaches to every WebContent shown in a tab. Waits until the WebContent is | 27 // Attaches to every WebContent shown in a tab. Waits until the WebContent is |
| 26 // loaded to proper degree and then makes a snapshot of the page. Removes the | 28 // loaded to proper degree and then makes a snapshot of the page. Removes the |
| 27 // oldest snapshot in the 'ring buffer'. As a result, there is always up to N | 29 // oldest snapshot in the 'ring buffer'. As a result, there is always up to N |
| 28 // snapshots of recent pages on the device. | 30 // snapshots of recent pages on the device. |
| 29 class RecentTabHelper | 31 class RecentTabHelper |
| 30 : public content::WebContentsObserver, | 32 : public content::WebContentsObserver, |
| 31 public content::WebContentsUserData<RecentTabHelper>, | 33 public content::WebContentsUserData<RecentTabHelper>, |
| 32 public SnapshotController::Client { | 34 public SnapshotController::Client { |
| 33 public: | 35 public: |
| 34 ~RecentTabHelper() override; | 36 ~RecentTabHelper() override; |
| 35 | 37 |
| 36 // content::WebContentsObserver | 38 // content::WebContentsObserver |
| 37 void DidFinishNavigation( | 39 void DidFinishNavigation( |
| 38 content::NavigationHandle* navigation_handle) override; | 40 content::NavigationHandle* navigation_handle) override; |
| 39 void DocumentAvailableInMainFrame() override; | 41 void DocumentAvailableInMainFrame() override; |
| 40 void DocumentOnLoadCompletedInMainFrame() override; | 42 void DocumentOnLoadCompletedInMainFrame() override; |
| 41 void WebContentsDestroyed() override; | 43 void WebContentsDestroyed() override; |
| 44 void WasHidden() override; | |
| 42 | 45 |
| 43 // SnapshotController::Client | 46 // SnapshotController::Client |
| 44 void StartSnapshot() override; | 47 void StartSnapshot() override; |
| 45 | 48 |
| 46 // Delegate that is used by RecentTabHelper to get external dependencies. | 49 // Delegate that is used by RecentTabHelper to get external dependencies. |
| 47 // Default implementation lives in .cc file, while tests provide an override. | 50 // Default implementation lives in .cc file, while tests provide an override. |
| 48 class Delegate { | 51 class Delegate { |
| 49 public: | 52 public: |
| 50 virtual ~Delegate() {} | 53 virtual ~Delegate() {} |
| 51 virtual std::unique_ptr<OfflinePageArchiver> CreatePageArchiver( | 54 virtual std::unique_ptr<OfflinePageArchiver> CreatePageArchiver( |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 87 ClientId client_id; | 90 ClientId client_id; |
| 88 | 91 |
| 89 // Id of the suspended request in Background Offliner. Used to un-suspend | 92 // Id of the suspended request in Background Offliner. Used to un-suspend |
| 90 // the request if the capture of the current page was not possible (e.g. | 93 // the request if the capture of the current page was not possible (e.g. |
| 91 // the user navigated to another page before current one was loaded). | 94 // the user navigated to another page before current one was loaded). |
| 92 // 0 if this is a "last_n" info. | 95 // 0 if this is a "last_n" info. |
| 93 int64_t request_id = OfflinePageModel::kInvalidOfflineId; | 96 int64_t request_id = OfflinePageModel::kInvalidOfflineId; |
| 94 | 97 |
| 95 // True if there was at least one snapshot successfully completed. | 98 // True if there was at least one snapshot successfully completed. |
| 96 bool page_snapshot_completed = false; | 99 bool page_snapshot_completed = false; |
| 100 | |
| 101 // Expected snapshot quality should the saving succeed. This value is only | |
| 102 // valid if |page_snapshot_completed| is true. | |
| 103 PageQuality expected_page_quality = PageQuality::POOR; | |
| 97 }; | 104 }; |
| 98 | 105 |
| 99 explicit RecentTabHelper(content::WebContents* web_contents); | 106 explicit RecentTabHelper(content::WebContents* web_contents); |
| 100 friend class content::WebContentsUserData<RecentTabHelper>; | 107 friend class content::WebContentsUserData<RecentTabHelper>; |
| 101 | 108 |
| 102 void EnsureInitialized(); | 109 bool EnsureInitialized(); |
| 103 void ContinueSnapshotWithIdsToPurge(const std::vector<int64_t>& page_ids); | 110 void ContinueSnapshotWithIdsToPurge(SnapshotProgressInfo* snapshot_info, |
| 104 void ContinueSnapshotAfterPurge(OfflinePageModel::DeletePageResult result); | 111 const std::vector<int64_t>& page_ids); |
| 105 void SavePageCallback(OfflinePageModel::SavePageResult result, | 112 void ContinueSnapshotAfterPurge(SnapshotProgressInfo* snapshot_info, |
| 113 OfflinePageModel::DeletePageResult result); | |
| 114 void SavePageCallback(SnapshotProgressInfo* snapshot_info, | |
| 115 OfflinePageModel::SavePageResult result, | |
| 106 int64_t offline_id); | 116 int64_t offline_id); |
| 107 void ReportSnapshotCompleted(); | 117 void ReportSnapshotCompleted(SnapshotProgressInfo* snapshot_info); |
| 108 void ReportDownloadStatusToRequestCoordinator(); | 118 void ReportDownloadStatusToRequestCoordinator( |
| 109 bool IsSamePage() const; | 119 SnapshotProgressInfo* snapshot_info); |
| 110 ClientId GetRecentPagesClientId() const; | 120 ClientId GetRecentPagesClientId() const; |
| 121 void SaveSnapshotForDownloads(); | |
| 111 | 122 |
| 112 // Page model is a service, no ownership. Can be null - for example, in | 123 // Page model is a service, no ownership. Can be null - for example, in |
| 113 // case when tab is in incognito profile. | 124 // case when tab is in incognito profile. |
| 114 OfflinePageModel* page_model_ = nullptr; | 125 OfflinePageModel* page_model_ = nullptr; |
| 115 | 126 |
| 116 // If false, never make snapshots off the attached WebContents. | 127 // If false, never make snapshots off the attached WebContents. |
| 117 // Not page-specific. | 128 // Not page-specific. |
| 118 bool snapshots_enabled_ = false; | 129 bool snapshots_enabled_ = false; |
| 119 | 130 |
| 120 // Becomes true during navigation if the page is ready for snapshot as | 131 // Snapshot progress information for a downloads triggered request. Null if |
| 121 // indicated by at least one callback from SnapshotController. | 132 // downloads is not capturing or hasn't captured the current page. |
| 122 bool is_page_ready_for_snapshot_ = false; | 133 std::unique_ptr<SnapshotProgressInfo> downloads_latest_snapshot_info_; |
| 123 | 134 |
| 124 // Info for the offline page to capture. Null if the tab is not capturing | 135 // Snapshot progress information for a last_n triggered request. Null if |
| 125 // current page. | 136 // last_n is not currently capturing the current page. |
| 126 std::unique_ptr<SnapshotProgressInfo> snapshot_info_; | 137 std::unique_ptr<SnapshotProgressInfo> last_n_ongoing_snapshot_info_; |
| 127 | 138 |
| 128 // If empty, the tab does not have AndroidId and can not capture pages. | 139 // If empty, the tab does not have AndroidId and can not capture pages. |
| 129 std::string tab_id_; | 140 std::string tab_id_; |
| 130 | 141 |
| 131 // The URL of the page that is currently being snapshotted. Used to check, | 142 // The URL of the page that is currently being snapshotted. Used to check, |
| 132 // during async operations, that WebContents still contains the same page. | 143 // during async operations, that WebContents still contains the same page. |
| 133 GURL snapshot_url_; | 144 GURL snapshot_url_; |
| 134 // This starts out null and used as a flag for EnsureInitialized() to do the | 145 |
| 135 // initialization only once. | 146 // Monitors page loads and starts snapshots when a download request exist. It |
| 147 // is also used as an initialization flag for EnsureInitialized() to be run | |
| 148 // only once. | |
| 136 std::unique_ptr<SnapshotController> snapshot_controller_; | 149 std::unique_ptr<SnapshotController> snapshot_controller_; |
| 137 | 150 |
| 138 std::unique_ptr<Delegate> delegate_; | 151 std::unique_ptr<Delegate> delegate_; |
| 139 | 152 |
| 153 // Set at each navigation to control if last_n should save snapshots of the | |
| 154 // current page being loaded. | |
| 155 bool last_n_listen_to_tab_hidden_ = false; | |
| 156 | |
| 157 // Track the page quality status of the last saved snapshot for the current | |
| 158 // page. It is generally reset upon new navigations. | |
| 159 PageQuality last_n_latest_saved_quality_ = PageQuality::POOR; | |
| 160 | |
| 140 base::WeakPtrFactory<RecentTabHelper> weak_ptr_factory_; | 161 base::WeakPtrFactory<RecentTabHelper> weak_ptr_factory_; |
| 141 | 162 |
| 142 DISALLOW_COPY_AND_ASSIGN(RecentTabHelper); | 163 DISALLOW_COPY_AND_ASSIGN(RecentTabHelper); |
| 143 }; | 164 }; |
| 144 | 165 |
| 145 } // namespace offline_pages | 166 } // namespace offline_pages |
| 146 | 167 |
| 147 #endif // CHROME_BROWSER_ANDROID_OFFLINE_PAGES_RECENT_TAB_HELPER_H_ | 168 #endif // CHROME_BROWSER_ANDROID_OFFLINE_PAGES_RECENT_TAB_HELPER_H_ |
| OLD | NEW |