OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2012 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 #import "ios/chrome/browser/ui/external_file_remover.h" |
| 6 |
| 7 #include "base/logging.h" |
| 8 #import "base/mac/bind_objc_block.h" |
| 9 #include "components/sessions/core/tab_restore_service.h" |
| 10 #include "ios/chrome/browser/browser_state/chrome_browser_state.h" |
| 11 #include "ios/chrome/browser/sessions/ios_chrome_tab_restore_service_factory.h" |
| 12 #import "ios/chrome/browser/ui/browser_view_controller.h" |
| 13 #import "ios/chrome/browser/ui/external_file_controller.h" |
| 14 #include "ios/web/public/web_thread.h" |
| 15 |
| 16 ExternalFileRemover::ExternalFileRemover(BrowserViewController* bvc) |
| 17 : tabRestoreService_(NULL), bvc_(bvc), weak_ptr_factory_(this) {} |
| 18 |
| 19 ExternalFileRemover::~ExternalFileRemover() { |
| 20 if (tabRestoreService_) |
| 21 tabRestoreService_->RemoveObserver(this); |
| 22 } |
| 23 |
| 24 void ExternalFileRemover::TabRestoreServiceChanged( |
| 25 sessions::TabRestoreService* service) { |
| 26 if (service->IsLoaded()) { |
| 27 tabRestoreService_->RemoveObserver(this); |
| 28 RemoveFiles(false, base::Closure()); |
| 29 } |
| 30 } |
| 31 |
| 32 void ExternalFileRemover::TabRestoreServiceDestroyed( |
| 33 sessions::TabRestoreService* service) { |
| 34 // This happens during shutdown and RemoveFiles() cannot be safely called, |
| 35 // so it is a no-op. |
| 36 } |
| 37 |
| 38 void ExternalFileRemover::Remove(bool all_files, |
| 39 const base::Closure& callback) { |
| 40 // |IOSChromeTabRestoreServiceFactory::GetForBrowserState| has to be called on |
| 41 // the UI thread. |
| 42 DCHECK_CURRENTLY_ON(web::WebThread::UI); |
| 43 tabRestoreService_ = |
| 44 IOSChromeTabRestoreServiceFactory::GetForBrowserState(bvc_.browserState); |
| 45 DCHECK(tabRestoreService_); |
| 46 if (!tabRestoreService_->IsLoaded()) { |
| 47 // TODO(crbug.com/430902): In the case of the presence of tab restore |
| 48 // service, only unreferenced files are removed. This can be addressed with |
| 49 // the larger problem of Clear All browsing data not clearing Tab Restore. |
| 50 tabRestoreService_->AddObserver(this); |
| 51 tabRestoreService_->LoadTabsFromLastSession(); |
| 52 if (!callback.is_null()) { |
| 53 web::WebThread::PostTask(web::WebThread::UI, FROM_HERE, callback); |
| 54 } |
| 55 } else { |
| 56 RemoveFiles(all_files, callback); |
| 57 } |
| 58 } |
| 59 |
| 60 void ExternalFileRemover::RemoveFiles(bool all_files, |
| 61 const base::Closure& callback) { |
| 62 NSSet* referencedFiles = all_files ? nil : [bvc_ referencedExternalFiles]; |
| 63 const NSInteger kMinimumAgeInDays = 30; |
| 64 NSInteger ageInDays = all_files ? 0 : kMinimumAgeInDays; |
| 65 |
| 66 base::Closure callback_wrapper = callback; |
| 67 if (callback_wrapper.is_null()) { |
| 68 callback_wrapper = base::Bind(&base::DoNothing); |
| 69 } |
| 70 web::WebThread::PostBlockingPoolTaskAndReply( |
| 71 FROM_HERE, base::BindBlock(^{ |
| 72 [ExternalFileController removeFilesExcluding:referencedFiles |
| 73 olderThan:ageInDays]; |
| 74 }), |
| 75 callback_wrapper); |
| 76 } |
| 77 |
| 78 void ExternalFileRemover::RemoveAfterDelay(const base::TimeDelta& delay, |
| 79 const base::Closure& callback) { |
| 80 bool remove_all_files = delay == base::TimeDelta::FromSeconds(0); |
| 81 // Creating a copy so it can be used from the block underneath. |
| 82 base::Closure callback_copy = callback; |
| 83 // Since a method on |this| is called from a block, this dance is necessary to |
| 84 // make sure a method on |this| is not called when the object has gone away. |
| 85 base::WeakPtr<ExternalFileRemover> weak_this = weak_ptr_factory_.GetWeakPtr(); |
| 86 web::WebThread::PostDelayedTask( |
| 87 web::WebThread::UI, FROM_HERE, base::BindBlock(^{ |
| 88 if (weak_this) { |
| 89 weak_this->Remove(remove_all_files, callback_copy); |
| 90 } else if (!callback_copy.is_null()) { |
| 91 callback_copy.Run(); |
| 92 } |
| 93 }), |
| 94 delay); |
| 95 } |
OLD | NEW |