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