| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2011 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. | |
| 4 | |
| 5 #ifndef CONTENT_RENDERER_PEPPER_QUOTA_FILE_IO_H_ | |
| 6 #define CONTENT_RENDERER_PEPPER_QUOTA_FILE_IO_H_ | |
| 7 | |
| 8 #include <deque> | |
| 9 | |
| 10 #include "base/callback_forward.h" | |
| 11 #include "base/files/file_util_proxy.h" | |
| 12 #include "base/memory/ref_counted.h" | |
| 13 #include "base/memory/scoped_ptr.h" | |
| 14 #include "base/memory/weak_ptr.h" | |
| 15 #include "base/platform_file.h" | |
| 16 #include "content/common/content_export.h" | |
| 17 #include "ppapi/c/pp_file_info.h" | |
| 18 #include "url/gurl.h" | |
| 19 #include "webkit/common/quota/quota_types.h" | |
| 20 | |
| 21 namespace base { | |
| 22 class MessageLoopProxy; | |
| 23 } | |
| 24 | |
| 25 namespace content { | |
| 26 | |
| 27 // This class is created per PPB_FileIO_Impl instance and provides | |
| 28 // write operations for quota-managed files (i.e. files of | |
| 29 // PP_FILESYSTEMTYPE_LOCAL{PERSISTENT,TEMPORARY}). | |
| 30 class QuotaFileIO { | |
| 31 public: | |
| 32 // For quota handlings for FileIO API. This will be owned by QuotaFileIO. | |
| 33 class Delegate { | |
| 34 public: | |
| 35 virtual ~Delegate() {} | |
| 36 typedef base::Callback<void (int64)> AvailableSpaceCallback; | |
| 37 virtual void QueryAvailableSpace( | |
| 38 const GURL& origin, | |
| 39 quota::StorageType type, | |
| 40 const AvailableSpaceCallback& callback) = 0; | |
| 41 virtual void WillUpdateFile(const GURL& file_path) = 0; | |
| 42 virtual void DidUpdateFile(const GURL& file_path, int64_t delta) = 0; | |
| 43 // Returns a MessageLoopProxy instance associated with the message loop of | |
| 44 // the file thread. | |
| 45 virtual scoped_refptr<base::MessageLoopProxy> | |
| 46 GetFileThreadMessageLoopProxy() = 0; | |
| 47 }; | |
| 48 | |
| 49 typedef base::FileUtilProxy::WriteCallback WriteCallback; | |
| 50 typedef base::FileUtilProxy::StatusCallback StatusCallback; | |
| 51 | |
| 52 CONTENT_EXPORT QuotaFileIO(Delegate* delegate, | |
| 53 base::PlatformFile file, | |
| 54 const GURL& path_url, | |
| 55 PP_FileSystemType type); | |
| 56 CONTENT_EXPORT ~QuotaFileIO(); | |
| 57 | |
| 58 // Performs write or setlength operation with quota checks. | |
| 59 // Returns true when the operation is successfully dispatched. | |
| 60 // |bytes_to_write| must be geater than zero. | |
| 61 // |callback| will be dispatched when the operation completes. | |
| 62 // Otherwise it returns false and |callback| will not be dispatched. | |
| 63 // |callback| will not be dispatched either when this instance is | |
| 64 // destroyed before the operation completes. | |
| 65 // SetLength cannot be called while there're any in-flight | |
| 66 // operations. For Write it is guaranteed that |callback| are | |
| 67 // always dispatched in the same order as Write being called. | |
| 68 CONTENT_EXPORT bool Write(int64_t offset, | |
| 69 const char* buffer, | |
| 70 int32_t bytes_to_write, | |
| 71 const WriteCallback& callback); | |
| 72 CONTENT_EXPORT bool SetLength(int64_t length, | |
| 73 const StatusCallback& callback); | |
| 74 | |
| 75 Delegate* delegate() const { return delegate_.get(); } | |
| 76 | |
| 77 private: | |
| 78 class PendingOperationBase; | |
| 79 class WriteOperation; | |
| 80 class SetLengthOperation; | |
| 81 | |
| 82 bool CheckIfExceedsQuota(int64_t new_file_size) const; | |
| 83 void WillUpdate(); | |
| 84 void DidWrite(WriteOperation* op, int64_t written_offset_end); | |
| 85 void DidSetLength(base::PlatformFileError error, int64_t new_file_size); | |
| 86 | |
| 87 bool RegisterOperationForQuotaChecks(PendingOperationBase* op); | |
| 88 void DidQueryInfoForQuota(base::PlatformFileError error_code, | |
| 89 const base::PlatformFileInfo& file_info); | |
| 90 void DidQueryAvailableSpace(int64_t avail_space); | |
| 91 void DidQueryForQuotaCheck(); | |
| 92 | |
| 93 scoped_ptr<Delegate> delegate_; | |
| 94 | |
| 95 // The file information associated to this instance. | |
| 96 base::PlatformFile file_; | |
| 97 GURL file_url_; | |
| 98 quota::StorageType storage_type_; | |
| 99 | |
| 100 // Pending operations that are waiting quota checks and pending | |
| 101 // callbacks that are to be fired after the operation; | |
| 102 // we use two separate queues for them so that we can safely dequeue the | |
| 103 // pending callbacks while enqueueing new operations. (This could | |
| 104 // happen when callbacks are dispatched synchronously due to error etc.) | |
| 105 std::deque<PendingOperationBase*> pending_operations_; | |
| 106 std::deque<PendingOperationBase*> pending_callbacks_; | |
| 107 | |
| 108 // Valid only while there're pending quota checks. | |
| 109 int64_t cached_file_size_; | |
| 110 int64_t cached_available_space_; | |
| 111 | |
| 112 // Quota-related queries and errors occurred during in-flight quota checks. | |
| 113 int outstanding_quota_queries_; | |
| 114 int outstanding_errors_; | |
| 115 | |
| 116 // For parallel writes bookkeeping. | |
| 117 int64_t max_written_offset_; | |
| 118 int inflight_operations_; | |
| 119 | |
| 120 base::WeakPtrFactory<QuotaFileIO> weak_factory_; | |
| 121 | |
| 122 DISALLOW_COPY_AND_ASSIGN(QuotaFileIO); | |
| 123 }; | |
| 124 | |
| 125 } // namespace content | |
| 126 | |
| 127 #endif // CONTENT_RENDERER_PEPPER_QUOTA_FILE_IO_H_ | |
| OLD | NEW |