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 #include <vector> |
10 | 11 |
11 #include "base/memory/ref_counted.h" | 12 #include "base/memory/ref_counted.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_item.h" |
15 #include "storage/browser/blob/blob_data_snapshot.h" | 16 #include "storage/browser/blob/blob_data_snapshot.h" |
| 17 #include "storage/browser/blob/internal_blob_data.h" |
| 18 #include "storage/browser/blob/shareable_blob_data_item.h" |
16 #include "storage/browser/storage_browser_export.h" | 19 #include "storage/browser/storage_browser_export.h" |
17 #include "storage/common/data_element.h" | 20 #include "storage/common/data_element.h" |
18 | 21 |
19 class GURL; | 22 class GURL; |
20 | 23 |
21 namespace base { | 24 namespace base { |
22 class FilePath; | 25 class FilePath; |
23 class Time; | 26 class Time; |
24 } | 27 } |
25 | 28 |
26 namespace content { | 29 namespace content { |
27 class BlobStorageHost; | 30 class BlobStorageHost; |
28 } | 31 } |
29 | 32 |
30 namespace storage { | 33 namespace storage { |
31 | 34 |
32 class BlobDataHandle; | |
33 class BlobDataBuilder; | 35 class BlobDataBuilder; |
| 36 class InternalBlobData; |
34 | 37 |
35 // This class handles the logistics of blob Storage within the browser process, | 38 // This class handles the logistics of blob Storage within the browser process, |
36 // and maintains a mapping from blob uuid to the data. The class is single | 39 // and maintains a mapping from blob uuid to the data. The class is single |
37 // threaded and should only be used on the IO thread. | 40 // threaded and should only be used on the IO thread. |
38 // In chromium, there is one instance per profile. | 41 // In chromium, there is one instance per profile. |
39 class STORAGE_EXPORT BlobStorageContext | 42 class STORAGE_EXPORT BlobStorageContext |
40 : public base::SupportsWeakPtr<BlobStorageContext> { | 43 : public base::SupportsWeakPtr<BlobStorageContext> { |
41 public: | 44 public: |
42 BlobStorageContext(); | 45 BlobStorageContext(); |
43 ~BlobStorageContext(); | 46 ~BlobStorageContext(); |
44 | 47 |
45 scoped_ptr<BlobDataHandle> GetBlobDataFromUUID(const std::string& uuid); | 48 scoped_ptr<BlobDataHandle> GetBlobDataFromUUID(const std::string& uuid); |
46 scoped_ptr<BlobDataHandle> GetBlobDataFromPublicURL(const GURL& url); | 49 scoped_ptr<BlobDataHandle> GetBlobDataFromPublicURL(const GURL& url); |
47 | 50 |
48 // Useful for coining blobs from within the browser process. If the | 51 // Useful for coining blobs from within the browser process. If the |
49 // blob cannot be added due to memory consumption, returns NULL. | 52 // blob cannot be added due to memory consumption, returns NULL. |
50 scoped_ptr<BlobDataHandle> AddFinishedBlob(const BlobDataBuilder& blob_data); | 53 // A builder should not be used twice to create blobs, as the internal |
| 54 // resources are refcounted instead of copied for memory efficiency. |
| 55 // To cleanly use a builder multiple times, please call Clone() on the |
| 56 // builder, or even better for memory savings, clear the builder and append |
| 57 // the previously constructed blob. |
| 58 scoped_ptr<BlobDataHandle> AddFinishedBlob(BlobDataBuilder* builder); |
51 | 59 |
52 // Useful for coining blob urls from within the browser process. | 60 // Useful for coining blob urls from within the browser process. |
53 bool RegisterPublicBlobURL(const GURL& url, const std::string& uuid); | 61 bool RegisterPublicBlobURL(const GURL& url, const std::string& uuid); |
54 void RevokePublicBlobURL(const GURL& url); | 62 void RevokePublicBlobURL(const GURL& url); |
55 | 63 |
| 64 size_t memory_usage() const { return memory_usage_; } |
| 65 |
56 private: | 66 private: |
57 friend class content::BlobStorageHost; | 67 friend class content::BlobStorageHost; |
58 friend class BlobDataHandle::BlobDataHandleShared; | 68 friend class BlobDataHandle::BlobDataHandleShared; |
59 friend class ViewBlobInternalsJob; | 69 friend class ViewBlobInternalsJob; |
60 | 70 |
61 enum EntryFlags { | 71 enum EntryFlags { |
62 EXCEEDED_MEMORY = 1 << 1, | 72 EXCEEDED_MEMORY = 1 << 1, |
63 }; | 73 }; |
64 | 74 |
65 struct BlobMapEntry { | 75 struct BlobMapEntry { |
66 int refcount; | 76 int refcount; |
67 int flags; | 77 int flags; |
68 // data and data_builder are mutually exclusive. | 78 // data and data_builder are mutually exclusive. |
69 scoped_ptr<BlobDataSnapshot> data; | 79 scoped_ptr<InternalBlobData> data; |
70 scoped_ptr<BlobDataBuilder> data_builder; | 80 scoped_ptr<InternalBlobData::Builder> data_builder; |
71 | 81 |
72 BlobMapEntry(); | 82 BlobMapEntry(); |
73 BlobMapEntry(int refcount, BlobDataBuilder* data); | 83 BlobMapEntry(int refcount, InternalBlobData::Builder* data); |
74 ~BlobMapEntry(); | 84 ~BlobMapEntry(); |
75 | 85 |
76 bool IsBeingBuilt(); | 86 bool IsBeingBuilt(); |
77 }; | 87 }; |
78 | 88 |
79 typedef std::map<std::string, BlobMapEntry*> BlobMap; | 89 typedef std::map<std::string, BlobMapEntry*> BlobMap; |
80 typedef std::map<GURL, std::string> BlobURLMap; | 90 typedef std::map<GURL, std::string> BlobURLMap; |
81 | 91 |
82 // Called by BlobDataHandle. | 92 // Called by BlobDataHandle. |
83 scoped_ptr<BlobDataSnapshot> CreateSnapshot(const std::string& uuid); | 93 scoped_ptr<BlobDataSnapshot> CreateSnapshot(const std::string& uuid); |
84 | 94 |
| 95 // ### Methods called by BlobStorageHost ### |
85 void StartBuildingBlob(const std::string& uuid); | 96 void StartBuildingBlob(const std::string& uuid); |
86 void AppendBlobDataItem(const std::string& uuid, | 97 void AppendBlobDataItem(const std::string& uuid, |
87 const DataElement& data_item); | 98 const DataElement& data_item); |
88 void FinishBuildingBlob(const std::string& uuid, const std::string& type); | 99 void FinishBuildingBlob(const std::string& uuid, const std::string& type); |
89 void CancelBuildingBlob(const std::string& uuid); | 100 void CancelBuildingBlob(const std::string& uuid); |
90 void IncrementBlobRefCount(const std::string& uuid); | 101 void IncrementBlobRefCount(const std::string& uuid); |
91 void DecrementBlobRefCount(const std::string& uuid); | 102 void DecrementBlobRefCount(const std::string& uuid); |
| 103 // ######################################### |
92 | 104 |
93 bool ExpandStorageItems(BlobDataBuilder* target_blob_data, | 105 // Flags the entry for exceeding memory, and resets the builder. |
94 const BlobDataSnapshot& src_blob_data, | 106 void BlobEntryExceededMemory(BlobMapEntry* entry); |
95 uint64 offset, | 107 |
96 uint64 length); | 108 // Allocates memory to hold the given data element and copies the data over. |
97 bool AppendBytesItem(BlobDataBuilder* target_blob_data, | 109 scoped_refptr<BlobDataItem> AllocateBlobItem(const std::string& uuid, |
98 const char* data, | 110 const DataElement& data_item); |
99 int64 length); | 111 |
100 void AppendFileItem(BlobDataBuilder* target_blob_data, | 112 // Appends the given blob item to the blob builder. The new blob |
101 const base::FilePath& file_path, | 113 // retains ownership of data_item if applicable, and returns false if there |
102 uint64 offset, | 114 // wasn't enough memory to hold the item. |
103 uint64 length, | 115 bool AppendAllocatedBlobItem(const std::string& target_blob_uuid, |
104 const base::Time& expected_modification_time); | 116 scoped_refptr<BlobDataItem> data_item, |
105 void AppendFileSystemFileItem(BlobDataBuilder* target_blob_data, | 117 InternalBlobData::Builder* target_blob_data); |
106 const GURL& url, | 118 |
107 uint64 offset, | 119 // Deconstructs the blob and appends it's contents to the target blob. Items |
108 uint64 length, | 120 // are shared if possible, and copied if the given offset and length |
109 const base::Time& expected_modification_time); | 121 // have to split an item. |
| 122 bool AppendBlob(const std::string& target_blob_uuid, |
| 123 const InternalBlobData& blob, |
| 124 size_t offset, |
| 125 size_t length, |
| 126 InternalBlobData::Builder* target_blob_data); |
110 | 127 |
111 bool IsInUse(const std::string& uuid); | 128 bool IsInUse(const std::string& uuid); |
112 bool IsBeingBuilt(const std::string& uuid); | 129 bool IsBeingBuilt(const std::string& uuid); |
113 bool IsUrlRegistered(const GURL& blob_url); | 130 bool IsUrlRegistered(const GURL& blob_url); |
114 | 131 |
115 BlobMap blob_map_; | 132 BlobMap blob_map_; |
116 BlobURLMap public_blob_urls_; | 133 BlobURLMap public_blob_urls_; |
117 | 134 |
118 // Used to keep track of how much memory is being utilized for blob data, | 135 // Used to keep track of how much memory is being utilized for blob data, |
119 // we count only the items of TYPE_DATA which are held in memory and not | 136 // we count only the items of TYPE_DATA which are held in memory and not |
120 // items of TYPE_FILE. | 137 // items of TYPE_FILE. |
121 int64 memory_usage_; | 138 size_t memory_usage_; |
122 | 139 |
123 DISALLOW_COPY_AND_ASSIGN(BlobStorageContext); | 140 DISALLOW_COPY_AND_ASSIGN(BlobStorageContext); |
124 }; | 141 }; |
125 | 142 |
126 } // namespace storage | 143 } // namespace storage |
127 | 144 |
128 #endif // STORAGE_BROWSER_BLOB_BLOB_STORAGE_CONTEXT_H_ | 145 #endif // STORAGE_BROWSER_BLOB_BLOB_STORAGE_CONTEXT_H_ |
OLD | NEW |