• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 CONTENT_BROWSER_RENDERER_HOST_PEPPER_QUOTA_RESERVATION_H_
6 #define CONTENT_BROWSER_RENDERER_HOST_PEPPER_QUOTA_RESERVATION_H_
7 
8 #include <map>
9 
10 #include "base/basictypes.h"
11 #include "base/callback.h"
12 #include "base/platform_file.h"
13 #include "content/common/content_export.h"
14 #include "ppapi/c/pp_stdint.h"  // For int64_t on Windows.
15 #include "url/gurl.h"
16 #include "webkit/browser/fileapi/file_system_context.h"
17 
18 namespace fileapi {
19 class FileSystemURL;
20 class OpenFileHandle;
21 class QuotaReservation;
22 }
23 
24 namespace content {
25 
26 struct QuotaReservationDeleter;
27 
28 // This class holds a QuotaReservation and manages OpenFileHandles for checking
29 // quota. It should be created, used, and destroyed on a FileSystemContext's
30 // default_file_task_runner() instance. This is a RefCountedThreadSafe object
31 // because it needs to be passed to the file thread and kept alive during
32 // potentially long-running quota operations.
33 class CONTENT_EXPORT QuotaReservation
34     : public base::RefCountedThreadSafe<QuotaReservation,
35                                         QuotaReservationDeleter> {
36  public:
37   // Static method to facilitate construction on the file task runner.
38   static scoped_refptr<QuotaReservation> Create(
39       scoped_refptr<fileapi::FileSystemContext> file_system_context,
40       const GURL& origin_url,
41       fileapi::FileSystemType file_system_type);
42 
43   // Opens a file with the given id and path and returns its current size.
44   int64_t OpenFile(int32_t id, const fileapi::FileSystemURL& url);
45   // Closes the file opened by OpenFile with the given id.
46   void CloseFile(int32_t id, int64_t max_written_offset);
47   // Refreshes the quota reservation to a new amount. A map that associates file
48   // ids with maximum written offsets is provided as input. The callback will
49   // receive a similar map with the updated file sizes.
50   typedef std::map<int32_t, int64_t> OffsetMap;
51   typedef base::Callback<void(int64_t, const OffsetMap&)> ReserveQuotaCallback;
52   void ReserveQuota(int64_t amount,
53                     const OffsetMap& max_written_offsets,
54                     const ReserveQuotaCallback& callback);
55  private:
56   friend class base::RefCountedThreadSafe<QuotaReservation,
57                                           QuotaReservationDeleter>;
58   friend class base::DeleteHelper<QuotaReservation>;
59   friend struct QuotaReservationDeleter;
60   friend class QuotaReservationTest;
61 
62   QuotaReservation(
63       scoped_refptr<fileapi::FileSystemContext> file_system_context,
64       const GURL& origin_url,
65       fileapi::FileSystemType file_system_type);
66 
67   // For unit testing only. A QuotaReservation intended for unit testing will
68   // have file_system_context_ == NULL.
69   QuotaReservation(
70       scoped_refptr<fileapi::QuotaReservation> quota_reservation,
71       const GURL& origin_url,
72       fileapi::FileSystemType file_system_type);
73 
74   ~QuotaReservation();
75 
76   void GotReservedQuota(const ReserveQuotaCallback& callback,
77                         base::PlatformFileError error);
78 
79   void DeleteOnCorrectThread() const;
80 
81   scoped_refptr<fileapi::FileSystemContext> file_system_context_;
82   scoped_refptr<fileapi::QuotaReservation> quota_reservation_;
83   typedef std::map<int32_t, fileapi::OpenFileHandle*> FileMap;
84   FileMap files_;
85 
86   DISALLOW_COPY_AND_ASSIGN(QuotaReservation);
87 };
88 
89 struct QuotaReservationDeleter {
DestructQuotaReservationDeleter90   static void Destruct(const QuotaReservation* quota_reservation) {
91     quota_reservation->DeleteOnCorrectThread();
92   }
93 };
94 
95 }  // namespace content
96 
97 #endif  // CONTENT_BROWSER_RENDERER_HOST_PEPPER_QUOTA_RESERVATION_H_
98