Chromium Code Reviews| Index: chrome/browser/offline_pages/background_loader_offliner.cc |
| diff --git a/chrome/browser/offline_pages/background_loader_offliner.cc b/chrome/browser/offline_pages/background_loader_offliner.cc |
| index 6cc5f3d0fd5de7c4d71000927dc4948a3f6a4bcc..6e7609afae1eefae704761fabe77f1bd996737a0 100644 |
| --- a/chrome/browser/offline_pages/background_loader_offliner.cc |
| +++ b/chrome/browser/offline_pages/background_loader_offliner.cc |
| @@ -12,6 +12,8 @@ |
| #include "base/time/time.h" |
| #include "chrome/browser/android/offline_pages/offline_page_mhtml_archiver.h" |
| #include "chrome/browser/android/offline_pages/offliner_helper.h" |
| +#include "chrome/browser/offline_pages/offliner_user_data.h" |
| +#include "chrome/browser/page_load_metrics/page_load_metrics_initialize.h" |
| #include "chrome/browser/profiles/profile.h" |
| #include "components/offline_pages/core/background/offliner_policy.h" |
| #include "components/offline_pages/core/background/save_page_request.h" |
| @@ -33,28 +35,6 @@ const char kContentTransferEncodingBinary[] = |
| "Content-Transfer-Encoding: binary"; |
| const char kXHeaderForSignals[] = "X-Chrome-Loading-Metrics-Data: 1"; |
| -class OfflinerData : public content::WebContentsUserData<OfflinerData> { |
| - public: |
| - static void AddToWebContents(content::WebContents* webcontents, |
| - BackgroundLoaderOffliner* offliner) { |
| - DCHECK(offliner); |
| - webcontents->SetUserData(UserDataKey(), std::unique_ptr<OfflinerData>( |
| - new OfflinerData(offliner))); |
| - } |
| - |
| - explicit OfflinerData(BackgroundLoaderOffliner* offliner) { |
| - offliner_ = offliner; |
| - } |
| - BackgroundLoaderOffliner* offliner() { return offliner_; } |
| - |
| - private: |
| - // The offliner that the WebContents is attached to. The offliner owns the |
| - // Delegate which owns the WebContents that this data is attached to. |
| - // Therefore, its lifetime should exceed that of the WebContents, so this |
| - // should always be non-null. |
| - BackgroundLoaderOffliner* offliner_; |
| -}; |
| - |
| std::string AddHistogramSuffix(const ClientId& client_id, |
| const char* histogram_name) { |
| if (client_id.name_space.empty()) { |
| @@ -105,9 +85,12 @@ BackgroundLoaderOffliner::~BackgroundLoaderOffliner() {} |
| // static |
| BackgroundLoaderOffliner* BackgroundLoaderOffliner::FromWebContents( |
| content::WebContents* contents) { |
| - OfflinerData* data = OfflinerData::FromWebContents(contents); |
| - if (data) |
| - return data->offliner(); |
| + Offliner* offliner = OfflinerUserData::OfflinerFromWebContents(contents); |
| + |
| + // Today we only have one kind of offliner that uses OfflinerUserData. If we |
| + // add other types, revisit this cast. |
| + if (offliner) |
| + return static_cast<BackgroundLoaderOffliner*>(offliner); |
| return nullptr; |
| } |
| @@ -238,6 +221,15 @@ bool BackgroundLoaderOffliner::HandleTimeout(int64_t request_id) { |
| return false; |
| } |
| +void BackgroundLoaderOffliner::ObserveResourceTracking( |
| + const ResourceDataType type, |
| + int64_t started_count, |
| + int64_t completed_count) { |
| + // Add the signal to extra data, and use for tracking. |
| + if (type == ResourceDataType::IMAGE) |
| + AddResourceSignal(type, started_count, completed_count); |
| +} |
| + |
| void BackgroundLoaderOffliner::MarkLoadStartTime() { |
| load_start_time_ = base::TimeTicks::Now(); |
| } |
| @@ -465,7 +457,12 @@ void BackgroundLoaderOffliner::ResetLoader() { |
| void BackgroundLoaderOffliner::AttachObservers() { |
| content::WebContents* contents = loader_->web_contents(); |
| content::WebContentsObserver::Observe(contents); |
| - OfflinerData::AddToWebContents(contents, this); |
| + OfflinerUserData::AddToWebContents(contents, this); |
| + |
| + // Attach the metrics observers to the web contents so we can get resoure |
| + // loading signals. |
| + 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
|
| + contents, true /* background loading */); |
| } |
| void BackgroundLoaderOffliner::OnApplicationStateChange( |
| @@ -493,6 +490,15 @@ void BackgroundLoaderOffliner::AddLoadingSignal(const char* signal_name) { |
| signal_data_.SetDouble(signal_name, delay); |
| } |
| -} // namespace offline_pages |
| +void BackgroundLoaderOffliner::AddResourceSignal(const ResourceDataType type, |
| + int64_t started_count, |
| + int64_t completed_count) { |
| + 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.
|
| + static_cast<double>(started_count); |
| + // TODO(petewil): Use actual signal type instead of hardcoding name to image. |
| + signal_data_.SetDouble("ImagePercentage", percentage); |
| + signal_data_.SetDouble("StartedImages", started_count); |
| + signal_data_.SetDouble("CompletedImages", completed_count); |
| +} |
| -DEFINE_WEB_CONTENTS_USER_DATA_KEY(offline_pages::OfflinerData); |
| +} // namespace offline_pages |