Revert 268405 "Make sure that ScratchBuffer::Allocate() always r..."
[chromium-blink-merge.git] / content / browser / renderer_host / pepper / quota_reservation.h
blobf003f940fb85b40c815d580f726d5e3f48cc2dfa
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.
5 #ifndef CONTENT_BROWSER_RENDERER_HOST_PEPPER_QUOTA_RESERVATION_H_
6 #define CONTENT_BROWSER_RENDERER_HOST_PEPPER_QUOTA_RESERVATION_H_
8 #include <map>
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 "ppapi/shared_impl/file_growth.h"
16 #include "url/gurl.h"
17 #include "webkit/browser/fileapi/file_system_context.h"
19 namespace fileapi {
20 class FileSystemURL;
21 class OpenFileHandle;
22 class QuotaReservation;
25 namespace content {
27 struct QuotaReservationDeleter;
29 // This class holds a QuotaReservation and manages OpenFileHandles for checking
30 // quota. It should be created, used, and destroyed on a FileSystemContext's
31 // default_file_task_runner() instance. This is a RefCountedThreadSafe object
32 // because it needs to be passed to the file thread and kept alive during
33 // potentially long-running quota operations.
34 class CONTENT_EXPORT QuotaReservation
35 : public base::RefCountedThreadSafe<QuotaReservation,
36 QuotaReservationDeleter> {
37 public:
38 // Static method to facilitate construction on the file task runner.
39 static scoped_refptr<QuotaReservation> Create(
40 scoped_refptr<fileapi::FileSystemContext> file_system_context,
41 const GURL& origin_url,
42 fileapi::FileSystemType file_system_type);
44 // Opens a file with the given id and path and returns its current size.
45 int64_t OpenFile(int32_t id, const fileapi::FileSystemURL& url);
46 // Closes the file opened by OpenFile with the given id.
47 void CloseFile(int32_t id, const ppapi::FileGrowth& file_growth);
48 // Refreshes the quota reservation to a new amount. A map that associates file
49 // ids with maximum written offsets is provided as input. The callback will
50 // receive a similar map with the updated file sizes.
51 typedef base::Callback<void(int64_t, const ppapi::FileSizeMap&)>
52 ReserveQuotaCallback;
53 void ReserveQuota(int64_t amount,
54 const ppapi::FileGrowthMap& file_growth,
55 const ReserveQuotaCallback& callback);
57 // Notifies underlying QuotaReservation that the associated client crashed,
58 // and that the reserved quota is no longer traceable.
59 void OnClientCrash();
61 private:
62 friend class base::RefCountedThreadSafe<QuotaReservation,
63 QuotaReservationDeleter>;
64 friend class base::DeleteHelper<QuotaReservation>;
65 friend struct QuotaReservationDeleter;
66 friend class QuotaReservationTest;
68 QuotaReservation(
69 scoped_refptr<fileapi::FileSystemContext> file_system_context,
70 const GURL& origin_url,
71 fileapi::FileSystemType file_system_type);
73 // For unit testing only. A QuotaReservation intended for unit testing will
74 // have file_system_context_ == NULL.
75 QuotaReservation(scoped_refptr<fileapi::QuotaReservation> quota_reservation,
76 const GURL& origin_url,
77 fileapi::FileSystemType file_system_type);
79 ~QuotaReservation();
81 void GotReservedQuota(const ReserveQuotaCallback& callback,
82 base::File::Error error);
84 void DeleteOnCorrectThread() const;
86 scoped_refptr<fileapi::FileSystemContext> file_system_context_;
87 scoped_refptr<fileapi::QuotaReservation> quota_reservation_;
88 typedef std::map<int32_t, fileapi::OpenFileHandle*> FileMap;
89 FileMap files_;
91 DISALLOW_COPY_AND_ASSIGN(QuotaReservation);
94 struct QuotaReservationDeleter {
95 static void Destruct(const QuotaReservation* quota_reservation) {
96 quota_reservation->DeleteOnCorrectThread();
100 } // namespace content
102 #endif // CONTENT_BROWSER_RENDERER_HOST_PEPPER_QUOTA_RESERVATION_H_