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
3 // found in the LICENSE file.
5 #ifndef STORAGE_BROWSER_BLOB_BLOB_STORAGE_CONTEXT_H_
6 #define STORAGE_BROWSER_BLOB_BLOB_STORAGE_CONTEXT_H_
12 #include "base/memory/ref_counted.h"
13 #include "base/memory/weak_ptr.h"
14 #include "storage/browser/blob/blob_data_handle.h"
15 #include "storage/browser/blob/blob_data_item.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"
19 #include "storage/browser/storage_browser_export.h"
20 #include "storage/common/data_element.h"
30 class BlobStorageHost
;
35 class BlobDataBuilder
;
36 class InternalBlobData
;
38 // This class handles the logistics of blob Storage within the browser process,
39 // and maintains a mapping from blob uuid to the data. The class is single
40 // threaded and should only be used on the IO thread.
41 // In chromium, there is one instance per profile.
42 class STORAGE_EXPORT BlobStorageContext
43 : public base::SupportsWeakPtr
<BlobStorageContext
> {
46 ~BlobStorageContext();
48 scoped_ptr
<BlobDataHandle
> GetBlobDataFromUUID(const std::string
& uuid
);
49 scoped_ptr
<BlobDataHandle
> GetBlobDataFromPublicURL(const GURL
& url
);
51 // Useful for coining blobs from within the browser process. If the
52 // blob cannot be added due to memory consumption, returns NULL.
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
);
60 // Useful for coining blob urls from within the browser process.
61 bool RegisterPublicBlobURL(const GURL
& url
, const std::string
& uuid
);
62 void RevokePublicBlobURL(const GURL
& url
);
64 size_t memory_usage() const { return memory_usage_
; }
65 size_t blob_count() const { return blob_map_
.size(); }
68 friend class content::BlobStorageHost
;
69 friend class BlobDataHandle::BlobDataHandleShared
;
70 friend class ViewBlobInternalsJob
;
73 EXCEEDED_MEMORY
= 1 << 1,
79 // data and data_builder are mutually exclusive.
80 scoped_ptr
<InternalBlobData
> data
;
81 scoped_ptr
<InternalBlobData::Builder
> data_builder
;
84 BlobMapEntry(int refcount
, InternalBlobData::Builder
* data
);
90 typedef std::map
<std::string
, BlobMapEntry
*> BlobMap
;
91 typedef std::map
<GURL
, std::string
> BlobURLMap
;
93 // Called by BlobDataHandle.
94 scoped_ptr
<BlobDataSnapshot
> CreateSnapshot(const std::string
& uuid
);
96 // ### Methods called by BlobStorageHost ###
97 void StartBuildingBlob(const std::string
& uuid
);
98 void AppendBlobDataItem(const std::string
& uuid
,
99 const DataElement
& data_item
);
100 void FinishBuildingBlob(const std::string
& uuid
, const std::string
& type
);
101 void CancelBuildingBlob(const std::string
& uuid
);
102 void IncrementBlobRefCount(const std::string
& uuid
);
103 void DecrementBlobRefCount(const std::string
& uuid
);
104 // #########################################
106 // Flags the entry for exceeding memory, and resets the builder.
107 void BlobEntryExceededMemory(BlobMapEntry
* entry
);
109 // Allocates memory to hold the given data element and copies the data over.
110 scoped_refptr
<BlobDataItem
> AllocateBlobItem(const std::string
& uuid
,
111 const DataElement
& data_item
);
113 // Appends the given blob item to the blob builder. The new blob
114 // retains ownership of data_item if applicable, and returns false if there
115 // wasn't enough memory to hold the item.
116 bool AppendAllocatedBlobItem(const std::string
& target_blob_uuid
,
117 scoped_refptr
<BlobDataItem
> data_item
,
118 InternalBlobData::Builder
* target_blob_data
);
120 // Deconstructs the blob and appends it's contents to the target blob. Items
121 // are shared if possible, and copied if the given offset and length
122 // have to split an item.
123 bool AppendBlob(const std::string
& target_blob_uuid
,
124 const InternalBlobData
& blob
,
127 InternalBlobData::Builder
* target_blob_data
);
129 bool IsInUse(const std::string
& uuid
);
130 bool IsBeingBuilt(const std::string
& uuid
);
131 bool IsUrlRegistered(const GURL
& blob_url
);
134 BlobURLMap public_blob_urls_
;
136 // Used to keep track of how much memory is being utilized for blob data,
137 // we count only the items of TYPE_DATA which are held in memory and not
138 // items of TYPE_FILE.
139 size_t memory_usage_
;
141 DISALLOW_COPY_AND_ASSIGN(BlobStorageContext
);
144 } // namespace storage
146 #endif // STORAGE_BROWSER_BLOB_BLOB_STORAGE_CONTEXT_H_