| 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" | |
| 8 #include "base/logging.h" | |
| 9 #include "base/threading/thread_task_runner_handle.h" | |
| 10 #include "chrome/browser/profiles/profile.h" | |
| 11 #include "content/public/browser/browser_context.h" | 7 #include "content/public/browser/browser_context.h" |
| 12 #include "content/public/browser/browser_thread.h" | |
| 13 #include "content/public/browser/web_contents.h" | 8 #include "content/public/browser/web_contents.h" |
| 14 #include "ui/gfx/geometry/size.h" | 9 #include "ui/gfx/geometry/size.h" |
| 15 | 10 |
| 16 namespace offline_pages { | 11 namespace offline_pages { |
| 17 | 12 |
| 18 PrerenderingLoader::PrerenderingLoader(content::BrowserContext* browser_context) | 13 PrerenderingLoader::PrerenderingLoader( |
| 19 : state_(State::IDLE), browser_context_(browser_context) { | 14 content::BrowserContext* browser_context) {} |
| 20 adapter_.reset(new PrerenderAdapter(this)); | |
| 21 } | |
| 22 | 15 |
| 23 PrerenderingLoader::~PrerenderingLoader() { | 16 PrerenderingLoader::~PrerenderingLoader() {} |
| 24 CancelPrerender(); | |
| 25 } | |
| 26 | 17 |
| 27 bool PrerenderingLoader::LoadPage(const GURL& url, | 18 bool PrerenderingLoader::LoadPage( |
| 28 const LoadPageCallback& callback) { | 19 const GURL& url, |
| 29 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 20 const LoadPageCallback& callback) { |
| 30 if (!IsIdle()) { | 21 // TODO(dougarnett): implement. |
| 31 DVLOG(1) | 22 return false; |
| 32 << "WARNING: Existing request in progress or waiting for StopLoading()"; | |
| 33 return false; | |
| 34 } | |
| 35 if (!CanPrerender()) | |
| 36 return false; | |
| 37 | |
| 38 // Create a WebContents instance to define and hold a SessionStorageNamespace | |
| 39 // for this load request. | |
| 40 DCHECK(!session_contents_.get()); | |
| 41 session_contents_.reset(content::WebContents::Create( | |
| 42 content::WebContents::CreateParams(browser_context_))); | |
| 43 content::SessionStorageNamespace* sessionStorageNamespace = | |
| 44 session_contents_->GetController().GetDefaultSessionStorageNamespace(); | |
| 45 gfx::Size renderWindowSize = session_contents_->GetContainerBounds().size(); | |
| 46 bool accepted = adapter_->StartPrerender( | |
| 47 browser_context_, url, sessionStorageNamespace, renderWindowSize); | |
| 48 if (!accepted) | |
| 49 return false; | |
| 50 | |
| 51 DCHECK(adapter_->IsActive()); | |
| 52 callback_ = callback; | |
| 53 state_ = State::PENDING; | |
| 54 return true; | |
| 55 } | 23 } |
| 56 | 24 |
| 57 void PrerenderingLoader::StopLoading() { | 25 void PrerenderingLoader::StopLoading() { |
| 58 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 26 // TODO(dougarnett): implement. |
| 59 CancelPrerender(); | |
| 60 } | |
| 61 | |
| 62 bool PrerenderingLoader::CanPrerender() { | |
| 63 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 64 return adapter_->CanPrerender(); | |
| 65 } | |
| 66 | |
| 67 bool PrerenderingLoader::IsIdle() { | |
| 68 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 69 return state_ == State::IDLE; | |
| 70 } | |
| 71 | |
| 72 bool PrerenderingLoader::IsLoaded() { | |
| 73 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 74 return state_ == State::LOADED; | |
| 75 } | |
| 76 | |
| 77 void PrerenderingLoader::SetAdapterForTesting( | |
| 78 std::unique_ptr<PrerenderAdapter> prerender_adapter) { | |
| 79 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 80 adapter_ = std::move(prerender_adapter); | |
| 81 } | |
| 82 | |
| 83 void PrerenderingLoader::OnPrerenderStart() { | |
| 84 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 85 DCHECK(state_ == State::PENDING); | |
| 86 state_ = State::LOADING; | |
| 87 } | |
| 88 | |
| 89 void PrerenderingLoader::OnPrerenderStopLoading() { | |
| 90 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 91 // TODO(dougarnett): Implement/integrate to delay policy here. | |
| 92 HandleLoadEvent(); | |
| 93 } | |
| 94 | |
| 95 void PrerenderingLoader::OnPrerenderDomContentLoaded() { | |
| 96 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 97 // TODO(dougarnett): Implement/integrate to delay policy here. | |
| 98 HandleLoadEvent(); | |
| 99 } | |
| 100 | |
| 101 void PrerenderingLoader::OnPrerenderStop() { | |
| 102 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 103 HandleLoadingStopped(); | |
| 104 } | |
| 105 | |
| 106 void PrerenderingLoader::HandleLoadEvent() { | |
| 107 // 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 | |
| 109 // callback. | |
| 110 // 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 | |
| 112 // the Loader may be idle at this point. | |
| 113 | |
| 114 if (IsIdle() || IsLoaded()) | |
| 115 return; | |
| 116 | |
| 117 content::WebContents* web_contents = adapter_->GetWebContents(); | |
| 118 if (web_contents) { | |
| 119 state_ = State::LOADED; | |
| 120 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
| 121 FROM_HERE, | |
| 122 base::Bind(callback_, Offliner::RequestStatus::LOADED, web_contents)); | |
| 123 } else { | |
| 124 // No WebContents means that the load failed (and it stopped). | |
| 125 HandleLoadingStopped(); | |
| 126 } | |
| 127 } | |
| 128 | |
| 129 void PrerenderingLoader::HandleLoadingStopped() { | |
| 130 // Loading has stopped so unless the Loader has already transistioned to the | |
| 131 // idle state, clean up the previous request state, transition to the idle | |
| 132 // state, and post callback. | |
| 133 // Note: it is possible to receive some asynchronous stopped indication after | |
| 134 // the request has completed/stopped via another path so the Loader may be | |
| 135 // idle at this point. | |
| 136 | |
| 137 if (IsIdle()) | |
| 138 return; | |
| 139 | |
| 140 if (adapter_->IsActive()) { | |
| 141 DVLOG(1) << "Load failed: " << adapter_->GetFinalStatus(); | |
| 142 adapter_->DestroyActive(); | |
| 143 } | |
| 144 // Request status depends on whether we are still loading (failed) or | |
| 145 // did load and then loading was stopped (cancel - from prerender stack). | |
| 146 Offliner::RequestStatus request_status = | |
| 147 IsLoaded() ? Offliner::RequestStatus::CANCELED | |
| 148 : Offliner::RequestStatus::FAILED; | |
| 149 // TODO(dougarnett): For failure, determine from final status if retry-able | |
| 150 // and report different failure statuses if retry-able or not. | |
| 151 session_contents_.reset(nullptr); | |
| 152 state_ = State::IDLE; | |
| 153 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
| 154 FROM_HERE, base::Bind(callback_, request_status, nullptr)); | |
| 155 } | |
| 156 | |
| 157 void PrerenderingLoader::CancelPrerender() { | |
| 158 if (adapter_->IsActive()) { | |
| 159 adapter_->DestroyActive(); | |
| 160 } | |
| 161 session_contents_.reset(nullptr); | |
| 162 if (!IsLoaded() && !IsIdle()) { | |
| 163 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
| 164 FROM_HERE, | |
| 165 base::Bind(callback_, Offliner::RequestStatus::CANCELED, nullptr)); | |
| 166 } | |
| 167 state_ = State::IDLE; | |
| 168 } | 27 } |
| 169 | 28 |
| 170 } // namespace offline_pages | 29 } // namespace offline_pages |
| OLD | NEW |