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

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

Issue 2873393004: [Offline pages] Stop observing web contents and reset to null after use. Only create a new web cont… (Closed)
Patch Set: Created 3 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/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"
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after
142 static_cast<int>(OfflinePagesCctApiPrerenderAllowedStatus:: 142 static_cast<int>(OfflinePagesCctApiPrerenderAllowedStatus::
143 NETWORK_PREDICTION_DISABLED) + 143 NETWORK_PREDICTION_DISABLED) +
144 1); 144 1);
145 } 145 }
146 146
147 if (!OfflinePageModel::CanSaveURL(request.url())) { 147 if (!OfflinePageModel::CanSaveURL(request.url())) {
148 DVLOG(1) << "Not able to save page for requested url: " << request.url(); 148 DVLOG(1) << "Not able to save page for requested url: " << request.url();
149 return false; 149 return false;
150 } 150 }
151 151
152 if (!loader_) 152 ResetLoader();
153 ResetState(); 153 AttachObservers();
154 154
155 // Track copy of pending request. 155 // Track copy of pending request.
156 pending_request_.reset(new SavePageRequest(request)); 156 pending_request_.reset(new SavePageRequest(request));
157 completion_callback_ = completion_callback; 157 completion_callback_ = completion_callback;
158 progress_callback_ = progress_callback; 158 progress_callback_ = progress_callback;
159 159
160 // Listen for app foreground/background change. 160 // Listen for app foreground/background change.
161 app_listener_.reset(new base::android::ApplicationStatusListener( 161 app_listener_.reset(new base::android::ApplicationStatusListener(
162 base::Bind(&BackgroundLoaderOffliner::OnApplicationStateChange, 162 base::Bind(&BackgroundLoaderOffliner::OnApplicationStateChange,
163 weak_ptr_factory_.GetWeakPtr()))); 163 weak_ptr_factory_.GetWeakPtr())));
(...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after
348 save_status = RequestStatus::SAVE_FAILED; 348 save_status = RequestStatus::SAVE_FAILED;
349 349
350 completion_callback_.Run(request, save_status); 350 completion_callback_.Run(request, save_status);
351 } 351 }
352 352
353 void BackgroundLoaderOffliner::ResetState() { 353 void BackgroundLoaderOffliner::ResetState() {
354 pending_request_.reset(); 354 pending_request_.reset();
355 snapshot_controller_.reset(); 355 snapshot_controller_.reset();
356 page_load_state_ = SUCCESS; 356 page_load_state_ = SUCCESS;
357 network_bytes_ = 0LL; 357 network_bytes_ = 0LL;
358 // TODO(chili): Remove after RequestCoordinator can handle multiple offliners. 358 content::WebContentsObserver::Observe(nullptr);
359 // We reset the loader and observer after completion so loaders 359 loader_.reset();
360 // will not be re-used across different requests/tries. This is a temporary 360 }
361 // solution while there exists assumptions about the number of offliners 361
362 // there are. 362 void BackgroundLoaderOffliner::ResetLoader() {
363 loader_.reset( 363 loader_.reset(
364 new background_loader::BackgroundLoaderContents(browser_context_)); 364 new background_loader::BackgroundLoaderContents(browser_context_));
365 }
366
367 void BackgroundLoaderOffliner::AttachObservers() {
365 content::WebContents* contents = loader_->web_contents(); 368 content::WebContents* contents = loader_->web_contents();
366 content::WebContentsObserver::Observe(contents); 369 content::WebContentsObserver::Observe(contents);
367 OfflinerData::AddToWebContents(contents, this); 370 OfflinerData::AddToWebContents(contents, this);
368 } 371 }
369 372
370 void BackgroundLoaderOffliner::OnApplicationStateChange( 373 void BackgroundLoaderOffliner::OnApplicationStateChange(
371 base::android::ApplicationState application_state) { 374 base::android::ApplicationState application_state) {
372 if (pending_request_ && is_low_end_device_ && 375 if (pending_request_ && is_low_end_device_ &&
373 application_state == 376 application_state ==
374 base::android::APPLICATION_STATE_HAS_RUNNING_ACTIVITIES) { 377 base::android::APPLICATION_STATE_HAS_RUNNING_ACTIVITIES) {
(...skipping 12 matching lines...) Expand all
387 int64_t offline_id) { 390 int64_t offline_id) {
388 // If for some reason the request was reset during while waiting for callback 391 // If for some reason the request was reset during while waiting for callback
389 // ignore the completion callback. 392 // ignore the completion callback.
390 if (pending_request_ && pending_request_->request_id() != offline_id) 393 if (pending_request_ && pending_request_->request_id() != offline_id)
391 return; 394 return;
392 completion_callback_.Run(request, RequestStatus::FOREGROUND_CANCELED); 395 completion_callback_.Run(request, RequestStatus::FOREGROUND_CANCELED);
393 } 396 }
394 } // namespace offline_pages 397 } // namespace offline_pages
395 398
396 DEFINE_WEB_CONTENTS_USER_DATA_KEY(offline_pages::OfflinerData); 399 DEFINE_WEB_CONTENTS_USER_DATA_KEY(offline_pages::OfflinerData);
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698