| 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" |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 72 case prerender::FINAL_STATUS_SSL_CLIENT_CERTIFICATE_REQUESTED: | 72 case prerender::FINAL_STATUS_SSL_CLIENT_CERTIFICATE_REQUESTED: |
| 73 case prerender::FINAL_STATUS_WINDOW_PRINT: | 73 case prerender::FINAL_STATUS_WINDOW_PRINT: |
| 74 default: | 74 default: |
| 75 return Offliner::RequestStatus::LOADING_FAILED; | 75 return Offliner::RequestStatus::LOADING_FAILED; |
| 76 } | 76 } |
| 77 } | 77 } |
| 78 | 78 |
| 79 PrerenderingLoader::PrerenderingLoader(content::BrowserContext* browser_context) | 79 PrerenderingLoader::PrerenderingLoader(content::BrowserContext* browser_context) |
| 80 : state_(State::IDLE), | 80 : state_(State::IDLE), |
| 81 snapshot_controller_(nullptr), | 81 snapshot_controller_(nullptr), |
| 82 browser_context_(browser_context) { | 82 browser_context_(browser_context), |
| 83 is_lowbar_met_(false) { |
| 83 adapter_.reset(new PrerenderAdapter(this)); | 84 adapter_.reset(new PrerenderAdapter(this)); |
| 84 } | 85 } |
| 85 | 86 |
| 86 PrerenderingLoader::~PrerenderingLoader() { | 87 PrerenderingLoader::~PrerenderingLoader() { |
| 87 CancelPrerender(); | 88 CancelPrerender(); |
| 88 } | 89 } |
| 89 | 90 |
| 90 bool PrerenderingLoader::LoadPage(const GURL& url, | 91 bool PrerenderingLoader::LoadPage(const GURL& url, |
| 91 const LoadPageCallback& load_done_callback, | 92 const LoadPageCallback& load_done_callback, |
| 92 const ProgressCallback& progress_callback) { | 93 const ProgressCallback& progress_callback) { |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 152 // when to consider it really LOADED. | 153 // when to consider it really LOADED. |
| 153 snapshot_controller_->DocumentOnLoadCompletedInMainFrame(); | 154 snapshot_controller_->DocumentOnLoadCompletedInMainFrame(); |
| 154 } | 155 } |
| 155 | 156 |
| 156 void PrerenderingLoader::OnPrerenderDomContentLoaded() { | 157 void PrerenderingLoader::OnPrerenderDomContentLoaded() { |
| 157 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 158 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 158 DCHECK(!IsIdle()); | 159 DCHECK(!IsIdle()); |
| 159 if (!adapter_->GetWebContents()) { | 160 if (!adapter_->GetWebContents()) { |
| 160 // Without a WebContents object at this point, we are done. | 161 // Without a WebContents object at this point, we are done. |
| 161 HandleLoadingStopped(); | 162 HandleLoadingStopped(); |
| 162 } else if (kConsiderDclForSnapshot) { | 163 } else { |
| 163 // Inform SnapshotController of DomContentLoaded event so it can | 164 is_lowbar_met_ = true; |
| 164 // determine when to consider it really LOADED (e.g., some multiple | 165 if (kConsiderDclForSnapshot) { |
| 165 // second delay from this event). | 166 // Inform SnapshotController of DomContentLoaded event so it can |
| 166 snapshot_controller_->DocumentAvailableInMainFrame(); | 167 // determine when to consider it really LOADED (e.g., some multiple |
| 168 // second delay from this event). |
| 169 snapshot_controller_->DocumentAvailableInMainFrame(); |
| 170 } |
| 167 } | 171 } |
| 168 } | 172 } |
| 169 | 173 |
| 170 void PrerenderingLoader::OnPrerenderStop() { | 174 void PrerenderingLoader::OnPrerenderStop() { |
| 171 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 175 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 172 HandleLoadingStopped(); | 176 HandleLoadingStopped(); |
| 173 } | 177 } |
| 174 | 178 |
| 175 void PrerenderingLoader::OnPrerenderNetworkBytesChanged(int64_t bytes) { | 179 void PrerenderingLoader::OnPrerenderNetworkBytesChanged(int64_t bytes) { |
| 176 if (state_ == State::LOADING) | 180 if (state_ == State::LOADING) |
| 177 progress_callback_.Run(bytes); | 181 progress_callback_.Run(bytes); |
| 178 } | 182 } |
| 179 | 183 |
| 180 void PrerenderingLoader::StartSnapshot() { | 184 void PrerenderingLoader::StartSnapshot() { |
| 181 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 185 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 182 HandleLoadEvent(); | 186 HandleLoadEvent(); |
| 183 } | 187 } |
| 184 | 188 |
| 189 bool PrerenderingLoader::IsLowbarMet() { |
| 190 return is_lowbar_met_; |
| 191 } |
| 192 |
| 185 void PrerenderingLoader::HandleLoadEvent() { | 193 void PrerenderingLoader::HandleLoadEvent() { |
| 186 // If still loading, check if the load succeeded or not, then update | 194 // If still loading, check if the load succeeded or not, then update |
| 187 // the internal state (LOADED for success or IDLE for failure) and post | 195 // the internal state (LOADED for success or IDLE for failure) and post |
| 188 // callback. | 196 // callback. |
| 189 // Note: it is possible to receive a load event (e.g., if timeout-based) | 197 // Note: it is possible to receive a load event (e.g., if timeout-based) |
| 190 // after the request has completed via another path (e.g., canceled) so | 198 // after the request has completed via another path (e.g., canceled) so |
| 191 // the Loader may be idle at this point. | 199 // the Loader may be idle at this point. |
| 192 | 200 |
| 193 if (IsIdle() || IsLoaded()) | 201 if (IsIdle() || IsLoaded()) |
| 194 return; | 202 return; |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 244 // Now clean up the active prerendering operation detail. | 252 // Now clean up the active prerendering operation detail. |
| 245 adapter_->DestroyActive(); | 253 adapter_->DestroyActive(); |
| 246 } else { | 254 } else { |
| 247 // No access to FinalStatus so classify as retryable failure. | 255 // No access to FinalStatus so classify as retryable failure. |
| 248 request_status = Offliner::RequestStatus::LOADING_FAILED; | 256 request_status = Offliner::RequestStatus::LOADING_FAILED; |
| 249 } | 257 } |
| 250 | 258 |
| 251 snapshot_controller_.reset(nullptr); | 259 snapshot_controller_.reset(nullptr); |
| 252 session_contents_.reset(nullptr); | 260 session_contents_.reset(nullptr); |
| 253 state_ = State::IDLE; | 261 state_ = State::IDLE; |
| 262 is_lowbar_met_ = false; |
| 254 base::ThreadTaskRunnerHandle::Get()->PostTask( | 263 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 255 FROM_HERE, base::Bind(load_done_callback_, request_status, nullptr)); | 264 FROM_HERE, base::Bind(load_done_callback_, request_status, nullptr)); |
| 256 } | 265 } |
| 257 | 266 |
| 258 void PrerenderingLoader::CancelPrerender() { | 267 void PrerenderingLoader::CancelPrerender() { |
| 259 if (adapter_->IsActive()) { | 268 if (adapter_->IsActive()) { |
| 260 adapter_->DestroyActive(); | 269 adapter_->DestroyActive(); |
| 261 } | 270 } |
| 262 snapshot_controller_.reset(nullptr); | 271 snapshot_controller_.reset(nullptr); |
| 263 session_contents_.reset(nullptr); | 272 session_contents_.reset(nullptr); |
| 264 state_ = State::IDLE; | 273 state_ = State::IDLE; |
| 274 is_lowbar_met_ = false; |
| 265 } | 275 } |
| 266 | 276 |
| 267 } // namespace offline_pages | 277 } // namespace offline_pages |
| OLD | NEW |