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

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: 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/downloads/download_ui_adapter.h"
14 #include "components/offline_pages/core/offline_page_model.h" 15 #include "components/offline_pages/core/offline_page_model.h"
15 #include "content/public/browser/browser_context.h" 16 #include "content/public/browser/browser_context.h"
16 #include "content/public/browser/navigation_handle.h" 17 #include "content/public/browser/navigation_handle.h"
17 #include "content/public/browser/web_contents.h" 18 #include "content/public/browser/web_contents.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;
23 } // namespace 24 } // namespace
24 25
26 BackgroundLoaderOffliner* BackgroundLoaderOffliner::offliner_ = nullptr;
27
25 BackgroundLoaderOffliner::BackgroundLoaderOffliner( 28 BackgroundLoaderOffliner::BackgroundLoaderOffliner(
26 content::BrowserContext* browser_context, 29 content::BrowserContext* browser_context,
27 const OfflinerPolicy* policy, 30 const OfflinerPolicy* policy,
28 OfflinePageModel* offline_page_model) 31 OfflinePageModel* offline_page_model)
29 : browser_context_(browser_context), 32 : browser_context_(browser_context),
30 offline_page_model_(offline_page_model), 33 offline_page_model_(offline_page_model),
31 is_low_end_device_(base::SysInfo::IsLowEndDevice()), 34 is_low_end_device_(base::SysInfo::IsLowEndDevice()),
32 save_state_(NONE), 35 save_state_(NONE),
33 page_load_state_(SUCCESS), 36 page_load_state_(SUCCESS),
34 page_delay_ms_(kOfflinePageDelayMs), 37 page_delay_ms_(kOfflinePageDelayMs),
38 network_bytes_(0LL),
35 weak_ptr_factory_(this) { 39 weak_ptr_factory_(this) {
36 DCHECK(offline_page_model_); 40 DCHECK(offline_page_model_);
37 DCHECK(browser_context_); 41 DCHECK(browser_context_);
42 offliner_ = this;
38 } 43 }
39 44
40 BackgroundLoaderOffliner::~BackgroundLoaderOffliner() {} 45 BackgroundLoaderOffliner::~BackgroundLoaderOffliner() {}
41 46
42 // TODO(dimich): Invoke progress_callback as appropriate. 47 // static
48 BackgroundLoaderOffliner* BackgroundLoaderOffliner::FromWebContents(
49 content::WebContents* contents) {
dewittj 2017/03/09 22:33:17 It might be better to get it this way: auto contex
50 if (offliner_ && offliner_->web_contents() == contents)
51 return offliner_;
52 return nullptr;
53 }
54
43 bool BackgroundLoaderOffliner::LoadAndSave( 55 bool BackgroundLoaderOffliner::LoadAndSave(
44 const SavePageRequest& request, 56 const SavePageRequest& request,
45 const CompletionCallback& completion_callback, 57 const CompletionCallback& completion_callback,
46 const ProgressCallback& progress_callback) { 58 const ProgressCallback& progress_callback) {
47 DCHECK(completion_callback); 59 DCHECK(completion_callback);
48 DCHECK(progress_callback); 60 DCHECK(progress_callback);
49 61
50 if (pending_request_) { 62 if (pending_request_) {
51 DVLOG(1) << "Already have pending request"; 63 DVLOG(1) << "Already have pending request";
52 return false; 64 return false;
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
102 114
103 if (!loader_) 115 if (!loader_)
104 ResetState(); 116 ResetState();
105 117
106 // Invalidate ptrs for all delayed/saving tasks. 118 // Invalidate ptrs for all delayed/saving tasks.
107 weak_ptr_factory_.InvalidateWeakPtrs(); 119 weak_ptr_factory_.InvalidateWeakPtrs();
108 120
109 // Track copy of pending request. 121 // Track copy of pending request.
110 pending_request_.reset(new SavePageRequest(request)); 122 pending_request_.reset(new SavePageRequest(request));
111 completion_callback_ = completion_callback; 123 completion_callback_ = completion_callback;
124 progress_callback_ = progress_callback;
112 125
113 // Listen for app foreground/background change. 126 // Listen for app foreground/background change.
114 app_listener_.reset(new base::android::ApplicationStatusListener( 127 app_listener_.reset(new base::android::ApplicationStatusListener(
115 base::Bind(&BackgroundLoaderOffliner::OnApplicationStateChange, 128 base::Bind(&BackgroundLoaderOffliner::OnApplicationStateChange,
116 weak_ptr_factory_.GetWeakPtr()))); 129 weak_ptr_factory_.GetWeakPtr())));
117 130
118 // Load page attempt. 131 // Load page attempt.
119 loader_.get()->LoadPage(request.url()); 132 loader_.get()->LoadPage(request.url());
120 133
121 return true; 134 return true;
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after
243 if (navigation_handle->HasCommitted() && 256 if (navigation_handle->HasCommitted() &&
244 !navigation_handle->IsSameDocument()) { 257 !navigation_handle->IsSameDocument()) {
245 weak_ptr_factory_.InvalidateWeakPtrs(); 258 weak_ptr_factory_.InvalidateWeakPtrs();
246 } 259 }
247 } 260 }
248 261
249 void BackgroundLoaderOffliner::SetPageDelayForTest(long delay_ms) { 262 void BackgroundLoaderOffliner::SetPageDelayForTest(long delay_ms) {
250 page_delay_ms_ = delay_ms; 263 page_delay_ms_ = delay_ms;
251 } 264 }
252 265
266 void BackgroundLoaderOffliner::OnNetworkBytesChanged(int64_t bytes) {
267 if (pending_request_ && save_state_ != SAVING) {
268 network_bytes_ += bytes;
269 SavePageRequest request(*pending_request_.get());
270 progress_callback_.Run(request, network_bytes_);
271 }
272 }
273
253 void BackgroundLoaderOffliner::SavePage() { 274 void BackgroundLoaderOffliner::SavePage() {
254 if (!pending_request_.get()) { 275 if (!pending_request_.get()) {
255 DVLOG(1) << "Pending request was cleared during delay."; 276 DVLOG(1) << "Pending request was cleared during delay.";
256 return; 277 return;
257 } 278 }
258 279
259 SavePageRequest request(*pending_request_.get()); 280 SavePageRequest request(*pending_request_.get());
260 // If there was an error navigating to page, return loading failed. 281 // If there was an error navigating to page, return loading failed.
261 if (page_load_state_ != SUCCESS) { 282 if (page_load_state_ != SUCCESS) {
262 Offliner::RequestStatus status; 283 Offliner::RequestStatus status;
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
327 save_status = RequestStatus::SAVED; 348 save_status = RequestStatus::SAVED;
328 else 349 else
329 save_status = RequestStatus::SAVE_FAILED; 350 save_status = RequestStatus::SAVE_FAILED;
330 351
331 completion_callback_.Run(request, save_status); 352 completion_callback_.Run(request, save_status);
332 } 353 }
333 354
334 void BackgroundLoaderOffliner::ResetState() { 355 void BackgroundLoaderOffliner::ResetState() {
335 pending_request_.reset(); 356 pending_request_.reset();
336 page_load_state_ = SUCCESS; 357 page_load_state_ = SUCCESS;
358 network_bytes_ = 0LL;
337 // TODO(chili): Remove after RequestCoordinator can handle multiple offliners. 359 // TODO(chili): Remove after RequestCoordinator can handle multiple offliners.
338 // We reset the loader and observer after completion so loaders 360 // We reset the loader and observer after completion so loaders
339 // will not be re-used across different requests/tries. This is a temporary 361 // will not be re-used across different requests/tries. This is a temporary
340 // solution while there exists assumptions about the number of offliners 362 // solution while there exists assumptions about the number of offliners
341 // there are. 363 // there are.
342 loader_.reset( 364 loader_.reset(
343 new background_loader::BackgroundLoaderContents(browser_context_)); 365 new background_loader::BackgroundLoaderContents(browser_context_));
344 content::WebContentsObserver::Observe(loader_.get()->web_contents()); 366 content::WebContentsObserver::Observe(loader_.get()->web_contents());
345 } 367 }
346 368
(...skipping 15 matching lines...) Expand all
362 void BackgroundLoaderOffliner::HandleApplicationStateChangeCancel( 384 void BackgroundLoaderOffliner::HandleApplicationStateChangeCancel(
363 const SavePageRequest& request, 385 const SavePageRequest& request,
364 int64_t offline_id) { 386 int64_t offline_id) {
365 // If for some reason the request was reset during while waiting for callback 387 // If for some reason the request was reset during while waiting for callback
366 // ignore the completion callback. 388 // ignore the completion callback.
367 if (pending_request_ && pending_request_->request_id() != offline_id) 389 if (pending_request_ && pending_request_->request_id() != offline_id)
368 return; 390 return;
369 completion_callback_.Run(request, RequestStatus::FOREGROUND_CANCELED); 391 completion_callback_.Run(request, RequestStatus::FOREGROUND_CANCELED);
370 } 392 }
371 } // namespace offline_pages 393 } // namespace offline_pages
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698