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 snapshot_controller_->Reset(); | |
|
Pete Williamson
2016/06/09 00:22:51
Why reset the controller here? I would guess that
dougarnett
2016/06/09 00:42:33
This is to prepare (or arm or clear or Reset()) th
Pete Williamson
2016/06/09 00:49:05
Ah, this is a bit confusing, since 'reset'-ing a s
dougarnett
2016/06/09 15:12:27
Added comment.
I wasn't part of the SnapshotCont
| |
| 52 callback_ = callback; | 55 callback_ = callback; |
| 53 state_ = State::PENDING; | 56 state_ = State::PENDING; |
| 54 return true; | 57 return true; |
| 55 } | 58 } |
| 56 | 59 |
| 57 void PrerenderingLoader::StopLoading() { | 60 void PrerenderingLoader::StopLoading() { |
| 58 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 61 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 59 CancelPrerender(); | 62 CancelPrerender(); |
| 60 } | 63 } |
| 61 | 64 |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 81 } | 84 } |
| 82 | 85 |
| 83 void PrerenderingLoader::OnPrerenderStart() { | 86 void PrerenderingLoader::OnPrerenderStart() { |
| 84 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 87 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 85 DCHECK(state_ == State::PENDING); | 88 DCHECK(state_ == State::PENDING); |
| 86 state_ = State::LOADING; | 89 state_ = State::LOADING; |
| 87 } | 90 } |
| 88 | 91 |
| 89 void PrerenderingLoader::OnPrerenderStopLoading() { | 92 void PrerenderingLoader::OnPrerenderStopLoading() { |
| 90 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 93 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 91 // TODO(dougarnett): Implement/integrate to delay policy here. | 94 DCHECK(!IsIdle()); |
| 92 HandleLoadEvent(); | 95 if (!IsLoaded()) { |
| 96 DCHECK(adapter_->GetWebContents()); | |
| 97 snapshot_controller_->DocumentOnLoadCompletedInMainFrame(); | |
|
Pete Williamson
2016/06/09 00:22:51
(For my education as a reviewer of this code):
Wha
dougarnett
2016/06/09 00:42:33
This code is single shot at the moment however the
Pete Williamson
2016/06/09 00:49:05
OK, code is good as is, then, but let's document t
dougarnett
2016/06/09 15:12:27
Done - let me know if not clear enough.
| |
| 98 } | |
| 93 } | 99 } |
| 94 | 100 |
| 95 void PrerenderingLoader::OnPrerenderDomContentLoaded() { | 101 void PrerenderingLoader::OnPrerenderDomContentLoaded() { |
| 96 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 102 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 97 // TODO(dougarnett): Implement/integrate to delay policy here. | 103 DCHECK(!IsIdle()); |
| 98 HandleLoadEvent(); | 104 if (!IsLoaded()) { |
| 105 if (!adapter_->GetWebContents()) { | |
| 106 // Without a WebContents object at this point, we are done. | |
| 107 HandleLoadingStopped(); | |
| 108 } else { | |
| 109 // Engage the logic to detect when to snapshot. | |
| 110 snapshot_controller_->DocumentAvailableInMainFrame(); | |
| 111 } | |
| 112 } | |
| 99 } | 113 } |
| 100 | 114 |
| 101 void PrerenderingLoader::OnPrerenderStop() { | 115 void PrerenderingLoader::OnPrerenderStop() { |
| 102 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 116 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 103 HandleLoadingStopped(); | 117 HandleLoadingStopped(); |
| 104 } | 118 } |
| 105 | 119 |
| 120 void PrerenderingLoader::StartSnapshot() { | |
| 121 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 122 DCHECK(!IsIdle()); | |
| 123 DCHECK(!IsLoaded()); | |
| 124 HandleLoadEvent(); | |
| 125 } | |
| 126 | |
| 106 void PrerenderingLoader::HandleLoadEvent() { | 127 void PrerenderingLoader::HandleLoadEvent() { |
| 107 // If still loading, check if the load succeeded or not, then update | 128 // 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 | 129 // the internal state (LOADED for success or IDLE for failure) and post |
| 109 // callback. | 130 // callback. |
| 110 // Note: it is possible to receive a load event (e.g., if timeout-based) | 131 // 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 | 132 // after the request has completed via another path (e.g., canceled) so |
| 112 // the Loader may be idle at this point. | 133 // the Loader may be idle at this point. |
| 113 | 134 |
| 114 if (IsIdle() || IsLoaded()) | 135 if (IsIdle() || IsLoaded()) |
| 115 return; | 136 return; |
| 116 | 137 |
| 117 content::WebContents* web_contents = adapter_->GetWebContents(); | 138 content::WebContents* web_contents = adapter_->GetWebContents(); |
| 118 if (web_contents) { | 139 if (web_contents) { |
| 140 snapshot_controller_->Stop(); | |
| 119 state_ = State::LOADED; | 141 state_ = State::LOADED; |
| 120 base::ThreadTaskRunnerHandle::Get()->PostTask( | 142 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 121 FROM_HERE, | 143 FROM_HERE, |
| 122 base::Bind(callback_, Offliner::RequestStatus::LOADED, web_contents)); | 144 base::Bind(callback_, Offliner::RequestStatus::LOADED, web_contents)); |
| 123 } else { | 145 } else { |
| 124 // No WebContents means that the load failed (and it stopped). | 146 // No WebContents means that the load failed (and it stopped). |
| 125 HandleLoadingStopped(); | 147 HandleLoadingStopped(); |
| 126 } | 148 } |
| 127 } | 149 } |
| 128 | 150 |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 141 DVLOG(1) << "Load failed: " << adapter_->GetFinalStatus(); | 163 DVLOG(1) << "Load failed: " << adapter_->GetFinalStatus(); |
| 142 adapter_->DestroyActive(); | 164 adapter_->DestroyActive(); |
| 143 } | 165 } |
| 144 // Request status depends on whether we are still loading (failed) or | 166 // Request status depends on whether we are still loading (failed) or |
| 145 // did load and then loading was stopped (cancel - from prerender stack). | 167 // did load and then loading was stopped (cancel - from prerender stack). |
| 146 Offliner::RequestStatus request_status = | 168 Offliner::RequestStatus request_status = |
| 147 IsLoaded() ? Offliner::RequestStatus::CANCELED | 169 IsLoaded() ? Offliner::RequestStatus::CANCELED |
| 148 : Offliner::RequestStatus::FAILED; | 170 : Offliner::RequestStatus::FAILED; |
| 149 // TODO(dougarnett): For failure, determine from final status if retry-able | 171 // TODO(dougarnett): For failure, determine from final status if retry-able |
| 150 // and report different failure statuses if retry-able or not. | 172 // and report different failure statuses if retry-able or not. |
| 173 snapshot_controller_->Stop(); | |
| 151 session_contents_.reset(nullptr); | 174 session_contents_.reset(nullptr); |
| 152 state_ = State::IDLE; | 175 state_ = State::IDLE; |
| 153 base::ThreadTaskRunnerHandle::Get()->PostTask( | 176 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 154 FROM_HERE, base::Bind(callback_, request_status, nullptr)); | 177 FROM_HERE, base::Bind(callback_, request_status, nullptr)); |
| 155 } | 178 } |
| 156 | 179 |
| 157 void PrerenderingLoader::CancelPrerender() { | 180 void PrerenderingLoader::CancelPrerender() { |
| 158 if (adapter_->IsActive()) { | 181 if (adapter_->IsActive()) { |
| 159 adapter_->DestroyActive(); | 182 adapter_->DestroyActive(); |
| 160 } | 183 } |
| 184 snapshot_controller_->Stop(); | |
| 161 session_contents_.reset(nullptr); | 185 session_contents_.reset(nullptr); |
| 162 if (!IsLoaded() && !IsIdle()) { | 186 if (!IsLoaded() && !IsIdle()) { |
| 163 base::ThreadTaskRunnerHandle::Get()->PostTask( | 187 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 164 FROM_HERE, | 188 FROM_HERE, |
| 165 base::Bind(callback_, Offliner::RequestStatus::CANCELED, nullptr)); | 189 base::Bind(callback_, Offliner::RequestStatus::CANCELED, nullptr)); |
| 166 } | 190 } |
| 167 state_ = State::IDLE; | 191 state_ = State::IDLE; |
| 168 } | 192 } |
| 169 | 193 |
| 170 } // namespace offline_pages | 194 } // namespace offline_pages |
| OLD | NEW |