| 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/android/offline_pages/prerendering_loader.h" | 5 #include "chrome/browser/android/offline_pages/prerendering_loader.h" |
| 6 | 6 |
| 7 #include "base/location.h" | 7 #include "base/location.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/metrics/histogram_macros.h" | 9 #include "base/metrics/histogram_macros.h" |
| 10 #include "base/threading/thread_task_runner_handle.h" | 10 #include "base/threading/thread_task_runner_handle.h" |
| 11 #include "base/time/time.h" |
| 11 #include "chrome/browser/profiles/profile.h" | 12 #include "chrome/browser/profiles/profile.h" |
| 12 #include "content/public/browser/browser_context.h" | 13 #include "content/public/browser/browser_context.h" |
| 13 #include "content/public/browser/browser_thread.h" | 14 #include "content/public/browser/browser_thread.h" |
| 14 #include "content/public/browser/web_contents.h" | 15 #include "content/public/browser/web_contents.h" |
| 15 #include "net/base/network_change_notifier.h" | 16 #include "net/base/network_change_notifier.h" |
| 16 #include "ui/gfx/geometry/size.h" | 17 #include "ui/gfx/geometry/size.h" |
| 17 | 18 |
| 18 namespace { | 19 namespace { |
| 19 // Whether to report DomContentLoaded event to the snapshot controller. | 20 // Whether to report DomContentLoaded event to the snapshot controller. |
| 20 bool kConsiderDclForSnapshot = false; | 21 bool kConsiderDclForSnapshot = false; |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 81 snapshot_controller_(nullptr), | 82 snapshot_controller_(nullptr), |
| 82 browser_context_(browser_context), | 83 browser_context_(browser_context), |
| 83 is_lowbar_met_(false) { | 84 is_lowbar_met_(false) { |
| 84 adapter_.reset(new PrerenderAdapter(this)); | 85 adapter_.reset(new PrerenderAdapter(this)); |
| 85 } | 86 } |
| 86 | 87 |
| 87 PrerenderingLoader::~PrerenderingLoader() { | 88 PrerenderingLoader::~PrerenderingLoader() { |
| 88 CancelPrerender(); | 89 CancelPrerender(); |
| 89 } | 90 } |
| 90 | 91 |
| 92 void PrerenderingLoader::AddLoadingSignal(const char* signal_name) { |
| 93 std::string signal(signal_name); |
| 94 signal += ": "; |
| 95 // We need a time in milliseconds, and JavaTime provides that. |
| 96 signal += std::to_string(base::Time::Now().ToJavaTime()); |
| 97 signal_data_.push_back(signal); |
| 98 } |
| 99 |
| 91 bool PrerenderingLoader::LoadPage(const GURL& url, | 100 bool PrerenderingLoader::LoadPage(const GURL& url, |
| 92 const LoadPageCallback& load_done_callback, | 101 const LoadPageCallback& load_done_callback, |
| 93 const ProgressCallback& progress_callback) { | 102 const ProgressCallback& progress_callback) { |
| 94 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 103 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 95 if (!IsIdle()) { | 104 if (!IsIdle()) { |
| 96 DVLOG(1) | 105 DVLOG(1) |
| 97 << "WARNING: Existing request in progress or waiting for StopLoading()"; | 106 << "WARNING: Existing request in progress or waiting for StopLoading()"; |
| 98 return false; | 107 return false; |
| 99 } | 108 } |
| 100 | 109 |
| 110 // Add this signal to signal_data_. |
| 111 AddLoadingSignal("StartLoading"); |
| 112 |
| 101 // Create a WebContents instance to define and hold a SessionStorageNamespace | 113 // Create a WebContents instance to define and hold a SessionStorageNamespace |
| 102 // for this load request. | 114 // for this load request. |
| 103 DCHECK(!session_contents_.get()); | 115 DCHECK(!session_contents_.get()); |
| 104 std::unique_ptr<content::WebContents> new_web_contents( | 116 std::unique_ptr<content::WebContents> new_web_contents( |
| 105 content::WebContents::Create( | 117 content::WebContents::Create( |
| 106 content::WebContents::CreateParams(browser_context_))); | 118 content::WebContents::CreateParams(browser_context_))); |
| 107 content::SessionStorageNamespace* sessionStorageNamespace = | 119 content::SessionStorageNamespace* sessionStorageNamespace = |
| 108 new_web_contents->GetController().GetDefaultSessionStorageNamespace(); | 120 new_web_contents->GetController().GetDefaultSessionStorageNamespace(); |
| 109 gfx::Size renderWindowSize = new_web_contents->GetContainerBounds().size(); | 121 gfx::Size renderWindowSize = new_web_contents->GetContainerBounds().size(); |
| 110 bool accepted = adapter_->StartPrerender( | 122 bool accepted = adapter_->StartPrerender( |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 145 adapter_ = std::move(prerender_adapter); | 157 adapter_ = std::move(prerender_adapter); |
| 146 } | 158 } |
| 147 | 159 |
| 148 void PrerenderingLoader::OnPrerenderStopLoading() { | 160 void PrerenderingLoader::OnPrerenderStopLoading() { |
| 149 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 161 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 150 DCHECK(!IsIdle()); | 162 DCHECK(!IsIdle()); |
| 151 DCHECK(adapter_->GetWebContents()); | 163 DCHECK(adapter_->GetWebContents()); |
| 152 // Inform SnapshotController of OnLoad event so it can determine | 164 // Inform SnapshotController of OnLoad event so it can determine |
| 153 // when to consider it really LOADED. | 165 // when to consider it really LOADED. |
| 154 snapshot_controller_->DocumentOnLoadCompletedInMainFrame(); | 166 snapshot_controller_->DocumentOnLoadCompletedInMainFrame(); |
| 167 |
| 168 // Add this signal to signal_data_. |
| 169 AddLoadingSignal("OnLoad"); |
| 155 } | 170 } |
| 156 | 171 |
| 157 void PrerenderingLoader::OnPrerenderDomContentLoaded() { | 172 void PrerenderingLoader::OnPrerenderDomContentLoaded() { |
| 158 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 173 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 159 DCHECK(!IsIdle()); | 174 DCHECK(!IsIdle()); |
| 160 if (!adapter_->GetWebContents()) { | 175 if (!adapter_->GetWebContents()) { |
| 161 // Without a WebContents object at this point, we are done. | 176 // Without a WebContents object at this point, we are done. |
| 162 HandleLoadingStopped(); | 177 HandleLoadingStopped(); |
| 163 } else { | 178 } else { |
| 164 is_lowbar_met_ = true; | 179 is_lowbar_met_ = true; |
| 165 if (kConsiderDclForSnapshot) { | 180 if (kConsiderDclForSnapshot) { |
| 166 // Inform SnapshotController of DomContentLoaded event so it can | 181 // Inform SnapshotController of DomContentLoaded event so it can |
| 167 // determine when to consider it really LOADED (e.g., some multiple | 182 // determine when to consider it really LOADED (e.g., some multiple |
| 168 // second delay from this event). | 183 // second delay from this event). |
| 169 snapshot_controller_->DocumentAvailableInMainFrame(); | 184 snapshot_controller_->DocumentAvailableInMainFrame(); |
| 170 } | 185 } |
| 186 |
| 187 // Add this signal to signal_data_. |
| 188 AddLoadingSignal("OnDomContentLoaded"); |
| 171 } | 189 } |
| 172 } | 190 } |
| 173 | 191 |
| 174 void PrerenderingLoader::OnPrerenderStop() { | 192 void PrerenderingLoader::OnPrerenderStop() { |
| 175 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 193 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 176 HandleLoadingStopped(); | 194 HandleLoadingStopped(); |
| 177 } | 195 } |
| 178 | 196 |
| 179 void PrerenderingLoader::OnPrerenderNetworkBytesChanged(int64_t bytes) { | 197 void PrerenderingLoader::OnPrerenderNetworkBytesChanged(int64_t bytes) { |
| 180 if (state_ == State::LOADING) | 198 if (state_ == State::LOADING) |
| 181 progress_callback_.Run(bytes); | 199 progress_callback_.Run(bytes); |
| 182 } | 200 } |
| 183 | 201 |
| 184 void PrerenderingLoader::StartSnapshot() { | 202 void PrerenderingLoader::StartSnapshot() { |
| 185 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 203 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 204 |
| 205 // Add this signal to signal_data_. |
| 206 AddLoadingSignal("Snapshotting"); |
| 207 |
| 186 HandleLoadEvent(); | 208 HandleLoadEvent(); |
| 187 } | 209 } |
| 188 | 210 |
| 189 bool PrerenderingLoader::IsLowbarMet() { | 211 bool PrerenderingLoader::IsLowbarMet() { |
| 190 return is_lowbar_met_; | 212 return is_lowbar_met_; |
| 191 } | 213 } |
| 192 | 214 |
| 193 void PrerenderingLoader::HandleLoadEvent() { | 215 void PrerenderingLoader::HandleLoadEvent() { |
| 194 // If still loading, check if the load succeeded or not, then update | 216 // If still loading, check if the load succeeded or not, then update |
| 195 // the internal state (LOADED for success or IDLE for failure) and post | 217 // the internal state (LOADED for success or IDLE for failure) and post |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 268 if (adapter_->IsActive()) { | 290 if (adapter_->IsActive()) { |
| 269 adapter_->DestroyActive(); | 291 adapter_->DestroyActive(); |
| 270 } | 292 } |
| 271 snapshot_controller_.reset(nullptr); | 293 snapshot_controller_.reset(nullptr); |
| 272 session_contents_.reset(nullptr); | 294 session_contents_.reset(nullptr); |
| 273 state_ = State::IDLE; | 295 state_ = State::IDLE; |
| 274 is_lowbar_met_ = false; | 296 is_lowbar_met_ = false; |
| 275 } | 297 } |
| 276 | 298 |
| 277 } // namespace offline_pages | 299 } // namespace offline_pages |
| OLD | NEW |