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

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

Issue 2506293002: Trigger redirect for offline pages (Closed)
Patch Set: Add test Created 4 years, 1 month 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_request_job.h" 5 #include "chrome/browser/android/offline_pages/offline_page_request_job.h"
6 6
7 #include <string> 7 #include <string>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/files/file_path.h" 10 #include "base/files/file_path.h"
11 #include "base/logging.h" 11 #include "base/logging.h"
12 #include "base/metrics/histogram_macros.h" 12 #include "base/metrics/histogram_macros.h"
13 #include "base/strings/string_number_conversions.h" 13 #include "base/strings/string_number_conversions.h"
14 #include "base/threading/thread_task_runner_handle.h" 14 #include "base/threading/thread_task_runner_handle.h"
15 #include "chrome/browser/android/offline_pages/offline_page_model_factory.h" 15 #include "chrome/browser/android/offline_pages/offline_page_model_factory.h"
16 #include "chrome/browser/android/offline_pages/offline_page_tab_helper.h" 16 #include "chrome/browser/android/offline_pages/offline_page_tab_helper.h"
17 #include "chrome/browser/android/offline_pages/offline_page_utils.h" 17 #include "chrome/browser/android/offline_pages/offline_page_utils.h"
18 #include "chrome/browser/browser_process.h" 18 #include "chrome/browser/browser_process.h"
19 #include "chrome/browser/profiles/profile_manager.h" 19 #include "chrome/browser/profiles/profile_manager.h"
20 #include "components/offline_pages/offline_page_model.h" 20 #include "components/offline_pages/offline_page_model.h"
21 #include "components/offline_pages/request_header/offline_page_header.h" 21 #include "components/offline_pages/request_header/offline_page_header.h"
22 #include "components/previews/core/previews_decider.h" 22 #include "components/previews/core/previews_decider.h"
23 #include "components/previews/core/previews_experiments.h" 23 #include "components/previews/core/previews_experiments.h"
24 #include "content/public/browser/browser_thread.h" 24 #include "content/public/browser/browser_thread.h"
25 #include "content/public/browser/resource_request_info.h" 25 #include "content/public/browser/resource_request_info.h"
26 #include "content/public/browser/web_contents.h" 26 #include "content/public/browser/web_contents.h"
27 #include "content/public/common/resource_type.h" 27 #include "content/public/common/resource_type.h"
28 #include "net/base/network_change_notifier.h" 28 #include "net/base/network_change_notifier.h"
29 #include "net/http/http_request_headers.h" 29 #include "net/http/http_request_headers.h"
30 #include "net/http/http_response_headers.h"
31 #include "net/http/http_util.cc"
30 #include "net/url_request/url_request.h" 32 #include "net/url_request/url_request.h"
33 #include "net/url_request/url_request_redirect_job.h"
31 34
32 namespace offline_pages { 35 namespace offline_pages {
33 36
34 namespace { 37 namespace {
35 38
36 enum class NetworkState { 39 enum class NetworkState {
37 // No network connection. 40 // No network connection.
38 DISCONNECTED_NETWORK, 41 DISCONNECTED_NETWORK,
39 // Prohibitively slow means that the NetworkQualityEstimator reported a 42 // Prohibitively slow means that the NetworkQualityEstimator reported a
40 // connection slow enough to warrant showing an offline page if available. 43 // connection slow enough to warrant showing an offline page if available.
(...skipping 519 matching lines...) Expand 10 before | Expand all | Expand 10 after
560 delegate_->GetWebContentsGetter(request()), 563 delegate_->GetWebContentsGetter(request()),
561 delegate_->GetTabIdGetter(), 564 delegate_->GetTabIdGetter(),
562 weak_ptr_factory_.GetWeakPtr())); 565 weak_ptr_factory_.GetWeakPtr()));
563 } 566 }
564 567
565 void OfflinePageRequestJob::Kill() { 568 void OfflinePageRequestJob::Kill() {
566 net::URLRequestJob::Kill(); 569 net::URLRequestJob::Kill();
567 weak_ptr_factory_.InvalidateWeakPtrs(); 570 weak_ptr_factory_.InvalidateWeakPtrs();
568 } 571 }
569 572
573 bool OfflinePageRequestJob::IsRedirectResponse(GURL* location,
574 int* http_status_code) {
575 // Invoke the default implementation in URLRequestJob which will process the
576 // redirect if GetResponseCode sets the redirect headers.
577 return URLRequestJob::IsRedirectResponse(location, http_status_code);
578 }
mmenke 2016/11/17 18:42:26 If you're relying on the default implementation, d
jianli 2016/11/17 23:42:05 We need this because default implementation goes t
579
580 void OfflinePageRequestJob::GetResponseInfo(net::HttpResponseInfo* info) {
581 if (!fake_headers_for_redirect_.get()) {
fgorski 2016/11/17 17:37:48 this should be enough: if (!fake_headers_for_redi
jianli 2016/11/17 23:42:05 Done.
582 URLRequestFileJob::GetResponseInfo(info);
583 return;
584 }
585
586 info->headers = fake_headers_for_redirect_;
587 info->request_time = redirect_response_time_;
588 info->response_time = redirect_response_time_;
589 }
590
591 void OfflinePageRequestJob::GetLoadTimingInfo(
592 net::LoadTimingInfo* load_timing_info) const {
593 // Set send_start and send_end to receive_redirect_headers_end_ to be
594 // consistent with network cache behavior.
595 load_timing_info->send_start = receive_redirect_headers_end_;
596 load_timing_info->send_end = receive_redirect_headers_end_;
597 load_timing_info->receive_headers_end = receive_redirect_headers_end_;
598 }
599
600 bool OfflinePageRequestJob::CopyFragmentOnRedirect(const GURL& location) const {
601 return false;
602 }
603
604 int OfflinePageRequestJob::GetResponseCode() const {
605 if (!fake_headers_for_redirect_.get())
fgorski 2016/11/17 17:37:48 ditto
jianli 2016/11/17 23:42:05 Done.
606 return URLRequestFileJob::GetResponseCode();
607
608 return net::URLRequestRedirectJob::REDIRECT_302_FOUND;
609 }
610
570 void OfflinePageRequestJob::FallbackToDefault() { 611 void OfflinePageRequestJob::FallbackToDefault() {
571 OfflinePageRequestInfo* info = 612 OfflinePageRequestInfo* info =
572 OfflinePageRequestInfo::GetFromRequest(request()); 613 OfflinePageRequestInfo::GetFromRequest(request());
573 DCHECK(info); 614 DCHECK(info);
574 info->set_use_default(true); 615 info->set_use_default(true);
575 616
576 URLRequestJob::NotifyRestartRequired(); 617 URLRequestJob::NotifyRestartRequired();
577 } 618 }
578 619
579 void OfflinePageRequestJob::OnOfflineFilePathAvailable( 620 void OfflinePageRequestJob::OnOfflineFilePathAvailable(
580 const base::FilePath& offline_file_path) { 621 const base::FilePath& offline_file_path) {
581 // If offline file path is empty, it means that offline page cannot be found 622 // If offline file path is empty, it means that offline page cannot be found
582 // and we want to restart the job to fall back to the default handling. 623 // and we want to restart the job to fall back to the default handling.
583 if (offline_file_path.empty()) { 624 if (offline_file_path.empty()) {
584 FallbackToDefault(); 625 FallbackToDefault();
585 return; 626 return;
586 } 627 }
587 628
588 // Sets the file path and lets URLRequestFileJob start to read from the file. 629 // Sets the file path and lets URLRequestFileJob start to read from the file.
589 file_path_ = offline_file_path; 630 file_path_ = offline_file_path;
590 URLRequestFileJob::Start(); 631 URLRequestFileJob::Start();
591 } 632 }
592 633
593 void OfflinePageRequestJob::OnOfflineRedirectAvailabe( 634 void OfflinePageRequestJob::OnOfflineRedirectAvailabe(
594 const GURL& redirected_url) { 635 const GURL& redirected_url) {
595 // TODO(jianli): kicks off the redirect. For now, use the default. 636 receive_redirect_headers_end_ = base::TimeTicks::Now();
596 FallbackToDefault(); 637 redirect_response_time_ = base::Time::Now();
638
639 std::string header_string =
640 base::StringPrintf("HTTP/1.1 %i Internal Redirect\n"
641 // Clear referrer to avoid leak when going online.
642 "Referrer-Policy: no-referrer\n"
643 "Location: %s\n"
644 "Non-Authoritative-Reason: offline redirects",
645 net::URLRequestRedirectJob::REDIRECT_302_FOUND,
mmenke 2016/11/17 18:42:26 Comment that a 302 is used to remove response bodi
jianli 2016/11/17 23:42:05 Done.
646 redirected_url.spec().c_str());
mmenke 2016/11/17 18:42:26 Need to include gurl.h
jianli 2016/11/17 23:42:05 Done.
647
648 fake_headers_for_redirect_ = new net::HttpResponseHeaders(
649 net::HttpUtil::AssembleRawHeaders(header_string.c_str(),
650 header_string.length()));
651 DCHECK(fake_headers_for_redirect_->IsRedirect(NULL));
fgorski 2016/11/17 17:37:48 nit: would nullptr work here?
jianli 2016/11/17 23:42:05 Done.
652
653 URLRequestJob::NotifyHeadersComplete();
597 } 654 }
598 655
599 void OfflinePageRequestJob::SetDelegateForTesting( 656 void OfflinePageRequestJob::SetDelegateForTesting(
600 std::unique_ptr<Delegate> delegate) { 657 std::unique_ptr<Delegate> delegate) {
601 delegate_ = std::move(delegate); 658 delegate_ = std::move(delegate);
602 } 659 }
603 660
604 } // namespace offline_pages 661 } // namespace offline_pages
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698