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

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

Issue 2388253002: Use the previews black list for offline previews (Closed)
Patch Set: 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_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_black_list.h"
22 #include "components/previews/core/previews_experiments.h" 23 #include "components/previews/core/previews_experiments.h"
24 #include "components/previews/core/previews_opt_out_store.h"
23 #include "content/public/browser/browser_thread.h" 25 #include "content/public/browser/browser_thread.h"
24 #include "content/public/browser/resource_request_info.h" 26 #include "content/public/browser/resource_request_info.h"
25 #include "content/public/browser/web_contents.h" 27 #include "content/public/browser/web_contents.h"
26 #include "content/public/common/resource_type.h" 28 #include "content/public/common/resource_type.h"
27 #include "net/base/network_change_notifier.h" 29 #include "net/base/network_change_notifier.h"
28 #include "net/http/http_request_headers.h" 30 #include "net/http/http_request_headers.h"
29 #include "net/nqe/network_quality_estimator.h" 31 #include "net/nqe/network_quality_estimator.h"
30 #include "net/url_request/url_request.h" 32 #include "net/url_request/url_request.h"
31 #include "net/url_request/url_request_context.h" 33 #include "net/url_request/url_request_context.h"
32 34
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
101 } 103 }
102 104
103 private: 105 private:
104 static bool GetTabId(content::WebContents* web_contents, int* tab_id) { 106 static bool GetTabId(content::WebContents* web_contents, int* tab_id) {
105 return OfflinePageUtils::GetTabId(web_contents, tab_id); 107 return OfflinePageUtils::GetTabId(web_contents, tab_id);
106 } 108 }
107 109
108 DISALLOW_COPY_AND_ASSIGN(DefaultDelegate); 110 DISALLOW_COPY_AND_ASSIGN(DefaultDelegate);
109 }; 111 };
110 112
111 bool IsNetworkProhibitivelySlow(net::URLRequest* request) { 113 bool IsNetworkProhibitivelySlow(
114 net::URLRequest* request,
115 previews::PreviewsBlackList* previews_black_list) {
112 // NetworkQualityEstimator only works when it is enabled. 116 // NetworkQualityEstimator only works when it is enabled.
113 if (!previews::IsOfflinePreviewsEnabled()) 117 if (!previews::IsOfflinePreviewsEnabled())
114 return false; 118 return false;
115 119
120 // If |previews_black_list| is non-null and the URL is not allowed for offline
121 // previews, do not allow the offline page to be shown.
122 if (previews_black_list &&
jianli 2016/10/03 22:56:05 Is it possible to merge this black list into Netwo
RyanSturm 2016/10/03 23:54:39 NQE is global, the blacklist is per profile. Embe
123 !previews_black_list->IsLoadedAndAllowed(
tbansal1 2016/10/04 12:38:04 I suppose the part where the black list is notifie
RyanSturm 2016/10/04 21:36:38 Yes.
124 request->url(), previews::PreviewsType::OFFLINE)) {
125 return false;
126 }
127
116 if (!request->context()) 128 if (!request->context())
117 return false; 129 return false;
118 130
119 net::NetworkQualityEstimator* network_quality_estimator = 131 net::NetworkQualityEstimator* network_quality_estimator =
120 request->context()->network_quality_estimator(); 132 request->context()->network_quality_estimator();
121 if (!network_quality_estimator) 133 if (!network_quality_estimator)
122 return false; 134 return false;
123 135
124 net::EffectiveConnectionType effective_connection_type = 136 net::EffectiveConnectionType effective_connection_type =
125 network_quality_estimator->GetEffectiveConnectionType(); 137 network_quality_estimator->GetEffectiveConnectionType();
126 return effective_connection_type >= net::EFFECTIVE_CONNECTION_TYPE_OFFLINE && 138 return effective_connection_type >= net::EFFECTIVE_CONNECTION_TYPE_OFFLINE &&
127 effective_connection_type <= net::EFFECTIVE_CONNECTION_TYPE_SLOW_2G; 139 effective_connection_type <= net::EFFECTIVE_CONNECTION_TYPE_SLOW_2G;
128 } 140 }
129 141
130 NetworkState GetNetworkState(net::URLRequest* request, 142 NetworkState GetNetworkState(net::URLRequest* request,
131 const OfflinePageHeader& offline_header) { 143 const OfflinePageHeader& offline_header,
144 previews::PreviewsBlackList* previews_black_list) {
132 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); 145 DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
133 146
134 if (offline_header.reason == OfflinePageHeader::Reason::NET_ERROR) 147 if (offline_header.reason == OfflinePageHeader::Reason::NET_ERROR)
135 return NetworkState::FLAKY_NETWORK; 148 return NetworkState::FLAKY_NETWORK;
136 149
137 if (net::NetworkChangeNotifier::IsOffline()) 150 if (net::NetworkChangeNotifier::IsOffline())
138 return NetworkState::DISCONNECTED_NETWORK; 151 return NetworkState::DISCONNECTED_NETWORK;
139 152
140 if (IsNetworkProhibitivelySlow(request)) 153 if (IsNetworkProhibitivelySlow(request, previews_black_list))
141 return NetworkState::PROHIBITIVELY_SLOW_NETWORK; 154 return NetworkState::PROHIBITIVELY_SLOW_NETWORK;
142 155
143 // If offline header contains a reason other than RELOAD, the offline page 156 // If offline header contains a reason other than RELOAD, the offline page
144 // should be forced to load even when the network is connected. 157 // should be forced to load even when the network is connected.
145 return (offline_header.reason != OfflinePageHeader::Reason::NONE && 158 return (offline_header.reason != OfflinePageHeader::Reason::NONE &&
146 offline_header.reason != OfflinePageHeader::Reason::RELOAD) 159 offline_header.reason != OfflinePageHeader::Reason::RELOAD)
147 ? NetworkState::FORCE_OFFLINE_ON_CONNECTED_NETWORK 160 ? NetworkState::FORCE_OFFLINE_ON_CONNECTED_NETWORK
148 : NetworkState::CONNECTED_NETWORK; 161 : NetworkState::CONNECTED_NETWORK;
149 } 162 }
150 163
(...skipping 297 matching lines...) Expand 10 before | Expand all | Expand 10 after
448 void OfflinePageRequestJob::ReportAggregatedRequestResult( 461 void OfflinePageRequestJob::ReportAggregatedRequestResult(
449 AggregatedRequestResult result) { 462 AggregatedRequestResult result) {
450 UMA_HISTOGRAM_ENUMERATION("OfflinePages.AggregatedRequestResult", 463 UMA_HISTOGRAM_ENUMERATION("OfflinePages.AggregatedRequestResult",
451 static_cast<int>(result), 464 static_cast<int>(result),
452 static_cast<int>(AggregatedRequestResult::AGGREGATED_REQUEST_RESULT_MAX)); 465 static_cast<int>(AggregatedRequestResult::AGGREGATED_REQUEST_RESULT_MAX));
453 } 466 }
454 467
455 // static 468 // static
456 OfflinePageRequestJob* OfflinePageRequestJob::Create( 469 OfflinePageRequestJob* OfflinePageRequestJob::Create(
457 net::URLRequest* request, 470 net::URLRequest* request,
458 net::NetworkDelegate* network_delegate) { 471 net::NetworkDelegate* network_delegate,
472 previews::PreviewsBlackList* previews_black_list) {
459 const content::ResourceRequestInfo* resource_request_info = 473 const content::ResourceRequestInfo* resource_request_info =
460 content::ResourceRequestInfo::ForRequest(request); 474 content::ResourceRequestInfo::ForRequest(request);
461 if (!resource_request_info) 475 if (!resource_request_info)
462 return nullptr; 476 return nullptr;
463 477
464 // Ignore the requests not for the main resource. 478 // Ignore the requests not for the main resource.
465 if (resource_request_info->GetResourceType() != 479 if (resource_request_info->GetResourceType() !=
466 content::RESOURCE_TYPE_MAIN_FRAME) { 480 content::RESOURCE_TYPE_MAIN_FRAME) {
467 return nullptr; 481 return nullptr;
468 } 482 }
(...skipping 10 matching lines...) Expand all
479 OfflinePageRequestInfo::GetFromRequest(request); 493 OfflinePageRequestInfo::GetFromRequest(request);
480 if (info) { 494 if (info) {
481 // Fall back to default which is set when an offline page cannot be served, 495 // Fall back to default which is set when an offline page cannot be served,
482 // either page not found or online version desired. 496 // either page not found or online version desired.
483 if (info->use_default()) 497 if (info->use_default())
484 return nullptr; 498 return nullptr;
485 } else { 499 } else {
486 request->SetUserData(&kUserDataKey, new OfflinePageRequestInfo()); 500 request->SetUserData(&kUserDataKey, new OfflinePageRequestInfo());
487 } 501 }
488 502
489 return new OfflinePageRequestJob(request, network_delegate); 503 return new OfflinePageRequestJob(request, network_delegate,
504 previews_black_list);
490 } 505 }
491 506
492 OfflinePageRequestJob::OfflinePageRequestJob( 507 OfflinePageRequestJob::OfflinePageRequestJob(
493 net::URLRequest* request, 508 net::URLRequest* request,
494 net::NetworkDelegate* network_delegate) 509 net::NetworkDelegate* network_delegate,
510 previews::PreviewsBlackList* previews_black_list)
495 : net::URLRequestFileJob( 511 : net::URLRequestFileJob(
496 request, 512 request,
497 network_delegate, 513 network_delegate,
498 base::FilePath(), 514 base::FilePath(),
499 content::BrowserThread::GetBlockingPool()-> 515 content::BrowserThread::GetBlockingPool()
500 GetTaskRunnerWithShutdownBehavior( 516 ->GetTaskRunnerWithShutdownBehavior(
501 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN)), 517 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN)),
502 delegate_(new DefaultDelegate()), 518 delegate_(new DefaultDelegate()),
503 weak_ptr_factory_(this) { 519 previews_black_list_(previews_black_list),
504 } 520 weak_ptr_factory_(this) {}
505 521
506 OfflinePageRequestJob::~OfflinePageRequestJob() { 522 OfflinePageRequestJob::~OfflinePageRequestJob() {
507 } 523 }
508 524
509 void OfflinePageRequestJob::Start() { 525 void OfflinePageRequestJob::Start() {
510 base::ThreadTaskRunnerHandle::Get()->PostTask( 526 base::ThreadTaskRunnerHandle::Get()->PostTask(
511 FROM_HERE, base::Bind(&OfflinePageRequestJob::StartAsync, 527 FROM_HERE, base::Bind(&OfflinePageRequestJob::StartAsync,
512 weak_ptr_factory_.GetWeakPtr())); 528 weak_ptr_factory_.GetWeakPtr()));
513 } 529 }
514 530
515 void OfflinePageRequestJob::StartAsync() { 531 void OfflinePageRequestJob::StartAsync() {
516 std::string offline_header_value; 532 std::string offline_header_value;
517 request()->extra_request_headers().GetHeader( 533 request()->extra_request_headers().GetHeader(
518 kOfflinePageHeader, &offline_header_value); 534 kOfflinePageHeader, &offline_header_value);
519 // Note that |offline_header| will be empty if parsing from the header value 535 // Note that |offline_header| will be empty if parsing from the header value
520 // fails. 536 // fails.
521 OfflinePageHeader offline_header(offline_header_value); 537 OfflinePageHeader offline_header(offline_header_value);
522 538
523 NetworkState network_state = GetNetworkState(request(), offline_header); 539 NetworkState network_state =
540 GetNetworkState(request(), offline_header, previews_black_list_);
524 if (network_state == NetworkState::CONNECTED_NETWORK) { 541 if (network_state == NetworkState::CONNECTED_NETWORK) {
525 FallbackToDefault(); 542 FallbackToDefault();
526 return; 543 return;
527 } 544 }
528 545
529 content::BrowserThread::PostTask( 546 content::BrowserThread::PostTask(
530 content::BrowserThread::UI, 547 content::BrowserThread::UI,
531 FROM_HERE, 548 FROM_HERE,
532 base::Bind(&SelectPage, 549 base::Bind(&SelectPage,
533 request()->url(), 550 request()->url(),
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
565 file_path_ = offline_file_path; 582 file_path_ = offline_file_path;
566 URLRequestFileJob::Start(); 583 URLRequestFileJob::Start();
567 } 584 }
568 585
569 void OfflinePageRequestJob::SetDelegateForTesting( 586 void OfflinePageRequestJob::SetDelegateForTesting(
570 std::unique_ptr<Delegate> delegate) { 587 std::unique_ptr<Delegate> delegate) {
571 delegate_ = std::move(delegate); 588 delegate_ = std::move(delegate);
572 } 589 }
573 590
574 } // namespace offline_pages 591 } // namespace offline_pages
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698