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

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

Issue 2737343002: [Offline Pages] Allow BackgroundLoader to track network bytes using prerenderer hook-in. (Closed)
Patch Set: move offline pages update out of prerender method Created 3 years, 9 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/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/web_contents.h" 17 #include "content/public/browser/web_contents.h"
18 #include "content/public/browser/web_contents_user_data.h"
18 19
19 namespace offline_pages { 20 namespace offline_pages {
20 21
21 namespace { 22 namespace {
22 long kOfflinePageDelayMs = 2000; 23 const long kOfflinePageDelayMs = 2000;
24
25 class OfflinerData : public content::WebContentsUserData<OfflinerData> {
26 public:
27 static void AddToWebContents(content::WebContents* webcontents,
28 BackgroundLoaderOffliner* offliner) {
29 DCHECK(offliner);
30 webcontents->SetUserData(UserDataKey(), std::unique_ptr<OfflinerData>(
31 new OfflinerData(offliner)));
32 }
33
34 explicit OfflinerData(BackgroundLoaderOffliner* offliner) {
35 offliner_ = offliner;
36 }
37 BackgroundLoaderOffliner* offliner() { return offliner_; }
38
39 private:
40 // The offliner that the WebContents is attached to. The offliner owns the
41 // Delegate which owns the WebContents that this data is attached to.
42 // Therefore, its lifetime should exceed that of the WebContents, so this
43 // should always be non-null.
44 BackgroundLoaderOffliner* offliner_;
45 };
46
23 } // namespace 47 } // namespace
24 48
25 BackgroundLoaderOffliner::BackgroundLoaderOffliner( 49 BackgroundLoaderOffliner::BackgroundLoaderOffliner(
26 content::BrowserContext* browser_context, 50 content::BrowserContext* browser_context,
27 const OfflinerPolicy* policy, 51 const OfflinerPolicy* policy,
28 OfflinePageModel* offline_page_model) 52 OfflinePageModel* offline_page_model)
29 : browser_context_(browser_context), 53 : browser_context_(browser_context),
30 offline_page_model_(offline_page_model), 54 offline_page_model_(offline_page_model),
31 is_low_end_device_(base::SysInfo::IsLowEndDevice()), 55 is_low_end_device_(base::SysInfo::IsLowEndDevice()),
32 save_state_(NONE), 56 save_state_(NONE),
33 page_load_state_(SUCCESS), 57 page_load_state_(SUCCESS),
34 page_delay_ms_(kOfflinePageDelayMs), 58 page_delay_ms_(kOfflinePageDelayMs),
59 network_bytes_(0LL),
35 weak_ptr_factory_(this) { 60 weak_ptr_factory_(this) {
36 DCHECK(offline_page_model_); 61 DCHECK(offline_page_model_);
37 DCHECK(browser_context_); 62 DCHECK(browser_context_);
38 } 63 }
39 64
40 BackgroundLoaderOffliner::~BackgroundLoaderOffliner() {} 65 BackgroundLoaderOffliner::~BackgroundLoaderOffliner() {}
41 66
42 // TODO(dimich): Invoke progress_callback as appropriate. 67 // static
68 BackgroundLoaderOffliner* BackgroundLoaderOffliner::FromWebContents(
69 content::WebContents* contents) {
70 OfflinerData* data = OfflinerData::FromWebContents(contents);
71 if (data)
72 return data->offliner();
73 return nullptr;
74 }
75
43 bool BackgroundLoaderOffliner::LoadAndSave( 76 bool BackgroundLoaderOffliner::LoadAndSave(
44 const SavePageRequest& request, 77 const SavePageRequest& request,
45 const CompletionCallback& completion_callback, 78 const CompletionCallback& completion_callback,
46 const ProgressCallback& progress_callback) { 79 const ProgressCallback& progress_callback) {
47 DCHECK(completion_callback); 80 DCHECK(completion_callback);
48 DCHECK(progress_callback); 81 DCHECK(progress_callback);
49 82
50 if (pending_request_) { 83 if (pending_request_) {
51 DVLOG(1) << "Already have pending request"; 84 DVLOG(1) << "Already have pending request";
52 return false; 85 return false;
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
102 135
103 if (!loader_) 136 if (!loader_)
104 ResetState(); 137 ResetState();
105 138
106 // Invalidate ptrs for all delayed/saving tasks. 139 // Invalidate ptrs for all delayed/saving tasks.
107 weak_ptr_factory_.InvalidateWeakPtrs(); 140 weak_ptr_factory_.InvalidateWeakPtrs();
108 141
109 // Track copy of pending request. 142 // Track copy of pending request.
110 pending_request_.reset(new SavePageRequest(request)); 143 pending_request_.reset(new SavePageRequest(request));
111 completion_callback_ = completion_callback; 144 completion_callback_ = completion_callback;
145 progress_callback_ = progress_callback;
112 146
113 // Listen for app foreground/background change. 147 // Listen for app foreground/background change.
114 app_listener_.reset(new base::android::ApplicationStatusListener( 148 app_listener_.reset(new base::android::ApplicationStatusListener(
115 base::Bind(&BackgroundLoaderOffliner::OnApplicationStateChange, 149 base::Bind(&BackgroundLoaderOffliner::OnApplicationStateChange,
116 weak_ptr_factory_.GetWeakPtr()))); 150 weak_ptr_factory_.GetWeakPtr())));
117 151
118 // Load page attempt. 152 // Load page attempt.
119 loader_.get()->LoadPage(request.url()); 153 loader_.get()->LoadPage(request.url());
120 154
121 return true; 155 return true;
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after
243 if (navigation_handle->HasCommitted() && 277 if (navigation_handle->HasCommitted() &&
244 !navigation_handle->IsSameDocument()) { 278 !navigation_handle->IsSameDocument()) {
245 weak_ptr_factory_.InvalidateWeakPtrs(); 279 weak_ptr_factory_.InvalidateWeakPtrs();
246 } 280 }
247 } 281 }
248 282
249 void BackgroundLoaderOffliner::SetPageDelayForTest(long delay_ms) { 283 void BackgroundLoaderOffliner::SetPageDelayForTest(long delay_ms) {
250 page_delay_ms_ = delay_ms; 284 page_delay_ms_ = delay_ms;
251 } 285 }
252 286
287 void BackgroundLoaderOffliner::OnNetworkBytesChanged(int64_t bytes) {
288 if (pending_request_ && save_state_ != SAVING) {
289 network_bytes_ += bytes;
290 progress_callback_.Run(*pending_request_, network_bytes_);
291 }
292 }
293
253 void BackgroundLoaderOffliner::SavePage() { 294 void BackgroundLoaderOffliner::SavePage() {
254 if (!pending_request_.get()) { 295 if (!pending_request_.get()) {
255 DVLOG(1) << "Pending request was cleared during delay."; 296 DVLOG(1) << "Pending request was cleared during delay.";
256 return; 297 return;
257 } 298 }
258 299
259 SavePageRequest request(*pending_request_.get()); 300 SavePageRequest request(*pending_request_.get());
260 // If there was an error navigating to page, return loading failed. 301 // If there was an error navigating to page, return loading failed.
261 if (page_load_state_ != SUCCESS) { 302 if (page_load_state_ != SUCCESS) {
262 Offliner::RequestStatus status; 303 Offliner::RequestStatus status;
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
327 save_status = RequestStatus::SAVED; 368 save_status = RequestStatus::SAVED;
328 else 369 else
329 save_status = RequestStatus::SAVE_FAILED; 370 save_status = RequestStatus::SAVE_FAILED;
330 371
331 completion_callback_.Run(request, save_status); 372 completion_callback_.Run(request, save_status);
332 } 373 }
333 374
334 void BackgroundLoaderOffliner::ResetState() { 375 void BackgroundLoaderOffliner::ResetState() {
335 pending_request_.reset(); 376 pending_request_.reset();
336 page_load_state_ = SUCCESS; 377 page_load_state_ = SUCCESS;
378 network_bytes_ = 0LL;
337 // TODO(chili): Remove after RequestCoordinator can handle multiple offliners. 379 // TODO(chili): Remove after RequestCoordinator can handle multiple offliners.
338 // We reset the loader and observer after completion so loaders 380 // We reset the loader and observer after completion so loaders
339 // will not be re-used across different requests/tries. This is a temporary 381 // will not be re-used across different requests/tries. This is a temporary
340 // solution while there exists assumptions about the number of offliners 382 // solution while there exists assumptions about the number of offliners
341 // there are. 383 // there are.
342 loader_.reset( 384 loader_.reset(
343 new background_loader::BackgroundLoaderContents(browser_context_)); 385 new background_loader::BackgroundLoaderContents(browser_context_));
344 content::WebContentsObserver::Observe(loader_.get()->web_contents()); 386 content::WebContents* contents = loader_->web_contents();
387 content::WebContentsObserver::Observe(contents);
388 OfflinerData::AddToWebContents(contents, this);
345 } 389 }
346 390
347 void BackgroundLoaderOffliner::OnApplicationStateChange( 391 void BackgroundLoaderOffliner::OnApplicationStateChange(
348 base::android::ApplicationState application_state) { 392 base::android::ApplicationState application_state) {
349 if (pending_request_ && is_low_end_device_ && 393 if (pending_request_ && is_low_end_device_ &&
350 application_state == 394 application_state ==
351 base::android::APPLICATION_STATE_HAS_RUNNING_ACTIVITIES) { 395 base::android::APPLICATION_STATE_HAS_RUNNING_ACTIVITIES) {
352 DVLOG(1) << "App became active, canceling current offlining request"; 396 DVLOG(1) << "App became active, canceling current offlining request";
353 SavePageRequest* request = pending_request_.get(); 397 SavePageRequest* request = pending_request_.get();
354 // This works because Bind will make a copy of request, and we 398 // This works because Bind will make a copy of request, and we
355 // should not have to worry about reset being called before cancel callback. 399 // should not have to worry about reset being called before cancel callback.
356 Cancel(base::Bind( 400 Cancel(base::Bind(
357 &BackgroundLoaderOffliner::HandleApplicationStateChangeCancel, 401 &BackgroundLoaderOffliner::HandleApplicationStateChangeCancel,
358 weak_ptr_factory_.GetWeakPtr(), *request)); 402 weak_ptr_factory_.GetWeakPtr(), *request));
359 } 403 }
360 } 404 }
361 405
362 void BackgroundLoaderOffliner::HandleApplicationStateChangeCancel( 406 void BackgroundLoaderOffliner::HandleApplicationStateChangeCancel(
363 const SavePageRequest& request, 407 const SavePageRequest& request,
364 int64_t offline_id) { 408 int64_t offline_id) {
365 // If for some reason the request was reset during while waiting for callback 409 // If for some reason the request was reset during while waiting for callback
366 // ignore the completion callback. 410 // ignore the completion callback.
367 if (pending_request_ && pending_request_->request_id() != offline_id) 411 if (pending_request_ && pending_request_->request_id() != offline_id)
368 return; 412 return;
369 completion_callback_.Run(request, RequestStatus::FOREGROUND_CANCELED); 413 completion_callback_.Run(request, RequestStatus::FOREGROUND_CANCELED);
370 } 414 }
371 } // namespace offline_pages 415 } // namespace offline_pages
416
417 DEFINE_WEB_CONTENTS_USER_DATA_KEY(offline_pages::OfflinerData);
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698