| 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/threading/thread_task_runner_handle.h" | 9 #include "base/threading/thread_task_runner_handle.h" |
| 10 #include "chrome/browser/profiles/profile.h" | 10 #include "chrome/browser/profiles/profile.h" |
| 11 #include "content/public/browser/browser_context.h" | 11 #include "content/public/browser/browser_context.h" |
| 12 #include "content/public/browser/browser_thread.h" | 12 #include "content/public/browser/browser_thread.h" |
| 13 #include "content/public/browser/web_contents.h" | 13 #include "content/public/browser/web_contents.h" |
| 14 #include "ui/gfx/geometry/size.h" | 14 #include "ui/gfx/geometry/size.h" |
| 15 | 15 |
| 16 namespace offline_pages { | 16 namespace offline_pages { |
| 17 | 17 |
| 18 PrerenderingLoader::PrerenderingLoader(content::BrowserContext* browser_context) | 18 PrerenderingLoader::PrerenderingLoader(content::BrowserContext* browser_context) |
| 19 : state_(State::IDLE), browser_context_(browser_context) { | 19 : state_(State::IDLE), |
| 20 snapshot_controller_(nullptr), |
| 21 browser_context_(browser_context) { |
| 20 adapter_.reset(new PrerenderAdapter(this)); | 22 adapter_.reset(new PrerenderAdapter(this)); |
| 21 } | 23 } |
| 22 | 24 |
| 23 PrerenderingLoader::~PrerenderingLoader() { | 25 PrerenderingLoader::~PrerenderingLoader() { |
| 24 CancelPrerender(); | 26 CancelPrerender(); |
| 25 } | 27 } |
| 26 | 28 |
| 27 bool PrerenderingLoader::LoadPage(const GURL& url, | 29 bool PrerenderingLoader::LoadPage(const GURL& url, |
| 28 const LoadPageCallback& callback) { | 30 const LoadPageCallback& callback) { |
| 29 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 31 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 42 content::WebContents::CreateParams(browser_context_))); | 44 content::WebContents::CreateParams(browser_context_))); |
| 43 content::SessionStorageNamespace* sessionStorageNamespace = | 45 content::SessionStorageNamespace* sessionStorageNamespace = |
| 44 session_contents_->GetController().GetDefaultSessionStorageNamespace(); | 46 session_contents_->GetController().GetDefaultSessionStorageNamespace(); |
| 45 gfx::Size renderWindowSize = session_contents_->GetContainerBounds().size(); | 47 gfx::Size renderWindowSize = session_contents_->GetContainerBounds().size(); |
| 46 bool accepted = adapter_->StartPrerender( | 48 bool accepted = adapter_->StartPrerender( |
| 47 browser_context_, url, sessionStorageNamespace, renderWindowSize); | 49 browser_context_, url, sessionStorageNamespace, renderWindowSize); |
| 48 if (!accepted) | 50 if (!accepted) |
| 49 return false; | 51 return false; |
| 50 | 52 |
| 51 DCHECK(adapter_->IsActive()); | 53 DCHECK(adapter_->IsActive()); |
| 54 snapshot_controller_.reset( |
| 55 new SnapshotController(base::ThreadTaskRunnerHandle::Get(), this)); |
| 52 callback_ = callback; | 56 callback_ = callback; |
| 53 state_ = State::PENDING; | 57 state_ = State::PENDING; |
| 54 return true; | 58 return true; |
| 55 } | 59 } |
| 56 | 60 |
| 57 void PrerenderingLoader::StopLoading() { | 61 void PrerenderingLoader::StopLoading() { |
| 58 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 62 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 59 CancelPrerender(); | 63 CancelPrerender(); |
| 60 } | 64 } |
| 61 | 65 |
| (...skipping 19 matching lines...) Expand all Loading... |
| 81 } | 85 } |
| 82 | 86 |
| 83 void PrerenderingLoader::OnPrerenderStart() { | 87 void PrerenderingLoader::OnPrerenderStart() { |
| 84 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 88 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 85 DCHECK(state_ == State::PENDING); | 89 DCHECK(state_ == State::PENDING); |
| 86 state_ = State::LOADING; | 90 state_ = State::LOADING; |
| 87 } | 91 } |
| 88 | 92 |
| 89 void PrerenderingLoader::OnPrerenderStopLoading() { | 93 void PrerenderingLoader::OnPrerenderStopLoading() { |
| 90 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 94 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 91 // TODO(dougarnett): Implement/integrate to delay policy here. | 95 DCHECK(!IsIdle()); |
| 92 HandleLoadEvent(); | 96 DCHECK(adapter_->GetWebContents()); |
| 97 // Inform SnapshotController of OnLoad event so it can determine |
| 98 // when to consider it really LOADED. |
| 99 snapshot_controller_->DocumentOnLoadCompletedInMainFrame(); |
| 93 } | 100 } |
| 94 | 101 |
| 95 void PrerenderingLoader::OnPrerenderDomContentLoaded() { | 102 void PrerenderingLoader::OnPrerenderDomContentLoaded() { |
| 96 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 103 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 97 // TODO(dougarnett): Implement/integrate to delay policy here. | 104 DCHECK(!IsIdle()); |
| 98 HandleLoadEvent(); | 105 if (!adapter_->GetWebContents()) { |
| 106 // Without a WebContents object at this point, we are done. |
| 107 HandleLoadingStopped(); |
| 108 } else { |
| 109 // Inform SnapshotController of DomContentContent event so it can |
| 110 // determine when to consider it really LOADED (e.g., some multiple |
| 111 // second delay from this event). |
| 112 snapshot_controller_->DocumentAvailableInMainFrame(); |
| 113 } |
| 99 } | 114 } |
| 100 | 115 |
| 101 void PrerenderingLoader::OnPrerenderStop() { | 116 void PrerenderingLoader::OnPrerenderStop() { |
| 102 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 117 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 103 HandleLoadingStopped(); | 118 HandleLoadingStopped(); |
| 104 } | 119 } |
| 105 | 120 |
| 121 void PrerenderingLoader::StartSnapshot() { |
| 122 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 123 HandleLoadEvent(); |
| 124 } |
| 125 |
| 106 void PrerenderingLoader::HandleLoadEvent() { | 126 void PrerenderingLoader::HandleLoadEvent() { |
| 107 // If still loading, check if the load succeeded or not, then update | 127 // If still loading, check if the load succeeded or not, then update |
| 108 // the internal state (LOADED for success or IDLE for failure) and post | 128 // the internal state (LOADED for success or IDLE for failure) and post |
| 109 // callback. | 129 // callback. |
| 110 // Note: it is possible to receive a load event (e.g., if timeout-based) | 130 // Note: it is possible to receive a load event (e.g., if timeout-based) |
| 111 // after the request has completed via another path (e.g., canceled) so | 131 // after the request has completed via another path (e.g., canceled) so |
| 112 // the Loader may be idle at this point. | 132 // the Loader may be idle at this point. |
| 113 | 133 |
| 114 if (IsIdle() || IsLoaded()) | 134 if (IsIdle() || IsLoaded()) |
| 115 return; | 135 return; |
| (...skipping 25 matching lines...) Expand all Loading... |
| 141 DVLOG(1) << "Load failed: " << adapter_->GetFinalStatus(); | 161 DVLOG(1) << "Load failed: " << adapter_->GetFinalStatus(); |
| 142 adapter_->DestroyActive(); | 162 adapter_->DestroyActive(); |
| 143 } | 163 } |
| 144 // Request status depends on whether we are still loading (failed) or | 164 // Request status depends on whether we are still loading (failed) or |
| 145 // did load and then loading was stopped (cancel - from prerender stack). | 165 // did load and then loading was stopped (cancel - from prerender stack). |
| 146 Offliner::RequestStatus request_status = | 166 Offliner::RequestStatus request_status = |
| 147 IsLoaded() ? Offliner::RequestStatus::CANCELED | 167 IsLoaded() ? Offliner::RequestStatus::CANCELED |
| 148 : Offliner::RequestStatus::FAILED; | 168 : Offliner::RequestStatus::FAILED; |
| 149 // TODO(dougarnett): For failure, determine from final status if retry-able | 169 // TODO(dougarnett): For failure, determine from final status if retry-able |
| 150 // and report different failure statuses if retry-able or not. | 170 // and report different failure statuses if retry-able or not. |
| 171 snapshot_controller_.reset(nullptr); |
| 151 session_contents_.reset(nullptr); | 172 session_contents_.reset(nullptr); |
| 152 state_ = State::IDLE; | 173 state_ = State::IDLE; |
| 153 base::ThreadTaskRunnerHandle::Get()->PostTask( | 174 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 154 FROM_HERE, base::Bind(callback_, request_status, nullptr)); | 175 FROM_HERE, base::Bind(callback_, request_status, nullptr)); |
| 155 } | 176 } |
| 156 | 177 |
| 157 void PrerenderingLoader::CancelPrerender() { | 178 void PrerenderingLoader::CancelPrerender() { |
| 158 if (adapter_->IsActive()) { | 179 if (adapter_->IsActive()) { |
| 159 adapter_->DestroyActive(); | 180 adapter_->DestroyActive(); |
| 160 } | 181 } |
| 182 snapshot_controller_.reset(nullptr); |
| 161 session_contents_.reset(nullptr); | 183 session_contents_.reset(nullptr); |
| 162 if (!IsLoaded() && !IsIdle()) { | 184 if (!IsLoaded() && !IsIdle()) { |
| 163 base::ThreadTaskRunnerHandle::Get()->PostTask( | 185 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 164 FROM_HERE, | 186 FROM_HERE, |
| 165 base::Bind(callback_, Offliner::RequestStatus::CANCELED, nullptr)); | 187 base::Bind(callback_, Offliner::RequestStatus::CANCELED, nullptr)); |
| 166 } | 188 } |
| 167 state_ = State::IDLE; | 189 state_ = State::IDLE; |
| 168 } | 190 } |
| 169 | 191 |
| 170 } // namespace offline_pages | 192 } // namespace offline_pages |
| OLD | NEW |