Index: content/browser/storage_partition_impl_map.cc |
diff --git a/content/browser/storage_partition_impl_map.cc b/content/browser/storage_partition_impl_map.cc |
index 01c0593f51067bac7b9e06ada782898a2c1b0e24..889fc9eb808d84b39982b22589e14f65ba797f73 100644 |
--- a/content/browser/storage_partition_impl_map.cc |
+++ b/content/browser/storage_partition_impl_map.cc |
@@ -9,8 +9,9 @@ |
#include "base/file_path.h" |
#include "base/file_util.h" |
#include "base/stl_util.h" |
-#include "base/string_util.h" |
#include "base/string_number_conversions.h" |
+#include "base/string_util.h" |
+#include "base/stringprintf.h" |
#include "base/threading/worker_pool.h" |
#include "content/browser/appcache/chrome_appcache_service.h" |
#include "content/browser/fileapi/browser_file_system_helper.h" |
@@ -210,6 +211,8 @@ const FilePath::CharType kExtensionsDirname[] = |
FILE_PATH_LITERAL("ext"); |
const FilePath::CharType kDefaultPartitionDirname[] = |
FILE_PATH_LITERAL("def"); |
+const FilePath::CharType kTrashDirname[] = |
+ FILE_PATH_LITERAL("trash"); |
// Because partition names are user specified, they can be arbitrarily long |
// which makes them unsuitable for paths names. We use a truncation of a |
@@ -267,6 +270,8 @@ FilePath GetStoragePartitionDomainPath( |
void ObliterateOneDirectory(const FilePath& current_dir, |
const std::vector<FilePath>& paths_to_keep, |
std::vector<FilePath>* paths_to_consider) { |
+ CHECK(current_dir.IsAbsolute()); |
+ |
file_util::FileEnumerator enumerator(current_dir, false, kAllFileTypes); |
for (FilePath to_delete = enumerator.Next(); !to_delete.empty(); |
to_delete = enumerator.Next()) { |
@@ -306,7 +311,10 @@ void ObliterateOneDirectory(const FilePath& current_dir, |
// |paths_to_keep|. If there are no entries in |paths_to_keep| on disk, then it |
// completely removes |root|. All paths must be absolute paths. |
void BlockingObliteratePath(const FilePath& root, |
- const std::vector<FilePath>& paths_to_keep) { |
+ const std::vector<FilePath>& paths_to_keep, |
+ const base::Closure& on_gc_required) { |
+ CHECK(root.IsAbsolute()); |
+ |
// Reduce |paths_to_keep| set to those under the root and actually on disk. |
std::vector<FilePath> valid_paths_to_keep; |
for (std::vector<FilePath>::const_iterator it = paths_to_keep.begin(); |
@@ -321,6 +329,8 @@ void BlockingObliteratePath(const FilePath& root, |
if (valid_paths_to_keep.empty()) { |
file_util::Delete(root, true); |
return; |
+ } else { |
+ on_gc_required.Run(); |
} |
// Otherwise, start at the root and delete everything that is not in |
@@ -334,6 +344,48 @@ void BlockingObliteratePath(const FilePath& root, |
} |
} |
+// Deletes all entries inside the |storage_root| that are not in the |
+// |active_paths|. Deletion is done in 2 steps: |
+// |
+// (1) Moving all garbage collected paths into a trash directory. |
+// (2) Asynchronously deleting the trash directory. |
+// |
+// The deletion is asynchronously because after (1) completes, calling code can |
Charlie Reis
2012/12/05 02:25:40
nit: asynchronous?
|
+// continue to use the paths that had just been garbage collected without |
+// fear of race conditions. |
+// |
+// This code also ignores failed moves rather than attempting a smarter retry. |
+// Moves shouldn't fail here unless there is some out-of-band error (eg., |
+// FS corruption). Retry logic is dangerous in the general case because |
+// there is not necessarily a guaranteed case where the logic may succeed. |
+void BlockingGarbageCollect( |
+ const FilePath& storage_root, |
+ scoped_ptr<base::hash_set<FilePath> > active_paths) { |
+ CHECK(storage_root.IsAbsolute()); |
+ |
+ file_util::FileEnumerator enumerator(storage_root, false, kAllFileTypes); |
+ FilePath trash_directory; |
+ if (!file_util::CreateTemporaryDirInDir(storage_root, kTrashDirname, |
+ &trash_directory)) { |
+ // Unable to continue without creating the trash directory so give up. |
+ return; |
+ } |
+ for (FilePath path = enumerator.Next(); !path.empty(); |
+ path = enumerator.Next()) { |
+ if (active_paths->find(path) == active_paths->end() && |
+ path != trash_directory) { |
+ // Since |trash_directory| is unique for each run of this function there |
+ // can be no colllisions on the move. |
+ file_util::Move(path, trash_directory.Append(path.BaseName())); |
+ } |
+ } |
+ |
+ BrowserThread::PostBlockingPoolTask( |
+ FROM_HERE, |
+ base::Bind(base::IgnoreResult(&file_util::Delete), trash_directory, |
+ true)); |
+} |
+ |
} // namespace |
// static |
@@ -418,7 +470,9 @@ StoragePartitionImpl* StoragePartitionImplMap::Get( |
return partition; |
} |
-void StoragePartitionImplMap::AsyncObliterate(const GURL& site) { |
+void StoragePartitionImplMap::AsyncObliterate( |
+ const GURL& site, |
+ const base::Closure& on_gc_required) { |
// This method should avoid creating any StoragePartition (which would |
// create more open file handles) so that it can delete as much of the |
// data off disk as possible. |
@@ -429,10 +483,6 @@ void StoragePartitionImplMap::AsyncObliterate(const GURL& site) { |
browser_context_, site, false, &partition_domain, |
&partition_name, &in_memory); |
- // The default partition is the whole profile. We can't obliterate that and |
- // should never even try. |
- CHECK(!partition_domain.empty()) << site; |
- |
// Find the active partitions for the domain. Because these partitions are |
// active, it is not possible to just delete the directories that contain |
// the backing data structures without causing the browser to crash. Instead, |
@@ -459,13 +509,41 @@ void StoragePartitionImplMap::AsyncObliterate(const GURL& site) { |
// run of the browser. |
FilePath domain_root = browser_context_->GetPath().Append( |
GetStoragePartitionDomainPath(partition_domain)); |
- base::WorkerPool::PostTask( |
+ |
+ // Never try to obliterate the browser context root. Die hard. |
Charlie Reis
2012/12/05 02:25:40
Yippie ki-yay. :)
|
+ CHECK(domain_root != browser_context_->GetPath()) << site; |
+ |
+ BrowserThread::PostBlockingPoolTask( |
FROM_HERE, |
- base::Bind(&BlockingObliteratePath, domain_root, paths_to_keep), |
- true); |
+ base::Bind(&BlockingObliteratePath, domain_root, paths_to_keep, |
+ on_gc_required)); |
+} |
+ |
+void StoragePartitionImplMap::GarbageCollect( |
+ scoped_ptr<base::hash_set<FilePath> > active_paths, |
+ const base::Closure& done) { |
+ // Include all paths for current StoragePartitions in the active_paths since |
+ // they cannot be deleted safely. |
+ for (PartitionMap::const_iterator it = partitions_.begin(); |
+ it != partitions_.end(); |
+ ++it) { |
+ const StoragePartitionConfig& config = it->first; |
+ if (!config.in_memory) |
+ active_paths->insert(it->second->GetPath()); |
+ } |
- // TODO(ajwong): Schedule a final AsyncObliterate of the whole directory on |
- // the next browser start. http://crbug.com/85127. |
+ // Find the directory holding the StoragePartitions and delete everything in |
+ // there that isn't considered active. |
+ // |
+ // TODO(ajwong): This should sequence on itself so we create the trash |
+ // directory safely. |
+ FilePath storage_root = browser_context_->GetPath().Append( |
+ GetStoragePartitionDomainPath(std::string())); |
+ BrowserThread::PostBlockingPoolTaskAndReply( |
+ FROM_HERE, |
+ base::Bind(&BlockingGarbageCollect, storage_root, |
+ base::Passed(&active_paths)), |
+ done); |
} |
void StoragePartitionImplMap::ForEach( |