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 #include "content/browser/storage_partition_map.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/callback.h" |
| 9 #include "base/file_path.h" |
| 10 #include "base/stl_util.h" |
| 11 #include "base/string_util.h" |
| 12 #include "content/browser/appcache/chrome_appcache_service.h" |
| 13 #include "content/browser/resource_context_impl.h" |
| 14 #include "content/browser/storage_partition.h" |
| 15 #include "content/public/browser/browser_context.h" |
| 16 #include "content/public/browser/browser_thread.h" |
| 17 #include "content/public/common/content_constants.h" |
| 18 |
| 19 using content::BrowserThread; |
| 20 |
| 21 // Dirname for storing persistent data for renderers with isolated storage. |
| 22 const FilePath::CharType kStoragePartitionDirName[] = |
| 23 FILE_PATH_LITERAL("Storage Partitions"); |
| 24 StoragePartitionMap::StoragePartitionMap( |
| 25 content::BrowserContext* browser_context) |
| 26 : browser_context_(browser_context) { |
| 27 } |
| 28 |
| 29 StoragePartitionMap::~StoragePartitionMap() { |
| 30 STLDeleteContainerPairSecondPointers(partitions_.begin(), |
| 31 partitions_.end()); |
| 32 } |
| 33 |
| 34 StoragePartition* StoragePartitionMap::Get(const std::string& partition_id) { |
| 35 // Find the previously created partition if it's available. |
| 36 std::map<std::string, StoragePartition*>::const_iterator it = |
| 37 partitions_.find(partition_id); |
| 38 if (it != partitions_.end()) |
| 39 return it->second; |
| 40 |
| 41 // There was no previous partition, so let's make a new one. |
| 42 FilePath partition_path = browser_context_->GetPath(); |
| 43 if (!partition_id.empty()) { |
| 44 // TODO(ajwong): This should check the pth is valid? |
| 45 CHECK(IsStringASCII(partition_id)); |
| 46 partition_path = partition_path.Append(kStoragePartitionDirName) |
| 47 .AppendASCII(partition_id); |
| 48 } |
| 49 |
| 50 StoragePartition* storage_partition = |
| 51 StoragePartition::Create(browser_context_, partition_path); |
| 52 partitions_[partition_id] = storage_partition; |
| 53 |
| 54 PostCreateInitialization(storage_partition, partition_path); |
| 55 |
| 56 // TODO(ajwong): We need to remove this conditional by making |
| 57 // InitializeResourceContext() understand having different partition data |
| 58 // based on the renderer_id. |
| 59 if (partition_id.empty()) { |
| 60 InitializeResourceContext(browser_context_); |
| 61 } |
| 62 |
| 63 return storage_partition; |
| 64 } |
| 65 |
| 66 void StoragePartitionMap::ForEach( |
| 67 const base::Callback<void(StoragePartition*)>& callback) { |
| 68 for (std::map<std::string, StoragePartition*>::const_iterator it = |
| 69 partitions_.begin(); |
| 70 it != partitions_.end(); |
| 71 ++it) { |
| 72 callback.Run(it->second); |
| 73 } |
| 74 } |
| 75 |
| 76 void StoragePartitionMap::PostCreateInitialization( |
| 77 StoragePartition* partition, |
| 78 const FilePath& partition_path) { |
| 79 // Check first to avoid memory leak in unittests. |
| 80 if (BrowserThread::IsMessageLoopValid(BrowserThread::IO)) { |
| 81 BrowserThread::PostTask( |
| 82 BrowserThread::IO, FROM_HERE, |
| 83 base::Bind(&ChromeAppCacheService::InitializeOnIOThread, |
| 84 partition->appcache_service(), |
| 85 browser_context_->IsOffTheRecord() ? FilePath() : |
| 86 partition_path.Append(content::kAppCacheDirname), |
| 87 browser_context_->GetResourceContext(), |
| 88 make_scoped_refptr( |
| 89 browser_context_->GetSpecialStoragePolicy()))); |
| 90 } |
| 91 } |
OLD | NEW |