summaryrefslogtreecommitdiffstats
path: root/webkit/browser/fileapi/quota/quota_reservation_buffer.cc
blob: cfa567fc53330d7a2b168091661de44237be1e60 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "webkit/browser/fileapi/quota/quota_reservation_buffer.h"

#include "base/bind.h"
#include "webkit/browser/fileapi/quota/open_file_handle.h"
#include "webkit/browser/fileapi/quota/open_file_handle_context.h"
#include "webkit/browser/fileapi/quota/quota_reservation.h"

namespace storage {

QuotaReservationBuffer::QuotaReservationBuffer(
    base::WeakPtr<QuotaReservationManager> reservation_manager,
    const GURL& origin,
    FileSystemType type)
    : reservation_manager_(reservation_manager),
      origin_(origin),
      type_(type),
      reserved_quota_(0) {
  DCHECK(origin.is_valid());
  DCHECK(sequence_checker_.CalledOnValidSequencedThread());
  reservation_manager_->IncrementDirtyCount(origin, type);
}

scoped_refptr<QuotaReservation> QuotaReservationBuffer::CreateReservation() {
  DCHECK(sequence_checker_.CalledOnValidSequencedThread());
  return make_scoped_refptr(new QuotaReservation(this));
}

scoped_ptr<OpenFileHandle> QuotaReservationBuffer::GetOpenFileHandle(
    QuotaReservation* reservation,
    const base::FilePath& platform_path) {
  DCHECK(sequence_checker_.CalledOnValidSequencedThread());
  OpenFileHandleContext** open_file = &open_files_[platform_path];
  if (!*open_file)
    *open_file = new OpenFileHandleContext(platform_path, this);
  return make_scoped_ptr(new OpenFileHandle(reservation, *open_file));
}

void QuotaReservationBuffer::CommitFileGrowth(int64 reserved_quota_consumption,
                                              int64 usage_delta) {
  DCHECK(sequence_checker_.CalledOnValidSequencedThread());
  if (!reservation_manager_)
    return;
  reservation_manager_->CommitQuotaUsage(origin_, type_, usage_delta);

  if (reserved_quota_consumption > 0) {
    if (reserved_quota_consumption > reserved_quota_) {
      LOG(ERROR) << "Detected over consumption of the storage quota beyond its"
                 << " reservation";
      reserved_quota_consumption = reserved_quota_;
    }

    reserved_quota_ -= reserved_quota_consumption;
    reservation_manager_->ReleaseReservedQuota(
        origin_, type_, reserved_quota_consumption);
  }
}

void QuotaReservationBuffer::DetachOpenFileHandleContext(
    OpenFileHandleContext* open_file) {
  DCHECK(sequence_checker_.CalledOnValidSequencedThread());
  DCHECK_EQ(open_file, open_files_[open_file->platform_path()]);
  open_files_.erase(open_file->platform_path());
}

void QuotaReservationBuffer::PutReservationToBuffer(int64 reservation) {
  DCHECK(sequence_checker_.CalledOnValidSequencedThread());
  DCHECK_LE(0, reservation);
  reserved_quota_ += reservation;
}

QuotaReservationBuffer::~QuotaReservationBuffer() {
  DCHECK(sequence_checker_.CalledOnValidSequencedThread());
  if (!reservation_manager_)
    return;

  DCHECK_LE(0, reserved_quota_);
  if (reserved_quota_ && reservation_manager_) {
    reservation_manager_->ReserveQuota(
        origin_, type_, -reserved_quota_,
        base::Bind(&QuotaReservationBuffer::DecrementDirtyCount,
                   reservation_manager_, origin_, type_));
  }
  reservation_manager_->ReleaseReservationBuffer(this);
}

// static
bool QuotaReservationBuffer::DecrementDirtyCount(
    base::WeakPtr<QuotaReservationManager> reservation_manager,
    const GURL& origin,
    FileSystemType type,
    base::File::Error error,
    int64 delta_unused) {
  DCHECK(origin.is_valid());
  if (error == base::File::FILE_OK && reservation_manager) {
    reservation_manager->DecrementDirtyCount(origin, type);
    return true;
  }
  return false;
}

}  // namespace storage