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); | |
|
chili
2017/05/17 18:00:07
just out of curiosity, what would happen if you do
Pete Williamson
2017/05/24 01:01:50
You get a nullptr with a C++ type "BackgroundLoade
| |
| 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(contents); | |
|
chili
2017/05/17 18:00:07
Do we want ALL the metrics observers? Assuming the
Charlie Harrison
2017/05/17 18:30:31
+1 can you explain the consequences of this change
RyanSturm
2017/05/17 18:36:58
I believe the navigation should be entirely in the
RyanSturm
2017/05/17 18:47:54
1 more thing. Is the background offliner a replace
Pete Williamson
2017/05/17 19:50:07
Chili: We do want to use several metrics observers
RyanSturm
2017/05/17 19:54:32
WRT to point 2.
My question was a little more sub
Charlie Harrison
2017/05/17 19:59:07
Most observers don't want to be notified of preren
Pete Williamson
2017/05/17 20:04:57
OK, let me clarify a bit and see if that helps:
To
Pete Williamson
2017/05/17 20:09:08
CHarrison: Ah, I didn't realize that some observer
Charlie Harrison
2017/05/17 20:14:30
Yeah I think it makes sense to align as closely wi
Pete Williamson
2017/05/17 22:01:15
CSHarrison: Done.
Pete Williamson
2017/05/24 01:01:50
We only want the ones we use, and InitializePLMFWC
| |
| 469 } | 465 } |
| 470 | 466 |
| 471 void BackgroundLoaderOffliner::OnApplicationStateChange( | 467 void BackgroundLoaderOffliner::OnApplicationStateChange( |
| 472 base::android::ApplicationState application_state) { | 468 base::android::ApplicationState application_state) { |
| 473 if (pending_request_ && is_low_end_device_ && | 469 if (pending_request_ && is_low_end_device_ && |
| 474 application_state == | 470 application_state == |
| 475 base::android::APPLICATION_STATE_HAS_RUNNING_ACTIVITIES) { | 471 base::android::APPLICATION_STATE_HAS_RUNNING_ACTIVITIES) { |
| 476 DVLOG(1) << "App became active, canceling current offlining request"; | 472 DVLOG(1) << "App became active, canceling current offlining request"; |
| 477 // No need to check the return value or complete early, as false would | 473 // 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 | 474 // indicate that there was no request, in which case the state change is |
| 479 // ignored. | 475 // ignored. |
| 480 Cancel( | 476 Cancel( |
| 481 base::Bind(HandleApplicationStateChangeCancel, completion_callback_)); | 477 base::Bind(HandleApplicationStateChangeCancel, completion_callback_)); |
| 482 } | 478 } |
| 483 } | 479 } |
| 484 | 480 |
| 485 void BackgroundLoaderOffliner::AddLoadingSignal(const char* signal_name) { | 481 void BackgroundLoaderOffliner::AddLoadingSignal(const char* signal_name) { |
| 486 base::TimeTicks current_time = base::TimeTicks::Now(); | 482 base::TimeTicks current_time = base::TimeTicks::Now(); |
| 487 base::TimeDelta delay_so_far = current_time - load_start_time_; | 483 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. | 484 // 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 | 485 // 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 | 486 // 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). | 487 // milliseconds than we can with a 2 bit int, 53 bits vs 32). |
| 492 double delay = delay_so_far.InMilliseconds(); | 488 double delay = delay_so_far.InMilliseconds(); |
| 493 signal_data_.SetDouble(signal_name, delay); | 489 signal_data_.SetDouble(signal_name, delay); |
| 494 } | 490 } |
| 495 | 491 |
| 492 void BackgroundLoaderOffliner::AddResourceSignal(const ResourceDataType type, | |
| 493 int64_t started_count, | |
| 494 int64_t completed_count) { | |
| 495 double percentage = 100.0 * static_cast<double>(completed_count) / | |
| 496 static_cast<double>(started_count); | |
| 497 // TODO(petewil): Use actual signal type instead of hardcoding name to image. | |
| 498 signal_data_.SetDouble("ImagePercentage", percentage); | |
| 499 signal_data_.SetDouble("StartedImages", started_count); | |
| 500 signal_data_.SetDouble("CompletedImages", completed_count); | |
| 501 } | |
| 502 | |
| 496 } // namespace offline_pages | 503 } // namespace offline_pages |
| 497 | |
| 498 DEFINE_WEB_CONTENTS_USER_DATA_KEY(offline_pages::OfflinerData); | |
| OLD | NEW |