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/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), browser_context_(browser_context) { |
| 20 adapter_.reset(new PrerenderAdapter(this)); | 20 adapter_.reset(new PrerenderAdapter(this)); |
| 21 snapshot_controller_.reset( | |
| 22 new SnapshotController(base::ThreadTaskRunnerHandle::Get(), 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); |
| 30 if (!IsIdle()) { | 32 if (!IsIdle()) { |
| (...skipping 11 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 // Reset the SnapshotController for this new load request. | |
| 55 snapshot_controller_->Reset(); | |
|
Dmitry Titov
2016/06/09 17:41:01
this is only needed if the LoadPage can be called
dougarnett
2016/06/09 18:25:46
Ok, creating new instance each LoadPage now
| |
| 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 if (!IsLoaded()) { |
|
Dmitry Titov
2016/06/09 17:41:01
I don't think these checks are really needed, espe
dougarnett
2016/06/09 18:25:46
Done.
| |
| 97 DCHECK(adapter_->GetWebContents()); | |
| 98 // Inform SnapshotController of OnLoad event so it can determine | |
| 99 // when to consider it really LOADED. | |
| 100 snapshot_controller_->DocumentOnLoadCompletedInMainFrame(); | |
| 101 } | |
| 93 } | 102 } |
| 94 | 103 |
| 95 void PrerenderingLoader::OnPrerenderDomContentLoaded() { | 104 void PrerenderingLoader::OnPrerenderDomContentLoaded() { |
| 96 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 105 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 97 // TODO(dougarnett): Implement/integrate to delay policy here. | 106 DCHECK(!IsIdle()); |
| 98 HandleLoadEvent(); | 107 if (!IsLoaded()) { |
|
Dmitry Titov
2016/06/09 17:41:01
Same as above
dougarnett
2016/06/09 18:25:46
Done.
| |
| 108 if (!adapter_->GetWebContents()) { | |
| 109 // Without a WebContents object at this point, we are done. | |
| 110 HandleLoadingStopped(); | |
| 111 } else { | |
| 112 // Inform SnapshotController of DomContentContent event so it can | |
| 113 // determine when to consider it really LOADED (e.g., some multiple | |
| 114 // second delay from this event). | |
| 115 snapshot_controller_->DocumentAvailableInMainFrame(); | |
| 116 } | |
| 117 } | |
| 99 } | 118 } |
| 100 | 119 |
| 101 void PrerenderingLoader::OnPrerenderStop() { | 120 void PrerenderingLoader::OnPrerenderStop() { |
| 102 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 121 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 103 HandleLoadingStopped(); | 122 HandleLoadingStopped(); |
| 104 } | 123 } |
| 105 | 124 |
| 125 void PrerenderingLoader::StartSnapshot() { | |
| 126 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 127 DCHECK(!IsIdle()); | |
| 128 DCHECK(!IsLoaded()); | |
|
Dmitry Titov
2016/06/09 17:41:01
This is confusing. Why start snapshot on something
dougarnett
2016/06/09 18:25:46
Precondition check, we are now waiting for StartSn
| |
| 129 HandleLoadEvent(); | |
| 130 } | |
| 131 | |
| 106 void PrerenderingLoader::HandleLoadEvent() { | 132 void PrerenderingLoader::HandleLoadEvent() { |
| 107 // If still loading, check if the load succeeded or not, then update | 133 // 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 | 134 // the internal state (LOADED for success or IDLE for failure) and post |
| 109 // callback. | 135 // callback. |
| 110 // Note: it is possible to receive a load event (e.g., if timeout-based) | 136 // 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 | 137 // after the request has completed via another path (e.g., canceled) so |
| 112 // the Loader may be idle at this point. | 138 // the Loader may be idle at this point. |
| 113 | 139 |
| 114 if (IsIdle() || IsLoaded()) | 140 if (IsIdle() || IsLoaded()) |
| 115 return; | 141 return; |
| 116 | 142 |
| 117 content::WebContents* web_contents = adapter_->GetWebContents(); | 143 content::WebContents* web_contents = adapter_->GetWebContents(); |
| 118 if (web_contents) { | 144 if (web_contents) { |
| 145 snapshot_controller_->Stop(); | |
|
Dmitry Titov
2016/06/09 17:41:01
No need ot call Stop here, while you didn't report
dougarnett
2016/06/09 18:25:46
Done.
| |
| 119 state_ = State::LOADED; | 146 state_ = State::LOADED; |
| 120 base::ThreadTaskRunnerHandle::Get()->PostTask( | 147 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 121 FROM_HERE, | 148 FROM_HERE, |
| 122 base::Bind(callback_, Offliner::RequestStatus::LOADED, web_contents)); | 149 base::Bind(callback_, Offliner::RequestStatus::LOADED, web_contents)); |
| 123 } else { | 150 } else { |
| 124 // No WebContents means that the load failed (and it stopped). | 151 // No WebContents means that the load failed (and it stopped). |
| 125 HandleLoadingStopped(); | 152 HandleLoadingStopped(); |
| 126 } | 153 } |
| 127 } | 154 } |
| 128 | 155 |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 141 DVLOG(1) << "Load failed: " << adapter_->GetFinalStatus(); | 168 DVLOG(1) << "Load failed: " << adapter_->GetFinalStatus(); |
| 142 adapter_->DestroyActive(); | 169 adapter_->DestroyActive(); |
| 143 } | 170 } |
| 144 // Request status depends on whether we are still loading (failed) or | 171 // Request status depends on whether we are still loading (failed) or |
| 145 // did load and then loading was stopped (cancel - from prerender stack). | 172 // did load and then loading was stopped (cancel - from prerender stack). |
| 146 Offliner::RequestStatus request_status = | 173 Offliner::RequestStatus request_status = |
| 147 IsLoaded() ? Offliner::RequestStatus::CANCELED | 174 IsLoaded() ? Offliner::RequestStatus::CANCELED |
| 148 : Offliner::RequestStatus::FAILED; | 175 : Offliner::RequestStatus::FAILED; |
| 149 // TODO(dougarnett): For failure, determine from final status if retry-able | 176 // TODO(dougarnett): For failure, determine from final status if retry-able |
| 150 // and report different failure statuses if retry-able or not. | 177 // and report different failure statuses if retry-able or not. |
| 178 snapshot_controller_->Stop(); | |
| 151 session_contents_.reset(nullptr); | 179 session_contents_.reset(nullptr); |
| 152 state_ = State::IDLE; | 180 state_ = State::IDLE; |
| 153 base::ThreadTaskRunnerHandle::Get()->PostTask( | 181 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 154 FROM_HERE, base::Bind(callback_, request_status, nullptr)); | 182 FROM_HERE, base::Bind(callback_, request_status, nullptr)); |
| 155 } | 183 } |
| 156 | 184 |
| 157 void PrerenderingLoader::CancelPrerender() { | 185 void PrerenderingLoader::CancelPrerender() { |
| 158 if (adapter_->IsActive()) { | 186 if (adapter_->IsActive()) { |
| 159 adapter_->DestroyActive(); | 187 adapter_->DestroyActive(); |
| 160 } | 188 } |
| 189 snapshot_controller_->Stop(); | |
| 161 session_contents_.reset(nullptr); | 190 session_contents_.reset(nullptr); |
| 162 if (!IsLoaded() && !IsIdle()) { | 191 if (!IsLoaded() && !IsIdle()) { |
| 163 base::ThreadTaskRunnerHandle::Get()->PostTask( | 192 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 164 FROM_HERE, | 193 FROM_HERE, |
| 165 base::Bind(callback_, Offliner::RequestStatus::CANCELED, nullptr)); | 194 base::Bind(callback_, Offliner::RequestStatus::CANCELED, nullptr)); |
| 166 } | 195 } |
| 167 state_ = State::IDLE; | 196 state_ = State::IDLE; |
| 168 } | 197 } |
| 169 | 198 |
| 170 } // namespace offline_pages | 199 } // namespace offline_pages |
| OLD | NEW |