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

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: Fix junit test 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() const { return offline_page_.get(); } 31 const OfflinePageItem* offline_page() { return offline_page_.get(); }
32 void SetOfflinePage(const OfflinePageItem& offline_page,
33 bool is_offline_preview);
72 34
73 // Whether the page is an offline preview. 35 // Whether the page is an offline preview.
74 bool is_offline_preview() const { return is_offline_preview_; } 36 bool is_offline_preview() const { return is_offline_preview_; }
75 37
76 private: 38 private:
77 friend class content::WebContentsUserData<OfflinePageTabHelper>; 39 friend class content::WebContentsUserData<OfflinePageTabHelper>;
78 friend class OfflinePageTabHelperTest;
79 FRIEND_TEST_ALL_PREFIXES(OfflinePageTabHelperTest,
80 NewNavigationCancelsPendingRedirects);
81 40
82 explicit OfflinePageTabHelper(content::WebContents* web_contents); 41 explicit OfflinePageTabHelper(content::WebContents* web_contents);
83 42
84 void SetDelegateForTesting(std::unique_ptr<Delegate> delegate);
85
86 // Overridden from content::WebContentsObserver: 43 // Overridden from content::WebContentsObserver:
87 void DidStartNavigation( 44 void DidStartNavigation(
88 content::NavigationHandle* navigation_handle) override; 45 content::NavigationHandle* navigation_handle) override;
89 void DidFinishNavigation( 46 void DidFinishNavigation(
90 content::NavigationHandle* navigation_handle) override; 47 content::NavigationHandle* navigation_handle) override;
91 48
92 void RedirectToOnline(const GURL& from_url, 49 void SelectPageForOnlineURLDone(const OfflinePageItem* offline_page);
93 const OfflinePageItem* offline_page);
94 50
95 // 3 step redirection to the offline page. First getting all the pages, then 51 // The cached copy of OfflinePageItem if offline page is loaded for current
96 // selecting appropriate page to redirect to and finally attempting to 52 // tab. This can be used to by the Tab to synchronously ask about the offline
97 // redirect to that offline page, and caching metadata of that page locally. 53 // info.
98 // RedirectResult is accumulated along the codepath to reflect the overall 54 std::unique_ptr<OfflinePageItem> offline_page_;
99 // result of redirection - and be reported to UMA at the end.
100 void GetBestPageForRedirectToOffline(RedirectResult result,
101 const GURL& online_url);
102 void SelectPageForOnlineURLDone(
103 RedirectResult result,
104 const GURL& online_url,
105 const OfflinePageItem* offline_page);
106 void TryRedirectToOffline(RedirectResult result,
107 const GURL& from_url,
108 const OfflinePageItem& offline_page);
109 55
110 void Redirect(const GURL& from_url, const GURL& to_url); 56 bool reloading_url_on_net_error_ = false;
111
112 // Returns true if a given URL is in redirect chain already.
113 bool IsInRedirectLoop(const GURL& to_url) const;
114
115 void ReportRedirectResultUMA(RedirectResult result);
116
117 // Iff the tab we are associated with is redirected to an offline page,
118 // |offline_page_| will be non-null. This can be used to synchronously ask
119 // about the offline state of the current web contents.
120 std::unique_ptr<OfflinePageItem> offline_page_;
121 std::unique_ptr<Delegate> delegate_;
122 57
123 // Whether the page is an offline preview. Offline page previews are shown 58 // Whether the page is an offline preview. Offline page previews are shown
124 // when a user's effective connection type is prohibitively slow. 59 // when a user's effective connection type is prohibitively slow.
125 bool is_offline_preview_; 60 bool is_offline_preview_;
126 61
127 base::WeakPtrFactory<OfflinePageTabHelper> weak_ptr_factory_; 62 base::WeakPtrFactory<OfflinePageTabHelper> weak_ptr_factory_;
128 63
129 DISALLOW_COPY_AND_ASSIGN(OfflinePageTabHelper); 64 DISALLOW_COPY_AND_ASSIGN(OfflinePageTabHelper);
130 }; 65 };
131 66
132 } // namespace offline_pages 67 } // namespace offline_pages
133 68
134 #endif // CHROME_BROWSER_ANDROID_OFFLINE_PAGES_OFFLINE_PAGE_TAB_HELPER_H_ 69 #endif // CHROME_BROWSER_ANDROID_OFFLINE_PAGES_OFFLINE_PAGE_TAB_HELPER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698