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

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

Issue 2337363002: Load live version when reloading an offline page on connected network (Closed)
Patch Set: Update BUILD.gn Created 4 years, 3 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 #include "chrome/browser/android/offline_pages/offline_page_tab_helper.h" 5 #include "chrome/browser/android/offline_pages/offline_page_tab_helper.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/logging.h" 8 #include "base/logging.h"
9 #include "base/memory/ptr_util.h" 9 #include "base/memory/ptr_util.h"
10 #include "chrome/browser/android/offline_pages/offline_page_request_job.h" 10 #include "chrome/browser/android/offline_pages/offline_page_request_job.h"
11 #include "chrome/browser/android/offline_pages/offline_page_utils.h" 11 #include "chrome/browser/android/offline_pages/offline_page_utils.h"
12 #include "components/offline_pages/offline_page_item.h" 12 #include "components/offline_pages/offline_page_item.h"
13 #include "content/public/browser/browser_thread.h" 13 #include "content/public/browser/browser_thread.h"
14 #include "content/public/browser/navigation_controller.h" 14 #include "content/public/browser/navigation_controller.h"
15 #include "content/public/browser/navigation_entry.h" 15 #include "content/public/browser/navigation_entry.h"
16 #include "content/public/browser/navigation_handle.h" 16 #include "content/public/browser/navigation_handle.h"
17 #include "content/public/browser/render_frame_host.h" 17 #include "content/public/browser/render_frame_host.h"
18 #include "content/public/browser/web_contents.h" 18 #include "content/public/browser/web_contents.h"
19 #include "ui/base/page_transition_types.h" 19 #include "ui/base/page_transition_types.h"
20 20
21 DEFINE_WEB_CONTENTS_USER_DATA_KEY(offline_pages::OfflinePageTabHelper); 21 DEFINE_WEB_CONTENTS_USER_DATA_KEY(offline_pages::OfflinePageTabHelper);
22 22
23 namespace offline_pages { 23 namespace offline_pages {
24 24
25 OfflinePageTabHelper::LoadedOfflinePageInfo::LoadedOfflinePageInfo() {}
26
27 OfflinePageTabHelper::LoadedOfflinePageInfo::~LoadedOfflinePageInfo() {}
28
29 void OfflinePageTabHelper::LoadedOfflinePageInfo::Clear() {
30 offline_page.reset();
31 offline_header.Clear();
32 is_offline_preview = false;
33 }
34
25 OfflinePageTabHelper::OfflinePageTabHelper(content::WebContents* web_contents) 35 OfflinePageTabHelper::OfflinePageTabHelper(content::WebContents* web_contents)
26 : content::WebContentsObserver(web_contents), 36 : content::WebContentsObserver(web_contents),
27 is_offline_preview_(false),
28 weak_ptr_factory_(this) { 37 weak_ptr_factory_(this) {
29 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); 38 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
30 } 39 }
31 40
32 OfflinePageTabHelper::~OfflinePageTabHelper() {} 41 OfflinePageTabHelper::~OfflinePageTabHelper() {}
33 42
34 void OfflinePageTabHelper::DidStartNavigation( 43 void OfflinePageTabHelper::DidStartNavigation(
35 content::NavigationHandle* navigation_handle) { 44 content::NavigationHandle* navigation_handle) {
36 // Skips non-main frame. 45 // Skips non-main frame.
37 if (!navigation_handle->IsInMainFrame()) 46 if (!navigation_handle->IsInMainFrame())
38 return; 47 return;
39 48
40 // This is a new navigation so we can invalidate any previously scheduled 49 // This is a new navigation so we can invalidate any previously scheduled
41 // operations. 50 // operations.
42 weak_ptr_factory_.InvalidateWeakPtrs(); 51 weak_ptr_factory_.InvalidateWeakPtrs();
52 reloading_url_on_net_error_ = false;
43 53
44 provisional_offline_page_ = nullptr; 54 // The provisional offline info can be cleared no matter how.
45 is_offline_preview_ = false; 55 provisional_offline_info_.Clear();
46 reloading_url_on_net_error_ = false; 56
57 // If not a fragment navigation, clear the cached offline info.
58 if (offline_info_.offline_page.get()) {
59 GURL::Replacements remove_params;
60 remove_params.ClearRef();
61 GURL offline_url =
62 offline_info_.offline_page->url.ReplaceComponents(remove_params);
63 GURL navigated_url =
64 navigation_handle->GetURL().ReplaceComponents(remove_params);
65
66 if (offline_url != navigated_url)
67 offline_info_.Clear();
68 }
47 } 69 }
48 70
49 void OfflinePageTabHelper::DidFinishNavigation( 71 void OfflinePageTabHelper::DidFinishNavigation(
50 content::NavigationHandle* navigation_handle) { 72 content::NavigationHandle* navigation_handle) {
51 // Skips non-main frame. 73 // Skips non-main frame.
52 if (!navigation_handle->IsInMainFrame()) 74 if (!navigation_handle->IsInMainFrame())
53 return; 75 return;
54 76
55 if (!navigation_handle->HasCommitted()) 77 if (!navigation_handle->HasCommitted())
56 return; 78 return;
57 79
58 if (navigation_handle->IsSamePage()) { 80 if (navigation_handle->IsSamePage())
59 return; 81 return;
82
83 GURL navigated_url = navigation_handle->GetURL();
84 if (navigation_handle->IsErrorPage()) {
85 offline_info_.Clear();
86 } else {
87 // The provisional offline info can now be committed if the navigation is
88 // done without error.
89 DCHECK(!provisional_offline_info_.offline_page ||
90 navigated_url == provisional_offline_info_.offline_page->url);
91 offline_info_.offline_page =
92 std::move(provisional_offline_info_.offline_page);
93 offline_info_.offline_header = provisional_offline_info_.offline_header;
94 offline_info_.is_offline_preview =
95 provisional_offline_info_.is_offline_preview;
60 } 96 }
61 97 provisional_offline_info_.Clear();
62 offline_page_ = std::move(provisional_offline_page_);
63 provisional_offline_page_ = nullptr;
64 98
65 // We might be reloading the URL in order to fetch the offline page. 99 // We might be reloading the URL in order to fetch the offline page.
66 // * If successful, nothing to do. 100 // * If successful, nothing to do.
67 // * Otherwise, we're hitting error again. Bail out to avoid loop. 101 // * Otherwise, we're hitting error again. Bail out to avoid loop.
68 if (reloading_url_on_net_error_) 102 if (reloading_url_on_net_error_)
69 return; 103 return;
70 104
71 // When the navigation starts, the request might be intercepted to serve the 105 // When the navigation starts, the request might be intercepted to serve the
72 // offline content if the network is detected to be in disconnected or poor 106 // offline content if the network is detected to be in disconnected or poor
73 // conditions. This detection might not work for some cases, i.e., connected 107 // conditions. This detection might not work for some cases, i.e., connected
(...skipping 19 matching lines...) Expand all
93 // page, so we can leave. 127 // page, so we can leave.
94 int tab_id; 128 int tab_id;
95 if (!OfflinePageUtils::GetTabId(web_contents(), &tab_id)) { 129 if (!OfflinePageUtils::GetTabId(web_contents(), &tab_id)) {
96 // No need to report NO_TAB_ID since it should have already been detected 130 // No need to report NO_TAB_ID since it should have already been detected
97 // and reported in offline page request handler. 131 // and reported in offline page request handler.
98 return; 132 return;
99 } 133 }
100 134
101 OfflinePageUtils::SelectPageForOnlineURL( 135 OfflinePageUtils::SelectPageForOnlineURL(
102 web_contents()->GetBrowserContext(), 136 web_contents()->GetBrowserContext(),
103 navigation_handle->GetURL(), 137 navigated_url,
104 tab_id, 138 tab_id,
105 base::Bind(&OfflinePageTabHelper::SelectPageForOnlineURLDone, 139 base::Bind(&OfflinePageTabHelper::SelectPageForOnlineURLDone,
106 weak_ptr_factory_.GetWeakPtr())); 140 weak_ptr_factory_.GetWeakPtr()));
107 } 141 }
108 142
109 void OfflinePageTabHelper::SelectPageForOnlineURLDone( 143 void OfflinePageTabHelper::SelectPageForOnlineURLDone(
110 const OfflinePageItem* offline_page) { 144 const OfflinePageItem* offline_page) {
111 // Bails out if no offline page is found. 145 // Bails out if no offline page is found.
112 if (!offline_page) { 146 if (!offline_page) {
113 OfflinePageRequestJob::ReportAggregatedRequestResult( 147 OfflinePageRequestJob::ReportAggregatedRequestResult(
114 OfflinePageRequestJob::AggregatedRequestResult:: 148 OfflinePageRequestJob::AggregatedRequestResult::
115 PAGE_NOT_FOUND_ON_FLAKY_NETWORK); 149 PAGE_NOT_FOUND_ON_FLAKY_NETWORK);
116 return; 150 return;
117 } 151 }
118 152
119 reloading_url_on_net_error_ = true; 153 reloading_url_on_net_error_ = true;
120 154
121 // Reloads the page with extra header set to force loading the offline page. 155 // Reloads the page with extra header set to force loading the offline page.
122 content::NavigationController::LoadURLParams load_params(offline_page->url); 156 content::NavigationController::LoadURLParams load_params(offline_page->url);
123 load_params.transition_type = ui::PAGE_TRANSITION_RELOAD; 157 load_params.transition_type = ui::PAGE_TRANSITION_RELOAD;
124 load_params.extra_headers = kOfflinePageHeader; 158 OfflinePageHeader offline_header;
125 load_params.extra_headers += ":"; 159 offline_header.reason = OfflinePageHeader::Reason::NET_ERROR;
126 load_params.extra_headers += kOfflinePageHeaderReasonKey; 160 load_params.extra_headers = offline_header.GetCompleteHeaderString();
127 load_params.extra_headers += "=";
128 load_params.extra_headers += kOfflinePageHeaderReasonValueDueToNetError;
129 web_contents()->GetController().LoadURLWithParams(load_params); 161 web_contents()->GetController().LoadURLWithParams(load_params);
130 } 162 }
131 163
132 // This is a callback from network request interceptor. It happens between 164 // This is a callback from network request interceptor. It happens between
133 // DidStartNavigation and DidFinishNavigation calls on this tab helper. 165 // DidStartNavigation and DidFinishNavigation calls on this tab helper.
134 void OfflinePageTabHelper::SetOfflinePage(const OfflinePageItem& offline_page, 166 void OfflinePageTabHelper::SetOfflinePage(
135 bool is_offline_preview) { 167 const OfflinePageItem& offline_page,
136 provisional_offline_page_ = base::MakeUnique<OfflinePageItem>(offline_page); 168 const OfflinePageHeader& offline_header,
137 is_offline_preview_ = is_offline_preview; 169 bool is_offline_preview) {
170 provisional_offline_info_.offline_page =
171 base::MakeUnique<OfflinePageItem>(offline_page);
172 provisional_offline_info_.offline_header = offline_header;
173 provisional_offline_info_.is_offline_preview = is_offline_preview;
138 } 174 }
139 175
140 const OfflinePageItem* OfflinePageTabHelper::GetOfflinePageForTest() const { 176 const OfflinePageItem* OfflinePageTabHelper::GetOfflinePageForTest() const {
141 return provisional_offline_page_.get(); 177 return provisional_offline_info_.offline_page.get();
142 } 178 }
143 179
144 } // namespace offline_pages 180 } // namespace offline_pages
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698