| 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 "base/time/time.h" |
| 10 #include "components/offline_pages/offline_page_types.h" | 11 #include "components/offline_pages/offline_page_types.h" |
| 11 #include "content/public/browser/web_contents_observer.h" | 12 #include "content/public/browser/web_contents_observer.h" |
| 12 #include "content/public/browser/web_contents_user_data.h" | 13 #include "content/public/browser/web_contents_user_data.h" |
| 13 #include "url/gurl.h" | 14 #include "url/gurl.h" |
| 14 | 15 |
| 15 namespace content { | 16 namespace content { |
| 16 class WebContents; | 17 class WebContents; |
| 17 } | 18 } |
| 18 | 19 |
| 19 namespace offline_pages { | 20 namespace offline_pages { |
| 20 | 21 |
| 21 struct OfflinePageItem; | 22 struct OfflinePageItem; |
| 22 | 23 |
| 23 // Per-tab class to manage switch between online version and offline version. | 24 // Per-tab class to manage switch between online version and offline version. |
| 24 class OfflinePageTabHelper : | 25 class OfflinePageTabHelper : |
| 25 public content::WebContentsObserver, | 26 public content::WebContentsObserver, |
| 26 public content::WebContentsUserData<OfflinePageTabHelper> { | 27 public content::WebContentsUserData<OfflinePageTabHelper> { |
| 27 public: | 28 public: |
| 28 // Delegate that is used to better handle external dependencies. | 29 // Delegate that is used to better handle external dependencies. |
| 29 // Default implementation is in .cc file, while tests provide an override. | 30 // Default implementation is in .cc file, while tests provide an override. |
| 30 class Delegate { | 31 class Delegate { |
| 31 public: | 32 public: |
| 32 virtual ~Delegate() {} | 33 virtual ~Delegate() {} |
| 33 virtual bool GetTabId(content::WebContents* web_contents, | 34 virtual bool GetTabId(content::WebContents* web_contents, |
| 34 std::string* tab_id) const = 0; | 35 std::string* tab_id) const = 0; |
| 36 virtual base::Time Now() const = 0; |
| 35 }; | 37 }; |
| 36 | 38 |
| 37 // This enum is used for UMA reporting. It contains all possible outcomes of | 39 // This enum is used for UMA reporting. It contains all possible outcomes of |
| 38 // redirect intent and result. Generally one of these outcomes will happen. | 40 // redirect intent and result. Generally one of these outcomes will happen. |
| 39 // The fringe errors (like no OfflinePageModel, etc.) are not reported due | 41 // The fringe errors (like no OfflinePageModel, etc.) are not reported due |
| 40 // to their low probability. | 42 // to their low probability. |
| 41 // NOTE: because this is used for UMA reporting, these values should not be | 43 // NOTE: because this is used for UMA reporting, these values should not be |
| 42 // changed or reused; new values should be ended immediately before the MAX | 44 // changed or reused; new values should be ended immediately before the MAX |
| 43 // value. Make sure to update the histogram enum | 45 // value. Make sure to update the histogram enum |
| 44 // (OfflinePagesRedirectResult in histograms.xml) accordingly. | 46 // (OfflinePagesRedirectResult in histograms.xml) accordingly. |
| 45 // Public for testing. | 47 // Public for testing. |
| 46 enum class RedirectResult { | 48 enum class RedirectResult { |
| 47 REDIRECTED_ON_DISCONNECTED_NETWORK = 0, | 49 REDIRECTED_ON_DISCONNECTED_NETWORK = 0, |
| 48 PAGE_NOT_FOUND_ON_DISCONNECTED_NETWORK = 1, | 50 PAGE_NOT_FOUND_ON_DISCONNECTED_NETWORK = 1, |
| 51 // Flaky Network means the network reported an error when trying to fetch |
| 52 // the resource. |
| 49 REDIRECTED_ON_FLAKY_NETWORK = 2, | 53 REDIRECTED_ON_FLAKY_NETWORK = 2, |
| 50 PAGE_NOT_FOUND_ON_FLAKY_NETWORK = 3, | 54 PAGE_NOT_FOUND_ON_FLAKY_NETWORK = 3, |
| 51 IGNORED_FLAKY_NETWORK_FORWARD_BACK = 4, | 55 IGNORED_FLAKY_NETWORK_FORWARD_BACK = 4, |
| 52 REDIRECTED_ON_CONNECTED_NETWORK = 5, | 56 REDIRECTED_ON_CONNECTED_NETWORK = 5, |
| 53 NO_TAB_ID = 6, | 57 NO_TAB_ID = 6, |
| 54 SHOW_NET_ERROR_PAGE = 7, | 58 SHOW_NET_ERROR_PAGE = 7, |
| 55 REDIRECT_LOOP_OFFLINE = 8, | 59 REDIRECT_LOOP_OFFLINE = 8, |
| 56 REDIRECT_LOOP_ONLINE = 9, | 60 REDIRECT_LOOP_ONLINE = 9, |
| 61 // Prohibitively slow means that the NetworkQualityEstimator reported a |
| 62 // connection slow enough to warrant showing an offline page if available. |
| 63 REDIRECTED_ON_PROHIBITIVELY_SLOW_NETWORK = 10, |
| 64 PAGE_NOT_FOUND_ON_PROHIBITIVELY_SLOW_NETWORK = 11, |
| 65 PAGE_NOT_FRESH_ON_PROHIBITIVELY_SLOW_NETWORK = 12, |
| 57 REDIRECT_RESULT_MAX, | 66 REDIRECT_RESULT_MAX, |
| 58 }; | 67 }; |
| 59 | 68 |
| 60 ~OfflinePageTabHelper() override; | 69 ~OfflinePageTabHelper() override; |
| 61 | 70 |
| 62 const OfflinePageItem* offline_page() { return offline_page_.get(); } | 71 const OfflinePageItem* offline_page() { return offline_page_.get(); } |
| 63 | 72 |
| 64 private: | 73 private: |
| 65 friend class content::WebContentsUserData<OfflinePageTabHelper>; | 74 friend class content::WebContentsUserData<OfflinePageTabHelper>; |
| 66 friend class OfflinePageTabHelperTest; | 75 friend class OfflinePageTabHelperTest; |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 100 // Returns true if a given URL is in redirect chain already. | 109 // Returns true if a given URL is in redirect chain already. |
| 101 bool IsInRedirectLoop(const GURL& to_url) const; | 110 bool IsInRedirectLoop(const GURL& to_url) const; |
| 102 | 111 |
| 103 void ReportRedirectResultUMA(RedirectResult result); | 112 void ReportRedirectResultUMA(RedirectResult result); |
| 104 | 113 |
| 105 // Iff the tab we are associated with is redirected to an offline page, | 114 // Iff the tab we are associated with is redirected to an offline page, |
| 106 // |offline_page_| will be non-null. This can be used to synchronously ask | 115 // |offline_page_| will be non-null. This can be used to synchronously ask |
| 107 // about the offline state of the current web contents. | 116 // about the offline state of the current web contents. |
| 108 std::unique_ptr<OfflinePageItem> offline_page_; | 117 std::unique_ptr<OfflinePageItem> offline_page_; |
| 109 std::unique_ptr<Delegate> delegate_; | 118 std::unique_ptr<Delegate> delegate_; |
| 119 |
| 110 base::WeakPtrFactory<OfflinePageTabHelper> weak_ptr_factory_; | 120 base::WeakPtrFactory<OfflinePageTabHelper> weak_ptr_factory_; |
| 111 | 121 |
| 112 DISALLOW_COPY_AND_ASSIGN(OfflinePageTabHelper); | 122 DISALLOW_COPY_AND_ASSIGN(OfflinePageTabHelper); |
| 113 }; | 123 }; |
| 114 | 124 |
| 115 } // namespace offline_pages | 125 } // namespace offline_pages |
| 116 | 126 |
| 117 #endif // CHROME_BROWSER_ANDROID_OFFLINE_PAGES_OFFLINE_PAGE_TAB_HELPER_H_ | 127 #endif // CHROME_BROWSER_ANDROID_OFFLINE_PAGES_OFFLINE_PAGE_TAB_HELPER_H_ |
| OLD | NEW |