OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 #ifndef WEBKIT_BLOB_BLOB_STORAGE_CONTROLLER_H_ | 5 #ifndef WEBKIT_BLOB_BLOB_STORAGE_CONTEXT_H_ |
6 #define WEBKIT_BLOB_BLOB_STORAGE_CONTROLLER_H_ | 6 #define WEBKIT_BLOB_BLOB_STORAGE_CONTEXT_H_ |
7 | 7 |
8 #include <map> | 8 #include <map> |
| 9 #include <set> |
9 #include <string> | 10 #include <string> |
| 11 #include <utility> |
10 | 12 |
11 #include "base/hash_tables.h" | 13 #include "base/hash_tables.h" |
| 14 #include "base/gtest_prod_util.h" |
12 #include "base/memory/ref_counted.h" | 15 #include "base/memory/ref_counted.h" |
| 16 #include "base/memory/weak_ptr.h" |
13 #include "base/process.h" | 17 #include "base/process.h" |
| 18 #include "base/shared_memory.h" |
| 19 #include "base/supports_user_data.h" |
14 #include "webkit/blob/blob_data.h" | 20 #include "webkit/blob/blob_data.h" |
15 #include "webkit/storage/webkit_storage_export.h" | 21 #include "webkit/storage/webkit_storage_export.h" |
16 | 22 |
17 class GURL; | 23 class GURL; |
18 class FilePath; | |
19 | 24 |
20 namespace base { | 25 namespace base { |
| 26 class FilePath; |
| 27 class SequencedTaskRunner; |
21 class Time; | 28 class Time; |
22 } | 29 } |
23 | 30 |
| 31 namespace webkit_glue { |
| 32 FORWARD_DECLARE_TEST(ResourceRequestBodyTest, ResolveBlobAndCreateUploadData); |
| 33 } |
| 34 |
24 namespace webkit_blob { | 35 namespace webkit_blob { |
25 | 36 |
26 // This class handles the logistics of blob Storage within the browser process. | 37 class BlobDataHandle; |
27 class WEBKIT_STORAGE_EXPORT BlobStorageController { | 38 class BlobStorageConsumer; |
| 39 class BlobStorageContext; |
| 40 |
| 41 // A scoper object for use in chrome's main browser process, ensures |
| 42 // the underlying BlobData and its uuid remain in BlobStorageContext's |
| 43 // collection for the duration. This object has delete semantics and |
| 44 // maybe deleted on any thread. |
| 45 class WEBKIT_STORAGE_EXPORT BlobDataHandle |
| 46 : public base::SupportsUserData::Data { |
28 public: | 47 public: |
29 BlobStorageController(); | 48 virtual ~BlobDataHandle(); // Maybe be deleted on any thread. |
30 ~BlobStorageController(); | 49 BlobData* data() const; // May only be accessed on the IO thread. |
31 | |
32 void StartBuildingBlob(const GURL& url); | |
33 void AppendBlobDataItem(const GURL& url, const BlobData::Item& data_item); | |
34 void FinishBuildingBlob(const GURL& url, const std::string& content_type); | |
35 void AddFinishedBlob(const GURL& url, const BlobData* blob_data); | |
36 void CloneBlob(const GURL& url, const GURL& src_url); | |
37 void RemoveBlob(const GURL& url); | |
38 BlobData* GetBlobDataFromUrl(const GURL& url); | |
39 | 50 |
40 private: | 51 private: |
| 52 friend class BlobStorageContext; |
| 53 BlobDataHandle(BlobData* blob_data, BlobStorageContext* context, |
| 54 base::SequencedTaskRunner* task_runner); |
| 55 |
| 56 static void DeleteHelper( |
| 57 scoped_ptr<base::WeakPtr<BlobStorageContext> > context, |
| 58 scoped_refptr<BlobData> blob_data); |
| 59 |
| 60 scoped_refptr<BlobData> blob_data_; |
| 61 scoped_ptr<base::WeakPtr<BlobStorageContext> > context_; |
| 62 scoped_refptr<base::SequencedTaskRunner> io_task_runner_; |
| 63 }; |
| 64 |
| 65 // This class handles the logistics of blob storage for a single child process. |
| 66 // There is generally one instance per child process. When the child process |
| 67 // terminates all blob references attibutable to that process go away upon |
| 68 // destruction of the instance. The class is single threaded and should |
| 69 // only be used on the IO thread. |
| 70 class WEBKIT_STORAGE_EXPORT BlobStorageConsumer { |
| 71 public: |
| 72 explicit BlobStorageConsumer(BlobStorageContext* context); |
| 73 ~BlobStorageConsumer(); |
| 74 |
| 75 // Methods to support the IPC message protocol. |
| 76 void StartBuildingBlob(const std::string& uuid); |
| 77 void AppendBlobDataItem(const std::string& uuid, |
| 78 const BlobData::Item& data_item); |
| 79 void CancelBuildingBlob(const std::string& uuid); |
| 80 void FinishBuildingBlob(const std::string& uuid, const std::string& type); |
| 81 void IncrementBlobRefCount(const std::string& uuid); |
| 82 void DecrementBlobRefCount(const std::string& uuid); |
| 83 void RegisterPublicBlobURL(const GURL& blob_url, const std::string& uuid); |
| 84 void RevokePublicBlobURL(const GURL& blob_url); |
| 85 |
| 86 private: |
| 87 typedef std::map<std::string, int> BlobReferenceMap; |
| 88 |
| 89 // Collection of blob ids and a count of how many usages |
| 90 // of that id are attributable to this consumer. |
| 91 BlobReferenceMap blobs_inuse_map_; |
| 92 |
| 93 // The set of public blob urls coined by this consumer. |
| 94 std::set<GURL> public_blob_urls_; |
| 95 |
| 96 base::WeakPtr<BlobStorageContext> context_; |
| 97 }; |
| 98 |
| 99 // This class handles the logistics of blob Storage within the browser process, |
| 100 // and maintains a mapping from blob uuid to the data. The class is single |
| 101 // threaded and should only be used on the IO thread. |
| 102 // In chromium, there is one instance per storage-partition. |
| 103 class WEBKIT_STORAGE_EXPORT BlobStorageContext |
| 104 : public base::SupportsWeakPtr<BlobStorageContext> { |
| 105 public: |
| 106 BlobStorageContext(); |
| 107 ~BlobStorageContext(); |
| 108 |
| 109 scoped_ptr<BlobDataHandle> GetBlobDataFromUUID(const std::string& uuid); |
| 110 scoped_ptr<BlobDataHandle> GetBlobDataFromPublicUrl(const GURL& url); |
| 111 |
| 112 private: |
| 113 friend class BlobDataHandle; |
| 114 friend class BlobStorageConsumer; |
41 friend class ViewBlobInternalsJob; | 115 friend class ViewBlobInternalsJob; |
| 116 friend class TestShellWebBlobRegistryImpl; |
| 117 friend class ScopedTextBlob; |
| 118 FRIEND_TEST_ALL_PREFIXES(webkit_glue::ResourceRequestBodyTest, ResolveBlobAndC
reateUploadData); |
42 | 119 |
43 typedef base::hash_map<std::string, scoped_refptr<BlobData> > BlobMap; | 120 typedef std::map<std::string, std::pair<int, scoped_refptr<BlobData> > > |
44 typedef std::map<BlobData*, int> BlobDataUsageMap; | 121 BlobMap; |
| 122 typedef std::map<GURL, std::string> BlobURLMap; |
| 123 |
| 124 void StartBuildingBlob(const std::string& uuid); |
| 125 void AppendBlobDataItem(const std::string& uuid, |
| 126 const BlobData::Item& data_item); |
| 127 void FinishBuildingBlob(const std::string& uuid, const std::string& type); |
| 128 void CancelBuildingBlob(const std::string& uuid); |
| 129 void AddFinishedBlob(const BlobData* blob_data); |
| 130 void IncrementBlobRefCount(const std::string& uuid); |
| 131 void DecrementBlobRefCount(const std::string& uuid); |
| 132 void RegisterPublicBlobURL(const GURL& url, const std::string& uuid); |
| 133 void RevokePublicBlobURL(const GURL& url); |
45 | 134 |
46 void AppendStorageItems(BlobData* target_blob_data, | 135 void AppendStorageItems(BlobData* target_blob_data, |
47 BlobData* src_blob_data, | 136 BlobData* src_blob_data, |
48 uint64 offset, | 137 uint64 offset, |
49 uint64 length); | 138 uint64 length); |
50 void AppendFileItem(BlobData* target_blob_data, | 139 void AppendFileItem(BlobData* target_blob_data, |
51 const FilePath& file_path, uint64 offset, uint64 length, | 140 const base::FilePath& file_path, |
| 141 uint64 offset, uint64 length, |
52 const base::Time& expected_modification_time); | 142 const base::Time& expected_modification_time); |
53 void AppendFileSystemFileItem( | 143 void AppendFileSystemFileItem( |
54 BlobData* target_blob_data, | 144 BlobData* target_blob_data, |
55 const GURL& url, uint64 offset, uint64 length, | 145 const GURL& url, uint64 offset, uint64 length, |
56 const base::Time& expected_modification_time); | 146 const base::Time& expected_modification_time); |
57 | 147 |
58 bool RemoveFromMapHelper(BlobMap* map, const GURL& url); | 148 bool DecrementBlobRefCountHelper(BlobMap* map, const std::string& uuid); |
59 | |
60 void IncrementBlobDataUsage(BlobData* blob_data); | |
61 // Returns true if no longer in use. | |
62 bool DecrementBlobDataUsage(BlobData* blob_data); | |
63 | 149 |
64 BlobMap blob_map_; | 150 BlobMap blob_map_; |
65 BlobMap unfinalized_blob_map_; | 151 BlobMap unfinalized_blob_map_; |
| 152 BlobURLMap public_blob_urls_; |
66 | 153 |
67 // Used to keep track of how much memory is being utitlized for blob data, | 154 // Used to keep track of how much memory is being utitlized for blob data, |
68 // we count only the items of TYPE_DATA which are held in memory and not | 155 // we count only the items of TYPE_DATA which are held in memory and not |
69 // items of TYPE_FILE. | 156 // items of TYPE_FILE. |
70 int64 memory_usage_; | 157 int64 memory_usage_; |
71 | 158 |
72 // Multiple urls can refer to the same blob data, this map keeps track of | 159 DISALLOW_COPY_AND_ASSIGN(BlobStorageContext); |
73 // how many urls refer to a BlobData. | |
74 BlobDataUsageMap blob_data_usage_count_; | |
75 | |
76 DISALLOW_COPY_AND_ASSIGN(BlobStorageController); | |
77 }; | 160 }; |
78 | 161 |
79 } // namespace webkit_blob | 162 } // namespace webkit_blob |
80 | 163 |
81 #endif // WEBKIT_BLOB_BLOB_STORAGE_CONTROLLER_H_ | 164 #endif // WEBKIT_BLOB_BLOB_STORAGE_CONTEXT_H_ |
OLD | NEW |