OLD | NEW |
1 // Copyright (c) 2011 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 WEBKIT_BLOB_BLOB_STORAGE_CONTROLLER_H_ | 5 #ifndef WEBKIT_BLOB_BLOB_STORAGE_CONTEXT_H_ |
6 #define WEBKIT_BLOB_BLOB_STORAGE_CONTROLLER_H_ | 6 #define WEBKIT_BLOB_BLOB_STORAGE_CONTEXT_H_ |
7 | 7 |
8 #include <map> | 8 #include <map> |
| 9 #include <set> |
9 #include <string> | 10 #include <string> |
| 11 #include <utility> |
10 | 12 |
11 #include "base/hash_tables.h" | 13 #include "base/hash_tables.h" |
| 14 #include "base/gtest_prod_util.h" |
12 #include "base/memory/ref_counted.h" | 15 #include "base/memory/ref_counted.h" |
| 16 #include "base/memory/weak_ptr.h" |
13 #include "base/process.h" | 17 #include "base/process.h" |
| 18 #include "base/shared_memory.h" |
| 19 #include "base/supports_user_data.h" |
14 #include "webkit/blob/blob_data.h" | 20 #include "webkit/blob/blob_data.h" |
15 #include "webkit/storage/webkit_storage_export.h" | 21 #include "webkit/storage/webkit_storage_export.h" |
16 | 22 |
17 class GURL; | 23 class GURL; |
18 | 24 |
19 namespace base { | 25 namespace base { |
20 class FilePath; | 26 class FilePath; |
| 27 class SequencedTaskRunner; |
21 class Time; | 28 class Time; |
22 } | 29 } |
23 | 30 |
24 namespace webkit_blob { | 31 namespace webkit_blob { |
25 | 32 |
26 // This class handles the logistics of blob Storage within the browser process. | 33 class BlobDataHandle; |
27 class WEBKIT_STORAGE_EXPORT BlobStorageController { | 34 class BlobStorageHost; |
| 35 class BlobStorageContext; |
| 36 |
| 37 // A scoper object for use in chrome's main browser process, ensures |
| 38 // the underlying BlobData and its uuid remain in BlobStorageContext's |
| 39 // collection for the duration. This object has delete semantics and |
| 40 // maybe deleted on any thread. |
| 41 class WEBKIT_STORAGE_EXPORT BlobDataHandle |
| 42 : public base::SupportsUserData::Data { |
28 public: | 43 public: |
29 BlobStorageController(); | 44 virtual ~BlobDataHandle(); // Maybe be deleted on any thread. |
30 ~BlobStorageController(); | 45 BlobData* data() const; // May only be accessed on the IO thread. |
31 | |
32 void StartBuildingBlob(const GURL& url); | |
33 void AppendBlobDataItem(const GURL& url, const BlobData::Item& data_item); | |
34 void FinishBuildingBlob(const GURL& url, const std::string& content_type); | |
35 void AddFinishedBlob(const GURL& url, const BlobData* blob_data); | |
36 void CloneBlob(const GURL& url, const GURL& src_url); | |
37 void RemoveBlob(const GURL& url); | |
38 BlobData* GetBlobDataFromUrl(const GURL& url); | |
39 | 46 |
40 private: | 47 private: |
41 friend class ViewBlobInternalsJob; | 48 friend class BlobStorageContext; |
| 49 BlobDataHandle(BlobData* blob_data, BlobStorageContext* context, |
| 50 base::SequencedTaskRunner* task_runner); |
42 | 51 |
43 typedef base::hash_map<std::string, scoped_refptr<BlobData> > BlobMap; | 52 static void DeleteHelper( |
44 typedef std::map<BlobData*, int> BlobDataUsageMap; | 53 scoped_ptr<base::WeakPtr<BlobStorageContext> > context, |
| 54 scoped_refptr<BlobData> blob_data); |
| 55 |
| 56 scoped_refptr<BlobData> blob_data_; |
| 57 scoped_ptr<base::WeakPtr<BlobStorageContext> > context_; |
| 58 scoped_refptr<base::SequencedTaskRunner> io_task_runner_; |
| 59 }; |
| 60 |
| 61 // This class handles the logistics of blob storage for a single child process. |
| 62 // There is generally one instance per child process. When the child process |
| 63 // terminates all blob references attibutable to that process go away upon |
| 64 // destruction of the instance. The class is single threaded and should |
| 65 // only be used on the IO thread. |
| 66 class WEBKIT_STORAGE_EXPORT BlobStorageHost { |
| 67 public: |
| 68 explicit BlobStorageHost(BlobStorageContext* context); |
| 69 ~BlobStorageHost(); |
| 70 |
| 71 // Methods to support the IPC message protocol. |
| 72 void StartBuildingBlob(const std::string& uuid); |
| 73 void AppendBlobDataItem(const std::string& uuid, |
| 74 const BlobData::Item& data_item); |
| 75 void CancelBuildingBlob(const std::string& uuid); |
| 76 void FinishBuildingBlob(const std::string& uuid, const std::string& type); |
| 77 void AddFinishedBlob(const BlobData* blob_data); |
| 78 void IncrementBlobRefCount(const std::string& uuid); |
| 79 void DecrementBlobRefCount(const std::string& uuid); |
| 80 void RegisterPublicBlobURL(const GURL& blob_url, const std::string& uuid); |
| 81 void RevokePublicBlobURL(const GURL& blob_url); |
| 82 |
| 83 private: |
| 84 typedef std::map<std::string, int> BlobReferenceMap; |
| 85 |
| 86 // Collection of blob ids and a count of how many usages |
| 87 // of that id are attributable to this consumer. |
| 88 BlobReferenceMap blobs_inuse_map_; |
| 89 |
| 90 // The set of public blob urls coined by this consumer. |
| 91 std::set<GURL> public_blob_urls_; |
| 92 |
| 93 base::WeakPtr<BlobStorageContext> context_; |
| 94 }; |
| 95 |
| 96 // This class handles the logistics of blob Storage within the browser process, |
| 97 // and maintains a mapping from blob uuid to the data. The class is single |
| 98 // threaded and should only be used on the IO thread. |
| 99 // In chromium, there is one instance per profile. |
| 100 class WEBKIT_STORAGE_EXPORT BlobStorageContext |
| 101 : public base::SupportsWeakPtr<BlobStorageContext> { |
| 102 public: |
| 103 BlobStorageContext(); |
| 104 ~BlobStorageContext(); |
| 105 |
| 106 scoped_ptr<BlobDataHandle> GetBlobDataFromUUID(const std::string& uuid); |
| 107 scoped_ptr<BlobDataHandle> GetBlobDataFromPublicURL(const GURL& url); |
| 108 |
| 109 private: |
| 110 friend class BlobDataHandle; |
| 111 friend class BlobStorageHost; |
| 112 |
| 113 typedef std::map<std::string, std::pair<int, scoped_refptr<BlobData> > > |
| 114 BlobMap; |
| 115 typedef std::map<GURL, std::string> BlobURLMap; |
| 116 |
| 117 void StartBuildingBlob(const std::string& uuid); |
| 118 void AppendBlobDataItem(const std::string& uuid, |
| 119 const BlobData::Item& data_item); |
| 120 void FinishBuildingBlob(const std::string& uuid, const std::string& type); |
| 121 void CancelBuildingBlob(const std::string& uuid); |
| 122 void AddFinishedBlob(const BlobData* blob_data); |
| 123 void IncrementBlobRefCount(const std::string& uuid); |
| 124 void DecrementBlobRefCount(const std::string& uuid); |
| 125 void RegisterPublicBlobURL(const GURL& url, const std::string& uuid); |
| 126 void RevokePublicBlobURL(const GURL& url); |
45 | 127 |
46 void AppendStorageItems(BlobData* target_blob_data, | 128 void AppendStorageItems(BlobData* target_blob_data, |
47 BlobData* src_blob_data, | 129 BlobData* src_blob_data, |
48 uint64 offset, | 130 uint64 offset, |
49 uint64 length); | 131 uint64 length); |
50 void AppendFileItem(BlobData* target_blob_data, | 132 void AppendFileItem(BlobData* target_blob_data, |
51 const base::FilePath& file_path, uint64 offset, | 133 const base::FilePath& file_path, |
52 uint64 length, | 134 uint64 offset, uint64 length, |
53 const base::Time& expected_modification_time); | 135 const base::Time& expected_modification_time); |
54 void AppendFileSystemFileItem( | 136 void AppendFileSystemFileItem( |
55 BlobData* target_blob_data, | 137 BlobData* target_blob_data, |
56 const GURL& url, uint64 offset, uint64 length, | 138 const GURL& url, uint64 offset, uint64 length, |
57 const base::Time& expected_modification_time); | 139 const base::Time& expected_modification_time); |
58 | 140 |
59 bool RemoveFromMapHelper(BlobMap* map, const GURL& url); | 141 bool DecrementBlobRefCountHelper(BlobMap* map, const std::string& uuid); |
60 | |
61 void IncrementBlobDataUsage(BlobData* blob_data); | |
62 // Returns true if no longer in use. | |
63 bool DecrementBlobDataUsage(BlobData* blob_data); | |
64 | 142 |
65 BlobMap blob_map_; | 143 BlobMap blob_map_; |
66 BlobMap unfinalized_blob_map_; | 144 BlobMap unfinalized_blob_map_; |
| 145 BlobURLMap public_blob_urls_; |
67 | 146 |
68 // Used to keep track of how much memory is being utitlized for blob data, | 147 // Used to keep track of how much memory is being utitlized for blob data, |
69 // we count only the items of TYPE_DATA which are held in memory and not | 148 // we count only the items of TYPE_DATA which are held in memory and not |
70 // items of TYPE_FILE. | 149 // items of TYPE_FILE. |
71 int64 memory_usage_; | 150 int64 memory_usage_; |
72 | 151 |
73 // Multiple urls can refer to the same blob data, this map keeps track of | 152 DISALLOW_COPY_AND_ASSIGN(BlobStorageContext); |
74 // how many urls refer to a BlobData. | |
75 BlobDataUsageMap blob_data_usage_count_; | |
76 | |
77 DISALLOW_COPY_AND_ASSIGN(BlobStorageController); | |
78 }; | 153 }; |
79 | 154 |
80 } // namespace webkit_blob | 155 } // namespace webkit_blob |
81 | 156 |
82 #endif // WEBKIT_BLOB_BLOB_STORAGE_CONTROLLER_H_ | 157 #endif // WEBKIT_BLOB_BLOB_STORAGE_CONTEXT_H_ |
OLD | NEW |