Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(106)

Side by Side Diff: chrome/browser/android/offline_pages/prerendering_loader.cc

Issue 1968593002: PrerenderingLoader initial integration with PrerenderManager/PrerenderHandle (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Changes per pasko feedback Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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) {
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_->AddPrerenderForOffline(
45 browser_context_, url,
46 GetSessionStorageNamespace(session_contents_.get()),
47 GetSize(session_contents_.get()));
48 if (!accepted)
49 return false;
50
51 DCHECK(adapter_->IsActive());
52 callback_ = callback;
53 adapter_->SetObserver(this);
54 state_ = State::LOADING;
55 return true;
23 } 56 }
24 57
25 void PrerenderingLoader::StopLoading() { 58 void PrerenderingLoader::StopLoading() {
26 // TODO(dougarnett): implement. 59 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
60 CancelPrerender();
61 }
62
63 bool PrerenderingLoader::CanPrerender() {
64 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
65 // First check if prerendering is enabled.
66 return adapter_->CanPrerender();
67 }
68
69 bool PrerenderingLoader::IsIdle() {
70 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
71 return state_ == State::IDLE;
72 }
73
74 void PrerenderingLoader::SetAdapterForTesting(
75 PrerenderAdapter* prerender_adapter) {
76 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
77 adapter_.reset(prerender_adapter);
78 }
79
80 void PrerenderingLoader::OnPrerenderStart(prerender::PrerenderHandle* handle) {
81 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
82 if (adapter_->IsActive(handle) && 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 if (adapter_->IsActive(handle)) {
gabadie 2016/05/13 07:55:37 I think asserts here would be more appropriate tha
dougarnett 2016/05/13 16:49:45 Yes, sounds good
91 // TODO(dougarnett): Implement/integrate to delay policy here.
92 ReportLoaded();
93 }
94 }
95
96 void PrerenderingLoader::OnPrerenderDomContentLoaded(
97 prerender::PrerenderHandle* handle) {
98 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
99 if (adapter_->IsActive(handle)) {
gabadie 2016/05/13 07:55:37 Please assert.
dougarnett 2016/05/13 16:49:45 Done.
100 // TODO(dougarnett): Implement/integrate to delay policy here.
101 ReportLoaded();
gabadie 2016/05/13 07:55:38 To me it looks weird that there is 2 ways we Repor
dougarnett 2016/05/13 16:49:45 Not complete yet. Expect to have two tunable timeo
102 }
103 }
104
105 void PrerenderingLoader::OnPrerenderStop(prerender::PrerenderHandle* handle) {
106 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
107 if (adapter_->IsActive(handle)) {
gabadie 2016/05/13 07:55:37 Please assert.
dougarnett 2016/05/13 16:49:45 Done.
108 ReportLoadFailed();
109 }
110 }
111
112 content::SessionStorageNamespace*
113 PrerenderingLoader::GetSessionStorageNamespace(content::WebContents* contents) {
gabadie 2016/05/13 07:55:37 Must be in an anonymous namespace
dougarnett 2016/05/13 16:49:45 Are you saying the function must be in C++ anonymo
pasko 2016/05/17 15:34:20 I think gabadie@ means that this function does not
dougarnett 2016/05/18 00:37:46 Thx, makes sense. I have since modified to use mem
114 return contents->GetController().GetDefaultSessionStorageNamespace();
115 }
116
117 const gfx::Size PrerenderingLoader::GetSize(content::WebContents* contents) {
gabadie 2016/05/13 07:55:37 Must be in an anonymous namespace
dougarnett 2016/05/16 23:51:38 Guillaume, can you explain further? I don't unders
dougarnett 2016/05/18 00:37:46 Egor clarified.
118 return contents->GetContainerBounds().size();
119 }
120
121 void PrerenderingLoader::ReportLoaded() {
122 if (state_ == State::LOADING) {
gabadie 2016/05/13 07:55:37 Why not asserting?
dougarnett 2016/05/13 16:49:45 Took stab at a comment. If/when we have timeout tr
123 content::WebContents* contents = adapter_->GetWebContents();
124 if (contents) {
125 state_ = State::LOADED;
126 base::ThreadTaskRunnerHandle::Get()->PostTask(
127 FROM_HERE,
128 base::Bind(callback_, Offliner::RequestStatus::LOADED, contents));
129 } else {
130 ReportLoadFailed();
131 }
132 }
133 }
134
135 void PrerenderingLoader::ReportLoadFailed() {
136 if (state_ != State::LOADED && state_ != State::IDLE) {
gabadie 2016/05/13 07:55:37 DCHECK(state_ == State::LOADING || state_ == State
dougarnett 2016/05/13 16:49:45 In my prototype I recall getting an OnPrerenderSto
137 if (adapter_->IsActive())
138 DVLOG(1) << "Load failed: " << adapter_->GetFinalStatus();
139 // TODO(dougarnett): Determine from final status if retry-able.
140 state_ = State::IDLE;
gabadie 2016/05/13 07:55:37 Looks like here your state tracking is bugged beca
dougarnett 2016/05/13 16:49:45 Done.
141 base::ThreadTaskRunnerHandle::Get()->PostTask(
142 FROM_HERE,
143 base::Bind(callback_, Offliner::RequestStatus::FAILED_DO_NOT_RETRY,
144 nullptr));
145 }
146 }
147
148 void PrerenderingLoader::CancelPrerender() {
149 if (adapter_->IsActive()) {
150 adapter_->DestroyActive();
151 }
152 session_contents_.reset(nullptr);
153 if (state_ != State::LOADED && state_ != State::IDLE) {
154 base::ThreadTaskRunnerHandle::Get()->PostTask(
155 FROM_HERE,
156 base::Bind(callback_, Offliner::RequestStatus::CANCELED, nullptr));
157 }
158 state_ = State::IDLE;
27 } 159 }
28 160
29 } // namespace offline_pages 161 } // namespace offline_pages
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698