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" | |
| 8 #include "base/logging.h" | |
| 9 #include "base/threading/thread_task_runner_handle.h" | |
| 10 #include "chrome/browser/profiles/profile.h" | |
| 7 #include "content/public/browser/browser_context.h" | 11 #include "content/public/browser/browser_context.h" |
| 12 #include "content/public/browser/browser_thread.h" | |
| 8 #include "content/public/browser/web_contents.h" | 13 #include "content/public/browser/web_contents.h" |
| 9 #include "ui/gfx/geometry/size.h" | 14 #include "ui/gfx/geometry/size.h" |
| 10 | 15 |
| 11 namespace offline_pages { | 16 namespace offline_pages { |
| 12 | 17 |
| 13 PrerenderingLoader::PrerenderingLoader( | 18 PrerenderingLoader::PrerenderingLoader(content::BrowserContext* browser_context) |
| 14 content::BrowserContext* browser_context) {} | 19 : state_(State::IDLE), browser_context_(browser_context) { |
| 20 adapter_.reset(new PrerenderAdapter(this)); | |
| 21 } | |
| 15 | 22 |
| 16 PrerenderingLoader::~PrerenderingLoader() {} | 23 PrerenderingLoader::~PrerenderingLoader() { |
| 24 CancelPrerender(); | |
| 25 } | |
| 17 | 26 |
| 18 bool PrerenderingLoader::LoadPage( | 27 bool PrerenderingLoader::LoadPage(const GURL& url, |
| 19 const GURL& url, | 28 const LoadPageCallback& callback) { |
| 20 const LoadPageCallback& callback) { | 29 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 21 // TODO(dougarnett): implement. | 30 if (!IsIdle()) { |
| 22 return false; | 31 DVLOG(1) << "WARNING: Existing request pending"; |
|
pasko
2016/05/20 19:04:53
it might be LOADED and LOADING, not just pending
dougarnett
2016/05/20 22:21:12
Done.
| |
| 32 return false; | |
| 33 } | |
| 34 if (!CanPrerender()) | |
| 35 return false; | |
| 36 | |
| 37 // Create a WebContents instance to define and hold a SessionStorageNamespace | |
| 38 // for this load request. | |
| 39 DCHECK(!session_contents_.get()); | |
| 40 session_contents_.reset(content::WebContents::Create( | |
| 41 content::WebContents::CreateParams(browser_context_))); | |
| 42 bool accepted = adapter_->StartPrerender( | |
| 43 browser_context_, url, GetSessionStorageNamespace(), GetSize()); | |
| 44 if (!accepted) | |
| 45 return false; | |
| 46 | |
| 47 DCHECK(adapter_->IsActive()); | |
| 48 callback_ = callback; | |
| 49 state_ = State::PENDING; | |
| 50 return true; | |
| 23 } | 51 } |
| 24 | 52 |
| 25 void PrerenderingLoader::StopLoading() { | 53 void PrerenderingLoader::StopLoading() { |
| 26 // TODO(dougarnett): implement. | 54 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 55 CancelPrerender(); | |
| 56 } | |
| 57 | |
| 58 bool PrerenderingLoader::CanPrerender() { | |
| 59 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 60 return adapter_->CanPrerender(); | |
| 61 } | |
| 62 | |
| 63 bool PrerenderingLoader::IsIdle() { | |
| 64 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 65 return state_ == State::IDLE; | |
| 66 } | |
| 67 | |
| 68 bool PrerenderingLoader::IsLoaded() { | |
| 69 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 70 return state_ == State::LOADED; | |
| 71 } | |
| 72 | |
| 73 void PrerenderingLoader::SetAdapterForTesting( | |
| 74 std::unique_ptr<PrerenderAdapter> prerender_adapter) { | |
| 75 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 76 adapter_ = std::move(prerender_adapter); | |
| 77 } | |
| 78 | |
| 79 void PrerenderingLoader::OnPrerenderStart() { | |
| 80 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 81 DCHECK(state_ == State::PENDING); | |
| 82 state_ = State::LOADING; | |
| 83 } | |
| 84 | |
| 85 void PrerenderingLoader::OnPrerenderStopLoading() { | |
| 86 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 87 // TODO(dougarnett): Implement/integrate to delay policy here. | |
| 88 ReportLoaded(); | |
| 89 } | |
| 90 | |
| 91 void PrerenderingLoader::OnPrerenderDomContentLoaded() { | |
| 92 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 93 // TODO(dougarnett): Implement/integrate to delay policy here. | |
| 94 ReportLoaded(); | |
| 95 } | |
| 96 | |
| 97 void PrerenderingLoader::OnPrerenderStop() { | |
| 98 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 99 ReportLoadingStopped(); | |
| 100 } | |
| 101 | |
| 102 content::SessionStorageNamespace* | |
| 103 PrerenderingLoader::GetSessionStorageNamespace() { | |
|
pasko
2016/05/20 19:04:53
called only once, does this really need to be a se
dougarnett
2016/05/20 22:21:12
Done.
| |
| 104 DCHECK(session_contents_); | |
| 105 return session_contents_->GetController().GetDefaultSessionStorageNamespace(); | |
| 106 } | |
| 107 | |
| 108 const gfx::Size PrerenderingLoader::GetSize() { | |
|
pasko
2016/05/20 19:04:53
this method is used only once, and the implementat
dougarnett
2016/05/20 22:21:12
Done.
| |
| 109 DCHECK(session_contents_); | |
| 110 return session_contents_->GetContainerBounds().size(); | |
| 111 } | |
| 112 | |
| 113 void PrerenderingLoader::ReportLoaded() { | |
|
pasko
2016/05/20 19:04:53
it does not only Report, but also performs state t
dougarnett
2016/05/20 22:21:12
Tried improved names and comments for these method
pasko
2016/05/23 20:05:41
I still find the state transitions here to be comp
dougarnett
2016/05/23 21:43:54
Will keep this feedback in mind going forward as I
| |
| 114 // Check if load is still active to see if reporting is applicable (i.e., | |
| 115 // no other load detection or canceling has already occurred). | |
| 116 if (!IsLoaded() && !IsIdle()) { | |
|
pasko
2016/05/20 19:04:53
We are not supposed to call ReportLoaded with stat
dougarnett
2016/05/20 22:21:12
see preceding comment
| |
| 117 content::WebContents* contents = adapter_->GetWebContents(); | |
| 118 if (contents) { | |
| 119 state_ = State::LOADED; | |
| 120 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
|
pasko
2016/05/20 19:04:53
I still do not understand the ThreadTaskRunnerHand
dougarnett
2016/05/20 22:21:12
I don't know which type of PostTask is preferred.
pasko
2016/05/23 20:05:41
OK, using ThreadTaskRunnerHandle to post to the sa
dougarnett
2016/05/23 21:43:54
I don't have a strong opinion here. I agree does n
| |
| 121 FROM_HERE, | |
| 122 base::Bind(callback_, Offliner::RequestStatus::LOADED, contents)); | |
| 123 } else { | |
| 124 // No WebContents means that the load failed. | |
| 125 ReportLoadingStopped(); | |
| 126 } | |
| 127 } | |
| 128 } | |
| 129 | |
| 130 void PrerenderingLoader::ReportLoadingStopped() { | |
| 131 // Check if request is still active to see if reporting is applicable. | |
|
pasko
2016/05/20 19:04:53
"Applicable" is not concrete. It would be clearer
dougarnett
2016/05/20 22:21:12
reworded
| |
| 132 if (!IsIdle()) { | |
| 133 if (adapter_->IsActive()) { | |
| 134 DVLOG(1) << "Load failed: " << adapter_->GetFinalStatus(); | |
| 135 adapter_->DestroyActive(); | |
| 136 } | |
| 137 // Request status depends on whether we are still loading (failed) or | |
| 138 // did load and then loading was stopped (cancel - from prerender stack). | |
| 139 Offliner::RequestStatus request_status = | |
| 140 IsLoaded() ? Offliner::RequestStatus::CANCELED | |
| 141 : Offliner::RequestStatus::FAILED; | |
| 142 // TODO(dougarnett): For failure, determine from final status if retry-able | |
| 143 // and report different failure statuses if retry-able or not. | |
| 144 session_contents_.reset(nullptr); | |
| 145 state_ = State::IDLE; | |
| 146 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
| 147 FROM_HERE, base::Bind(callback_, request_status, nullptr)); | |
| 148 } | |
| 149 } | |
| 150 | |
| 151 void PrerenderingLoader::CancelPrerender() { | |
| 152 if (adapter_->IsActive()) { | |
| 153 adapter_->DestroyActive(); | |
| 154 } | |
| 155 session_contents_.reset(nullptr); | |
| 156 if (!IsLoaded() && !IsIdle()) { | |
| 157 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
| 158 FROM_HERE, | |
| 159 base::Bind(callback_, Offliner::RequestStatus::CANCELED, nullptr)); | |
| 160 } | |
| 161 state_ = State::IDLE; | |
| 27 } | 162 } |
| 28 | 163 |
| 29 } // namespace offline_pages | 164 } // namespace offline_pages |
| OLD | NEW |