| 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/offline_pages/background_loader_offliner.h" | 5 #include "chrome/browser/offline_pages/background_loader_offliner.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/json/json_writer.h" | 8 #include "base/json/json_writer.h" |
| 9 #include "base/metrics/histogram_functions.h" | 9 #include "base/metrics/histogram_functions.h" |
| 10 #include "base/metrics/histogram_macros.h" | 10 #include "base/metrics/histogram_macros.h" |
| 11 #include "base/sys_info.h" | 11 #include "base/sys_info.h" |
| 12 #include "base/threading/thread_task_runner_handle.h" | 12 #include "base/threading/thread_task_runner_handle.h" |
| 13 #include "base/time/time.h" | 13 #include "base/time/time.h" |
| 14 #include "chrome/browser/android/offline_pages/offline_page_mhtml_archiver.h" | 14 #include "chrome/browser/android/offline_pages/offline_page_mhtml_archiver.h" |
| 15 #include "chrome/browser/android/offline_pages/offliner_helper.h" | 15 #include "chrome/browser/android/offline_pages/offliner_helper.h" |
| 16 #include "chrome/browser/loader/chrome_navigation_data.h" | 16 #include "chrome/browser/loader/chrome_navigation_data.h" |
| 17 #include "chrome/browser/offline_pages/offliner_user_data.h" |
| 17 #include "chrome/browser/profiles/profile.h" | 18 #include "chrome/browser/profiles/profile.h" |
| 18 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_data
.h" | 19 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_data
.h" |
| 19 #include "components/offline_pages/core/background/offliner_policy.h" | 20 #include "components/offline_pages/core/background/offliner_policy.h" |
| 20 #include "components/offline_pages/core/background/save_page_request.h" | 21 #include "components/offline_pages/core/background/save_page_request.h" |
| 21 #include "components/offline_pages/core/client_namespace_constants.h" | 22 #include "components/offline_pages/core/client_namespace_constants.h" |
| 22 #include "components/offline_pages/core/offline_page_feature.h" | 23 #include "components/offline_pages/core/offline_page_feature.h" |
| 23 #include "components/offline_pages/core/offline_page_model.h" | 24 #include "components/offline_pages/core/offline_page_model.h" |
| 24 #include "content/public/browser/browser_context.h" | 25 #include "content/public/browser/browser_context.h" |
| 25 #include "content/public/browser/mhtml_extra_parts.h" | 26 #include "content/public/browser/mhtml_extra_parts.h" |
| 26 #include "content/public/browser/navigation_handle.h" | 27 #include "content/public/browser/navigation_handle.h" |
| 27 #include "content/public/browser/render_frame_host.h" | 28 #include "content/public/browser/render_frame_host.h" |
| 28 #include "content/public/browser/web_contents.h" | 29 #include "content/public/browser/web_contents.h" |
| 29 #include "content/public/browser/web_contents_user_data.h" | 30 #include "content/public/browser/web_contents_user_data.h" |
| 30 #include "content/public/common/previews_state.h" | 31 #include "content/public/common/previews_state.h" |
| 31 | 32 |
| 32 namespace offline_pages { | 33 namespace offline_pages { |
| 33 | 34 |
| 34 namespace { | 35 namespace { |
| 35 const char kContentType[] = "text/plain"; | 36 const char kContentType[] = "text/plain"; |
| 36 const char kContentTransferEncodingBinary[] = | 37 const char kContentTransferEncodingBinary[] = |
| 37 "Content-Transfer-Encoding: binary"; | 38 "Content-Transfer-Encoding: binary"; |
| 38 const char kXHeaderForSignals[] = "X-Chrome-Loading-Metrics-Data: 1"; | 39 const char kXHeaderForSignals[] = "X-Chrome-Loading-Metrics-Data: 1"; |
| 39 | 40 |
| 40 class OfflinerData : public content::WebContentsUserData<OfflinerData> { | |
| 41 public: | |
| 42 static void AddToWebContents(content::WebContents* webcontents, | |
| 43 BackgroundLoaderOffliner* offliner) { | |
| 44 DCHECK(offliner); | |
| 45 webcontents->SetUserData(UserDataKey(), std::unique_ptr<OfflinerData>( | |
| 46 new OfflinerData(offliner))); | |
| 47 } | |
| 48 | |
| 49 explicit OfflinerData(BackgroundLoaderOffliner* offliner) { | |
| 50 offliner_ = offliner; | |
| 51 } | |
| 52 BackgroundLoaderOffliner* offliner() { return offliner_; } | |
| 53 | |
| 54 private: | |
| 55 // The offliner that the WebContents is attached to. The offliner owns the | |
| 56 // Delegate which owns the WebContents that this data is attached to. | |
| 57 // Therefore, its lifetime should exceed that of the WebContents, so this | |
| 58 // should always be non-null. | |
| 59 BackgroundLoaderOffliner* offliner_; | |
| 60 }; | |
| 61 | |
| 62 std::string AddHistogramSuffix(const ClientId& client_id, | 41 std::string AddHistogramSuffix(const ClientId& client_id, |
| 63 const char* histogram_name) { | 42 const char* histogram_name) { |
| 64 if (client_id.name_space.empty()) { | 43 if (client_id.name_space.empty()) { |
| 65 NOTREACHED(); | 44 NOTREACHED(); |
| 66 return histogram_name; | 45 return histogram_name; |
| 67 } | 46 } |
| 68 std::string adjusted_histogram_name(histogram_name); | 47 std::string adjusted_histogram_name(histogram_name); |
| 69 adjusted_histogram_name += "." + client_id.name_space; | 48 adjusted_histogram_name += "." + client_id.name_space; |
| 70 return adjusted_histogram_name; | 49 return adjusted_histogram_name; |
| 71 } | 50 } |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 130 DCHECK(offline_page_model_); | 109 DCHECK(offline_page_model_); |
| 131 DCHECK(browser_context_); | 110 DCHECK(browser_context_); |
| 132 load_termination_listener_->set_offliner(this); | 111 load_termination_listener_->set_offliner(this); |
| 133 } | 112 } |
| 134 | 113 |
| 135 BackgroundLoaderOffliner::~BackgroundLoaderOffliner() {} | 114 BackgroundLoaderOffliner::~BackgroundLoaderOffliner() {} |
| 136 | 115 |
| 137 // static | 116 // static |
| 138 BackgroundLoaderOffliner* BackgroundLoaderOffliner::FromWebContents( | 117 BackgroundLoaderOffliner* BackgroundLoaderOffliner::FromWebContents( |
| 139 content::WebContents* contents) { | 118 content::WebContents* contents) { |
| 140 OfflinerData* data = OfflinerData::FromWebContents(contents); | 119 Offliner* offliner = OfflinerUserData::OfflinerFromWebContents(contents); |
| 141 if (data) | 120 // Today we only have one kind of offliner that uses OfflinerUserData. If we |
| 142 return data->offliner(); | 121 // add other types, revisit this cast. |
| 122 if (offliner) |
| 123 return static_cast<BackgroundLoaderOffliner*>(offliner); |
| 143 return nullptr; | 124 return nullptr; |
| 144 } | 125 } |
| 145 | 126 |
| 146 bool BackgroundLoaderOffliner::LoadAndSave( | 127 bool BackgroundLoaderOffliner::LoadAndSave( |
| 147 const SavePageRequest& request, | 128 const SavePageRequest& request, |
| 148 const CompletionCallback& completion_callback, | 129 const CompletionCallback& completion_callback, |
| 149 const ProgressCallback& progress_callback) { | 130 const ProgressCallback& progress_callback) { |
| 150 DCHECK(completion_callback); | 131 DCHECK(completion_callback); |
| 151 DCHECK(progress_callback); | 132 DCHECK(progress_callback); |
| 152 DCHECK(offline_page_model_); | 133 DCHECK(offline_page_model_); |
| (...skipping 198 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 351 ChromeNavigationData* navigation_data = static_cast<ChromeNavigationData*>( | 332 ChromeNavigationData* navigation_data = static_cast<ChromeNavigationData*>( |
| 352 navigation_handle->GetNavigationData()); | 333 navigation_handle->GetNavigationData()); |
| 353 | 334 |
| 354 RecordOffliningPreviewsUMA(pending_request_->client_id(), navigation_data); | 335 RecordOffliningPreviewsUMA(pending_request_->client_id(), navigation_data); |
| 355 } | 336 } |
| 356 | 337 |
| 357 void BackgroundLoaderOffliner::SetSnapshotControllerForTest( | 338 void BackgroundLoaderOffliner::SetSnapshotControllerForTest( |
| 358 std::unique_ptr<SnapshotController> controller) { | 339 std::unique_ptr<SnapshotController> controller) { |
| 359 snapshot_controller_ = std::move(controller); | 340 snapshot_controller_ = std::move(controller); |
| 360 } | 341 } |
| 342 void BackgroundLoaderOffliner::ObserveResourceTracking( |
| 343 ResourceTrackerObserver::ResourceDataType type, |
| 344 bool started) { |
| 345 // TODO(petewil) Not implemented yet. |
| 346 return; |
| 347 } |
| 361 | 348 |
| 362 void BackgroundLoaderOffliner::OnNetworkBytesChanged(int64_t bytes) { | 349 void BackgroundLoaderOffliner::OnNetworkBytesChanged(int64_t bytes) { |
| 363 if (pending_request_ && save_state_ != SAVING) { | 350 if (pending_request_ && save_state_ != SAVING) { |
| 364 network_bytes_ += bytes; | 351 network_bytes_ += bytes; |
| 365 progress_callback_.Run(*pending_request_, network_bytes_); | 352 progress_callback_.Run(*pending_request_, network_bytes_); |
| 366 } | 353 } |
| 367 } | 354 } |
| 368 | 355 |
| 369 void BackgroundLoaderOffliner::StartSnapshot() { | 356 void BackgroundLoaderOffliner::StartSnapshot() { |
| 370 if (!pending_request_.get()) { | 357 if (!pending_request_.get()) { |
| (...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 504 } | 491 } |
| 505 | 492 |
| 506 void BackgroundLoaderOffliner::ResetLoader() { | 493 void BackgroundLoaderOffliner::ResetLoader() { |
| 507 loader_.reset( | 494 loader_.reset( |
| 508 new background_loader::BackgroundLoaderContents(browser_context_)); | 495 new background_loader::BackgroundLoaderContents(browser_context_)); |
| 509 } | 496 } |
| 510 | 497 |
| 511 void BackgroundLoaderOffliner::AttachObservers() { | 498 void BackgroundLoaderOffliner::AttachObservers() { |
| 512 content::WebContents* contents = loader_->web_contents(); | 499 content::WebContents* contents = loader_->web_contents(); |
| 513 content::WebContentsObserver::Observe(contents); | 500 content::WebContentsObserver::Observe(contents); |
| 514 OfflinerData::AddToWebContents(contents, this); | 501 OfflinerUserData::AddToWebContents(contents, this); |
| 515 } | 502 } |
| 516 | 503 |
| 517 void BackgroundLoaderOffliner::AddLoadingSignal(const char* signal_name) { | 504 void BackgroundLoaderOffliner::AddLoadingSignal(const char* signal_name) { |
| 518 base::TimeTicks current_time = base::TimeTicks::Now(); | 505 base::TimeTicks current_time = base::TimeTicks::Now(); |
| 519 base::TimeDelta delay_so_far = current_time - load_start_time_; | 506 base::TimeDelta delay_so_far = current_time - load_start_time_; |
| 520 // We would prefer to use int64_t here, but JSON does not support that type. | 507 // We would prefer to use int64_t here, but JSON does not support that type. |
| 521 // Given the choice between int and double, we choose to implicitly convert to | 508 // Given the choice between int and double, we choose to implicitly convert to |
| 522 // a double since it maintains more precision (we can get a longer time in | 509 // a double since it maintains more precision (we can get a longer time in |
| 523 // milliseconds than we can with a 2 bit int, 53 bits vs 32). | 510 // milliseconds than we can with a 2 bit int, 53 bits vs 32). |
| 524 double delay = delay_so_far.InMilliseconds(); | 511 double delay = delay_so_far.InMilliseconds(); |
| 525 signal_data_.SetDouble(signal_name, delay); | 512 signal_data_.SetDouble(signal_name, delay); |
| 526 } | 513 } |
| 527 | 514 |
| 528 } // namespace offline_pages | 515 } // namespace offline_pages |
| 529 | |
| 530 DEFINE_WEB_CONTENTS_USER_DATA_KEY(offline_pages::OfflinerData); | |
| OLD | NEW |