| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rihts reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef STORAGE_BROWSER_BLOB_BLOB_MEMORY_CONTROLLER_H_ |
| 6 #define STORAGE_BROWSER_BLOB_BLOB_MEMORY_CONTROLLER_H_ |
| 7 |
| 8 #include <stdint.h> |
| 9 #include <algorithm> |
| 10 #include <list> |
| 11 #include <map> |
| 12 #include <string> |
| 13 #include <vector> |
| 14 |
| 15 #include "base/callback_forward.h" |
| 16 #include "base/containers/mru_cache.h" |
| 17 #include "base/files/file.h" |
| 18 #include "base/files/file_path.h" |
| 19 #include "base/macros.h" |
| 20 #include "base/memory/ref_counted.h" |
| 21 #include "base/memory/weak_ptr.h" |
| 22 #include "base/time/time.h" |
| 23 #include "storage/common/blob_storage/blob_storage_constants.h" |
| 24 #include "storage/common/data_element.h" |
| 25 |
| 26 namespace base { |
| 27 class TaskRunner; |
| 28 } |
| 29 |
| 30 namespace storage { |
| 31 |
| 32 class ShareableBlobDataItem; |
| 33 class ShareableFileReference; |
| 34 |
| 35 // There should be no memory sizes of 0. |
| 36 // This class is responsible for |
| 37 // * our current memory usage for blobs, both on disk and in memory, |
| 38 // * when we can add a blob w/ a given size, |
| 39 // * creating temporary files, |
| 40 // * paging blobs to disk, and |
| 41 // * refcounting blob page files. |
| 42 class BlobMemoryController |
| 43 : public base::SupportsWeakPtr<BlobMemoryController> { |
| 44 public: |
| 45 enum class BlobTrasportationMemoryStrategyResult { |
| 46 // We don't have enough memory for this blob. |
| 47 TOO_LARGE, |
| 48 // There isn't any memory that needs transporting. |
| 49 NONE_NEEDED, |
| 50 // Sh transportation strategies. |
| 51 SHORTCUT, |
| 52 IPC, |
| 53 SHARED_MEMORY, |
| 54 FILE |
| 55 }; |
| 56 struct FileCreationInfo { |
| 57 FileCreationInfo(); |
| 58 ~FileCreationInfo(); |
| 59 |
| 60 base::File::Error error = base::File::FILE_ERROR_FAILED; |
| 61 scoped_refptr<ShareableFileReference> file_reference; |
| 62 base::Time last_modified; |
| 63 }; |
| 64 |
| 65 // Using this constructor means that the disk is disabled. |
| 66 BlobMemoryController(); |
| 67 BlobMemoryController(bool enable_disk, |
| 68 const base::FilePath& blob_storage_dir, |
| 69 scoped_refptr<base::TaskRunner> file_runner); |
| 70 virtual ~BlobMemoryController(); |
| 71 |
| 72 void SetMemoryLimits(size_t max_blob_in_memory_size_bytes, |
| 73 size_t max_blob_memory_space_bytes, |
| 74 uint64_t max_blob_disk_space_bytes, |
| 75 size_t in_flight_space_bytes, |
| 76 uint64_t min_page_file_size) { |
| 77 max_blob_in_memory_size_ = max_blob_in_memory_size_bytes; |
| 78 max_blob_memory_space_ = max_blob_memory_space_bytes; |
| 79 max_blob_disk_space_ = max_blob_disk_space_bytes; |
| 80 in_flight_space_ = in_flight_space_bytes; |
| 81 min_page_file_size_ = min_page_file_size; |
| 82 } |
| 83 |
| 84 // Returns if the data in the descriptions is good data (not a bad IPC). |
| 85 bool DecideBlobTransportationMemoryStrategy( |
| 86 const std::vector<DataElement>& descriptions, |
| 87 uint64_t* total_bytes, |
| 88 BlobTrasportationMemoryStrategyResult* result); |
| 89 |
| 90 // Creates a temporary file that we store in our blob temporary directory. |
| 91 // This temp file is not registered with the ref count system, and the client |
| 92 // must call IncreaseTempFileRefCount to have the file correctly deleted when |
| 93 // unused. |
| 94 void CreateTemporaryFileForRenderer( |
| 95 uint64_t size_bytes, |
| 96 base::Callback<void(const FileCreationInfo&)> done); |
| 97 |
| 98 // Notifies the caller when a blob of the given size can be stored in our blob |
| 99 // system. This assumes the caller has called CanNewBlobFit, and does not |
| 100 // check whether the size will fit. |
| 101 // The argument on can_request is true if we can create the blob, and |
| 102 // false if there was an error and the new blob needs to be broken. |
| 103 void NotifyWhenBlobCanBeRequested(uint64_t memory_size_bytes, |
| 104 base::Callback<void(bool)> can_request); |
| 105 |
| 106 void UpdateBlobItemInRecents(ShareableBlobDataItem* item); |
| 107 void RemoveBlobItemInRecents(uint64_t id); |
| 108 |
| 109 // Disables the disk. |
| 110 void DisableDisk(); |
| 111 |
| 112 bool disk_enabled() const { return disk_used_; } |
| 113 |
| 114 private: |
| 115 // Returns if we didn't overflow our total size. |
| 116 static bool CalculateBlobMemorySize( |
| 117 const std::vector<DataElement>& descriptions, |
| 118 size_t* shortcut_bytes, |
| 119 uint64_t* total_bytes); |
| 120 |
| 121 // Creates a file in the given directory w/ the given filename and size. |
| 122 static FileCreationInfo CreateFile(const base::FilePath& directory_path, |
| 123 const std::string& file_name, |
| 124 size_t size_bytes); |
| 125 |
| 126 static FileCreationInfo WriteItemsToFile( |
| 127 std::vector<scoped_refptr<ShareableBlobDataItem>>* items, |
| 128 size_t total_size_bytes, |
| 129 const base::FilePath& directory_path, |
| 130 const std::string& file_name); |
| 131 |
| 132 // We add a delete callback to the file reference to decrement our disk usage. |
| 133 void OnCreateFile(uint64_t file_size, |
| 134 base::Callback<void(const FileCreationInfo&)> done, |
| 135 FileCreationInfo result); |
| 136 |
| 137 // Returns the size we'll be freeing. |
| 138 size_t ScheduleBlobPaging(); |
| 139 void SchedulePagingUntilWeCanFit(size_t total_memory_needed); |
| 140 |
| 141 // Called when we've completed paging a list of items |
| 142 void OnPagingComplete( |
| 143 scoped_ptr<std::vector<scoped_refptr<ShareableBlobDataItem>>> items, |
| 144 size_t total_items_size, |
| 145 FileCreationInfo result); |
| 146 |
| 147 void RecordTracingCounters(); |
| 148 size_t GetAvailableMemoryForBlobs(); |
| 149 uint64_t GetAvailableDiskSpaceForBlobs(); |
| 150 |
| 151 // We decrement the disk space used. |
| 152 void OnBlobFileDelete(uint64_t size, const base::FilePath& path); |
| 153 |
| 154 using RecentItemsCache = base::MRUCache<uint64_t, ShareableBlobDataItem*>; |
| 155 |
| 156 size_t blob_memory_used_ = 0; |
| 157 size_t in_flight_memory_used_ = 0; |
| 158 uint64_t disk_used_ = 0; |
| 159 |
| 160 std::map<base::FilePath, size_t> page_file_references_; |
| 161 |
| 162 scoped_refptr<base::TaskRunner> file_runner_; |
| 163 std::list<std::pair<uint64_t, base::Callback<void(bool)>>> |
| 164 blobs_waiting_for_paging_; |
| 165 size_t blobs_waiting_for_paging_size_ = 0; |
| 166 |
| 167 size_t max_ipc_memory_size_ = kBlobStorageIPCThresholdBytes; |
| 168 size_t max_blob_in_memory_size_ = kBlobStorageMaxBlobMemorySize; |
| 169 size_t max_blob_memory_space_ = kBlobStorageMaxMemoryUsage; |
| 170 uint64_t max_blob_disk_space_ = kBlobStorageMaxDiskSpace; |
| 171 size_t in_flight_space_ = kBlobStorageInFlightMemory; |
| 172 uint64_t min_page_file_size_ = kBlobStorageMinFileSizeBytes; |
| 173 bool enable_disk_ = false; |
| 174 |
| 175 base::FilePath blob_storage_dir_; |
| 176 uint64_t current_file_num_ = 0; |
| 177 |
| 178 // Lifetime of the ShareableBlobDataItem objects is handles externally in the |
| 179 // BlobStorageContext class. |
| 180 RecentItemsCache recent_item_cache_; |
| 181 |
| 182 DISALLOW_COPY_AND_ASSIGN(BlobMemoryController); |
| 183 }; |
| 184 |
| 185 } // namespace storage |
| 186 #endif // STORAGE_BROWSER_BLOB_BLOB_MEMORY_CONTROLLER_H_ |
| OLD | NEW |