| 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/downloads/download_ui_item.h" |
| 12 #include "components/offline_pages/offline_page_model.h" | 13 #include "components/offline_pages/offline_page_model.h" |
| 13 #include "components/offline_pages/snapshot_controller.h" | 14 #include "components/offline_pages/snapshot_controller.h" |
| 14 #include "content/public/browser/web_contents_observer.h" | 15 #include "content/public/browser/web_contents_observer.h" |
| 15 #include "content/public/browser/web_contents_user_data.h" | 16 #include "content/public/browser/web_contents_user_data.h" |
| 16 #include "url/gurl.h" | 17 #include "url/gurl.h" |
| 17 | 18 |
| 18 namespace content { | 19 namespace content { |
| 19 class NavigationEntry; | 20 class NavigationEntry; |
| 20 class NavigationHandle; | 21 class NavigationHandle; |
| 21 } | 22 } |
| 22 | 23 |
| 23 namespace offline_pages { | 24 namespace offline_pages { |
| 24 | 25 |
| 25 // Attaches to every WebContent shown in a tab. Waits until the WebContent is | 26 // 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 | 27 // 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 | 28 // oldest snapshot in the 'ring buffer'. As a result, there is always up to N |
| 28 // snapshots of recent pages on the device. | 29 // snapshots of recent pages on the device. |
| 29 class RecentTabHelper | 30 class RecentTabHelper |
| 30 : public content::WebContentsObserver, | 31 : public content::WebContentsObserver, |
| 31 public content::WebContentsUserData<RecentTabHelper>, | 32 public content::WebContentsUserData<RecentTabHelper>, |
| 32 public SnapshotController::Client { | 33 public SnapshotController::Client { |
| 33 public: | 34 public: |
| 34 ~RecentTabHelper() override; | 35 ~RecentTabHelper() override; |
| 35 | 36 |
| 36 // content::WebContentsObserver | 37 // content::WebContentsObserver |
| 37 void DidFinishNavigation( | 38 void DidFinishNavigation( |
| 38 content::NavigationHandle* navigation_handle) override; | 39 content::NavigationHandle* navigation_handle) override; |
| 39 void DocumentAvailableInMainFrame() override; | 40 void DocumentAvailableInMainFrame() override; |
| 40 void DocumentOnLoadCompletedInMainFrame() override; | 41 void DocumentOnLoadCompletedInMainFrame() override; |
| 42 void WebContentsDestroyed() override; |
| 41 | 43 |
| 42 // SnapshotController::Client | 44 // SnapshotController::Client |
| 43 void StartSnapshot() override; | 45 void StartSnapshot() override; |
| 44 | 46 |
| 45 // Delegate that is used by RecentTabHelper to get external dependencies. | 47 // Delegate that is used by RecentTabHelper to get external dependencies. |
| 46 // Default implementation lives in .cc file, while tests provide an override. | 48 // Default implementation lives in .cc file, while tests provide an override. |
| 47 class Delegate { | 49 class Delegate { |
| 48 public: | 50 public: |
| 49 virtual ~Delegate() {} | 51 virtual ~Delegate() {} |
| 50 virtual std::unique_ptr<OfflinePageArchiver> CreatePageArchiver( | 52 virtual std::unique_ptr<OfflinePageArchiver> CreatePageArchiver( |
| 51 content::WebContents* web_contents) = 0; | 53 content::WebContents* web_contents) = 0; |
| 52 virtual scoped_refptr<base::SingleThreadTaskRunner> GetTaskRunner() = 0; | 54 virtual scoped_refptr<base::SingleThreadTaskRunner> GetTaskRunner() = 0; |
| 53 // There is no expectations that tab_id is always present. | 55 // There is no expectations that tab_id is always present. |
| 54 virtual bool GetTabId(content::WebContents* web_contents, int* tab_id) = 0; | 56 virtual bool GetTabId(content::WebContents* web_contents, int* tab_id) = 0; |
| 55 }; | 57 }; |
| 56 void SetDelegate(std::unique_ptr<RecentTabHelper::Delegate> delegate); | 58 void SetDelegate(std::unique_ptr<RecentTabHelper::Delegate> delegate); |
| 57 | 59 |
| 58 bool is_page_ready_for_snapshot() const { | 60 // Support for Download Page feature. The Download Page button does this: |
| 59 return is_page_ready_for_snapshot_; | 61 // 1. Creates suspended request for Background Offliner. |
| 60 } | 62 // 2. Calls this method with properly filled ClientId. |
| 63 // 3. This tab helper observes the page load and captures the page |
| 64 // if the load progresses far enough, as indicated by SnapshotController. |
| 65 // 4. If capture is successful, this tab helper removes the suspended request. |
| 66 // Otherwise (navigation to other page, close tab) tab helper un-suspends |
| 67 // the request so the Background Offliner starts working on it. |
| 68 // 5. If Chrome is killed at any point, next time Background Offliner loads |
| 69 // it converts all suspended requests from last session into active. |
| 70 void ObserveAndDownloadCurrentPage(const ClientId& client_id, |
| 71 int64_t request_id); |
| 61 | 72 |
| 62 private: | 73 private: |
| 74 // Keeps client_id/request_id that will be used for the offline snapshot. |
| 75 struct DownloadPageInfo { |
| 76 public: |
| 77 DownloadPageInfo(const ClientId& client_id, |
| 78 int64_t request_id) |
| 79 : client_id_(client_id), |
| 80 request_id_(request_id), |
| 81 page_snapshot_completed_(false) {} |
| 82 |
| 83 // The ClientID to go with the offline page. |
| 84 ClientId client_id_; |
| 85 // Id of the suspended request in Background Offliner. Used to un-suspend |
| 86 // the request if the capture of the current page was not possible (e.g. |
| 87 // the user navigated to another page before current one was loaded). |
| 88 // 0 if this is a "last_1" info. |
| 89 int64_t request_id_; |
| 90 // True if there was at least one snapshot successfully completed. |
| 91 bool page_snapshot_completed_; |
| 92 }; |
| 93 |
| 63 explicit RecentTabHelper(content::WebContents* web_contents); | 94 explicit RecentTabHelper(content::WebContents* web_contents); |
| 64 friend class content::WebContentsUserData<RecentTabHelper>; | 95 friend class content::WebContentsUserData<RecentTabHelper>; |
| 65 | 96 |
| 66 | |
| 67 void EnsureInitialized(); | 97 void EnsureInitialized(); |
| 68 void ContinueSnapshotWithIdsToPurge(const std::vector<int64_t>& page_ids); | 98 void ContinueSnapshotWithIdsToPurge(const std::vector<int64_t>& page_ids); |
| 69 void ContinueSnapshotAfterPurge(OfflinePageModel::DeletePageResult result); | 99 void ContinueSnapshotAfterPurge(OfflinePageModel::DeletePageResult result); |
| 70 void SavePageCallback(OfflinePageModel::SavePageResult result, | 100 void SavePageCallback(OfflinePageModel::SavePageResult result, |
| 71 int64_t offline_id); | 101 int64_t offline_id); |
| 72 | |
| 73 void ReportSnapshotCompleted(); | 102 void ReportSnapshotCompleted(); |
| 103 void ReportDownloadStatusToRequestCoordinator(); |
| 74 bool IsSamePage() const; | 104 bool IsSamePage() const; |
| 75 ClientId client_id() const; | 105 ClientId GetRecentPagesClientId() const; |
| 76 | 106 |
| 77 // 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 |
| 78 // case when tab is in incognito profile. | 108 // case when tab is in incognito profile. |
| 79 OfflinePageModel* page_model_; | 109 OfflinePageModel* page_model_; |
| 110 |
| 80 // If false, never make snapshots off the attached WebContents. | 111 // If false, never make snapshots off the attached WebContents. |
| 81 // Not page-specific. | 112 // Not page-specific. |
| 82 bool snapshots_enabled_; | 113 bool snapshots_enabled_; |
| 114 |
| 83 // Becomes true during navigation if the page is ready for snapshot as | 115 // Becomes true during navigation if the page is ready for snapshot as |
| 84 // indicated by at least one callback from SnapshotController. | 116 // indicated by at least one callback from SnapshotController. |
| 85 bool is_page_ready_for_snapshot_; | 117 bool is_page_ready_for_snapshot_; |
| 118 |
| 119 // Info for the offline page to capture. Null if the tab is not capturing |
| 120 // current page. |
| 121 std::unique_ptr<DownloadPageInfo> download_info_; |
| 122 |
| 86 // If empty, the tab does not have AndroidId and can not capture pages. | 123 // If empty, the tab does not have AndroidId and can not capture pages. |
| 87 std::string tab_id_; | 124 std::string tab_id_; |
| 88 | 125 |
| 89 // The URL of the page that is currently being snapshotted. Used to check, | 126 // The URL of the page that is currently being snapshotted. Used to check, |
| 90 // during async operations, that WebContents still contains the same page. | 127 // during async operations, that WebContents still contains the same page. |
| 91 GURL snapshot_url_; | 128 GURL snapshot_url_; |
| 92 // This starts out null and used as a flag for EnsureInitialized() to do the | 129 // This starts out null and used as a flag for EnsureInitialized() to do the |
| 93 // initialization only once. | 130 // initialization only once. |
| 94 std::unique_ptr<SnapshotController> snapshot_controller_; | 131 std::unique_ptr<SnapshotController> snapshot_controller_; |
| 95 | 132 |
| 96 std::unique_ptr<Delegate> delegate_; | 133 std::unique_ptr<Delegate> delegate_; |
| 97 | 134 |
| 98 base::WeakPtrFactory<RecentTabHelper> weak_ptr_factory_; | 135 base::WeakPtrFactory<RecentTabHelper> weak_ptr_factory_; |
| 99 | 136 |
| 100 DISALLOW_COPY_AND_ASSIGN(RecentTabHelper); | 137 DISALLOW_COPY_AND_ASSIGN(RecentTabHelper); |
| 101 }; | 138 }; |
| 102 | 139 |
| 103 } // namespace offline_pages | 140 } // namespace offline_pages |
| 104 #endif // CHROME_BROWSER_ANDROID_OFFLINE_PAGES_RECENT_TAB_HELPER_H_ | 141 #endif // CHROME_BROWSER_ANDROID_OFFLINE_PAGES_RECENT_TAB_HELPER_H_ |
| OLD | NEW |