| 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 <map> | 8 #include <map> |
| 9 #include <string> | 9 #include <string> |
| 10 | 10 |
| 11 #include "base/memory/ref_counted.h" | 11 #include "base/memory/ref_counted.h" |
| 12 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
| 13 #include "base/memory/weak_ptr.h" | 13 #include "base/memory/weak_ptr.h" |
| 14 #include "storage/browser/blob/blob_data_handle.h" | 14 #include "storage/browser/blob/blob_data_handle.h" |
| 15 #include "storage/browser/blob/blob_data_snapshot.h" |
| 15 #include "storage/browser/storage_browser_export.h" | 16 #include "storage/browser/storage_browser_export.h" |
| 16 #include "storage/common/blob/blob_data.h" | 17 #include "storage/common/data_element.h" |
| 17 | 18 |
| 18 class GURL; | 19 class GURL; |
| 19 | 20 |
| 20 namespace base { | 21 namespace base { |
| 21 class FilePath; | 22 class FilePath; |
| 22 class Time; | 23 class Time; |
| 23 } | 24 } |
| 24 | 25 |
| 25 namespace content { | 26 namespace content { |
| 26 class BlobStorageHost; | 27 class BlobStorageHost; |
| 27 } | 28 } |
| 28 | 29 |
| 29 namespace storage { | 30 namespace storage { |
| 30 | 31 |
| 31 class BlobDataHandle; | 32 class BlobDataHandle; |
| 33 class BlobDataBuilder; |
| 32 | 34 |
| 33 // This class handles the logistics of blob Storage within the browser process, | 35 // This class handles the logistics of blob Storage within the browser process, |
| 34 // and maintains a mapping from blob uuid to the data. The class is single | 36 // and maintains a mapping from blob uuid to the data. The class is single |
| 35 // threaded and should only be used on the IO thread. | 37 // threaded and should only be used on the IO thread. |
| 36 // In chromium, there is one instance per profile. | 38 // In chromium, there is one instance per profile. |
| 37 class STORAGE_EXPORT BlobStorageContext | 39 class STORAGE_EXPORT BlobStorageContext |
| 38 : public base::SupportsWeakPtr<BlobStorageContext> { | 40 : public base::SupportsWeakPtr<BlobStorageContext> { |
| 39 public: | 41 public: |
| 40 BlobStorageContext(); | 42 BlobStorageContext(); |
| 41 ~BlobStorageContext(); | 43 ~BlobStorageContext(); |
| 42 | 44 |
| 43 scoped_ptr<BlobDataHandle> GetBlobDataFromUUID(const std::string& uuid); | 45 scoped_ptr<BlobDataHandle> GetBlobDataFromUUID(const std::string& uuid); |
| 44 scoped_ptr<BlobDataHandle> GetBlobDataFromPublicURL(const GURL& url); | 46 scoped_ptr<BlobDataHandle> GetBlobDataFromPublicURL(const GURL& url); |
| 45 | 47 |
| 46 // Useful for coining blobs from within the browser process. If the | 48 // Useful for coining blobs from within the browser process. If the |
| 47 // blob cannot be added due to memory consumption, returns NULL. | 49 // blob cannot be added due to memory consumption, returns NULL. |
| 48 scoped_ptr<BlobDataHandle> AddFinishedBlob(const BlobData* blob_data); | 50 scoped_ptr<BlobDataHandle> AddFinishedBlob(const BlobDataBuilder& blob_data); |
| 49 | 51 |
| 50 // Useful for coining blob urls from within the browser process. | 52 // Useful for coining blob urls from within the browser process. |
| 51 bool RegisterPublicBlobURL(const GURL& url, const std::string& uuid); | 53 bool RegisterPublicBlobURL(const GURL& url, const std::string& uuid); |
| 52 void RevokePublicBlobURL(const GURL& url); | 54 void RevokePublicBlobURL(const GURL& url); |
| 53 | 55 |
| 54 private: | 56 private: |
| 55 friend class content::BlobStorageHost; | 57 friend class content::BlobStorageHost; |
| 56 friend class BlobDataHandle::BlobDataHandleShared; | 58 friend class BlobDataHandle::BlobDataHandleShared; |
| 57 friend class ViewBlobInternalsJob; | 59 friend class ViewBlobInternalsJob; |
| 58 | 60 |
| 59 enum EntryFlags { | 61 enum EntryFlags { |
| 60 BEING_BUILT = 1 << 0, | |
| 61 EXCEEDED_MEMORY = 1 << 1, | 62 EXCEEDED_MEMORY = 1 << 1, |
| 62 }; | 63 }; |
| 63 | 64 |
| 64 struct BlobMapEntry { | 65 struct BlobMapEntry { |
| 65 int refcount; | 66 int refcount; |
| 66 int flags; | 67 int flags; |
| 67 scoped_refptr<BlobData> data; | 68 // data and data_builder are mutually exclusive. |
| 69 scoped_ptr<BlobDataSnapshot> data; |
| 70 scoped_ptr<BlobDataBuilder> data_builder; |
| 68 | 71 |
| 69 BlobMapEntry(); | 72 BlobMapEntry(); |
| 70 BlobMapEntry(int refcount, int flags, BlobData* data); | 73 BlobMapEntry(int refcount, BlobDataBuilder* data); |
| 71 ~BlobMapEntry(); | 74 ~BlobMapEntry(); |
| 75 |
| 76 bool IsBeingBuilt(); |
| 72 }; | 77 }; |
| 73 | 78 |
| 74 typedef std::map<std::string, BlobMapEntry> | 79 typedef std::map<std::string, BlobMapEntry*> BlobMap; |
| 75 BlobMap; | |
| 76 typedef std::map<GURL, std::string> BlobURLMap; | 80 typedef std::map<GURL, std::string> BlobURLMap; |
| 77 | 81 |
| 82 // Called by BlobDataHandle. |
| 83 scoped_ptr<BlobDataSnapshot> CreateSnapshot(const std::string& uuid); |
| 84 |
| 78 void StartBuildingBlob(const std::string& uuid); | 85 void StartBuildingBlob(const std::string& uuid); |
| 79 void AppendBlobDataItem(const std::string& uuid, | 86 void AppendBlobDataItem(const std::string& uuid, |
| 80 const BlobData::Item& data_item); | 87 const DataElement& data_item); |
| 81 void FinishBuildingBlob(const std::string& uuid, const std::string& type); | 88 void FinishBuildingBlob(const std::string& uuid, const std::string& type); |
| 82 void CancelBuildingBlob(const std::string& uuid); | 89 void CancelBuildingBlob(const std::string& uuid); |
| 83 void IncrementBlobRefCount(const std::string& uuid); | 90 void IncrementBlobRefCount(const std::string& uuid); |
| 84 void DecrementBlobRefCount(const std::string& uuid); | 91 void DecrementBlobRefCount(const std::string& uuid); |
| 85 | 92 |
| 86 bool ExpandStorageItems(BlobData* target_blob_data, | 93 bool ExpandStorageItems(BlobDataBuilder* target_blob_data, |
| 87 BlobData* src_blob_data, | 94 const BlobDataSnapshot& src_blob_data, |
| 88 uint64 offset, | 95 uint64 offset, |
| 89 uint64 length); | 96 uint64 length); |
| 90 bool AppendBytesItem(BlobData* target_blob_data, | 97 bool AppendBytesItem(BlobDataBuilder* target_blob_data, |
| 91 const char* data, int64 length); | 98 const char* data, |
| 92 void AppendFileItem(BlobData* target_blob_data, | 99 int64 length); |
| 100 void AppendFileItem(BlobDataBuilder* target_blob_data, |
| 93 const base::FilePath& file_path, | 101 const base::FilePath& file_path, |
| 94 uint64 offset, uint64 length, | 102 uint64 offset, |
| 103 uint64 length, |
| 95 const base::Time& expected_modification_time); | 104 const base::Time& expected_modification_time); |
| 96 void AppendFileSystemFileItem( | 105 void AppendFileSystemFileItem(BlobDataBuilder* target_blob_data, |
| 97 BlobData* target_blob_data, | 106 const GURL& url, |
| 98 const GURL& url, uint64 offset, uint64 length, | 107 uint64 offset, |
| 99 const base::Time& expected_modification_time); | 108 uint64 length, |
| 109 const base::Time& expected_modification_time); |
| 100 | 110 |
| 101 bool IsInUse(const std::string& uuid); | 111 bool IsInUse(const std::string& uuid); |
| 102 bool IsBeingBuilt(const std::string& uuid); | 112 bool IsBeingBuilt(const std::string& uuid); |
| 103 bool IsUrlRegistered(const GURL& blob_url); | 113 bool IsUrlRegistered(const GURL& blob_url); |
| 104 | 114 |
| 105 BlobMap blob_map_; | 115 BlobMap blob_map_; |
| 106 BlobURLMap public_blob_urls_; | 116 BlobURLMap public_blob_urls_; |
| 107 | 117 |
| 108 // Used to keep track of how much memory is being utilized for blob data, | 118 // Used to keep track of how much memory is being utilized for blob data, |
| 109 // we count only the items of TYPE_DATA which are held in memory and not | 119 // we count only the items of TYPE_DATA which are held in memory and not |
| 110 // items of TYPE_FILE. | 120 // items of TYPE_FILE. |
| 111 int64 memory_usage_; | 121 int64 memory_usage_; |
| 112 | 122 |
| 113 DISALLOW_COPY_AND_ASSIGN(BlobStorageContext); | 123 DISALLOW_COPY_AND_ASSIGN(BlobStorageContext); |
| 114 }; | 124 }; |
| 115 | 125 |
| 116 } // namespace storage | 126 } // namespace storage |
| 117 | 127 |
| 118 #endif // STORAGE_BROWSER_BLOB_BLOB_STORAGE_CONTEXT_H_ | 128 #endif // STORAGE_BROWSER_BLOB_BLOB_STORAGE_CONTEXT_H_ |
| OLD | NEW |