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

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

Issue 2362033002: Showing previews UI for Offline Previews (Closed)
Patch Set: megjablon comments rebase Created 4 years, 2 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 "chrome/browser/loader/chrome_navigation_data.h"
13 #include "chrome/browser/previews/previews_infobar_tab_helper.h"
14 #include "components/offline_pages/loaded_offline_page_info.h"
12 #include "components/offline_pages/offline_page_item.h" 15 #include "components/offline_pages/offline_page_item.h"
13 #include "content/public/browser/browser_thread.h" 16 #include "content/public/browser/browser_thread.h"
14 #include "content/public/browser/navigation_controller.h" 17 #include "content/public/browser/navigation_controller.h"
15 #include "content/public/browser/navigation_entry.h" 18 #include "content/public/browser/navigation_entry.h"
16 #include "content/public/browser/navigation_handle.h" 19 #include "content/public/browser/navigation_handle.h"
17 #include "content/public/browser/render_frame_host.h" 20 #include "content/public/browser/render_frame_host.h"
18 #include "content/public/browser/web_contents.h" 21 #include "content/public/browser/web_contents.h"
19 #include "ui/base/page_transition_types.h" 22 #include "ui/base/page_transition_types.h"
20 23
21 DEFINE_WEB_CONTENTS_USER_DATA_KEY(offline_pages::OfflinePageTabHelper); 24 DEFINE_WEB_CONTENTS_USER_DATA_KEY(offline_pages::OfflinePageTabHelper);
22 25
23 namespace offline_pages { 26 namespace offline_pages {
24 27
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
35 OfflinePageTabHelper::OfflinePageTabHelper(content::WebContents* web_contents) 28 OfflinePageTabHelper::OfflinePageTabHelper(content::WebContents* web_contents)
36 : content::WebContentsObserver(web_contents), 29 : content::WebContentsObserver(web_contents),
37 weak_ptr_factory_(this) { 30 weak_ptr_factory_(this) {
38 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); 31 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
39 } 32 }
40 33
41 OfflinePageTabHelper::~OfflinePageTabHelper() {} 34 OfflinePageTabHelper::~OfflinePageTabHelper() {}
42 35
43 void OfflinePageTabHelper::DidStartNavigation( 36 void OfflinePageTabHelper::DidStartNavigation(
44 content::NavigationHandle* navigation_handle) { 37 content::NavigationHandle* navigation_handle) {
45 // Skips non-main frame. 38 // Skips non-main frame.
46 if (!navigation_handle->IsInMainFrame()) 39 if (!navigation_handle->IsInMainFrame())
47 return; 40 return;
48 41
49 // This is a new navigation so we can invalidate any previously scheduled 42 // This is a new navigation so we can invalidate any previously scheduled
50 // operations. 43 // operations.
51 weak_ptr_factory_.InvalidateWeakPtrs(); 44 weak_ptr_factory_.InvalidateWeakPtrs();
52 reloading_url_on_net_error_ = false; 45 reloading_url_on_net_error_ = false;
53 46
54 // The provisional offline info can be cleared no matter how.
55 provisional_offline_info_.Clear();
56
57 // If not a fragment navigation, clear the cached offline info. 47 // If not a fragment navigation, clear the cached offline info.
58 if (offline_info_.offline_page.get()) { 48 if (offline_info_ && offline_info_->offline_page()) {
59 GURL::Replacements remove_params; 49 GURL::Replacements remove_params;
60 remove_params.ClearRef(); 50 remove_params.ClearRef();
61 GURL offline_url = 51 GURL offline_url =
62 offline_info_.offline_page->url.ReplaceComponents(remove_params); 52 offline_info_->offline_page()->url.ReplaceComponents(remove_params);
63 GURL navigated_url = 53 GURL navigated_url =
64 navigation_handle->GetURL().ReplaceComponents(remove_params); 54 navigation_handle->GetURL().ReplaceComponents(remove_params);
65 55
66 if (offline_url != navigated_url) 56 if (offline_url != navigated_url)
67 offline_info_.Clear(); 57 offline_info_.reset();
68 } 58 }
69 } 59 }
70 60
71 void OfflinePageTabHelper::DidFinishNavigation( 61 void OfflinePageTabHelper::DidFinishNavigation(
72 content::NavigationHandle* navigation_handle) { 62 content::NavigationHandle* navigation_handle) {
73 // Skips non-main frame. 63 // Skips non-main frame.
74 if (!navigation_handle->IsInMainFrame()) 64 if (!navigation_handle->IsInMainFrame())
75 return; 65 return;
76 66
77 if (!navigation_handle->HasCommitted()) 67 if (!navigation_handle->HasCommitted())
78 return; 68 return;
79 69
80 if (navigation_handle->IsSamePage()) 70 if (navigation_handle->IsSamePage())
81 return; 71 return;
82 72
83 GURL navigated_url = navigation_handle->GetURL(); 73 GURL navigated_url = navigation_handle->GetURL();
84 if (navigation_handle->IsErrorPage()) { 74 if (navigation_handle->IsErrorPage()) {
85 offline_info_.Clear(); 75 offline_info_.reset();
86 } else { 76 } else {
87 // The provisional offline info can now be committed if the navigation is 77 // The NavigationHandle's offline info can now be copied if the navigation
88 // done without error. 78 // is done without error.
89 DCHECK(!provisional_offline_info_.offline_page || 79 ChromeNavigationData* chrome_navigation_data =
90 navigated_url == provisional_offline_info_.offline_page->url); 80 ChromeNavigationData::GetForNavigationHandle(navigation_handle);
91 offline_info_.offline_page = 81 if (chrome_navigation_data) {
92 std::move(provisional_offline_info_.offline_page); 82 LoadedOfflinePageInfo* info =
93 offline_info_.offline_header = provisional_offline_info_.offline_header; 83 chrome_navigation_data->GetLoadedOfflinePageInfo();
94 offline_info_.is_offline_preview = 84 if (info) {
95 provisional_offline_info_.is_offline_preview; 85 DCHECK(!info->offline_page() ||
86 navigated_url == info->offline_page()->url);
87 offline_info_ = info->DeepCopy();
88 }
89 }
96 } 90 }
97 provisional_offline_info_.Clear();
98 91
99 // We might be reloading the URL in order to fetch the offline page. 92 // We might be reloading the URL in order to fetch the offline page.
100 // * If successful, nothing to do. 93 // * If successful, nothing to do.
101 // * Otherwise, we're hitting error again. Bail out to avoid loop. 94 // * Otherwise, we're hitting error again. Bail out to avoid loop.
102 if (reloading_url_on_net_error_) 95 if (reloading_url_on_net_error_)
103 return; 96 return;
104 97
105 // When the navigation starts, the request might be intercepted to serve the 98 // When the navigation starts, the request might be intercepted to serve the
106 // offline content if the network is detected to be in disconnected or poor 99 // offline content if the network is detected to be in disconnected or poor
107 // conditions. This detection might not work for some cases, i.e., connected 100 // conditions. This detection might not work for some cases, i.e., connected
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
154 147
155 // Reloads the page with extra header set to force loading the offline page. 148 // Reloads the page with extra header set to force loading the offline page.
156 content::NavigationController::LoadURLParams load_params(offline_page->url); 149 content::NavigationController::LoadURLParams load_params(offline_page->url);
157 load_params.transition_type = ui::PAGE_TRANSITION_RELOAD; 150 load_params.transition_type = ui::PAGE_TRANSITION_RELOAD;
158 OfflinePageHeader offline_header; 151 OfflinePageHeader offline_header;
159 offline_header.reason = OfflinePageHeader::Reason::NET_ERROR; 152 offline_header.reason = OfflinePageHeader::Reason::NET_ERROR;
160 load_params.extra_headers = offline_header.GetCompleteHeaderString(); 153 load_params.extra_headers = offline_header.GetCompleteHeaderString();
161 web_contents()->GetController().LoadURLWithParams(load_params); 154 web_contents()->GetController().LoadURLWithParams(load_params);
162 } 155 }
163 156
164 // This is a callback from network request interceptor. It happens between
165 // DidStartNavigation and DidFinishNavigation calls on this tab helper.
166 void OfflinePageTabHelper::SetOfflinePage(
167 const OfflinePageItem& offline_page,
168 const OfflinePageHeader& offline_header,
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;
174 }
175
176 const OfflinePageItem* OfflinePageTabHelper::GetOfflinePageForTest() const {
177 return provisional_offline_info_.offline_page.get();
178 }
179
180 } // namespace offline_pages 157 } // namespace offline_pages
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698