| Index: storage/browser/blob/blob_memory_controller.h
|
| diff --git a/storage/browser/blob/blob_memory_controller.h b/storage/browser/blob/blob_memory_controller.h
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..a94a79ec78d6f69f2d4e7acb5c9557bbb1e35526
|
| --- /dev/null
|
| +++ b/storage/browser/blob/blob_memory_controller.h
|
| @@ -0,0 +1,186 @@
|
| +// Copyright 2015 The Chromium Authors. All rihts reserved.
|
| +// Use of this source code is governed by a BSD-style license that can be
|
| +// found in the LICENSE file.
|
| +
|
| +#ifndef STORAGE_BROWSER_BLOB_BLOB_MEMORY_CONTROLLER_H_
|
| +#define STORAGE_BROWSER_BLOB_BLOB_MEMORY_CONTROLLER_H_
|
| +
|
| +#include <stdint.h>
|
| +#include <algorithm>
|
| +#include <list>
|
| +#include <map>
|
| +#include <string>
|
| +#include <vector>
|
| +
|
| +#include "base/callback_forward.h"
|
| +#include "base/containers/mru_cache.h"
|
| +#include "base/files/file.h"
|
| +#include "base/files/file_path.h"
|
| +#include "base/macros.h"
|
| +#include "base/memory/ref_counted.h"
|
| +#include "base/memory/weak_ptr.h"
|
| +#include "base/time/time.h"
|
| +#include "storage/common/blob_storage/blob_storage_constants.h"
|
| +#include "storage/common/data_element.h"
|
| +
|
| +namespace base {
|
| +class TaskRunner;
|
| +}
|
| +
|
| +namespace storage {
|
| +
|
| +class ShareableBlobDataItem;
|
| +class ShareableFileReference;
|
| +
|
| +// There should be no memory sizes of 0.
|
| +// This class is responsible for
|
| +// * our current memory usage for blobs, both on disk and in memory,
|
| +// * when we can add a blob w/ a given size,
|
| +// * creating temporary files,
|
| +// * paging blobs to disk, and
|
| +// * refcounting blob page files.
|
| +class BlobMemoryController
|
| + : public base::SupportsWeakPtr<BlobMemoryController> {
|
| + public:
|
| + enum class BlobTrasportationMemoryStrategyResult {
|
| + // We don't have enough memory for this blob.
|
| + TOO_LARGE,
|
| + // There isn't any memory that needs transporting.
|
| + NONE_NEEDED,
|
| + // Sh transportation strategies.
|
| + SHORTCUT,
|
| + IPC,
|
| + SHARED_MEMORY,
|
| + FILE
|
| + };
|
| + struct FileCreationInfo {
|
| + FileCreationInfo();
|
| + ~FileCreationInfo();
|
| +
|
| + base::File::Error error = base::File::FILE_ERROR_FAILED;
|
| + scoped_refptr<ShareableFileReference> file_reference;
|
| + base::Time last_modified;
|
| + };
|
| +
|
| + // Using this constructor means that the disk is disabled.
|
| + BlobMemoryController();
|
| + BlobMemoryController(bool enable_disk,
|
| + const base::FilePath& blob_storage_dir,
|
| + scoped_refptr<base::TaskRunner> file_runner);
|
| + virtual ~BlobMemoryController();
|
| +
|
| + void SetMemoryLimits(size_t max_blob_in_memory_size_bytes,
|
| + size_t max_blob_memory_space_bytes,
|
| + uint64_t max_blob_disk_space_bytes,
|
| + size_t in_flight_space_bytes,
|
| + uint64_t min_page_file_size) {
|
| + max_blob_in_memory_size_ = max_blob_in_memory_size_bytes;
|
| + max_blob_memory_space_ = max_blob_memory_space_bytes;
|
| + max_blob_disk_space_ = max_blob_disk_space_bytes;
|
| + in_flight_space_ = in_flight_space_bytes;
|
| + min_page_file_size_ = min_page_file_size;
|
| + }
|
| +
|
| + // Returns if the data in the descriptions is good data (not a bad IPC).
|
| + bool DecideBlobTransportationMemoryStrategy(
|
| + const std::vector<DataElement>& descriptions,
|
| + uint64_t* total_bytes,
|
| + BlobTrasportationMemoryStrategyResult* result);
|
| +
|
| + // Creates a temporary file that we store in our blob temporary directory.
|
| + // This temp file is not registered with the ref count system, and the client
|
| + // must call IncreaseTempFileRefCount to have the file correctly deleted when
|
| + // unused.
|
| + void CreateTemporaryFileForRenderer(
|
| + uint64_t size_bytes,
|
| + base::Callback<void(const FileCreationInfo&)> done);
|
| +
|
| + // Notifies the caller when a blob of the given size can be stored in our blob
|
| + // system. This assumes the caller has called CanNewBlobFit, and does not
|
| + // check whether the size will fit.
|
| + // The argument on can_request is true if we can create the blob, and
|
| + // false if there was an error and the new blob needs to be broken.
|
| + void NotifyWhenBlobCanBeRequested(uint64_t memory_size_bytes,
|
| + base::Callback<void(bool)> can_request);
|
| +
|
| + void UpdateBlobItemInRecents(ShareableBlobDataItem* item);
|
| + void RemoveBlobItemInRecents(uint64_t id);
|
| +
|
| + // Disables the disk.
|
| + void DisableDisk();
|
| +
|
| + bool disk_enabled() const { return disk_used_; }
|
| +
|
| + private:
|
| + // Returns if we didn't overflow our total size.
|
| + static bool CalculateBlobMemorySize(
|
| + const std::vector<DataElement>& descriptions,
|
| + size_t* shortcut_bytes,
|
| + uint64_t* total_bytes);
|
| +
|
| + // Creates a file in the given directory w/ the given filename and size.
|
| + static FileCreationInfo CreateFile(const base::FilePath& directory_path,
|
| + const std::string& file_name,
|
| + size_t size_bytes);
|
| +
|
| + static FileCreationInfo WriteItemsToFile(
|
| + std::vector<scoped_refptr<ShareableBlobDataItem>>* items,
|
| + size_t total_size_bytes,
|
| + const base::FilePath& directory_path,
|
| + const std::string& file_name);
|
| +
|
| + // We add a delete callback to the file reference to decrement our disk usage.
|
| + void OnCreateFile(uint64_t file_size,
|
| + base::Callback<void(const FileCreationInfo&)> done,
|
| + FileCreationInfo result);
|
| +
|
| + // Returns the size we'll be freeing.
|
| + size_t ScheduleBlobPaging();
|
| + void SchedulePagingUntilWeCanFit(size_t total_memory_needed);
|
| +
|
| + // Called when we've completed paging a list of items
|
| + void OnPagingComplete(
|
| + scoped_ptr<std::vector<scoped_refptr<ShareableBlobDataItem>>> items,
|
| + size_t total_items_size,
|
| + FileCreationInfo result);
|
| +
|
| + void RecordTracingCounters();
|
| + size_t GetAvailableMemoryForBlobs();
|
| + uint64_t GetAvailableDiskSpaceForBlobs();
|
| +
|
| + // We decrement the disk space used.
|
| + void OnBlobFileDelete(uint64_t size, const base::FilePath& path);
|
| +
|
| + using RecentItemsCache = base::MRUCache<uint64_t, ShareableBlobDataItem*>;
|
| +
|
| + size_t blob_memory_used_ = 0;
|
| + size_t in_flight_memory_used_ = 0;
|
| + uint64_t disk_used_ = 0;
|
| +
|
| + std::map<base::FilePath, size_t> page_file_references_;
|
| +
|
| + scoped_refptr<base::TaskRunner> file_runner_;
|
| + std::list<std::pair<uint64_t, base::Callback<void(bool)>>>
|
| + blobs_waiting_for_paging_;
|
| + size_t blobs_waiting_for_paging_size_ = 0;
|
| +
|
| + size_t max_ipc_memory_size_ = kBlobStorageIPCThresholdBytes;
|
| + size_t max_blob_in_memory_size_ = kBlobStorageMaxBlobMemorySize;
|
| + size_t max_blob_memory_space_ = kBlobStorageMaxMemoryUsage;
|
| + uint64_t max_blob_disk_space_ = kBlobStorageMaxDiskSpace;
|
| + size_t in_flight_space_ = kBlobStorageInFlightMemory;
|
| + uint64_t min_page_file_size_ = kBlobStorageMinFileSizeBytes;
|
| + bool enable_disk_ = false;
|
| +
|
| + base::FilePath blob_storage_dir_;
|
| + uint64_t current_file_num_ = 0;
|
| +
|
| + // Lifetime of the ShareableBlobDataItem objects is handles externally in the
|
| + // BlobStorageContext class.
|
| + RecentItemsCache recent_item_cache_;
|
| +
|
| + DISALLOW_COPY_AND_ASSIGN(BlobMemoryController);
|
| +};
|
| +
|
| +} // namespace storage
|
| +#endif // STORAGE_BROWSER_BLOB_BLOB_MEMORY_CONTROLLER_H_
|
|
|