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 "chrome/browser/prerender/prerender_manager.h" | |
| 10 #include "chrome/browser/prerender/prerender_manager_factory.h" | |
| 11 #include "chrome/browser/profiles/profile.h" | |
| 7 #include "content/public/browser/browser_context.h" | 12 #include "content/public/browser/browser_context.h" |
| 13 #include "content/public/browser/browser_thread.h" | |
| 8 #include "content/public/browser/web_contents.h" | 14 #include "content/public/browser/web_contents.h" |
| 9 #include "ui/gfx/geometry/size.h" | 15 #include "ui/gfx/geometry/size.h" |
| 10 | 16 |
| 11 namespace offline_pages { | 17 namespace offline_pages { |
| 12 | 18 |
| 13 PrerenderingLoader::PrerenderingLoader( | 19 PrerenderingLoader::PrerenderingLoader(content::BrowserContext* browser_context) |
| 14 content::BrowserContext* browser_context) {} | 20 : state_(State::IDLE), |
| 21 browser_context_(browser_context), | |
| 22 adapter_(new PrerenderAdapter()) {} | |
| 15 | 23 |
| 16 PrerenderingLoader::~PrerenderingLoader() {} | 24 PrerenderingLoader::~PrerenderingLoader() { |
| 25 CancelPrerender(); | |
| 26 } | |
| 17 | 27 |
| 18 bool PrerenderingLoader::LoadPage( | 28 bool PrerenderingLoader::LoadPage(const GURL& url, |
| 19 const GURL& url, | 29 const LoadPageCallback& callback) { |
| 20 const LoadPageCallback& callback) { | 30 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 21 // TODO(dougarnett): implement. | 31 if (state_ != State::IDLE) { |
|
fgorski
2016/05/13 17:39:58
if (!IsIdle())
dougarnett
2016/05/13 20:58:32
Done.
| |
| 22 return false; | 32 DVLOG(1) << "WARNING: Existing request pending"; |
| 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 | |
| 44 bool accepted = adapter_->StartPrerender( | |
|
fgorski
2016/05/13 17:39:58
This code does not try to setobserver. Please make
dougarnett
2016/05/13 20:58:32
Not sure what you mean. It used to SetObserver her
fgorski
2016/05/17 05:09:51
Yes, getting the proof is what I was after.
dougarnett
2016/05/18 00:37:46
Done (it did not work so dropped inheritance altog
| |
| 45 browser_context_, url, | |
| 46 GetSessionStorageNamespace(session_contents_.get()), | |
| 47 GetSize(session_contents_.get()), this /* observer */); | |
| 48 if (!accepted) | |
| 49 return false; | |
| 50 | |
| 51 DCHECK(adapter_->IsActive()); | |
| 52 callback_ = callback; | |
| 53 state_ = State::LOADING; | |
| 54 return true; | |
| 23 } | 55 } |
| 24 | 56 |
| 25 void PrerenderingLoader::StopLoading() { | 57 void PrerenderingLoader::StopLoading() { |
| 26 // TODO(dougarnett): implement. | 58 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 59 CancelPrerender(); | |
| 60 } | |
| 61 | |
| 62 bool PrerenderingLoader::CanPrerender() { | |
| 63 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 64 // First check if prerendering is enabled. | |
|
fgorski
2016/05/13 17:39:58
Is this comment relevant?
dougarnett
2016/05/13 20:58:32
Done.
| |
| 65 return adapter_->CanPrerender(); | |
| 66 } | |
| 67 | |
| 68 bool PrerenderingLoader::IsIdle() { | |
| 69 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 70 return state_ == State::IDLE; | |
| 71 } | |
| 72 | |
| 73 void PrerenderingLoader::SetAdapterForTesting( | |
| 74 PrerenderAdapter* prerender_adapter) { | |
| 75 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 76 adapter_.reset(prerender_adapter); | |
| 77 } | |
| 78 | |
| 79 void PrerenderingLoader::OnPrerenderStart(prerender::PrerenderHandle* handle) { | |
| 80 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 81 DCHECK(adapter_->HasHandle(handle)); | |
|
fgorski
2016/05/13 17:39:58
Is there any state that is incorrect when this eve
dougarnett
2016/05/13 20:58:32
Done.
| |
| 82 if (state_ == State::PENDING) { | |
| 83 state_ = State::LOADING; | |
| 84 } | |
| 85 } | |
| 86 | |
| 87 void PrerenderingLoader::OnPrerenderStopLoading( | |
| 88 prerender::PrerenderHandle* handle) { | |
| 89 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 90 DCHECK(adapter_->HasHandle(handle)); | |
| 91 // TODO(dougarnett): Implement/integrate to delay policy here. | |
| 92 ReportLoadedIfStillLoading(); | |
| 93 } | |
| 94 | |
| 95 void PrerenderingLoader::OnPrerenderDomContentLoaded( | |
| 96 prerender::PrerenderHandle* handle) { | |
| 97 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 98 DCHECK(adapter_->HasHandle(handle)); | |
| 99 // TODO(dougarnett): Implement/integrate to delay policy here. | |
| 100 ReportLoadedIfStillLoading(); | |
| 101 } | |
| 102 | |
| 103 void PrerenderingLoader::OnPrerenderStop(prerender::PrerenderHandle* handle) { | |
| 104 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 105 DCHECK(adapter_->HasHandle(handle)); | |
| 106 ReportLoadFailedIfStillLoading(); | |
| 107 } | |
| 108 | |
| 109 content::SessionStorageNamespace* | |
| 110 PrerenderingLoader::GetSessionStorageNamespace(content::WebContents* contents) { | |
|
fgorski
2016/05/13 17:39:58
since this is a private method and you own the con
dougarnett
2016/05/13 20:58:32
Done.
| |
| 111 return contents->GetController().GetDefaultSessionStorageNamespace(); | |
| 112 } | |
| 113 | |
| 114 const gfx::Size PrerenderingLoader::GetSize(content::WebContents* contents) { | |
|
fgorski
2016/05/13 17:39:58
ditto
dougarnett
2016/05/13 20:58:32
Done.
| |
| 115 return contents->GetContainerBounds().size(); | |
| 116 } | |
| 117 | |
| 118 void PrerenderingLoader::ReportLoadedIfStillLoading() { | |
| 119 // Make sure still loading in case some other load detection or canceling | |
| 120 // has already occurred. | |
| 121 if (state_ == State::LOADING) { | |
| 122 content::WebContents* contents = adapter_->GetWebContents(); | |
| 123 if (contents) { | |
| 124 state_ = State::LOADED; | |
| 125 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
| 126 FROM_HERE, | |
| 127 base::Bind(callback_, Offliner::RequestStatus::LOADED, contents)); | |
| 128 } else { | |
| 129 ReportLoadFailedIfStillLoading(); | |
| 130 } | |
| 131 } | |
| 132 } | |
| 133 | |
| 134 void PrerenderingLoader::ReportLoadFailedIfStillLoading() { | |
| 135 // Check if load is still active to see if reporting is applicable. | |
| 136 if (state_ != State::LOADED && state_ != State::IDLE) { | |
| 137 if (adapter_->IsActive()) | |
| 138 DVLOG(1) << "Load failed: " << adapter_->GetFinalStatus(); | |
| 139 // TODO(dougarnett): Determine from final status if retry-able. | |
| 140 session_contents_.reset(nullptr); | |
| 141 state_ = State::IDLE; | |
| 142 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
| 143 FROM_HERE, | |
| 144 base::Bind(callback_, Offliner::RequestStatus::FAILED_DO_NOT_RETRY, | |
| 145 nullptr)); | |
| 146 } | |
| 147 } | |
| 148 | |
| 149 void PrerenderingLoader::CancelPrerender() { | |
| 150 if (adapter_->IsActive()) { | |
| 151 adapter_->DestroyActive(); | |
| 152 } | |
| 153 session_contents_.reset(nullptr); | |
| 154 if (state_ != State::LOADED && state_ != State::IDLE) { | |
| 155 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
| 156 FROM_HERE, | |
| 157 base::Bind(callback_, Offliner::RequestStatus::CANCELED, nullptr)); | |
| 158 } | |
| 159 state_ = State::IDLE; | |
| 27 } | 160 } |
| 28 | 161 |
| 29 } // namespace offline_pages | 162 } // namespace offline_pages |
| OLD | NEW |