Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / content / browser / renderer_host / pepper / quota_reservation.cc
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 "content/browser/renderer_host/pepper/quota_reservation.h"
6
7 #include "base/bind.h"
8 #include "base/callback.h"
9 #include "content/public/browser/browser_thread.h"
10 #include "webkit/browser/fileapi/file_system_operation_runner.h"
11 #include "webkit/browser/fileapi/quota/open_file_handle.h"
12 #include "webkit/browser/fileapi/quota/quota_reservation.h"
13 #include "webkit/common/fileapi/file_system_util.h"
14
15 namespace content {
16
17 // static
18 scoped_refptr<QuotaReservation> QuotaReservation::Create(
19     scoped_refptr<fileapi::FileSystemContext> file_system_context,
20     const GURL& origin_url,
21     fileapi::FileSystemType type) {
22   return scoped_refptr<QuotaReservation>(new QuotaReservation(
23       file_system_context, origin_url, type));
24 }
25
26 QuotaReservation::QuotaReservation(
27     scoped_refptr<fileapi::FileSystemContext> file_system_context,
28     const GURL& origin_url,
29     fileapi::FileSystemType file_system_type)
30     : file_system_context_(file_system_context) {
31   quota_reservation_ =
32       file_system_context->CreateQuotaReservationOnFileTaskRunner(
33           origin_url,
34           file_system_type);
35 }
36
37 // For unit testing only.
38 QuotaReservation::QuotaReservation(
39     scoped_refptr<fileapi::QuotaReservation> quota_reservation,
40     const GURL& /* origin_url */,
41     fileapi::FileSystemType /* file_system_type */)
42     : quota_reservation_(quota_reservation) {
43 }
44
45 QuotaReservation::~QuotaReservation() {
46   // We should have no open files at this point.
47   DCHECK(files_.size() == 0);
48   for (FileMap::iterator it = files_.begin(); it != files_.end(); ++ it)
49     delete it->second;
50 }
51
52 int64_t QuotaReservation::OpenFile(int32_t id,
53                                    const fileapi::FileSystemURL& url) {
54   base::FilePath platform_file_path;
55   if (file_system_context_) {
56     base::File::Error error =
57         file_system_context_->operation_runner()->SyncGetPlatformPath(
58             url, &platform_file_path);
59     if (error != base::File::FILE_OK) {
60       NOTREACHED();
61       return 0;
62     }
63   } else {
64     // For test.
65     platform_file_path = url.path();
66   }
67
68   scoped_ptr<fileapi::OpenFileHandle> file_handle =
69       quota_reservation_->GetOpenFileHandle(platform_file_path);
70   std::pair<FileMap::iterator, bool> insert_result =
71       files_.insert(std::make_pair(id, file_handle.get()));
72   if (insert_result.second) {
73     int64_t max_written_offset = file_handle->GetMaxWrittenOffset();
74     ignore_result(file_handle.release());
75     return max_written_offset;
76   }
77   NOTREACHED();
78   return 0;
79 }
80
81 void QuotaReservation::CloseFile(int32_t id,
82                                  const ppapi::FileGrowth& file_growth) {
83   FileMap::iterator it = files_.find(id);
84   if (it != files_.end()) {
85     it->second->UpdateMaxWrittenOffset(file_growth.max_written_offset);
86     it->second->AddAppendModeWriteAmount(file_growth.append_mode_write_amount);
87     delete it->second;
88     files_.erase(it);
89   } else {
90     NOTREACHED();
91   }
92 }
93
94 void QuotaReservation::ReserveQuota(
95     int64_t amount,
96     const ppapi::FileGrowthMap& file_growths,
97     const ReserveQuotaCallback& callback) {
98   for (FileMap::iterator it = files_.begin(); it != files_.end(); ++it) {
99     ppapi::FileGrowthMap::const_iterator growth_it =
100         file_growths.find(it->first);
101     if (growth_it != file_growths.end()) {
102       it->second->UpdateMaxWrittenOffset(growth_it->second.max_written_offset);
103       it->second->AddAppendModeWriteAmount(
104           growth_it->second.append_mode_write_amount);
105     } else {
106       NOTREACHED();
107     }
108   }
109
110   quota_reservation_->RefreshReservation(
111       amount,
112       base::Bind(&QuotaReservation::GotReservedQuota,
113                  this,
114                  callback));
115 }
116
117 void QuotaReservation::OnClientCrash() {
118   quota_reservation_->OnClientCrash();
119 }
120
121 void QuotaReservation::GotReservedQuota(
122     const ReserveQuotaCallback& callback,
123     base::File::Error error) {
124   ppapi::FileSizeMap file_sizes;
125   for (FileMap::iterator it = files_.begin(); it != files_.end(); ++ it)
126     file_sizes[it->first] = it->second->GetMaxWrittenOffset();
127
128   if (file_system_context_) {
129     BrowserThread::PostTask(
130         BrowserThread::IO,
131         FROM_HERE,
132         base::Bind(callback,
133                    quota_reservation_->remaining_quota(),
134                    file_sizes));
135   } else {
136     // Unit testing code path.
137     callback.Run(quota_reservation_->remaining_quota(), file_sizes);
138   }
139 }
140
141 void QuotaReservation::DeleteOnCorrectThread() const {
142   if (file_system_context_ &&
143       !file_system_context_->
144           default_file_task_runner()->RunsTasksOnCurrentThread()) {
145     file_system_context_->default_file_task_runner()->DeleteSoon(
146         FROM_HERE,
147         this);
148   } else {
149     // We're on the right thread to delete, or unit test.
150     delete this;
151   }
152 }
153
154 }  // namespace content