| 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_OFFLINE_PAGE_TAB_HELPER_H_ | 5 #ifndef CHROME_BROWSER_ANDROID_OFFLINE_PAGES_OFFLINE_PAGE_TAB_HELPER_H_ |
| 6 #define CHROME_BROWSER_ANDROID_OFFLINE_PAGES_OFFLINE_PAGE_TAB_HELPER_H_ | 6 #define CHROME_BROWSER_ANDROID_OFFLINE_PAGES_OFFLINE_PAGE_TAB_HELPER_H_ |
| 7 | 7 |
| 8 #include "base/macros.h" | 8 #include "base/macros.h" |
| 9 #include "base/memory/weak_ptr.h" | 9 #include "base/memory/weak_ptr.h" |
| 10 #include "components/offline_pages/offline_page_types.h" | 10 #include "components/offline_pages/offline_page_types.h" |
| 11 #include "content/public/browser/web_contents_observer.h" | 11 #include "content/public/browser/web_contents_observer.h" |
| 12 #include "content/public/browser/web_contents_user_data.h" | 12 #include "content/public/browser/web_contents_user_data.h" |
| 13 #include "url/gurl.h" | 13 #include "url/gurl.h" |
| 14 | 14 |
| 15 namespace content { | 15 namespace content { |
| 16 class WebContents; | 16 class WebContents; |
| 17 } | 17 } |
| 18 | 18 |
| 19 namespace offline_pages { | 19 namespace offline_pages { |
| 20 | 20 |
| 21 struct OfflinePageItem; | 21 struct OfflinePageItem; |
| 22 | 22 |
| 23 // Per-tab class to manage switch between online version and offline version. | 23 // Per-tab class to manage switch between online version and offline version. |
| 24 class OfflinePageTabHelper : | 24 class OfflinePageTabHelper : |
| 25 public content::WebContentsObserver, | 25 public content::WebContentsObserver, |
| 26 public content::WebContentsUserData<OfflinePageTabHelper> { | 26 public content::WebContentsUserData<OfflinePageTabHelper> { |
| 27 public: | 27 public: |
| 28 // Delegate that is used to better handle external dependencies. |
| 29 // Default implementation is in .cc file, while tests provide an override. |
| 30 class Delegate { |
| 31 public: |
| 32 virtual ~Delegate() {} |
| 33 virtual bool GetTabId(content::WebContents* web_contents, |
| 34 std::string* tab_id) const = 0; |
| 35 }; |
| 36 |
| 28 ~OfflinePageTabHelper() override; | 37 ~OfflinePageTabHelper() override; |
| 29 | 38 |
| 30 const OfflinePageItem* offline_page() { return offline_page_.get(); } | 39 const OfflinePageItem* offline_page() { return offline_page_.get(); } |
| 31 | 40 |
| 32 private: | 41 private: |
| 33 enum class RedirectReason { | 42 enum class RedirectReason { |
| 34 DISCONNECTED_NETWORK, | 43 DISCONNECTED_NETWORK, |
| 35 FLAKY_NETWORK, | 44 FLAKY_NETWORK, |
| 36 FLAKY_NETWORK_FORWARD_BACK | 45 FLAKY_NETWORK_FORWARD_BACK |
| 37 }; | 46 }; |
| 38 | 47 |
| 39 friend class content::WebContentsUserData<OfflinePageTabHelper>; | 48 friend class content::WebContentsUserData<OfflinePageTabHelper>; |
| 40 friend class OfflinePageTabHelperTest; | 49 friend class OfflinePageTabHelperTest; |
| 41 FRIEND_TEST_ALL_PREFIXES(OfflinePageTabHelperTest, | 50 FRIEND_TEST_ALL_PREFIXES(OfflinePageTabHelperTest, |
| 42 NewNavigationCancelsPendingRedirects); | 51 NewNavigationCancelsPendingRedirects); |
| 43 | 52 |
| 44 explicit OfflinePageTabHelper(content::WebContents* web_contents); | 53 explicit OfflinePageTabHelper(content::WebContents* web_contents); |
| 45 | 54 |
| 55 void SetDelegateForTesting(std::unique_ptr<Delegate> delegate); |
| 56 |
| 46 // Overridden from content::WebContentsObserver: | 57 // Overridden from content::WebContentsObserver: |
| 47 void DidStartNavigation( | 58 void DidStartNavigation( |
| 48 content::NavigationHandle* navigation_handle) override; | 59 content::NavigationHandle* navigation_handle) override; |
| 49 void DidFinishNavigation( | 60 void DidFinishNavigation( |
| 50 content::NavigationHandle* navigation_handle) override; | 61 content::NavigationHandle* navigation_handle) override; |
| 51 | 62 |
| 52 void RedirectToOnline(const GURL& from_url, | 63 void RedirectToOnline(const GURL& from_url, |
| 53 const OfflinePageItem* offline_page); | 64 const OfflinePageItem* offline_page); |
| 65 |
| 66 // 3 step redirection to the offline page. First getting all the pages, then |
| 67 // selecting appropriate page to redirect to and finally attempting to |
| 68 // redirect to that offline page, and caching metadata of that page locally. |
| 69 void GetPagesForRedirectToOffline(const GURL& online_url, |
| 70 RedirectReason reason); |
| 71 void SelectBestPageForRedirectToOffline( |
| 72 const GURL& online_url, |
| 73 RedirectReason reason, |
| 74 const MultipleOfflinePageItemResult& pages); |
| 54 void TryRedirectToOffline(RedirectReason redirect_reason, | 75 void TryRedirectToOffline(RedirectReason redirect_reason, |
| 55 const GURL& from_url, | 76 const GURL& from_url, |
| 56 const OfflinePageItem* offline_page); | 77 const OfflinePageItem& offline_page); |
| 57 | 78 |
| 58 void Redirect(const GURL& from_url, const GURL& to_url); | 79 void Redirect(const GURL& from_url, const GURL& to_url); |
| 59 | 80 |
| 60 // Iff the tab we are associated with is redirected to an offline page, | 81 // Iff the tab we are associated with is redirected to an offline page, |
| 61 // |offline_page_| will be non-null. This can be used to synchronously ask | 82 // |offline_page_| will be non-null. This can be used to synchronously ask |
| 62 // about the offline state of the current web contents. | 83 // about the offline state of the current web contents. |
| 63 std::unique_ptr<OfflinePageItem> offline_page_; | 84 std::unique_ptr<OfflinePageItem> offline_page_; |
| 85 std::unique_ptr<Delegate> delegate_; |
| 64 base::WeakPtrFactory<OfflinePageTabHelper> weak_ptr_factory_; | 86 base::WeakPtrFactory<OfflinePageTabHelper> weak_ptr_factory_; |
| 65 | 87 |
| 66 DISALLOW_COPY_AND_ASSIGN(OfflinePageTabHelper); | 88 DISALLOW_COPY_AND_ASSIGN(OfflinePageTabHelper); |
| 67 }; | 89 }; |
| 68 | 90 |
| 69 } // namespace offline_pages | 91 } // namespace offline_pages |
| 70 | 92 |
| 71 #endif // CHROME_BROWSER_ANDROID_OFFLINE_PAGES_OFFLINE_PAGE_TAB_HELPER_H_ | 93 #endif // CHROME_BROWSER_ANDROID_OFFLINE_PAGES_OFFLINE_PAGE_TAB_HELPER_H_ |
| OLD | NEW |