| 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 Offliner::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 // TODO(petewil): Use actual signal type instead of hardcoding name to |
| 231 // image. |
| 232 signal_data_.SetDouble("StartedImages", started_count); |
| 233 signal_data_.SetDouble("CompletedImages", completed_count); |
| 234 } |
| 235 } |
| 236 |
| 241 void BackgroundLoaderOffliner::MarkLoadStartTime() { | 237 void BackgroundLoaderOffliner::MarkLoadStartTime() { |
| 242 load_start_time_ = base::TimeTicks::Now(); | 238 load_start_time_ = base::TimeTicks::Now(); |
| 243 } | 239 } |
| 244 | 240 |
| 245 void BackgroundLoaderOffliner::DocumentAvailableInMainFrame() { | 241 void BackgroundLoaderOffliner::DocumentAvailableInMainFrame() { |
| 246 snapshot_controller_->DocumentAvailableInMainFrame(); | 242 snapshot_controller_->DocumentAvailableInMainFrame(); |
| 247 is_low_bar_met_ = true; | 243 is_low_bar_met_ = true; |
| 248 | 244 |
| 249 // Add this signal to signal_data_. | 245 // Add this signal to signal_data_. |
| 250 AddLoadingSignal("DocumentAvailableInMainFrame"); | 246 AddLoadingSignal("DocumentAvailableInMainFrame"); |
| (...skipping 207 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 458 } | 454 } |
| 459 | 455 |
| 460 void BackgroundLoaderOffliner::ResetLoader() { | 456 void BackgroundLoaderOffliner::ResetLoader() { |
| 461 loader_.reset( | 457 loader_.reset( |
| 462 new background_loader::BackgroundLoaderContents(browser_context_)); | 458 new background_loader::BackgroundLoaderContents(browser_context_)); |
| 463 } | 459 } |
| 464 | 460 |
| 465 void BackgroundLoaderOffliner::AttachObservers() { | 461 void BackgroundLoaderOffliner::AttachObservers() { |
| 466 content::WebContents* contents = loader_->web_contents(); | 462 content::WebContents* contents = loader_->web_contents(); |
| 467 content::WebContentsObserver::Observe(contents); | 463 content::WebContentsObserver::Observe(contents); |
| 468 OfflinerData::AddToWebContents(contents, this); | 464 OfflinerUserData::AddToWebContents(contents, this); |
| 465 |
| 466 // Attach the metrics observers to the web contents so we can get resoure |
| 467 // loading signals. |
| 468 chrome::InitializePageLoadMetricsForWebContents( |
| 469 contents, true /* background loading */); |
| 469 } | 470 } |
| 470 | 471 |
| 471 void BackgroundLoaderOffliner::OnApplicationStateChange( | 472 void BackgroundLoaderOffliner::OnApplicationStateChange( |
| 472 base::android::ApplicationState application_state) { | 473 base::android::ApplicationState application_state) { |
| 473 if (pending_request_ && is_low_end_device_ && | 474 if (pending_request_ && is_low_end_device_ && |
| 474 application_state == | 475 application_state == |
| 475 base::android::APPLICATION_STATE_HAS_RUNNING_ACTIVITIES) { | 476 base::android::APPLICATION_STATE_HAS_RUNNING_ACTIVITIES) { |
| 476 DVLOG(1) << "App became active, canceling current offlining request"; | 477 DVLOG(1) << "App became active, canceling current offlining request"; |
| 477 // No need to check the return value or complete early, as false would | 478 // 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 | 479 // indicate that there was no request, in which case the state change is |
| 479 // ignored. | 480 // ignored. |
| 480 Cancel( | 481 Cancel( |
| 481 base::Bind(HandleApplicationStateChangeCancel, completion_callback_)); | 482 base::Bind(HandleApplicationStateChangeCancel, completion_callback_)); |
| 482 } | 483 } |
| 483 } | 484 } |
| 484 | 485 |
| 485 void BackgroundLoaderOffliner::AddLoadingSignal(const char* signal_name) { | 486 void BackgroundLoaderOffliner::AddLoadingSignal(const char* signal_name) { |
| 486 base::TimeTicks current_time = base::TimeTicks::Now(); | 487 base::TimeTicks current_time = base::TimeTicks::Now(); |
| 487 base::TimeDelta delay_so_far = current_time - load_start_time_; | 488 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. | 489 // 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 | 490 // 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 | 491 // 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). | 492 // milliseconds than we can with a 2 bit int, 53 bits vs 32). |
| 492 double delay = delay_so_far.InMilliseconds(); | 493 double delay = delay_so_far.InMilliseconds(); |
| 493 signal_data_.SetDouble(signal_name, delay); | 494 signal_data_.SetDouble(signal_name, delay); |
| 494 } | 495 } |
| 495 | 496 |
| 496 } // namespace offline_pages | 497 } // namespace offline_pages |
| 497 | |
| 498 DEFINE_WEB_CONTENTS_USER_DATA_KEY(offline_pages::OfflinerData); | |
| OLD | NEW |