Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(39)

Side by Side Diff: chrome/browser/android/offline_pages/offline_page_tab_helper.h

Issue 2245733004: Serve offline page for online URL on disconnected or bad networks (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Address some more feedback Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 "base/time/time.h"
11 #include "components/offline_pages/offline_page_types.h"
12 #include "content/public/browser/web_contents_observer.h" 11 #include "content/public/browser/web_contents_observer.h"
13 #include "content/public/browser/web_contents_user_data.h" 12 #include "content/public/browser/web_contents_user_data.h"
14 #include "url/gurl.h" 13 #include "url/gurl.h"
15 14
16 namespace content { 15 namespace content {
17 class WebContents; 16 class WebContents;
18 } 17 }
19 18
20 namespace offline_pages { 19 namespace offline_pages {
21 20
22 struct OfflinePageItem; 21 struct OfflinePageItem;
23 22
24 // Per-tab class to manage switch between online version and offline version. 23 // Per-tab class that monitors the navigations and stores the necessary info
24 // to facilitate the synchronous access to offline information.
25 class OfflinePageTabHelper : 25 class OfflinePageTabHelper :
26 public content::WebContentsObserver, 26 public content::WebContentsObserver,
27 public content::WebContentsUserData<OfflinePageTabHelper> { 27 public content::WebContentsUserData<OfflinePageTabHelper> {
28 public: 28 public:
29 // Delegate that is used to better handle external dependencies.
30 // Default implementation is in .cc file, while tests provide an override.
31 class Delegate {
32 public:
33 virtual ~Delegate() {}
34 virtual bool GetTabId(content::WebContents* web_contents,
35 int* tab_id) const = 0;
36 virtual base::Time Now() const = 0;
37 };
38
39 // This enum is used for UMA reporting. It contains all possible outcomes of
40 // redirect intent and result. Generally one of these outcomes will happen.
41 // The fringe errors (like no OfflinePageModel, etc.) are not reported due
42 // to their low probability.
43 // NOTE: because this is used for UMA reporting, these values should not be
44 // changed or reused; new values should be ended immediately before the MAX
45 // value. Make sure to update the histogram enum
46 // (OfflinePagesRedirectResult in histograms.xml) accordingly.
47 // Public for testing.
48 enum class RedirectResult {
49 REDIRECTED_ON_DISCONNECTED_NETWORK = 0,
50 PAGE_NOT_FOUND_ON_DISCONNECTED_NETWORK = 1,
51 // Flaky Network means the network reported an error when trying to fetch
52 // the resource.
53 REDIRECTED_ON_FLAKY_NETWORK = 2,
54 PAGE_NOT_FOUND_ON_FLAKY_NETWORK = 3,
55 IGNORED_FLAKY_NETWORK_FORWARD_BACK = 4,
56 REDIRECTED_ON_CONNECTED_NETWORK = 5,
57 NO_TAB_ID = 6,
58 SHOW_NET_ERROR_PAGE = 7,
59 REDIRECT_LOOP_OFFLINE = 8,
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,
66 REDIRECT_RESULT_MAX,
67 };
68
69 ~OfflinePageTabHelper() override; 29 ~OfflinePageTabHelper() override;
70 30
71 const OfflinePageItem* offline_page() { return offline_page_.get(); } 31 const OfflinePageItem* offline_page() { return offline_page_.get(); }
32 void SetOfflinePage(const OfflinePageItem& offline_page);
72 33
73 private: 34 private:
74 friend class content::WebContentsUserData<OfflinePageTabHelper>; 35 friend class content::WebContentsUserData<OfflinePageTabHelper>;
75 friend class OfflinePageTabHelperTest;
76 FRIEND_TEST_ALL_PREFIXES(OfflinePageTabHelperTest,
77 NewNavigationCancelsPendingRedirects);
78 36
79 explicit OfflinePageTabHelper(content::WebContents* web_contents); 37 explicit OfflinePageTabHelper(content::WebContents* web_contents);
80 38
81 void SetDelegateForTesting(std::unique_ptr<Delegate> delegate);
82
83 // Overridden from content::WebContentsObserver: 39 // Overridden from content::WebContentsObserver:
84 void DidStartNavigation( 40 void DidStartNavigation(
85 content::NavigationHandle* navigation_handle) override; 41 content::NavigationHandle* navigation_handle) override;
86 void DidFinishNavigation( 42 void DidFinishNavigation(
87 content::NavigationHandle* navigation_handle) override; 43 content::NavigationHandle* navigation_handle) override;
88 44
89 void RedirectToOnline(const GURL& from_url, 45 void SelectPageForOnlineURLDone(const OfflinePageItem* offline_page);
90 const OfflinePageItem* offline_page);
91 46
92 // 3 step redirection to the offline page. First getting all the pages, then 47 // The cached copy of OfflinePageItem if offline page is loaded for current
93 // selecting appropriate page to redirect to and finally attempting to 48 // tab. This can be used to by the Tab to synchronously ask about the offline
94 // redirect to that offline page, and caching metadata of that page locally. 49 // info.
95 // RedirectResult is accumulated along the codepath to reflect the overall 50 std::unique_ptr<OfflinePageItem> offline_page_;
96 // result of redirection - and be reported to UMA at the end.
97 void GetBestPageForRedirectToOffline(RedirectResult result,
98 const GURL& online_url);
99 void SelectPageForOnlineURLDone(
100 RedirectResult result,
101 const GURL& online_url,
102 const OfflinePageItem* offline_page);
103 void TryRedirectToOffline(RedirectResult result,
104 const GURL& from_url,
105 const OfflinePageItem& offline_page);
106 51
107 void Redirect(const GURL& from_url, const GURL& to_url); 52 bool reloading_url_on_net_error_ = false;
108
109 // Returns true if a given URL is in redirect chain already.
110 bool IsInRedirectLoop(const GURL& to_url) const;
111
112 void ReportRedirectResultUMA(RedirectResult result);
113
114 // Iff the tab we are associated with is redirected to an offline page,
115 // |offline_page_| will be non-null. This can be used to synchronously ask
116 // about the offline state of the current web contents.
117 std::unique_ptr<OfflinePageItem> offline_page_;
118 std::unique_ptr<Delegate> delegate_;
119 53
120 base::WeakPtrFactory<OfflinePageTabHelper> weak_ptr_factory_; 54 base::WeakPtrFactory<OfflinePageTabHelper> weak_ptr_factory_;
121 55
122 DISALLOW_COPY_AND_ASSIGN(OfflinePageTabHelper); 56 DISALLOW_COPY_AND_ASSIGN(OfflinePageTabHelper);
123 }; 57 };
124 58
125 } // namespace offline_pages 59 } // namespace offline_pages
126 60
127 #endif // CHROME_BROWSER_ANDROID_OFFLINE_PAGES_OFFLINE_PAGE_TAB_HELPER_H_ 61 #endif // CHROME_BROWSER_ANDROID_OFFLINE_PAGES_OFFLINE_PAGE_TAB_HELPER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698