| 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 <list> |
| 8 #include <map> | 9 #include <map> |
| 9 #include <string> | 10 #include <string> |
| 10 #include <vector> | 11 #include <vector> |
| 11 | 12 |
| 12 #include "base/callback_forward.h" | 13 #include "base/callback_forward.h" |
| 14 #include "base/containers/mru_cache.h" |
| 13 #include "base/memory/ref_counted.h" | 15 #include "base/memory/ref_counted.h" |
| 14 #include "base/memory/weak_ptr.h" | 16 #include "base/memory/weak_ptr.h" |
| 15 #include "storage/browser/blob/blob_data_handle.h" | 17 #include "storage/browser/blob/blob_data_handle.h" |
| 18 #include "storage/browser/blob/blob_memory_controller.h" |
| 16 #include "storage/browser/blob/blob_storage_registry.h" | 19 #include "storage/browser/blob/blob_storage_registry.h" |
| 17 #include "storage/browser/blob/internal_blob_data.h" | 20 #include "storage/browser/blob/internal_blob_data.h" |
| 18 #include "storage/browser/storage_browser_export.h" | 21 #include "storage/browser/storage_browser_export.h" |
| 19 #include "storage/common/blob_storage/blob_storage_constants.h" | 22 #include "storage/common/blob_storage/blob_storage_constants.h" |
| 20 #include "storage/common/data_element.h" | 23 #include "storage/common/data_element.h" |
| 21 | 24 |
| 22 class GURL; | 25 class GURL; |
| 23 | 26 |
| 24 namespace base { | 27 namespace base { |
| 25 class FilePath; | 28 class FilePath; |
| 26 class Time; | 29 class Time; |
| 27 } | 30 } |
| 28 | 31 |
| 29 namespace content { | 32 namespace content { |
| 30 class BlobStorageContextTest; | 33 class BlobStorageContextTest; |
| 31 class BlobStorageHost; | 34 class BlobStorageHost; |
| 32 } | 35 } |
| 33 | 36 |
| 34 namespace storage { | 37 namespace storage { |
| 35 | 38 |
| 36 class BlobDataBuilder; | 39 class BlobDataBuilder; |
| 37 class BlobDataItem; | 40 class BlobDataItem; |
| 38 class BlobDataSnapshot; | 41 class BlobDataSnapshot; |
| 42 class BlobMemoryController; |
| 39 class ShareableBlobDataItem; | 43 class ShareableBlobDataItem; |
| 40 | 44 |
| 41 // This class handles the logistics of blob Storage within the browser process, | 45 // This class handles the logistics of blob Storage within the browser process, |
| 42 // and maintains a mapping from blob uuid to the data. The class is single | 46 // and maintains a mapping from blob uuid to the data. The class is single |
| 43 // threaded and should only be used on the IO thread. | 47 // threaded and should only be used on the IO thread. |
| 44 // In chromium, there is one instance per profile. | 48 // In chromium, there is one instance per profile. |
| 45 class STORAGE_EXPORT BlobStorageContext | 49 class STORAGE_EXPORT BlobStorageContext |
| 46 : public base::SupportsWeakPtr<BlobStorageContext> { | 50 : public base::SupportsWeakPtr<BlobStorageContext> { |
| 47 public: | 51 public: |
| 48 BlobStorageContext(); | 52 BlobStorageContext(); |
| 53 BlobStorageContext(bool enable_disk, |
| 54 const base::FilePath& blob_storage_dir, |
| 55 scoped_refptr<base::TaskRunner> file_runner); |
| 49 ~BlobStorageContext(); | 56 ~BlobStorageContext(); |
| 50 | 57 |
| 51 scoped_ptr<BlobDataHandle> GetBlobDataFromUUID(const std::string& uuid); | 58 scoped_ptr<BlobDataHandle> GetBlobDataFromUUID(const std::string& uuid); |
| 52 scoped_ptr<BlobDataHandle> GetBlobDataFromPublicURL(const GURL& url); | 59 scoped_ptr<BlobDataHandle> GetBlobDataFromPublicURL(const GURL& url); |
| 53 | 60 |
| 54 // Useful for coining blobs from within the browser process. If the | 61 // Useful for coining blobs from within the browser process. If the |
| 55 // blob cannot be added due to memory consumption, returns NULL. | 62 // blob cannot be added due to memory consumption, returns NULL. |
| 56 // A builder should not be used twice to create blobs, as the internal | 63 // A builder should not be used twice to create blobs, as the internal |
| 57 // resources are refcounted instead of copied for memory efficiency. | 64 // resources are refcounted instead of copied for memory efficiency. |
| 58 // To cleanly use a builder multiple times, please call Clone() on the | 65 // To cleanly use a builder multiple times, please call Clone() on the |
| 59 // builder, or even better for memory savings, clear the builder and append | 66 // builder, or even better for memory savings, clear the builder and append |
| 60 // the previously constructed blob. | 67 // the previously constructed blob. |
| 61 scoped_ptr<BlobDataHandle> AddFinishedBlob(const BlobDataBuilder& builder); | 68 scoped_ptr<BlobDataHandle> AddFinishedBlob(const BlobDataBuilder& builder); |
| 62 | 69 |
| 63 // Deprecated, use const ref version above. | 70 // Deprecated, use const ref version above. |
| 64 scoped_ptr<BlobDataHandle> AddFinishedBlob(const BlobDataBuilder* builder); | 71 scoped_ptr<BlobDataHandle> AddFinishedBlob(const BlobDataBuilder* builder); |
| 65 | 72 |
| 66 // Useful for coining blob urls from within the browser process. | 73 // Useful for coining blob urls from within the browser process. |
| 67 bool RegisterPublicBlobURL(const GURL& url, const std::string& uuid); | 74 bool RegisterPublicBlobURL(const GURL& url, const std::string& uuid); |
| 68 void RevokePublicBlobURL(const GURL& url); | 75 void RevokePublicBlobURL(const GURL& url); |
| 69 | 76 |
| 70 size_t memory_usage() const { return memory_usage_; } | 77 size_t memory_usage() const { return memory_usage_; } |
| 71 size_t memory_available() const { | 78 size_t memory_available() const { |
| 72 return kBlobStorageMaxMemoryUsage - memory_usage_; | 79 return kBlobStorageMaxMemoryUsage - memory_usage_; |
| 73 } | 80 } |
| 74 | 81 |
| 75 const BlobStorageRegistry& registry() { return registry_; } | 82 const BlobStorageRegistry& registry() { return registry_; } |
| 76 | 83 |
| 77 private: | 84 protected: |
| 78 typedef BlobStorageRegistry::Entry BlobRegistryEntry; | 85 friend class BlobAsyncBuilderHost; |
| 79 friend class content::BlobStorageHost; | |
| 80 friend class content::BlobStorageContextTest; | |
| 81 friend class BlobDataHandle::BlobDataHandleShared; | 86 friend class BlobDataHandle::BlobDataHandleShared; |
| 82 friend class ViewBlobInternalsJob; | |
| 83 | 87 |
| 84 void BuildAndStoreBlob(const BlobDataBuilder& external_builder); | 88 void BuildAndStoreBlob(const BlobDataBuilder& external_builder); |
| 85 | 89 |
| 86 // #### Called by BlobDataHandle #### | 90 // #### Called by BlobDataHandle #### |
| 87 scoped_ptr<BlobDataSnapshot> CreateSnapshot(const std::string& uuid); | 91 scoped_ptr<BlobDataSnapshot> CreateSnapshot(const std::string& uuid); |
| 88 bool IsBlobConstructing(const std::string& uuid) const; | 92 bool IsBlobConstructing(const std::string& uuid) const; |
| 89 // Returns if construction was successful. | 93 // Returns if construction was successful. |
| 90 void RunOnConstructionComplete(const std::string& uuid, | 94 void RunOnConstructionComplete(const std::string& uuid, |
| 91 const base::Callback<void(bool)>& done); | 95 const base::Callback<void(bool)>& done); |
| 92 // ################################## | 96 // ################################## |
| 93 | 97 |
| 98 private: |
| 99 typedef BlobStorageRegistry::Entry BlobRegistryEntry; |
| 100 |
| 101 friend class content::BlobStorageHost; |
| 102 friend class content::BlobStorageContextTest; |
| 103 friend class ViewBlobInternalsJob; |
| 104 |
| 105 void BuildAndStoreBlob(BlobDataBuilder* external_builder); |
| 106 |
| 94 // ### Methods called by BlobStorageHost ### | 107 // ### Methods called by BlobStorageHost ### |
| 95 void RegisterBlobUUID(const std::string& uuid); | 108 void RegisterBlobUUID(const std::string& uuid); |
| 96 bool MaybeStartAsyncBlobTransfer(const std::string& uuid); | 109 bool MaybeStartAsyncBlobTransfer(const std::string& uuid); |
| 97 void FinishAsyncBlobTransfer( | 110 void FinishAsyncBlobTransfer( |
| 98 base::Closure done, | 111 base::Closure done, |
| 99 base::Callback<void(storage::IPCBlobCreationCancelCode)> cancel, | 112 base::Callback<void(storage::IPCBlobCreationCancelCode)> cancel, |
| 100 const storage::BlobDataBuilder& builder); | 113 const storage::BlobDataBuilder& builder); |
| 101 void CancelAsyncBlobTransfer( | 114 void CancelAsyncBlobTransfer( |
| 102 const std::string& uuid, | 115 const std::string& uuid, |
| 103 base::Callback<void(storage::IPCBlobCreationCancelCode)> cancel, | 116 base::Callback<void(storage::IPCBlobCreationCancelCode)> cancel, |
| (...skipping 21 matching lines...) Expand all Loading... |
| 125 | 138 |
| 126 // Deconstructs the blob and appends it's contents to the target blob. Items | 139 // Deconstructs the blob and appends it's contents to the target blob. Items |
| 127 // are shared if possible, and copied if the given offset and length | 140 // are shared if possible, and copied if the given offset and length |
| 128 // have to split an item. | 141 // have to split an item. |
| 129 bool AppendBlob(const std::string& target_blob_uuid, | 142 bool AppendBlob(const std::string& target_blob_uuid, |
| 130 const InternalBlobData& blob, | 143 const InternalBlobData& blob, |
| 131 uint64_t offset, | 144 uint64_t offset, |
| 132 uint64_t length, | 145 uint64_t length, |
| 133 InternalBlobData::Builder* target_blob_data); | 146 InternalBlobData::Builder* target_blob_data); |
| 134 | 147 |
| 148 uint64_t GetAndIncrementItemId(); |
| 149 void UpdateItemsInRecents(BlobRegistryEntry* entry); |
| 150 |
| 135 BlobStorageRegistry registry_; | 151 BlobStorageRegistry registry_; |
| 152 BlobMemoryController memory_controller_; |
| 153 uint64_t next_item_id_ = 0; |
| 136 | 154 |
| 137 // Used to keep track of how much memory is being utilized for blob data, | 155 // Used to keep track of how much memory is being utilized for blob data, |
| 138 // we count only the items of TYPE_DATA which are held in memory and not | 156 // we count only the items of TYPE_DATA which are held in memory and not |
| 139 // items of TYPE_FILE. | 157 // items of TYPE_FILE. |
| 140 size_t memory_usage_; | 158 size_t memory_usage_ = 0; |
| 141 | 159 |
| 142 DISALLOW_COPY_AND_ASSIGN(BlobStorageContext); | 160 DISALLOW_COPY_AND_ASSIGN(BlobStorageContext); |
| 143 }; | 161 }; |
| 144 | 162 |
| 145 } // namespace storage | 163 } // namespace storage |
| 146 | 164 |
| 147 #endif // STORAGE_BROWSER_BLOB_BLOB_STORAGE_CONTEXT_H_ | 165 #endif // STORAGE_BROWSER_BLOB_BLOB_STORAGE_CONTEXT_H_ |
| OLD | NEW |