| 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_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.h" |
| 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" |
| 34 #include "url/gurl.h" |
| 31 | 35 |
| 32 namespace offline_pages { | 36 namespace offline_pages { |
| 33 | 37 |
| 34 namespace { | 38 namespace { |
| 35 | 39 |
| 36 enum class NetworkState { | 40 enum class NetworkState { |
| 37 // No network connection. | 41 // No network connection. |
| 38 DISCONNECTED_NETWORK, | 42 DISCONNECTED_NETWORK, |
| 39 // Prohibitively slow means that the NetworkQualityEstimator reported a | 43 // Prohibitively slow means that the NetworkQualityEstimator reported a |
| 40 // connection slow enough to warrant showing an offline page if available. | 44 // connection slow enough to warrant showing an offline page if available. |
| (...skipping 519 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 560 delegate_->GetWebContentsGetter(request()), | 564 delegate_->GetWebContentsGetter(request()), |
| 561 delegate_->GetTabIdGetter(), | 565 delegate_->GetTabIdGetter(), |
| 562 weak_ptr_factory_.GetWeakPtr())); | 566 weak_ptr_factory_.GetWeakPtr())); |
| 563 } | 567 } |
| 564 | 568 |
| 565 void OfflinePageRequestJob::Kill() { | 569 void OfflinePageRequestJob::Kill() { |
| 566 net::URLRequestJob::Kill(); | 570 net::URLRequestJob::Kill(); |
| 567 weak_ptr_factory_.InvalidateWeakPtrs(); | 571 weak_ptr_factory_.InvalidateWeakPtrs(); |
| 568 } | 572 } |
| 569 | 573 |
| 574 bool OfflinePageRequestJob::IsRedirectResponse(GURL* location, |
| 575 int* http_status_code) { |
| 576 // OfflinePageRequestJob derives from URLRequestFileJob which derives from |
| 577 // URLRequestJob. We need to invoke the implementation of IsRedirectResponse |
| 578 // in URLRequestJob directly since URLRequestFileJob overrode it with the |
| 579 // logic we don't want. |
| 580 return URLRequestJob::IsRedirectResponse(location, http_status_code); |
| 581 } |
| 582 |
| 583 void OfflinePageRequestJob::GetResponseInfo(net::HttpResponseInfo* info) { |
| 584 if (!fake_headers_for_redirect_) { |
| 585 URLRequestFileJob::GetResponseInfo(info); |
| 586 return; |
| 587 } |
| 588 |
| 589 info->headers = fake_headers_for_redirect_; |
| 590 info->request_time = redirect_response_time_; |
| 591 info->response_time = redirect_response_time_; |
| 592 } |
| 593 |
| 594 void OfflinePageRequestJob::GetLoadTimingInfo( |
| 595 net::LoadTimingInfo* load_timing_info) const { |
| 596 // Set send_start and send_end to receive_redirect_headers_end_ to be |
| 597 // consistent with network cache behavior. |
| 598 load_timing_info->send_start = receive_redirect_headers_end_; |
| 599 load_timing_info->send_end = receive_redirect_headers_end_; |
| 600 load_timing_info->receive_headers_end = receive_redirect_headers_end_; |
| 601 } |
| 602 |
| 603 bool OfflinePageRequestJob::CopyFragmentOnRedirect(const GURL& location) const { |
| 604 return false; |
| 605 } |
| 606 |
| 607 int OfflinePageRequestJob::GetResponseCode() const { |
| 608 if (!fake_headers_for_redirect_) |
| 609 return URLRequestFileJob::GetResponseCode(); |
| 610 |
| 611 return net::URLRequestRedirectJob::REDIRECT_302_FOUND; |
| 612 } |
| 613 |
| 570 void OfflinePageRequestJob::FallbackToDefault() { | 614 void OfflinePageRequestJob::FallbackToDefault() { |
| 571 OfflinePageRequestInfo* info = | 615 OfflinePageRequestInfo* info = |
| 572 OfflinePageRequestInfo::GetFromRequest(request()); | 616 OfflinePageRequestInfo::GetFromRequest(request()); |
| 573 DCHECK(info); | 617 DCHECK(info); |
| 574 info->set_use_default(true); | 618 info->set_use_default(true); |
| 575 | 619 |
| 576 URLRequestJob::NotifyRestartRequired(); | 620 URLRequestJob::NotifyRestartRequired(); |
| 577 } | 621 } |
| 578 | 622 |
| 579 void OfflinePageRequestJob::OnOfflineFilePathAvailable( | 623 void OfflinePageRequestJob::OnOfflineFilePathAvailable( |
| 580 const base::FilePath& offline_file_path) { | 624 const base::FilePath& offline_file_path) { |
| 581 // If offline file path is empty, it means that offline page cannot be found | 625 // 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. | 626 // and we want to restart the job to fall back to the default handling. |
| 583 if (offline_file_path.empty()) { | 627 if (offline_file_path.empty()) { |
| 584 FallbackToDefault(); | 628 FallbackToDefault(); |
| 585 return; | 629 return; |
| 586 } | 630 } |
| 587 | 631 |
| 588 // Sets the file path and lets URLRequestFileJob start to read from the file. | 632 // Sets the file path and lets URLRequestFileJob start to read from the file. |
| 589 file_path_ = offline_file_path; | 633 file_path_ = offline_file_path; |
| 590 URLRequestFileJob::Start(); | 634 URLRequestFileJob::Start(); |
| 591 } | 635 } |
| 592 | 636 |
| 593 void OfflinePageRequestJob::OnOfflineRedirectAvailabe( | 637 void OfflinePageRequestJob::OnOfflineRedirectAvailabe( |
| 594 const GURL& redirected_url) { | 638 const GURL& redirected_url) { |
| 595 // TODO(jianli): kicks off the redirect. For now, use the default. | 639 receive_redirect_headers_end_ = base::TimeTicks::Now(); |
| 596 FallbackToDefault(); | 640 redirect_response_time_ = base::Time::Now(); |
| 641 |
| 642 std::string header_string = |
| 643 base::StringPrintf("HTTP/1.1 %i Internal Redirect\n" |
| 644 // Clear referrer to avoid leak when going online. |
| 645 "Referrer-Policy: no-referrer\n" |
| 646 "Location: %s\n" |
| 647 "Non-Authoritative-Reason: offline redirects", |
| 648 // 302 is used to remove response bodies in order to |
| 649 // avoid leak when going online. |
| 650 net::URLRequestRedirectJob::REDIRECT_302_FOUND, |
| 651 redirected_url.spec().c_str()); |
| 652 |
| 653 fake_headers_for_redirect_ = new net::HttpResponseHeaders( |
| 654 net::HttpUtil::AssembleRawHeaders(header_string.c_str(), |
| 655 header_string.length())); |
| 656 DCHECK(fake_headers_for_redirect_->IsRedirect(nullptr)); |
| 657 |
| 658 URLRequestJob::NotifyHeadersComplete(); |
| 597 } | 659 } |
| 598 | 660 |
| 599 void OfflinePageRequestJob::SetDelegateForTesting( | 661 void OfflinePageRequestJob::SetDelegateForTesting( |
| 600 std::unique_ptr<Delegate> delegate) { | 662 std::unique_ptr<Delegate> delegate) { |
| 601 delegate_ = std::move(delegate); | 663 delegate_ = std::move(delegate); |
| 602 } | 664 } |
| 603 | 665 |
| 604 } // namespace offline_pages | 666 } // namespace offline_pages |
| OLD | NEW |