Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/android/offline_pages/background_loader_offliner.h" | |
| 6 | |
| 7 #include "base/sys_info.h" | |
| 8 #include "chrome/browser/android/offline_pages/offline_page_mhtml_archiver.h" | |
| 9 #include "components/offline_pages/core/background/save_page_request.h" | |
| 10 #include "components/offline_pages/core/offline_page_model.h" | |
| 11 #include "content/public/browser/browser_context.h" | |
| 12 #include "content/public/browser/web_contents.h" | |
| 13 | |
| 14 namespace offline_pages { | |
| 15 | |
| 16 BackgroundLoaderOffliner::BackgroundLoaderOffliner( | |
| 17 content::BrowserContext* browser_context, | |
| 18 const OfflinerPolicy* policy, | |
| 19 OfflinePageModel* offline_page_model) | |
| 20 : WebContentsObserver(), | |
|
fgorski
2016/12/12 17:59:16
Is this line needed?
chili
2016/12/15 10:34:39
Done.
| |
| 21 browser_context_(browser_context), | |
| 22 offline_page_model_(offline_page_model), | |
|
fgorski
2016/12/12 17:59:16
why do you need both browser_context_ and offline_
chili
2016/12/15 10:34:39
following example for the prerendering_offliner he
| |
| 23 pending_request_(nullptr), | |
|
fgorski
2016/12/12 17:59:16
not needed.
chili
2016/12/15 10:34:39
Done.
| |
| 24 app_listener_(nullptr), | |
|
fgorski
2016/12/12 17:59:16
not needed.
chili
2016/12/15 10:34:39
Done.
| |
| 25 is_low_end_device_(base::SysInfo::IsLowEndDevice()), | |
| 26 weak_ptr_factory_(this) {} | |
| 27 | |
| 28 BackgroundLoaderOffliner::~BackgroundLoaderOffliner() {} | |
| 29 | |
| 30 bool BackgroundLoaderOffliner::LoadAndSave(const SavePageRequest& request, | |
| 31 const CompletionCallback& callback) { | |
|
fgorski
2016/12/12 17:59:16
dcheck completion_callback_ please.
chili
2016/12/15 10:34:39
Done.
| |
| 32 DCHECK(!pending_request_.get()); | |
| 33 | |
| 34 if (pending_request_) { | |
| 35 DVLOG(1) << "Already have pending request"; | |
|
fgorski
2016/12/12 17:59:16
DCHECK above prevents this DVLOG
chili
2016/12/15 10:34:39
Done.
| |
| 36 return false; | |
| 37 } | |
| 38 | |
| 39 if (!OfflinePageModel::CanSaveURL(request.url())) { | |
| 40 DVLOG(1) << "Not able to save page for requested url: " << request.url(); | |
| 41 return false; | |
| 42 } | |
| 43 | |
| 44 // Track copy of pending request. | |
| 45 pending_request_.reset(new SavePageRequest(request)); | |
| 46 completion_callback_ = callback; | |
| 47 | |
| 48 if (!loader_) | |
| 49 ResetState(); | |
| 50 | |
| 51 // Listen for app foreground/background change. | |
| 52 app_listener_.reset(new base::android::ApplicationStatusListener( | |
| 53 base::Bind(&BackgroundLoaderOffliner::OnApplicationStateChange, | |
| 54 weak_ptr_factory_.GetWeakPtr()))); | |
| 55 | |
| 56 loader_.get()->LoadPage(request.url()); | |
| 57 | |
| 58 return true; | |
| 59 } | |
| 60 | |
| 61 void BackgroundLoaderOffliner::Cancel() { | |
| 62 if (pending_request_) { | |
| 63 pending_request_.reset(nullptr); | |
|
fgorski
2016/12/12 17:59:16
will using just: reset() work?
dougarnett
2016/12/12 19:24:17
Interesting, I didn't know reset() was defined on
chili
2016/12/15 10:34:39
yes it does. I always thought we passed in nullptr
| |
| 64 ResetState(); | |
|
fgorski
2016/12/12 17:59:16
This usage of ResetState contradicts line 49.
Thi
dougarnett
2016/12/12 19:24:17
Also consider comment/TODO about not currently abl
chili
2016/12/15 10:34:39
Line 49 is mainly there because I can't call Reset
chili
2016/12/15 10:34:39
Done.
| |
| 65 } | |
| 66 } | |
| 67 | |
| 68 void BackgroundLoaderOffliner::DidStopLoading() { | |
| 69 if (!pending_request_.get()) { | |
|
fgorski
2016/12/12 17:59:16
Is there a chance Cancel and DidStopLoading are qu
chili
2016/12/15 10:34:39
I don't think so. The DidStopLoading() is called s
| |
| 70 DVLOG(1) << "DidStopLoading called even though no pending request."; | |
| 71 return; | |
| 72 } | |
| 73 | |
| 74 SavePageRequest request(*pending_request_.get()); | |
| 75 content::WebContents* web_contents( | |
| 76 content::WebContentsObserver::web_contents()); | |
| 77 | |
| 78 std::unique_ptr<OfflinePageArchiver> archiver( | |
| 79 new OfflinePageMHTMLArchiver(web_contents)); | |
| 80 | |
| 81 DCHECK(offline_page_model_); | |
|
fgorski
2016/12/12 17:59:16
this dcheck comes a little late.
chili
2016/12/15 10:34:39
Done.
| |
| 82 OfflinePageModel::SavePageParams params; | |
| 83 params.url = web_contents->GetLastCommittedURL(); | |
| 84 params.client_id = request.client_id(); | |
| 85 params.proposed_offline_id = request.request_id(); | |
| 86 offline_page_model_->SavePage( | |
| 87 params, std::move(archiver), | |
| 88 base::Bind(&BackgroundLoaderOffliner::OnPageSaved, | |
| 89 weak_ptr_factory_.GetWeakPtr())); | |
| 90 } | |
| 91 | |
| 92 void BackgroundLoaderOffliner::OnPageSaved(SavePageResult save_result, | |
| 93 int64_t offline_id) { | |
| 94 if (!pending_request_) | |
| 95 return; | |
| 96 | |
| 97 SavePageRequest request(*pending_request_.get()); | |
| 98 | |
| 99 pending_request_.reset(nullptr); | |
|
fgorski
2016/12/12 17:59:16
one more reason to put this in ResetState()
also y
chili
2016/12/15 10:34:39
Done.
| |
| 100 ResetState(); | |
| 101 | |
| 102 Offliner::RequestStatus save_status; | |
| 103 if (save_result == SavePageResult::SUCCESS) { | |
|
fgorski
2016/12/12 17:59:16
{} not needed. remove, please.
chili
2016/12/15 10:34:39
for some reason i thought it's necessary for if/el
| |
| 104 save_status = RequestStatus::SAVED; | |
| 105 } else { | |
| 106 save_status = RequestStatus::SAVE_FAILED; | |
| 107 } | |
| 108 | |
| 109 completion_callback_.Run(request, save_status); | |
| 110 } | |
| 111 | |
| 112 void BackgroundLoaderOffliner::ResetState() { | |
| 113 // TODO(chili): Remove after RequestCoordinator can handle multiple offliners. | |
| 114 // We reset the loader and observer after completion so loaders | |
| 115 // will not be re-used across different requests/tries. This is a temporary | |
| 116 // solution while there exists assumptions about the number of offliners | |
| 117 // there are. | |
| 118 loader_.reset( | |
| 119 new background_loader::BackgroundLoaderContents(browser_context_)); | |
| 120 content::WebContentsObserver::Observe(loader_.get()->web_contents()); | |
| 121 } | |
| 122 | |
| 123 void BackgroundLoaderOffliner::OnApplicationStateChange( | |
| 124 base::android::ApplicationState application_state) { | |
| 125 if (pending_request_ && is_low_end_device_ && | |
| 126 application_state == | |
| 127 base::android::APPLICATION_STATE_HAS_RUNNING_ACTIVITIES) { | |
| 128 DVLOG(1) << "App became active, canceling current offlining request"; | |
| 129 SavePageRequest* request = pending_request_.get(); | |
| 130 Cancel(); | |
| 131 completion_callback_.Run(*request, RequestStatus::FOREGROUND_CANCELED); | |
| 132 } | |
| 133 } | |
| 134 | |
| 135 } // namespace offline_pages | |
| OLD | NEW |