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