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 |
| 13 #include "base/gtest_prod_util.h" |
11 #include "base/hash_tables.h" | 14 #include "base/hash_tables.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 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 // A false return indicates a problem with the inputs |
| 73 // like a non-existent or pre-existent uuid or url. |
| 74 bool StartBuildingBlob(const std::string& uuid); |
| 75 bool AppendBlobDataItem(const std::string& uuid, |
| 76 const BlobData::Item& data_item); |
| 77 bool CancelBuildingBlob(const std::string& uuid); |
| 78 bool FinishBuildingBlob(const std::string& uuid, const std::string& type); |
| 79 bool IncrementBlobRefCount(const std::string& uuid); |
| 80 bool DecrementBlobRefCount(const std::string& uuid); |
| 81 bool RegisterPublicBlobURL(const GURL& blob_url, const std::string& uuid); |
| 82 bool RevokePublicBlobURL(const GURL& blob_url); |
| 83 |
| 84 private: |
| 85 typedef std::map<std::string, int> BlobReferenceMap; |
| 86 |
| 87 bool IsInUse(const std::string& uuid); |
| 88 bool HasUseCountOfOne(const std::string& uuid); |
| 89 bool IsUrlRegistered(const GURL& blob_url); |
| 90 |
| 91 // Collection of blob ids and a count of how many usages |
| 92 // of that id are attributable to this consumer. |
| 93 BlobReferenceMap blobs_inuse_map_; |
| 94 |
| 95 // The set of public blob urls coined by this consumer. |
| 96 std::set<GURL> public_blob_urls_; |
| 97 |
| 98 base::WeakPtr<BlobStorageContext> context_; |
| 99 }; |
| 100 |
| 101 // This class handles the logistics of blob Storage within the browser process, |
| 102 // and maintains a mapping from blob uuid to the data. The class is single |
| 103 // threaded and should only be used on the IO thread. |
| 104 // In chromium, there is one instance per profile. |
| 105 class WEBKIT_STORAGE_EXPORT BlobStorageContext |
| 106 : public base::SupportsWeakPtr<BlobStorageContext> { |
| 107 public: |
| 108 BlobStorageContext(); |
| 109 ~BlobStorageContext(); |
| 110 |
| 111 scoped_ptr<BlobDataHandle> GetBlobDataFromUUID(const std::string& uuid); |
| 112 scoped_ptr<BlobDataHandle> GetBlobDataFromPublicURL(const GURL& url); |
| 113 |
| 114 // Useful for coining blobs from within the browser process. |
| 115 scoped_ptr<BlobDataHandle> AddFinishedBlob(const BlobData* blob_data); |
| 116 |
| 117 private: |
| 118 friend class BlobDataHandle; |
| 119 friend class BlobStorageHost; |
| 120 |
| 121 typedef std::map<std::string, std::pair<int, scoped_refptr<BlobData> > > |
| 122 BlobMap; |
| 123 typedef std::map<GURL, std::string> BlobURLMap; |
| 124 |
| 125 void StartBuildingBlob(const std::string& uuid); |
| 126 void AppendBlobDataItem(const std::string& uuid, |
| 127 const BlobData::Item& data_item); |
| 128 void FinishBuildingBlob(const std::string& uuid, const std::string& type); |
| 129 void CancelBuildingBlob(const std::string& uuid); |
| 130 void IncrementBlobRefCount(const std::string& uuid); |
| 131 void DecrementBlobRefCount(const std::string& uuid); |
| 132 void RegisterPublicBlobURL(const GURL& url, const std::string& uuid); |
| 133 void RevokePublicBlobURL(const GURL& url); |
45 | 134 |
46 void AppendStorageItems(BlobData* target_blob_data, | 135 void AppendStorageItems(BlobData* target_blob_data, |
47 BlobData* src_blob_data, | 136 BlobData* src_blob_data, |
48 uint64 offset, | 137 uint64 offset, |
49 uint64 length); | 138 uint64 length); |
50 void AppendFileItem(BlobData* target_blob_data, | 139 void AppendFileItem(BlobData* target_blob_data, |
51 const base::FilePath& file_path, uint64 offset, | 140 const base::FilePath& file_path, |
52 uint64 length, | 141 uint64 offset, uint64 length, |
53 const base::Time& expected_modification_time); | 142 const base::Time& expected_modification_time); |
54 void AppendFileSystemFileItem( | 143 void AppendFileSystemFileItem( |
55 BlobData* target_blob_data, | 144 BlobData* target_blob_data, |
56 const GURL& url, uint64 offset, uint64 length, | 145 const GURL& url, uint64 offset, uint64 length, |
57 const base::Time& expected_modification_time); | 146 const base::Time& expected_modification_time); |
58 | 147 |
59 bool RemoveFromMapHelper(BlobMap* map, const GURL& url); | 148 bool IsInUse(const std::string& uuid); |
60 | 149 bool HasUseCountOfOne(const std::string& uuid); |
61 void IncrementBlobDataUsage(BlobData* blob_data); | 150 bool IsUrlRegistered(const GURL& blob_url); |
62 // Returns true if no longer in use. | |
63 bool DecrementBlobDataUsage(BlobData* blob_data); | |
64 | 151 |
65 BlobMap blob_map_; | 152 BlobMap blob_map_; |
66 BlobMap unfinalized_blob_map_; | 153 BlobURLMap public_blob_urls_; |
67 | 154 |
68 // Used to keep track of how much memory is being utitlized for blob data, | 155 // 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 | 156 // we count only the items of TYPE_DATA which are held in memory and not |
70 // items of TYPE_FILE. | 157 // items of TYPE_FILE. |
71 int64 memory_usage_; | 158 int64 memory_usage_; |
72 | 159 |
73 // Multiple urls can refer to the same blob data, this map keeps track of | 160 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 }; | 161 }; |
79 | 162 |
80 } // namespace webkit_blob | 163 } // namespace webkit_blob |
81 | 164 |
82 #endif // WEBKIT_BLOB_BLOB_STORAGE_CONTROLLER_H_ | 165 #endif // WEBKIT_BLOB_BLOB_STORAGE_CONTEXT_H_ |
OLD | NEW |