Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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 |
| 13 #include "base/gtest_prod_util.h" | |
| 11 #include "base/hash_tables.h" | 14 #include "base/hash_tables.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 | 24 |
| 19 namespace base { | 25 namespace base { |
| 20 class FilePath; | 26 class FilePath; |
| 27 class SequencedTaskRunner; | |
| 21 class Time; | 28 class Time; |
| 22 } | 29 } |
| 23 | 30 |
| 24 namespace webkit_blob { | 31 namespace webkit_blob { |
| 25 | 32 |
| 26 // This class handles the logistics of blob Storage within the browser process. | 33 class BlobDataHandle; |
| 27 class WEBKIT_STORAGE_EXPORT BlobStorageController { | 34 class BlobStorageHost; |
| 35 class BlobStorageContext; | |
| 36 | |
| 37 // A scoper object for use in chrome's main browser process, ensures | |
| 38 // the underlying BlobData and its uuid remain in BlobStorageContext's | |
| 39 // collection for the duration. This object has delete semantics and | |
| 40 // maybe deleted on any thread. | |
| 41 class WEBKIT_STORAGE_EXPORT BlobDataHandle | |
| 42 : public base::SupportsUserData::Data { | |
| 28 public: | 43 public: |
| 29 BlobStorageController(); | 44 virtual ~BlobDataHandle(); // Maybe be deleted on any thread. |
| 30 ~BlobStorageController(); | 45 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 | 46 |
| 40 private: | 47 private: |
| 41 friend class ViewBlobInternalsJob; | 48 friend class BlobStorageContext; |
| 49 BlobDataHandle(BlobData* blob_data, BlobStorageContext* context, | |
| 50 base::SequencedTaskRunner* task_runner); | |
| 42 | 51 |
| 43 typedef base::hash_map<std::string, scoped_refptr<BlobData> > BlobMap; | 52 static void DeleteHelper( |
| 44 typedef std::map<BlobData*, int> BlobDataUsageMap; | 53 base::WeakPtr<BlobStorageContext> context, |
| 54 BlobData* blob_data); | |
| 55 | |
| 56 BlobData* blob_data_; // Intentionally a raw ptr to a non-thread-safe ref. | |
|
ericu
2013/04/24 23:54:43
Why?
ericu
2013/04/26 22:17:37
This is still worth a more-helpful comment, though
michaeln
2013/04/29 18:32:48
I added that greater explanation comment in the .c
| |
| 57 base::WeakPtr<BlobStorageContext> context_; | |
| 58 scoped_refptr<base::SequencedTaskRunner> io_task_runner_; | |
| 59 }; | |
| 60 | |
| 61 // This class handles the logistics of blob storage for a single child process. | |
| 62 // There is one instance per child process. When the child process | |
| 63 // terminates all blob references attibutable to that process go away upon | |
| 64 // destruction of the instance. The class is single threaded and should | |
| 65 // only be used on the IO thread. | |
| 66 class WEBKIT_STORAGE_EXPORT BlobStorageHost { | |
| 67 public: | |
| 68 explicit BlobStorageHost(BlobStorageContext* context); | |
| 69 ~BlobStorageHost(); | |
| 70 | |
| 71 // Methods to support the IPC message protocol. | |
| 72 // A false return indicates a problem with the inputs | |
| 73 // like a non-existent or pre-existent uuid or url. | |
| 74 bool StartBuildingBlob(const std::string& uuid); | |
| 75 bool AppendBlobDataItem(const std::string& uuid, | |
| 76 const BlobData::Item& data_item); | |
| 77 bool CancelBuildingBlob(const std::string& uuid); | |
|
ericu
2013/04/24 23:54:43
What would you think about WARN_UNUSED_RETURN on t
michaeln
2013/04/29 18:32:48
Done.
| |
| 78 bool FinishBuildingBlob(const std::string& uuid, const std::string& type); | |
| 79 bool IncrementBlobRefCount(const std::string& uuid); | |
| 80 bool DecrementBlobRefCount(const std::string& uuid); | |
| 81 bool RegisterPublicBlobURL(const GURL& blob_url, const std::string& uuid); | |
| 82 bool RevokePublicBlobURL(const GURL& blob_url); | |
| 83 | |
| 84 private: | |
| 85 typedef std::map<std::string, int> BlobReferenceMap; | |
| 86 | |
| 87 bool IsInUse(const std::string& uuid); | |
| 88 bool HasUseCountOfOne(const std::string& uuid); | |
| 89 bool IsUrlRegistered(const GURL& blob_url); | |
| 90 | |
| 91 // Collection of blob ids and a count of how many usages | |
| 92 // of that id are attributable to this consumer. | |
| 93 BlobReferenceMap blobs_inuse_map_; | |
| 94 | |
| 95 // The set of public blob urls coined by this consumer. | |
| 96 std::set<GURL> public_blob_urls_; | |
| 97 | |
| 98 base::WeakPtr<BlobStorageContext> context_; | |
| 99 }; | |
| 100 | |
| 101 // This class handles the logistics of blob Storage within the browser process, | |
| 102 // and maintains a mapping from blob uuid to the data. The class is single | |
| 103 // threaded and should only be used on the IO thread. | |
| 104 // In chromium, there is one instance per profile. | |
| 105 class WEBKIT_STORAGE_EXPORT BlobStorageContext | |
| 106 : public base::SupportsWeakPtr<BlobStorageContext> { | |
| 107 public: | |
| 108 BlobStorageContext(); | |
| 109 ~BlobStorageContext(); | |
| 110 | |
| 111 scoped_ptr<BlobDataHandle> GetBlobDataFromUUID(const std::string& uuid); | |
| 112 scoped_ptr<BlobDataHandle> GetBlobDataFromPublicURL(const GURL& url); | |
| 113 | |
| 114 // Useful for coining blobs from within the browser process. If the | |
| 115 // blob cannot be added due to memory consumption, returns NULL. | |
| 116 scoped_ptr<BlobDataHandle> AddFinishedBlob(const BlobData* blob_data); | |
| 117 | |
| 118 private: | |
| 119 friend class BlobDataHandle; | |
| 120 friend class BlobStorageHost; | |
| 121 | |
| 122 enum EntryFlags { | |
| 123 BEING_BUILT = 1 << 0, | |
| 124 EXCEEDED_MEMORY = 1 << 1, | |
| 125 }; | |
| 126 | |
| 127 struct BlobMapEntry { | |
| 128 int refcount; | |
| 129 int flags; | |
| 130 scoped_refptr<BlobData> data; | |
| 131 | |
| 132 BlobMapEntry() : refcount(0), flags(0) {} | |
| 133 BlobMapEntry(int refcount, int flags, BlobData* data) | |
| 134 : refcount(refcount), flags(flags), data(data) {} | |
| 135 }; | |
| 136 | |
| 137 typedef std::map<std::string, BlobMapEntry> | |
| 138 BlobMap; | |
| 139 typedef std::map<GURL, std::string> BlobURLMap; | |
| 140 | |
| 141 void StartBuildingBlob(const std::string& uuid); | |
| 142 void AppendBlobDataItem(const std::string& uuid, | |
| 143 const BlobData::Item& data_item); | |
| 144 void FinishBuildingBlob(const std::string& uuid, const std::string& type); | |
| 145 void CancelBuildingBlob(const std::string& uuid); | |
| 146 void IncrementBlobRefCount(const std::string& uuid); | |
| 147 void DecrementBlobRefCount(const std::string& uuid); | |
| 148 void RegisterPublicBlobURL(const GURL& url, const std::string& uuid); | |
| 149 void RevokePublicBlobURL(const GURL& url); | |
| 45 | 150 |
| 46 void AppendStorageItems(BlobData* target_blob_data, | 151 void AppendStorageItems(BlobData* target_blob_data, |
| 47 BlobData* src_blob_data, | 152 BlobData* src_blob_data, |
| 48 uint64 offset, | 153 uint64 offset, |
| 49 uint64 length); | 154 uint64 length); |
| 50 void AppendFileItem(BlobData* target_blob_data, | 155 void AppendFileItem(BlobData* target_blob_data, |
| 51 const base::FilePath& file_path, uint64 offset, | 156 const base::FilePath& file_path, |
| 52 uint64 length, | 157 uint64 offset, uint64 length, |
| 53 const base::Time& expected_modification_time); | 158 const base::Time& expected_modification_time); |
| 54 void AppendFileSystemFileItem( | 159 void AppendFileSystemFileItem( |
| 55 BlobData* target_blob_data, | 160 BlobData* target_blob_data, |
| 56 const GURL& url, uint64 offset, uint64 length, | 161 const GURL& url, uint64 offset, uint64 length, |
| 57 const base::Time& expected_modification_time); | 162 const base::Time& expected_modification_time); |
| 58 | 163 |
| 59 bool RemoveFromMapHelper(BlobMap* map, const GURL& url); | 164 bool IsInUse(const std::string& uuid); |
| 60 | 165 bool HasUseCountOfOne(const std::string& uuid); |
| 61 void IncrementBlobDataUsage(BlobData* blob_data); | 166 bool IsUrlRegistered(const GURL& blob_url); |
| 62 // Returns true if no longer in use. | |
| 63 bool DecrementBlobDataUsage(BlobData* blob_data); | |
| 64 | 167 |
| 65 BlobMap blob_map_; | 168 BlobMap blob_map_; |
| 66 BlobMap unfinalized_blob_map_; | 169 BlobURLMap public_blob_urls_; |
| 67 | 170 |
| 68 // Used to keep track of how much memory is being utitlized for blob data, | 171 // Used to keep track of how much memory is being utitlized for blob data, |
| 69 // we count only the items of TYPE_DATA which are held in memory and not | 172 // we count only the items of TYPE_DATA which are held in memory and not |
| 70 // items of TYPE_FILE. | 173 // items of TYPE_FILE. |
| 71 int64 memory_usage_; | 174 int64 memory_usage_; |
| 72 | 175 |
| 73 // Multiple urls can refer to the same blob data, this map keeps track of | 176 DISALLOW_COPY_AND_ASSIGN(BlobStorageContext); |
| 74 // how many urls refer to a BlobData. | |
| 75 BlobDataUsageMap blob_data_usage_count_; | |
| 76 | |
| 77 DISALLOW_COPY_AND_ASSIGN(BlobStorageController); | |
| 78 }; | 177 }; |
| 79 | 178 |
| 80 } // namespace webkit_blob | 179 } // namespace webkit_blob |
| 81 | 180 |
| 82 #endif // WEBKIT_BLOB_BLOB_STORAGE_CONTROLLER_H_ | 181 #endif // WEBKIT_BLOB_BLOB_STORAGE_CONTEXT_H_ |
| OLD | NEW |