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_
11 #include "base/memory/ref_counted.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/memory/weak_ptr.h"
14 #include "storage/browser/blob/blob_data_handle.h"
15 #include "storage/browser/storage_browser_export.h"
16 #include "storage/common/blob/blob_data.h"
26 class BlobStorageHost;
33 // 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
35 // threaded and should only be used on the IO thread.
36 // In chromium, there is one instance per profile.
37 class STORAGE_EXPORT BlobStorageContext
38 : public base::SupportsWeakPtr<BlobStorageContext> {
41 ~BlobStorageContext();
43 scoped_ptr<BlobDataHandle> GetBlobDataFromUUID(const std::string& uuid);
44 scoped_ptr<BlobDataHandle> GetBlobDataFromPublicURL(const GURL& url);
46 // Useful for coining blobs from within the browser process. If the
47 // blob cannot be added due to memory consumption, returns NULL.
48 scoped_ptr<BlobDataHandle> AddFinishedBlob(const BlobData* blob_data);
50 // Useful for coining blob urls from within the browser process.
51 bool RegisterPublicBlobURL(const GURL& url, const std::string& uuid);
52 void RevokePublicBlobURL(const GURL& url);
55 friend class content::BlobStorageHost;
56 friend class BlobDataHandle::BlobDataHandleShared;
57 friend class ViewBlobInternalsJob;
61 EXCEEDED_MEMORY = 1 << 1,
67 scoped_refptr<BlobData> data;
70 BlobMapEntry(int refcount, int flags, BlobData* data);
74 typedef std::map<std::string, BlobMapEntry>
76 typedef std::map<GURL, std::string> BlobURLMap;
78 void StartBuildingBlob(const std::string& uuid);
79 void AppendBlobDataItem(const std::string& uuid,
80 const BlobData::Item& data_item);
81 void FinishBuildingBlob(const std::string& uuid, const std::string& type);
82 void CancelBuildingBlob(const std::string& uuid);
83 void IncrementBlobRefCount(const std::string& uuid);
84 void DecrementBlobRefCount(const std::string& uuid);
86 bool ExpandStorageItems(BlobData* target_blob_data,
87 BlobData* src_blob_data,
90 bool AppendBytesItem(BlobData* target_blob_data,
91 const char* data, int64 length);
92 void AppendFileItem(BlobData* target_blob_data,
93 const base::FilePath& file_path,
94 uint64 offset, uint64 length,
95 const base::Time& expected_modification_time);
96 void AppendFileSystemFileItem(
97 BlobData* target_blob_data,
98 const GURL& url, uint64 offset, uint64 length,
99 const base::Time& expected_modification_time);
101 bool IsInUse(const std::string& uuid);
102 bool IsBeingBuilt(const std::string& uuid);
103 bool IsUrlRegistered(const GURL& blob_url);
106 BlobURLMap public_blob_urls_;
108 // 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
110 // items of TYPE_FILE.
113 DISALLOW_COPY_AND_ASSIGN(BlobStorageContext);
116 } // namespace storage
118 #endif // STORAGE_BROWSER_BLOB_BLOB_STORAGE_CONTEXT_H_