1 // Copyright 2013 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 STORAGE_BROWSER_FILE_SYSTEM_QUOTA_OPEN_FILE_HANDLE_H_
6 #define STORAGE_BROWSER_FILE_SYSTEM_QUOTA_OPEN_FILE_HANDLE_H_
7 
8 #include <stdint.h>
9 
10 #include "base/component_export.h"
11 #include "base/macros.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/memory/weak_ptr.h"
14 
15 namespace base {
16 class FilePath;
17 }
18 
19 namespace storage {
20 
21 class QuotaReservation;
22 class OpenFileHandleContext;
23 class QuotaReservationBuffer;
24 
25 // Represents an open file like a file descriptor.
26 // This should be alive while a consumer keeps a file opened and should be
27 // deleted when the plugin closes the file.
COMPONENT_EXPORT(STORAGE_BROWSER)28 class COMPONENT_EXPORT(STORAGE_BROWSER) OpenFileHandle {
29  public:
30   ~OpenFileHandle();
31 
32   // Updates cached file size and consumes quota for that.
33   // Both this and AddAppendModeWriteAmount should be called for each modified
34   // file before calling QuotaReservation::RefreshQuota and before closing the
35   // file.
36   void UpdateMaxWrittenOffset(int64_t offset);
37 
38   // Notifies that |amount| of data is written to the file in append mode, and
39   // consumes quota for that.
40   // Both this and UpdateMaxWrittenOffset should be called for each modified
41   // file before calling QuotaReservation::RefreshQuota and before closing the
42   // file.
43   void AddAppendModeWriteAmount(int64_t amount);
44 
45   // Returns the estimated file size for the quota consumption calculation.
46   // The client must consume its reserved quota when it writes data to the file
47   // beyond the estimated file size.
48   // The estimated file size is greater than or equal to actual file size after
49   // all clients report their file usage,  and is monotonically increasing over
50   // OpenFileHandle object life cycle, so that client may cache the value.
51   int64_t GetEstimatedFileSize() const;
52 
53   int64_t GetMaxWrittenOffset() const;
54   const base::FilePath& platform_path() const;
55 
56  private:
57   friend class QuotaReservationBuffer;
58 
59   OpenFileHandle(QuotaReservation* reservation, OpenFileHandleContext* context);
60 
61   scoped_refptr<QuotaReservation> reservation_;
62   scoped_refptr<OpenFileHandleContext> context_;
63 
64   base::SequenceChecker sequence_checker_;
65 
66   DISALLOW_COPY_AND_ASSIGN(OpenFileHandle);
67 };
68 
69 }  // namespace storage
70 
71 #endif  // STORAGE_BROWSER_FILE_SYSTEM_QUOTA_OPEN_FILE_HANDLE_H_
72