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/background_loader_offliner.h" | 5 #include "chrome/browser/android/offline_pages/background_loader_offliner.h" |
| 6 | 6 |
| 7 #include "base/metrics/histogram_macros.h" | 7 #include "base/metrics/histogram_macros.h" |
| 8 #include "base/sys_info.h" | 8 #include "base/sys_info.h" |
| 9 #include "chrome/browser/android/offline_pages/offline_page_mhtml_archiver.h" | 9 #include "chrome/browser/android/offline_pages/offline_page_mhtml_archiver.h" |
| 10 #include "chrome/browser/android/offline_pages/offliner_helper.h" | 10 #include "chrome/browser/android/offline_pages/offliner_helper.h" |
| 11 #include "chrome/browser/profiles/profile.h" | 11 #include "chrome/browser/profiles/profile.h" |
| 12 #include "components/offline_pages/core/background/save_page_request.h" | 12 #include "components/offline_pages/core/background/save_page_request.h" |
| 13 #include "components/offline_pages/core/client_namespace_constants.h" | 13 #include "components/offline_pages/core/client_namespace_constants.h" |
| 14 #include "components/offline_pages/core/offline_page_model.h" | 14 #include "components/offline_pages/core/offline_page_model.h" |
| 15 #include "content/public/browser/browser_context.h" | 15 #include "content/public/browser/browser_context.h" |
| 16 #include "content/public/browser/navigation_handle.h" | 16 #include "content/public/browser/navigation_handle.h" |
| 17 #include "content/public/browser/render_frame_host.h" | |
| 17 #include "content/public/browser/web_contents.h" | 18 #include "content/public/browser/web_contents.h" |
| 18 #include "content/public/browser/web_contents_user_data.h" | 19 #include "content/public/browser/web_contents_user_data.h" |
| 19 | 20 |
| 20 namespace offline_pages { | 21 namespace offline_pages { |
| 21 | 22 |
| 22 namespace { | 23 namespace { |
| 23 const long kOfflinePageDelayMs = 2000; | 24 const long kOfflinePageDelayMs = 2000; |
| 25 const long kOfflineDomContentLoadedMs = 25000; | |
| 24 | 26 |
| 25 class OfflinerData : public content::WebContentsUserData<OfflinerData> { | 27 class OfflinerData : public content::WebContentsUserData<OfflinerData> { |
| 26 public: | 28 public: |
| 27 static void AddToWebContents(content::WebContents* webcontents, | 29 static void AddToWebContents(content::WebContents* webcontents, |
| 28 BackgroundLoaderOffliner* offliner) { | 30 BackgroundLoaderOffliner* offliner) { |
| 29 DCHECK(offliner); | 31 DCHECK(offliner); |
| 30 webcontents->SetUserData(UserDataKey(), std::unique_ptr<OfflinerData>( | 32 webcontents->SetUserData(UserDataKey(), std::unique_ptr<OfflinerData>( |
| 31 new OfflinerData(offliner))); | 33 new OfflinerData(offliner))); |
| 32 } | 34 } |
| 33 | 35 |
| (...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 145 progress_callback_ = progress_callback; | 147 progress_callback_ = progress_callback; |
| 146 | 148 |
| 147 // Listen for app foreground/background change. | 149 // Listen for app foreground/background change. |
| 148 app_listener_.reset(new base::android::ApplicationStatusListener( | 150 app_listener_.reset(new base::android::ApplicationStatusListener( |
| 149 base::Bind(&BackgroundLoaderOffliner::OnApplicationStateChange, | 151 base::Bind(&BackgroundLoaderOffliner::OnApplicationStateChange, |
| 150 weak_ptr_factory_.GetWeakPtr()))); | 152 weak_ptr_factory_.GetWeakPtr()))); |
| 151 | 153 |
| 152 // Load page attempt. | 154 // Load page attempt. |
| 153 loader_.get()->LoadPage(request.url()); | 155 loader_.get()->LoadPage(request.url()); |
| 154 | 156 |
| 157 snapshot_controller_.reset( | |
| 158 new SnapshotController(base::ThreadTaskRunnerHandle::Get(), this, | |
| 159 kOfflineDomContentLoadedMs, page_delay_ms_)); | |
| 160 | |
| 155 return true; | 161 return true; |
| 156 } | 162 } |
| 157 | 163 |
| 158 void BackgroundLoaderOffliner::Cancel(const CancelCallback& callback) { | 164 void BackgroundLoaderOffliner::Cancel(const CancelCallback& callback) { |
| 159 // TODO(chili): We are not able to cancel a pending | 165 // TODO(chili): We are not able to cancel a pending |
| 160 // OfflinePageModel::SavePage() operation. We will notify caller that | 166 // OfflinePageModel::SaveSnapshot() operation. We will notify caller that |
| 161 // cancel completed once the SavePage operation returns. | 167 // cancel completed once the SavePage operation returns. |
| 162 if (!pending_request_) { | 168 if (!pending_request_) { |
| 163 callback.Run(0LL); | 169 callback.Run(0LL); |
| 164 return; | 170 return; |
| 165 } | 171 } |
| 166 | 172 |
| 167 if (save_state_ != NONE) { | 173 if (save_state_ != NONE) { |
| 168 save_state_ = DELETE_AFTER_SAVE; | 174 save_state_ = DELETE_AFTER_SAVE; |
| 169 cancel_callback_ = callback; | 175 cancel_callback_ = callback; |
| 170 return; | 176 return; |
| 171 } | 177 } |
| 172 | 178 |
| 173 int64_t request_id = pending_request_->request_id(); | 179 int64_t request_id = pending_request_->request_id(); |
| 174 ResetState(); | 180 ResetState(); |
| 175 callback.Run(request_id); | 181 callback.Run(request_id); |
| 176 } | 182 } |
| 177 | 183 |
| 178 bool BackgroundLoaderOffliner::HandleTimeout(const SavePageRequest& request) { | 184 bool BackgroundLoaderOffliner::HandleTimeout(const SavePageRequest& request) { |
| 179 // TODO(romax) Decide if we want to also take a snapshot on the last timeout | 185 // TODO(romax) Decide if we want to also take a snapshot on the last timeout |
| 180 // for the background loader offliner. | 186 // for the background loader offliner. |
| 181 return false; | 187 return false; |
| 182 } | 188 } |
| 183 | 189 |
| 190 void BackgroundLoaderOffliner::DocumentLoadedInFrame( | |
| 191 content::RenderFrameHost* render_host) { | |
| 192 if (!render_host->GetParent()) | |
|
Pete Williamson
2017/03/27 21:45:45
Let's add a comment here:
// If this was for the m
chili
2017/03/27 23:43:43
Done.
| |
| 193 snapshot_controller_->DocumentAvailableInMainFrame(); | |
| 194 } | |
| 195 | |
| 184 void BackgroundLoaderOffliner::DidStopLoading() { | 196 void BackgroundLoaderOffliner::DidStopLoading() { |
| 185 if (!pending_request_.get()) { | 197 if (!pending_request_.get()) { |
| 186 DVLOG(1) << "DidStopLoading called even though no pending request."; | 198 DVLOG(1) << "DidStopLoading called even though no pending request."; |
| 187 return; | 199 return; |
| 188 } | 200 } |
| 189 | 201 |
| 190 // Invalidate ptrs for any ongoing save operation. | 202 snapshot_controller_->DocumentOnLoadCompletedInMainFrame(); |
| 191 weak_ptr_factory_.InvalidateWeakPtrs(); | |
| 192 | |
| 193 // Post SavePage task with 2 second delay. | |
| 194 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( | |
| 195 FROM_HERE, | |
| 196 base::Bind(&BackgroundLoaderOffliner::SavePage, | |
| 197 weak_ptr_factory_.GetWeakPtr()), | |
| 198 base::TimeDelta::FromMilliseconds(page_delay_ms_)); | |
| 199 } | 203 } |
| 200 | 204 |
| 201 void BackgroundLoaderOffliner::RenderProcessGone( | 205 void BackgroundLoaderOffliner::RenderProcessGone( |
| 202 base::TerminationStatus status) { | 206 base::TerminationStatus status) { |
| 203 if (pending_request_) { | 207 if (pending_request_) { |
| 204 SavePageRequest request(*pending_request_.get()); | 208 SavePageRequest request(*pending_request_.get()); |
| 205 switch (status) { | 209 switch (status) { |
| 206 case base::TERMINATION_STATUS_OOM: | 210 case base::TERMINATION_STATUS_OOM: |
| 207 case base::TERMINATION_STATUS_PROCESS_CRASHED: | 211 case base::TERMINATION_STATUS_PROCESS_CRASHED: |
| 208 case base::TERMINATION_STATUS_STILL_RUNNING: | 212 case base::TERMINATION_STATUS_STILL_RUNNING: |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 284 page_delay_ms_ = delay_ms; | 288 page_delay_ms_ = delay_ms; |
| 285 } | 289 } |
| 286 | 290 |
| 287 void BackgroundLoaderOffliner::OnNetworkBytesChanged(int64_t bytes) { | 291 void BackgroundLoaderOffliner::OnNetworkBytesChanged(int64_t bytes) { |
| 288 if (pending_request_ && save_state_ != SAVING) { | 292 if (pending_request_ && save_state_ != SAVING) { |
| 289 network_bytes_ += bytes; | 293 network_bytes_ += bytes; |
| 290 progress_callback_.Run(*pending_request_, network_bytes_); | 294 progress_callback_.Run(*pending_request_, network_bytes_); |
| 291 } | 295 } |
| 292 } | 296 } |
| 293 | 297 |
| 294 void BackgroundLoaderOffliner::SavePage() { | 298 void BackgroundLoaderOffliner::StartSnapshot() { |
| 295 if (!pending_request_.get()) { | 299 if (!pending_request_.get()) { |
| 296 DVLOG(1) << "Pending request was cleared during delay."; | 300 DVLOG(1) << "Pending request was cleared during delay."; |
| 297 return; | 301 return; |
| 298 } | 302 } |
| 299 | 303 |
| 300 SavePageRequest request(*pending_request_.get()); | 304 SavePageRequest request(*pending_request_.get()); |
| 301 // If there was an error navigating to page, return loading failed. | 305 // If there was an error navigating to page, return loading failed. |
| 302 if (page_load_state_ != SUCCESS) { | 306 if (page_load_state_ != SUCCESS) { |
| 303 Offliner::RequestStatus status; | 307 Offliner::RequestStatus status; |
| 304 switch (page_load_state_) { | 308 switch (page_load_state_) { |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 365 | 369 |
| 366 Offliner::RequestStatus save_status; | 370 Offliner::RequestStatus save_status; |
| 367 if (save_result == SavePageResult::SUCCESS) | 371 if (save_result == SavePageResult::SUCCESS) |
| 368 save_status = RequestStatus::SAVED; | 372 save_status = RequestStatus::SAVED; |
| 369 else | 373 else |
| 370 save_status = RequestStatus::SAVE_FAILED; | 374 save_status = RequestStatus::SAVE_FAILED; |
| 371 | 375 |
| 372 completion_callback_.Run(request, save_status); | 376 completion_callback_.Run(request, save_status); |
| 373 } | 377 } |
| 374 | 378 |
| 375 void BackgroundLoaderOffliner::ResetState() { | 379 void BackgroundLoaderOffliner::ResetState() { |
|
fgorski
2017/03/27 22:51:54
did you consider resetting the snapshot controller
chili
2017/03/27 23:43:43
Added now.
At first I didn't think it mattered as
| |
| 376 pending_request_.reset(); | 380 pending_request_.reset(); |
| 377 page_load_state_ = SUCCESS; | 381 page_load_state_ = SUCCESS; |
| 378 network_bytes_ = 0LL; | 382 network_bytes_ = 0LL; |
| 379 // TODO(chili): Remove after RequestCoordinator can handle multiple offliners. | 383 // TODO(chili): Remove after RequestCoordinator can handle multiple offliners. |
| 380 // We reset the loader and observer after completion so loaders | 384 // We reset the loader and observer after completion so loaders |
| 381 // will not be re-used across different requests/tries. This is a temporary | 385 // will not be re-used across different requests/tries. This is a temporary |
| 382 // solution while there exists assumptions about the number of offliners | 386 // solution while there exists assumptions about the number of offliners |
| 383 // there are. | 387 // there are. |
| 384 loader_.reset( | 388 loader_.reset( |
| 385 new background_loader::BackgroundLoaderContents(browser_context_)); | 389 new background_loader::BackgroundLoaderContents(browser_context_)); |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 408 int64_t offline_id) { | 412 int64_t offline_id) { |
| 409 // If for some reason the request was reset during while waiting for callback | 413 // If for some reason the request was reset during while waiting for callback |
| 410 // ignore the completion callback. | 414 // ignore the completion callback. |
| 411 if (pending_request_ && pending_request_->request_id() != offline_id) | 415 if (pending_request_ && pending_request_->request_id() != offline_id) |
| 412 return; | 416 return; |
| 413 completion_callback_.Run(request, RequestStatus::FOREGROUND_CANCELED); | 417 completion_callback_.Run(request, RequestStatus::FOREGROUND_CANCELED); |
| 414 } | 418 } |
| 415 } // namespace offline_pages | 419 } // namespace offline_pages |
| 416 | 420 |
| 417 DEFINE_WEB_CONTENTS_USER_DATA_KEY(offline_pages::OfflinerData); | 421 DEFINE_WEB_CONTENTS_USER_DATA_KEY(offline_pages::OfflinerData); |
| OLD | NEW |