| OLD | NEW |
| 1 // Copyright (c) 2013 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 STORAGE_BROWSER_BLOB_BLOB_STORAGE_CONTEXT_H_ | 5 #ifndef STORAGE_BROWSER_BLOB_BLOB_STORAGE_CONTEXT_H_ |
| 6 #define STORAGE_BROWSER_BLOB_BLOB_STORAGE_CONTEXT_H_ | 6 #define STORAGE_BROWSER_BLOB_BLOB_STORAGE_CONTEXT_H_ |
| 7 | 7 |
| 8 #include <stddef.h> | 8 #include <stddef.h> |
| 9 #include <stdint.h> | 9 #include <stdint.h> |
| 10 | 10 |
| 11 #include <map> | 11 #include <map> |
| 12 #include <string> | 12 #include <string> |
| 13 #include <vector> | 13 #include <vector> |
| 14 | 14 |
| 15 #include "base/callback_forward.h" |
| 16 #include "base/gtest_prod_util.h" |
| 15 #include "base/macros.h" | 17 #include "base/macros.h" |
| 16 #include "base/memory/ref_counted.h" | 18 #include "base/memory/ref_counted.h" |
| 17 #include "base/memory/weak_ptr.h" | 19 #include "base/memory/weak_ptr.h" |
| 18 #include "storage/browser/blob/blob_data_handle.h" | 20 #include "storage/browser/blob/blob_data_handle.h" |
| 19 #include "storage/browser/blob/blob_data_item.h" | 21 #include "storage/browser/blob/blob_storage_registry.h" |
| 20 #include "storage/browser/blob/blob_data_snapshot.h" | |
| 21 #include "storage/browser/blob/internal_blob_data.h" | 22 #include "storage/browser/blob/internal_blob_data.h" |
| 22 #include "storage/browser/blob/shareable_blob_data_item.h" | |
| 23 #include "storage/browser/storage_browser_export.h" | 23 #include "storage/browser/storage_browser_export.h" |
| 24 #include "storage/common/blob_storage/blob_storage_constants.h" |
| 24 #include "storage/common/data_element.h" | 25 #include "storage/common/data_element.h" |
| 25 | 26 |
| 26 class GURL; | 27 class GURL; |
| 27 | 28 |
| 28 namespace base { | 29 namespace base { |
| 29 class FilePath; | 30 class FilePath; |
| 30 class Time; | 31 class Time; |
| 31 } | 32 } |
| 32 | 33 |
| 33 namespace content { | 34 namespace content { |
| 34 class BlobStorageHost; | 35 class BlobDispatcherHost; |
| 36 class BlobDispatcherHostTest; |
| 35 } | 37 } |
| 36 | 38 |
| 37 namespace storage { | 39 namespace storage { |
| 38 | 40 |
| 39 class BlobDataBuilder; | 41 class BlobDataBuilder; |
| 40 class InternalBlobData; | 42 class BlobDataItem; |
| 43 class BlobDataSnapshot; |
| 44 class ShareableBlobDataItem; |
| 41 | 45 |
| 42 // This class handles the logistics of blob Storage within the browser process, | 46 // This class handles the logistics of blob Storage within the browser process, |
| 43 // and maintains a mapping from blob uuid to the data. The class is single | 47 // and maintains a mapping from blob uuid to the data. The class is single |
| 44 // threaded and should only be used on the IO thread. | 48 // threaded and should only be used on the IO thread. |
| 45 // In chromium, there is one instance per profile. | 49 // In chromium, there is one instance per profile. |
| 46 class STORAGE_EXPORT BlobStorageContext | 50 class STORAGE_EXPORT BlobStorageContext |
| 47 : public base::SupportsWeakPtr<BlobStorageContext> { | 51 : public base::SupportsWeakPtr<BlobStorageContext> { |
| 48 public: | 52 public: |
| 49 BlobStorageContext(); | 53 BlobStorageContext(); |
| 50 ~BlobStorageContext(); | 54 ~BlobStorageContext(); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 62 scoped_ptr<BlobDataHandle> AddFinishedBlob(const BlobDataBuilder& builder); | 66 scoped_ptr<BlobDataHandle> AddFinishedBlob(const BlobDataBuilder& builder); |
| 63 | 67 |
| 64 // Deprecated, use const ref version above. | 68 // Deprecated, use const ref version above. |
| 65 scoped_ptr<BlobDataHandle> AddFinishedBlob(const BlobDataBuilder* builder); | 69 scoped_ptr<BlobDataHandle> AddFinishedBlob(const BlobDataBuilder* builder); |
| 66 | 70 |
| 67 // Useful for coining blob urls from within the browser process. | 71 // Useful for coining blob urls from within the browser process. |
| 68 bool RegisterPublicBlobURL(const GURL& url, const std::string& uuid); | 72 bool RegisterPublicBlobURL(const GURL& url, const std::string& uuid); |
| 69 void RevokePublicBlobURL(const GURL& url); | 73 void RevokePublicBlobURL(const GURL& url); |
| 70 | 74 |
| 71 size_t memory_usage() const { return memory_usage_; } | 75 size_t memory_usage() const { return memory_usage_; } |
| 72 size_t blob_count() const { return blob_map_.size(); } | 76 size_t blob_count() const { return registry_.blob_count(); } |
| 77 size_t memory_available() const { |
| 78 return kBlobStorageMaxMemoryUsage - memory_usage_; |
| 79 } |
| 80 |
| 81 const BlobStorageRegistry& registry() { return registry_; } |
| 73 | 82 |
| 74 private: | 83 private: |
| 75 friend class content::BlobStorageHost; | 84 using BlobRegistryEntry = BlobStorageRegistry::Entry; |
| 85 friend class content::BlobDispatcherHost; |
| 86 friend class BlobAsyncBuilderHost; |
| 87 friend class BlobAsyncBuilderHostTest; |
| 88 friend class BlobDataHandle; |
| 76 friend class BlobDataHandle::BlobDataHandleShared; | 89 friend class BlobDataHandle::BlobDataHandleShared; |
| 90 friend class BlobReaderTest; |
| 91 FRIEND_TEST_ALL_PREFIXES(BlobReaderTest, HandleBeforeAsyncCancel); |
| 92 friend class BlobStorageContextTest; |
| 93 FRIEND_TEST_ALL_PREFIXES(BlobStorageContextTest, IncrementDecrementRef); |
| 94 FRIEND_TEST_ALL_PREFIXES(BlobStorageContextTest, OnCancelBuildingBlob); |
| 95 FRIEND_TEST_ALL_PREFIXES(BlobStorageContextTest, PublicBlobUrls); |
| 96 FRIEND_TEST_ALL_PREFIXES(BlobStorageContextTest, |
| 97 TestUnknownBrokenAndBuildingBlobReference); |
| 77 friend class ViewBlobInternalsJob; | 98 friend class ViewBlobInternalsJob; |
| 78 | 99 |
| 79 enum EntryFlags { | 100 // CompletePendingBlob or CancelPendingBlob should be called after this. |
| 80 EXCEEDED_MEMORY = 1 << 1, | 101 void CreatePendingBlob(const std::string& uuid, |
| 81 }; | 102 const std::string& content_type, |
| 103 const std::string& content_disposition); |
| 82 | 104 |
| 83 struct BlobMapEntry { | 105 // This includes resolving blob references in the builder. This will run the |
| 84 int refcount; | 106 // callbacks given in RunOnConstructionComplete. |
| 85 int flags; | 107 void CompletePendingBlob(const BlobDataBuilder& external_builder); |
| 86 // data and data_builder are mutually exclusive. | |
| 87 scoped_ptr<InternalBlobData> data; | |
| 88 scoped_ptr<InternalBlobData::Builder> data_builder; | |
| 89 | 108 |
| 90 BlobMapEntry(); | 109 // This will run the callbacks given in RunOnConstructionComplete. |
| 91 BlobMapEntry(int refcount, InternalBlobData::Builder* data); | 110 void CancelPendingBlob(const std::string& uuid, |
| 92 ~BlobMapEntry(); | 111 IPCBlobCreationCancelCode reason); |
| 93 | 112 |
| 94 bool IsBeingBuilt(); | |
| 95 }; | |
| 96 | |
| 97 typedef std::map<std::string, BlobMapEntry*> BlobMap; | |
| 98 typedef std::map<GURL, std::string> BlobURLMap; | |
| 99 | |
| 100 // Called by BlobDataHandle. | |
| 101 scoped_ptr<BlobDataSnapshot> CreateSnapshot(const std::string& uuid); | |
| 102 | |
| 103 // ### Methods called by BlobStorageHost ### | |
| 104 void StartBuildingBlob(const std::string& uuid); | |
| 105 void AppendBlobDataItem(const std::string& uuid, | |
| 106 const DataElement& data_item); | |
| 107 void FinishBuildingBlob(const std::string& uuid, const std::string& type); | |
| 108 void CancelBuildingBlob(const std::string& uuid); | |
| 109 void IncrementBlobRefCount(const std::string& uuid); | 113 void IncrementBlobRefCount(const std::string& uuid); |
| 110 void DecrementBlobRefCount(const std::string& uuid); | 114 void DecrementBlobRefCount(const std::string& uuid); |
| 111 // ######################################### | |
| 112 | 115 |
| 113 // Flags the entry for exceeding memory, and resets the builder. | 116 // Methods called by BlobDataHandle: |
| 114 void BlobEntryExceededMemory(BlobMapEntry* entry); | 117 // This will return an empty snapshot until the blob is complete. |
| 115 | 118 // TODO(dmurph): After we make the snapshot method in BlobHandle private, then |
| 116 // Allocates memory to hold the given data element and copies the data over. | 119 // make this DCHECK on the blob not being complete. |
| 117 scoped_refptr<BlobDataItem> AllocateBlobItem(const std::string& uuid, | 120 scoped_ptr<BlobDataSnapshot> CreateSnapshot(const std::string& uuid); |
| 118 const DataElement& data_item); | 121 bool IsBroken(const std::string& uuid) const; |
| 122 bool IsBeingBuilt(const std::string& uuid) const; |
| 123 // Runs |done| when construction completes, with true if it was successful. |
| 124 void RunOnConstructionComplete(const std::string& uuid, |
| 125 const base::Callback<void(bool)>& done); |
| 119 | 126 |
| 120 // Appends the given blob item to the blob builder. The new blob | 127 // Appends the given blob item to the blob builder. The new blob |
| 121 // retains ownership of data_item if applicable, and returns false if there | 128 // retains ownership of data_item if applicable, and returns false if there |
| 122 // wasn't enough memory to hold the item. | 129 // was an error and pouplates the error_code. We can either have an error of: |
| 130 // OUT_OF_MEMORY: We are out of memory to store this blob. |
| 131 // REFERENCED_BLOB_BROKEN: One of the referenced blobs is broken. |
| 123 bool AppendAllocatedBlobItem(const std::string& target_blob_uuid, | 132 bool AppendAllocatedBlobItem(const std::string& target_blob_uuid, |
| 124 scoped_refptr<BlobDataItem> data_item, | 133 scoped_refptr<BlobDataItem> data_item, |
| 125 InternalBlobData::Builder* target_blob_data); | 134 InternalBlobData::Builder* target_blob_data, |
| 135 IPCBlobCreationCancelCode* error_code); |
| 136 |
| 137 // Allocates a shareable blob data item, with blob references resolved. If |
| 138 // there isn't enough memory, then a nullptr is returned. |
| 139 scoped_refptr<ShareableBlobDataItem> AllocateShareableBlobDataItem( |
| 140 const std::string& target_blob_uuid, |
| 141 scoped_refptr<BlobDataItem> data_item); |
| 126 | 142 |
| 127 // Deconstructs the blob and appends it's contents to the target blob. Items | 143 // Deconstructs the blob and appends it's contents to the target blob. Items |
| 128 // are shared if possible, and copied if the given offset and length | 144 // are shared if possible, and copied if the given offset and length |
| 129 // have to split an item. | 145 // have to split an item. |
| 130 bool AppendBlob(const std::string& target_blob_uuid, | 146 bool AppendBlob(const std::string& target_blob_uuid, |
| 131 const InternalBlobData& blob, | 147 const InternalBlobData& blob, |
| 132 uint64_t offset, | 148 uint64_t offset, |
| 133 uint64_t length, | 149 uint64_t length, |
| 134 InternalBlobData::Builder* target_blob_data); | 150 InternalBlobData::Builder* target_blob_data); |
| 135 | 151 |
| 136 bool IsInUse(const std::string& uuid); | 152 BlobStorageRegistry registry_; |
| 137 bool IsBeingBuilt(const std::string& uuid); | |
| 138 bool IsUrlRegistered(const GURL& blob_url); | |
| 139 | |
| 140 BlobMap blob_map_; | |
| 141 BlobURLMap public_blob_urls_; | |
| 142 | 153 |
| 143 // Used to keep track of how much memory is being utilized for blob data, | 154 // Used to keep track of how much memory is being utilized for blob data, |
| 144 // 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 |
| 145 // items of TYPE_FILE. | 156 // items of TYPE_FILE. |
| 146 size_t memory_usage_; | 157 size_t memory_usage_; |
| 147 | 158 |
| 148 DISALLOW_COPY_AND_ASSIGN(BlobStorageContext); | 159 DISALLOW_COPY_AND_ASSIGN(BlobStorageContext); |
| 149 }; | 160 }; |
| 150 | 161 |
| 151 } // namespace storage | 162 } // namespace storage |
| 152 | 163 |
| 153 #endif // STORAGE_BROWSER_BLOB_BLOB_STORAGE_CONTEXT_H_ | 164 #endif // STORAGE_BROWSER_BLOB_BLOB_STORAGE_CONTEXT_H_ |
| OLD | NEW |