• 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 #include "webkit/browser/fileapi/quota/quota_reservation.h"
6 
7 #include "base/bind.h"
8 #include "webkit/browser/fileapi/quota/open_file_handle.h"
9 #include "webkit/browser/fileapi/quota/quota_reservation_buffer.h"
10 
11 namespace fileapi {
12 
RefreshReservation(int64 size,const StatusCallback & callback)13 void QuotaReservation::RefreshReservation(
14     int64 size,
15     const StatusCallback& callback) {
16   DCHECK(sequence_checker_.CalledOnValidSequencedThread());
17   DCHECK(!running_refresh_request_);
18   if (!reservation_manager())
19     return;
20 
21   running_refresh_request_ = true;
22 
23   reservation_manager()->ReserveQuota(
24       origin(), type(), size - remaining_quota_,
25       base::Bind(&QuotaReservation::AdaptDidUpdateReservedQuota,
26                  weak_ptr_factory_.GetWeakPtr(),
27                  size, callback));
28 
29   if (running_refresh_request_)
30     remaining_quota_ = 0;
31 }
32 
GetOpenFileHandle(const base::FilePath & platform_path)33 scoped_ptr<OpenFileHandle> QuotaReservation::GetOpenFileHandle(
34     const base::FilePath& platform_path) {
35   DCHECK(sequence_checker_.CalledOnValidSequencedThread());
36   return reservation_buffer_->GetOpenFileHandle(this, platform_path);
37 }
38 
OnClientCrash()39 void QuotaReservation::OnClientCrash() {
40   DCHECK(sequence_checker_.CalledOnValidSequencedThread());
41 
42   if (remaining_quota_) {
43     reservation_buffer_->PutReservationToBuffer(remaining_quota_);
44     remaining_quota_ = 0;
45   }
46 }
47 
ConsumeReservation(int64 size)48 void QuotaReservation::ConsumeReservation(int64 size) {
49   DCHECK(sequence_checker_.CalledOnValidSequencedThread());
50   DCHECK_LT(0, size);
51   DCHECK_LT(size, remaining_quota_);
52 
53   remaining_quota_ -= size;
54   reservation_buffer_->PutReservationToBuffer(size);
55 }
56 
reservation_manager()57 QuotaReservationManager* QuotaReservation::reservation_manager() {
58   return reservation_buffer_->reservation_manager();
59 }
60 
origin() const61 const GURL& QuotaReservation::origin() const {
62   return reservation_buffer_->origin();
63 }
64 
type() const65 FileSystemType QuotaReservation::type() const {
66   return reservation_buffer_->type();
67 }
68 
QuotaReservation(QuotaReservationBuffer * reservation_buffer)69 QuotaReservation::QuotaReservation(
70     QuotaReservationBuffer* reservation_buffer)
71     : running_refresh_request_(false),
72       remaining_quota_(0),
73       reservation_buffer_(reservation_buffer),
74       weak_ptr_factory_(this) {
75   DCHECK(sequence_checker_.CalledOnValidSequencedThread());
76 }
77 
~QuotaReservation()78 QuotaReservation::~QuotaReservation() {
79   DCHECK(sequence_checker_.CalledOnValidSequencedThread());
80 
81   if (remaining_quota_ && reservation_manager()) {
82     reservation_manager()->ReleaseReservedQuota(
83         origin(), type(), remaining_quota_);
84   }
85 }
86 
87 // static
AdaptDidUpdateReservedQuota(const base::WeakPtr<QuotaReservation> & reservation,int64 new_reserved_size,const StatusCallback & callback,base::PlatformFileError error)88 bool QuotaReservation::AdaptDidUpdateReservedQuota(
89     const base::WeakPtr<QuotaReservation>& reservation,
90     int64 new_reserved_size,
91     const StatusCallback& callback,
92     base::PlatformFileError error) {
93   if (!reservation)
94     return false;
95 
96   reservation->DidUpdateReservedQuota(new_reserved_size, callback, error);
97   return true;
98 }
99 
DidUpdateReservedQuota(int64 new_reserved_size,const StatusCallback & callback,base::PlatformFileError error)100 void QuotaReservation::DidUpdateReservedQuota(
101     int64 new_reserved_size,
102     const StatusCallback& callback,
103     base::PlatformFileError error) {
104   DCHECK(sequence_checker_.CalledOnValidSequencedThread());
105   DCHECK(running_refresh_request_);
106   running_refresh_request_ = false;
107 
108   if (error == base::PLATFORM_FILE_OK)
109     remaining_quota_ = new_reserved_size;
110   callback.Run(error);
111 }
112 
113 }  // namespace fileapi
114