Chromium Code Reviews| 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_macros.h" | 9 #include "base/metrics/histogram_macros.h" |
| 10 #include "base/sys_info.h" | 10 #include "base/sys_info.h" |
| 11 #include "base/threading/thread_task_runner_handle.h" | 11 #include "base/threading/thread_task_runner_handle.h" |
| 12 #include "base/time/time.h" | 12 #include "base/time/time.h" |
| 13 #include "chrome/browser/android/offline_pages/offline_page_mhtml_archiver.h" | 13 #include "chrome/browser/android/offline_pages/offline_page_mhtml_archiver.h" |
| 14 #include "chrome/browser/android/offline_pages/offliner_helper.h" | 14 #include "chrome/browser/android/offline_pages/offliner_helper.h" |
| 15 #include "chrome/browser/offline_pages/offliner_user_data.h" | |
| 16 #include "chrome/browser/page_load_metrics/page_load_metrics_initialize.h" | |
| 15 #include "chrome/browser/profiles/profile.h" | 17 #include "chrome/browser/profiles/profile.h" |
| 16 #include "components/offline_pages/core/background/offliner_policy.h" | 18 #include "components/offline_pages/core/background/offliner_policy.h" |
| 17 #include "components/offline_pages/core/background/save_page_request.h" | 19 #include "components/offline_pages/core/background/save_page_request.h" |
| 18 #include "components/offline_pages/core/client_namespace_constants.h" | 20 #include "components/offline_pages/core/client_namespace_constants.h" |
| 19 #include "components/offline_pages/core/offline_page_feature.h" | 21 #include "components/offline_pages/core/offline_page_feature.h" |
| 20 #include "components/offline_pages/core/offline_page_model.h" | 22 #include "components/offline_pages/core/offline_page_model.h" |
| 21 #include "content/public/browser/browser_context.h" | 23 #include "content/public/browser/browser_context.h" |
| 22 #include "content/public/browser/mhtml_extra_parts.h" | 24 #include "content/public/browser/mhtml_extra_parts.h" |
| 23 #include "content/public/browser/navigation_handle.h" | 25 #include "content/public/browser/navigation_handle.h" |
| 24 #include "content/public/browser/render_frame_host.h" | 26 #include "content/public/browser/render_frame_host.h" |
| 25 #include "content/public/browser/web_contents.h" | 27 #include "content/public/browser/web_contents.h" |
| 26 #include "content/public/browser/web_contents_user_data.h" | 28 #include "content/public/browser/web_contents_user_data.h" |
| 27 | 29 |
| 28 namespace offline_pages { | 30 namespace offline_pages { |
| 29 | 31 |
| 30 namespace { | 32 namespace { |
| 31 const char kContentType[] = "text/plain"; | 33 const char kContentType[] = "text/plain"; |
| 32 const char kContentTransferEncodingBinary[] = | 34 const char kContentTransferEncodingBinary[] = |
| 33 "Content-Transfer-Encoding: binary"; | 35 "Content-Transfer-Encoding: binary"; |
| 34 const char kXHeaderForSignals[] = "X-Chrome-Loading-Metrics-Data: 1"; | 36 const char kXHeaderForSignals[] = "X-Chrome-Loading-Metrics-Data: 1"; |
| 35 | 37 |
| 36 class OfflinerData : public content::WebContentsUserData<OfflinerData> { | |
| 37 public: | |
| 38 static void AddToWebContents(content::WebContents* webcontents, | |
| 39 BackgroundLoaderOffliner* offliner) { | |
| 40 DCHECK(offliner); | |
| 41 webcontents->SetUserData(UserDataKey(), std::unique_ptr<OfflinerData>( | |
| 42 new OfflinerData(offliner))); | |
| 43 } | |
| 44 | |
| 45 explicit OfflinerData(BackgroundLoaderOffliner* offliner) { | |
| 46 offliner_ = offliner; | |
| 47 } | |
| 48 BackgroundLoaderOffliner* offliner() { return offliner_; } | |
| 49 | |
| 50 private: | |
| 51 // The offliner that the WebContents is attached to. The offliner owns the | |
| 52 // Delegate which owns the WebContents that this data is attached to. | |
| 53 // Therefore, its lifetime should exceed that of the WebContents, so this | |
| 54 // should always be non-null. | |
| 55 BackgroundLoaderOffliner* offliner_; | |
| 56 }; | |
| 57 | |
| 58 std::string AddHistogramSuffix(const ClientId& client_id, | 38 std::string AddHistogramSuffix(const ClientId& client_id, |
| 59 const char* histogram_name) { | 39 const char* histogram_name) { |
| 60 if (client_id.name_space.empty()) { | 40 if (client_id.name_space.empty()) { |
| 61 NOTREACHED(); | 41 NOTREACHED(); |
| 62 return histogram_name; | 42 return histogram_name; |
| 63 } | 43 } |
| 64 std::string adjusted_histogram_name(histogram_name); | 44 std::string adjusted_histogram_name(histogram_name); |
| 65 adjusted_histogram_name += "." + client_id.name_space; | 45 adjusted_histogram_name += "." + client_id.name_space; |
| 66 return adjusted_histogram_name; | 46 return adjusted_histogram_name; |
| 67 } | 47 } |
| (...skipping 30 matching lines...) Expand all Loading... | |
| 98 weak_ptr_factory_(this) { | 78 weak_ptr_factory_(this) { |
| 99 DCHECK(offline_page_model_); | 79 DCHECK(offline_page_model_); |
| 100 DCHECK(browser_context_); | 80 DCHECK(browser_context_); |
| 101 } | 81 } |
| 102 | 82 |
| 103 BackgroundLoaderOffliner::~BackgroundLoaderOffliner() {} | 83 BackgroundLoaderOffliner::~BackgroundLoaderOffliner() {} |
| 104 | 84 |
| 105 // static | 85 // static |
| 106 BackgroundLoaderOffliner* BackgroundLoaderOffliner::FromWebContents( | 86 BackgroundLoaderOffliner* BackgroundLoaderOffliner::FromWebContents( |
| 107 content::WebContents* contents) { | 87 content::WebContents* contents) { |
| 108 OfflinerData* data = OfflinerData::FromWebContents(contents); | 88 Offliner* offliner = OfflinerUserData::OfflinerFromWebContents(contents); |
| 109 if (data) | 89 |
| 110 return data->offliner(); | 90 // Today we only have one kind of offliner that uses OfflinerUserData. If we |
| 91 // add other types, revisit this cast. | |
| 92 if (offliner) | |
| 93 return static_cast<BackgroundLoaderOffliner*>(offliner); | |
| 111 return nullptr; | 94 return nullptr; |
| 112 } | 95 } |
| 113 | 96 |
| 114 bool BackgroundLoaderOffliner::LoadAndSave( | 97 bool BackgroundLoaderOffliner::LoadAndSave( |
| 115 const SavePageRequest& request, | 98 const SavePageRequest& request, |
| 116 const CompletionCallback& completion_callback, | 99 const CompletionCallback& completion_callback, |
| 117 const ProgressCallback& progress_callback) { | 100 const ProgressCallback& progress_callback) { |
| 118 DCHECK(completion_callback); | 101 DCHECK(completion_callback); |
| 119 DCHECK(progress_callback); | 102 DCHECK(progress_callback); |
| 120 DCHECK(offline_page_model_); | 103 DCHECK(offline_page_model_); |
| (...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 231 if (save_state_ == NONE) { | 214 if (save_state_ == NONE) { |
| 232 did_snapshot_on_last_retry_ = true; | 215 did_snapshot_on_last_retry_ = true; |
| 233 StartSnapshot(); | 216 StartSnapshot(); |
| 234 } | 217 } |
| 235 return true; | 218 return true; |
| 236 } | 219 } |
| 237 } | 220 } |
| 238 return false; | 221 return false; |
| 239 } | 222 } |
| 240 | 223 |
| 224 void BackgroundLoaderOffliner::ObserveResourceTracking( | |
| 225 const ResourceDataType type, | |
| 226 int64_t started_count, | |
| 227 int64_t completed_count) { | |
| 228 // Add the signal to extra data, and use for tracking. | |
| 229 if (type == ResourceDataType::IMAGE) | |
| 230 AddResourceSignal(type, started_count, completed_count); | |
| 231 } | |
| 232 | |
| 241 void BackgroundLoaderOffliner::MarkLoadStartTime() { | 233 void BackgroundLoaderOffliner::MarkLoadStartTime() { |
| 242 load_start_time_ = base::TimeTicks::Now(); | 234 load_start_time_ = base::TimeTicks::Now(); |
| 243 } | 235 } |
| 244 | 236 |
| 245 void BackgroundLoaderOffliner::DocumentAvailableInMainFrame() { | 237 void BackgroundLoaderOffliner::DocumentAvailableInMainFrame() { |
| 246 snapshot_controller_->DocumentAvailableInMainFrame(); | 238 snapshot_controller_->DocumentAvailableInMainFrame(); |
| 247 is_low_bar_met_ = true; | 239 is_low_bar_met_ = true; |
| 248 | 240 |
| 249 // Add this signal to signal_data_. | 241 // Add this signal to signal_data_. |
| 250 AddLoadingSignal("DocumentAvailableInMainFrame"); | 242 AddLoadingSignal("DocumentAvailableInMainFrame"); |
| (...skipping 207 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 458 } | 450 } |
| 459 | 451 |
| 460 void BackgroundLoaderOffliner::ResetLoader() { | 452 void BackgroundLoaderOffliner::ResetLoader() { |
| 461 loader_.reset( | 453 loader_.reset( |
| 462 new background_loader::BackgroundLoaderContents(browser_context_)); | 454 new background_loader::BackgroundLoaderContents(browser_context_)); |
| 463 } | 455 } |
| 464 | 456 |
| 465 void BackgroundLoaderOffliner::AttachObservers() { | 457 void BackgroundLoaderOffliner::AttachObservers() { |
| 466 content::WebContents* contents = loader_->web_contents(); | 458 content::WebContents* contents = loader_->web_contents(); |
| 467 content::WebContentsObserver::Observe(contents); | 459 content::WebContentsObserver::Observe(contents); |
| 468 OfflinerData::AddToWebContents(contents, this); | 460 OfflinerUserData::AddToWebContents(contents, this); |
| 461 | |
| 462 // Attach the metrics observers to the web contents so we can get resoure | |
| 463 // loading signals. | |
| 464 chrome::InitializePageLoadMetricsForWebContents( | |
|
Bryan McQuade
2017/05/23 05:00:01
do we want to bring up the full page load metrics
Pete Williamson
2017/05/24 01:01:50
As the code is written now, InitializePageLoadMetr
| |
| 465 contents, true /* background loading */); | |
| 469 } | 466 } |
| 470 | 467 |
| 471 void BackgroundLoaderOffliner::OnApplicationStateChange( | 468 void BackgroundLoaderOffliner::OnApplicationStateChange( |
| 472 base::android::ApplicationState application_state) { | 469 base::android::ApplicationState application_state) { |
| 473 if (pending_request_ && is_low_end_device_ && | 470 if (pending_request_ && is_low_end_device_ && |
| 474 application_state == | 471 application_state == |
| 475 base::android::APPLICATION_STATE_HAS_RUNNING_ACTIVITIES) { | 472 base::android::APPLICATION_STATE_HAS_RUNNING_ACTIVITIES) { |
| 476 DVLOG(1) << "App became active, canceling current offlining request"; | 473 DVLOG(1) << "App became active, canceling current offlining request"; |
| 477 // No need to check the return value or complete early, as false would | 474 // No need to check the return value or complete early, as false would |
| 478 // indicate that there was no request, in which case the state change is | 475 // indicate that there was no request, in which case the state change is |
| 479 // ignored. | 476 // ignored. |
| 480 Cancel( | 477 Cancel( |
| 481 base::Bind(HandleApplicationStateChangeCancel, completion_callback_)); | 478 base::Bind(HandleApplicationStateChangeCancel, completion_callback_)); |
| 482 } | 479 } |
| 483 } | 480 } |
| 484 | 481 |
| 485 void BackgroundLoaderOffliner::AddLoadingSignal(const char* signal_name) { | 482 void BackgroundLoaderOffliner::AddLoadingSignal(const char* signal_name) { |
| 486 base::TimeTicks current_time = base::TimeTicks::Now(); | 483 base::TimeTicks current_time = base::TimeTicks::Now(); |
| 487 base::TimeDelta delay_so_far = current_time - load_start_time_; | 484 base::TimeDelta delay_so_far = current_time - load_start_time_; |
| 488 // We would prefer to use int64_t here, but JSON does not support that type. | 485 // We would prefer to use int64_t here, but JSON does not support that type. |
| 489 // Given the choice between int and double, we choose to implicitly convert to | 486 // Given the choice between int and double, we choose to implicitly convert to |
| 490 // a double since it maintains more precision (we can get a longer time in | 487 // a double since it maintains more precision (we can get a longer time in |
| 491 // milliseconds than we can with a 2 bit int, 53 bits vs 32). | 488 // milliseconds than we can with a 2 bit int, 53 bits vs 32). |
| 492 double delay = delay_so_far.InMilliseconds(); | 489 double delay = delay_so_far.InMilliseconds(); |
| 493 signal_data_.SetDouble(signal_name, delay); | 490 signal_data_.SetDouble(signal_name, delay); |
| 494 } | 491 } |
| 495 | 492 |
| 493 void BackgroundLoaderOffliner::AddResourceSignal(const ResourceDataType type, | |
| 494 int64_t started_count, | |
| 495 int64_t completed_count) { | |
| 496 double percentage = 100.0 * static_cast<double>(completed_count) / | |
|
Dmitry Titov
2017/05/18 02:28:10
Not clear why do we need the percentage here in ad
Pete Williamson
2017/05/24 01:01:50
OK, removed percentage.
| |
| 497 static_cast<double>(started_count); | |
| 498 // TODO(petewil): Use actual signal type instead of hardcoding name to image. | |
| 499 signal_data_.SetDouble("ImagePercentage", percentage); | |
| 500 signal_data_.SetDouble("StartedImages", started_count); | |
| 501 signal_data_.SetDouble("CompletedImages", completed_count); | |
| 502 } | |
| 503 | |
| 496 } // namespace offline_pages | 504 } // namespace offline_pages |
| 497 | |
| 498 DEFINE_WEB_CONTENTS_USER_DATA_KEY(offline_pages::OfflinerData); | |
| OLD | NEW |