OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "webkit/fileapi/file_system_quota_client.h" | 5 #include "webkit/fileapi/file_system_quota_client.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <set> | 8 #include <set> |
9 | 9 |
10 #include "base/bind.h" | |
10 #include "base/file_path.h" | 11 #include "base/file_path.h" |
11 #include "base/file_util.h" | 12 #include "base/file_util.h" |
13 #include "base/location.h" | |
12 #include "base/logging.h" | 14 #include "base/logging.h" |
13 #include "base/memory/scoped_ptr.h" | 15 #include "base/memory/scoped_ptr.h" |
14 #include "base/sequenced_task_runner.h" | 16 #include "base/sequenced_task_runner.h" |
17 #include "base/task_runner_util.h" | |
15 #include "googleurl/src/gurl.h" | 18 #include "googleurl/src/gurl.h" |
16 #include "net/base/net_util.h" | 19 #include "net/base/net_util.h" |
17 #include "webkit/fileapi/file_system_context.h" | 20 #include "webkit/fileapi/file_system_context.h" |
18 #include "webkit/fileapi/file_system_quota_util.h" | 21 #include "webkit/fileapi/file_system_quota_util.h" |
19 #include "webkit/fileapi/file_system_usage_cache.h" | 22 #include "webkit/fileapi/file_system_usage_cache.h" |
20 #include "webkit/fileapi/file_system_util.h" | 23 #include "webkit/fileapi/file_system_util.h" |
21 #include "webkit/fileapi/sandbox_mount_point_provider.h" | 24 #include "webkit/fileapi/sandbox_mount_point_provider.h" |
22 | 25 |
23 using base::SequencedTaskRunner; | |
24 using quota::QuotaThreadTask; | |
25 using quota::StorageType; | 26 using quota::StorageType; |
26 | 27 |
27 namespace fileapi { | 28 namespace fileapi { |
28 | 29 |
29 class FileSystemQuotaClient::GetOriginUsageTask : public QuotaThreadTask { | 30 namespace { |
30 public: | |
31 GetOriginUsageTask( | |
32 FileSystemQuotaClient* quota_client, | |
33 const GURL& origin_url, | |
34 FileSystemType type) | |
35 : QuotaThreadTask(quota_client, quota_client->file_task_runner()), | |
36 quota_client_(quota_client), | |
37 origin_url_(origin_url), | |
38 type_(type), | |
39 fs_usage_(0) { | |
40 DCHECK(quota_client_); | |
41 file_system_context_ = quota_client_->file_system_context_; | |
42 } | |
43 | 31 |
44 protected: | 32 void GetOriginsForTypeOnFileThread(FileSystemContext* context, |
45 virtual ~GetOriginUsageTask() {} | 33 StorageType storage_type, |
34 std::set<GURL>* origins_ptr) { | |
35 FileSystemType type = QuotaStorageTypeToFileSystemType(storage_type); | |
36 DCHECK(type != kFileSystemTypeUnknown); | |
46 | 37 |
47 // QuotaThreadTask: | 38 FileSystemQuotaUtil* quota_util = context->GetQuotaUtil(type); |
48 virtual void RunOnTargetThread() OVERRIDE { | 39 if (!quota_util) |
49 FileSystemQuotaUtil* quota_util = file_system_context_->GetQuotaUtil(type_); | 40 return; |
50 if (quota_util) | 41 quota_util->GetOriginsForTypeOnFileThread(type, origins_ptr); |
51 fs_usage_ = quota_util->GetOriginUsageOnFileThread( | 42 } |
52 file_system_context_, origin_url_, type_); | |
53 } | |
54 | 43 |
55 virtual void Completed() OVERRIDE { | 44 void GetOriginsForHostOnFileThread(FileSystemContext* context, |
56 quota_client_->DidGetOriginUsage(type_, origin_url_, fs_usage_); | 45 StorageType storage_type, |
57 } | 46 const std::string& host, |
47 std::set<GURL>* origins_ptr) { | |
48 FileSystemType type = QuotaStorageTypeToFileSystemType(storage_type); | |
49 DCHECK(type != kFileSystemTypeUnknown); | |
58 | 50 |
59 FileSystemQuotaClient* quota_client_; | 51 FileSystemQuotaUtil* quota_util = context->GetQuotaUtil(type); |
60 scoped_refptr<FileSystemContext> file_system_context_; | 52 if (!quota_util) |
61 GURL origin_url_; | 53 return; |
62 FileSystemType type_; | 54 quota_util->GetOriginsForHostOnFileThread(type, host, origins_ptr); |
63 int64 fs_usage_; | 55 } |
64 }; | |
65 | 56 |
66 class FileSystemQuotaClient::GetOriginsForTypeTask : public QuotaThreadTask { | 57 void DidGetOrigins( |
67 public: | 58 const base::Callback<void(const std::set<GURL>&, StorageType)>& callback, |
68 GetOriginsForTypeTask( | 59 std::set<GURL>* origins_ptr, |
69 FileSystemQuotaClient* quota_client, | 60 StorageType storage_type) { |
70 FileSystemType type) | 61 callback.Run(*origins_ptr, storage_type); |
71 : QuotaThreadTask(quota_client, quota_client->file_task_runner()), | 62 } |
72 quota_client_(quota_client), | |
73 type_(type) { | |
74 DCHECK(quota_client_); | |
75 file_system_context_ = quota_client_->file_system_context_; | |
76 } | |
77 | 63 |
78 protected: | 64 quota::QuotaStatusCode DeleteOriginOnTargetThread( |
79 virtual ~GetOriginsForTypeTask() {} | 65 FileSystemContext* context, |
66 const GURL& origin, | |
67 FileSystemType type) { | |
68 base::PlatformFileError result = | |
69 context->sandbox_provider()->DeleteOriginDataOnFileThread( | |
70 context, context->quota_manager_proxy(), origin, type); | |
71 if (result == base::PLATFORM_FILE_OK) | |
72 return quota::kQuotaStatusOk; | |
73 return quota::kQuotaErrorInvalidModification; | |
74 } | |
80 | 75 |
81 // QuotaThreadTask: | 76 } // namespace |
82 virtual void RunOnTargetThread() OVERRIDE { | |
83 FileSystemQuotaUtil* quota_util = file_system_context_->GetQuotaUtil(type_); | |
84 if (quota_util) | |
85 quota_util->GetOriginsForTypeOnFileThread(type_, &origins_); | |
86 } | |
87 | |
88 virtual void Completed() OVERRIDE { | |
89 quota_client_->DidGetOriginsForType(type_, origins_); | |
90 } | |
91 | |
92 private: | |
93 FileSystemQuotaClient* quota_client_; | |
94 scoped_refptr<FileSystemContext> file_system_context_; | |
95 std::set<GURL> origins_; | |
96 FileSystemType type_; | |
97 }; | |
98 | |
99 class FileSystemQuotaClient::GetOriginsForHostTask : public QuotaThreadTask { | |
100 public: | |
101 GetOriginsForHostTask( | |
102 FileSystemQuotaClient* quota_client, | |
103 FileSystemType type, | |
104 const std::string& host) | |
105 : QuotaThreadTask(quota_client, quota_client->file_task_runner()), | |
106 quota_client_(quota_client), | |
107 type_(type), | |
108 host_(host) { | |
109 DCHECK(quota_client_); | |
110 file_system_context_ = quota_client_->file_system_context_; | |
111 } | |
112 | |
113 protected: | |
114 virtual ~GetOriginsForHostTask() {} | |
115 | |
116 // QuotaThreadTask: | |
117 virtual void RunOnTargetThread() OVERRIDE { | |
118 FileSystemQuotaUtil* quota_util = file_system_context_->GetQuotaUtil(type_); | |
119 if (quota_util) | |
120 quota_util->GetOriginsForHostOnFileThread(type_, host_, &origins_); | |
121 } | |
122 | |
123 virtual void Completed() OVERRIDE { | |
124 quota_client_->DidGetOriginsForHost(std::make_pair(type_, host_), origins_); | |
125 } | |
126 | |
127 private: | |
128 FileSystemQuotaClient* quota_client_; | |
129 scoped_refptr<FileSystemContext> file_system_context_; | |
130 std::set<GURL> origins_; | |
131 FileSystemType type_; | |
132 std::string host_; | |
133 }; | |
134 | |
135 class FileSystemQuotaClient::DeleteOriginTask | |
136 : public QuotaThreadTask { | |
137 public: | |
138 DeleteOriginTask( | |
139 FileSystemQuotaClient* quota_client, | |
140 const GURL& origin, | |
141 FileSystemType type, | |
142 const DeletionCallback& callback) | |
143 : QuotaThreadTask(quota_client, quota_client->file_task_runner()), | |
144 file_system_context_(quota_client->file_system_context_), | |
145 origin_(origin), | |
146 type_(type), | |
147 status_(quota::kQuotaStatusUnknown), | |
148 callback_(callback) { | |
149 } | |
150 | |
151 protected: | |
152 virtual ~DeleteOriginTask() {} | |
153 | |
154 // QuotaThreadTask: | |
155 virtual void RunOnTargetThread() OVERRIDE { | |
156 base::PlatformFileError result = | |
157 file_system_context_->sandbox_provider()->DeleteOriginDataOnFileThread( | |
158 file_system_context_, | |
159 file_system_context_->quota_manager_proxy(), | |
160 origin_, | |
161 type_); | |
162 if (result == base::PLATFORM_FILE_OK) | |
163 status_ = quota::kQuotaStatusOk; | |
164 else | |
165 status_ = quota::kQuotaErrorInvalidModification; | |
166 } | |
167 | |
168 virtual void Completed() OVERRIDE { | |
169 callback_.Run(status_); | |
170 } | |
171 | |
172 private: | |
173 FileSystemContext* file_system_context_; | |
174 GURL origin_; | |
175 FileSystemType type_; | |
176 quota::QuotaStatusCode status_; | |
177 DeletionCallback callback_; | |
178 }; | |
179 | 77 |
180 FileSystemQuotaClient::FileSystemQuotaClient( | 78 FileSystemQuotaClient::FileSystemQuotaClient( |
181 FileSystemContext* file_system_context, | 79 FileSystemContext* file_system_context, |
182 bool is_incognito) | 80 bool is_incognito) |
183 : file_system_context_(file_system_context), | 81 : file_system_context_(file_system_context), |
184 is_incognito_(is_incognito) { | 82 is_incognito_(is_incognito) { |
185 } | 83 } |
186 | 84 |
187 FileSystemQuotaClient::~FileSystemQuotaClient() {} | 85 FileSystemQuotaClient::~FileSystemQuotaClient() {} |
188 | 86 |
(...skipping 13 matching lines...) Expand all Loading... | |
202 | 100 |
203 if (is_incognito_) { | 101 if (is_incognito_) { |
204 // We don't support FileSystem in incognito mode yet. | 102 // We don't support FileSystem in incognito mode yet. |
205 callback.Run(0); | 103 callback.Run(0); |
206 return; | 104 return; |
207 } | 105 } |
208 | 106 |
209 FileSystemType type = QuotaStorageTypeToFileSystemType(storage_type); | 107 FileSystemType type = QuotaStorageTypeToFileSystemType(storage_type); |
210 DCHECK(type != kFileSystemTypeUnknown); | 108 DCHECK(type != kFileSystemTypeUnknown); |
211 | 109 |
212 if (pending_usage_callbacks_.Add( | 110 FileSystemQuotaUtil* quota_util = file_system_context_->GetQuotaUtil(type); |
213 std::make_pair(type, origin_url.spec()), callback)) { | 111 if (!quota_util) { |
214 scoped_refptr<GetOriginUsageTask> task( | 112 callback.Run(0); |
215 new GetOriginUsageTask(this, origin_url, type)); | 113 return; |
216 task->Start(); | |
217 } | 114 } |
115 | |
116 base::PostTaskAndReplyWithResult( | |
117 file_task_runner(), | |
118 FROM_HERE, | |
119 // It is safe to pass Unretained(quota_util) since context owns it. | |
120 base::Bind(&FileSystemQuotaUtil::GetOriginUsageOnFileThread, | |
121 base::Unretained(quota_util), | |
122 make_scoped_refptr(file_system_context_.get()), | |
123 origin_url, | |
124 type), | |
125 callback); | |
218 } | 126 } |
219 | 127 |
220 void FileSystemQuotaClient::GetOriginsForType( | 128 void FileSystemQuotaClient::GetOriginsForType( |
221 StorageType storage_type, | 129 StorageType storage_type, |
222 const GetOriginsCallback& callback) { | 130 const GetOriginsCallback& callback) { |
223 DCHECK(!callback.is_null()); | 131 DCHECK(!callback.is_null()); |
224 | 132 |
225 std::set<GURL> origins; | |
226 if (is_incognito_) { | 133 if (is_incognito_) { |
227 // We don't support FileSystem in incognito mode yet. | 134 // We don't support FileSystem in incognito mode yet. |
135 std::set<GURL> origins; | |
228 callback.Run(origins, storage_type); | 136 callback.Run(origins, storage_type); |
229 return; | 137 return; |
230 } | 138 } |
231 | 139 |
232 FileSystemType type = QuotaStorageTypeToFileSystemType(storage_type); | 140 std::set<GURL>* origins_ptr = new std::set<GURL>(); |
233 DCHECK(type != kFileSystemTypeUnknown); | 141 file_task_runner()->PostTaskAndReply( |
234 | 142 FROM_HERE, |
235 if (pending_origins_for_type_callbacks_.Add(type, callback)) { | 143 base::Bind(&GetOriginsForTypeOnFileThread, |
236 scoped_refptr<GetOriginsForTypeTask> task( | 144 make_scoped_refptr(file_system_context_.get()), |
237 new GetOriginsForTypeTask(this, type)); | 145 storage_type, |
238 task->Start(); | 146 base::Unretained(origins_ptr)), |
239 } | 147 base::Bind(&DidGetOrigins, |
148 callback, | |
149 base::Owned(origins_ptr), | |
150 storage_type)); | |
240 } | 151 } |
241 | 152 |
242 void FileSystemQuotaClient::GetOriginsForHost( | 153 void FileSystemQuotaClient::GetOriginsForHost( |
243 StorageType storage_type, | 154 StorageType storage_type, |
244 const std::string& host, | 155 const std::string& host, |
245 const GetOriginsCallback& callback) { | 156 const GetOriginsCallback& callback) { |
246 DCHECK(!callback.is_null()); | 157 DCHECK(!callback.is_null()); |
247 | 158 |
248 std::set<GURL> origins; | |
249 if (is_incognito_) { | 159 if (is_incognito_) { |
250 // We don't support FileSystem in incognito mode yet. | 160 // We don't support FileSystem in incognito mode yet. |
161 std::set<GURL> origins; | |
251 callback.Run(origins, storage_type); | 162 callback.Run(origins, storage_type); |
252 return; | 163 return; |
253 } | 164 } |
254 | 165 |
255 FileSystemType type = QuotaStorageTypeToFileSystemType(storage_type); | 166 std::set<GURL>* origins_ptr = new std::set<GURL>(); |
256 DCHECK(type != kFileSystemTypeUnknown); | 167 file_task_runner()->PostTaskAndReply( |
257 | 168 FROM_HERE, |
258 if (pending_origins_for_host_callbacks_.Add( | 169 base::Bind(&GetOriginsForHostOnFileThread, |
259 std::make_pair(type, host), callback)) { | 170 make_scoped_refptr(file_system_context_.get()), |
260 scoped_refptr<GetOriginsForHostTask> task( | 171 storage_type, |
261 new GetOriginsForHostTask(this, type, host)); | 172 host, |
262 task->Start(); | 173 base::Unretained(origins_ptr)), |
263 } | 174 base::Bind(&DidGetOrigins, |
175 callback, | |
176 base::Owned(origins_ptr), | |
177 storage_type)); | |
264 } | 178 } |
265 | 179 |
266 void FileSystemQuotaClient::DeleteOriginData(const GURL& origin, | 180 void FileSystemQuotaClient::DeleteOriginData(const GURL& origin, |
267 StorageType type, | 181 StorageType type, |
268 const DeletionCallback& callback) { | 182 const DeletionCallback& callback) { |
269 FileSystemType fs_type = QuotaStorageTypeToFileSystemType(type); | 183 FileSystemType fs_type = QuotaStorageTypeToFileSystemType(type); |
270 DCHECK(fs_type != kFileSystemTypeUnknown); | 184 DCHECK(fs_type != kFileSystemTypeUnknown); |
271 scoped_refptr<DeleteOriginTask> task( | |
272 new DeleteOriginTask(this, origin, fs_type, callback)); | |
273 task->Start(); | |
274 } | |
275 | 185 |
276 void FileSystemQuotaClient::DidGetOriginUsage( | 186 base::PostTaskAndReplyWithResult( |
277 FileSystemType type, const GURL& origin_url, int64 usage) { | 187 file_task_runner(), |
278 TypeAndHostOrOrigin type_and_origin(std::make_pair( | 188 FROM_HERE, |
279 type, origin_url.spec())); | 189 base::Bind(&DeleteOriginOnTargetThread, |
280 DCHECK(pending_usage_callbacks_.HasCallbacks(type_and_origin)); | 190 make_scoped_refptr(file_system_context_.get()), |
kinuko
2012/08/06 21:06:27
nit: maybe you can just pass file_system_context_
nhiroki
2012/08/06 21:30:34
Done.
| |
281 pending_usage_callbacks_.Run(type_and_origin, usage); | 191 origin, |
282 } | 192 fs_type), |
283 | 193 callback); |
284 void FileSystemQuotaClient::DidGetOriginsForType( | |
285 FileSystemType type, const std::set<GURL>& origins) { | |
286 DCHECK(pending_origins_for_type_callbacks_.HasCallbacks(type)); | |
287 pending_origins_for_type_callbacks_.Run(type, origins, | |
288 FileSystemTypeToQuotaStorageType(type)); | |
289 } | |
290 | |
291 void FileSystemQuotaClient::DidGetOriginsForHost( | |
292 const TypeAndHostOrOrigin& type_and_host, const std::set<GURL>& origins) { | |
293 DCHECK(pending_origins_for_host_callbacks_.HasCallbacks(type_and_host)); | |
294 pending_origins_for_host_callbacks_.Run(type_and_host, origins, | |
295 FileSystemTypeToQuotaStorageType(type_and_host.first)); | |
296 } | 194 } |
297 | 195 |
298 base::SequencedTaskRunner* FileSystemQuotaClient::file_task_runner() const { | 196 base::SequencedTaskRunner* FileSystemQuotaClient::file_task_runner() const { |
299 return file_system_context_->file_task_runner(); | 197 return file_system_context_->file_task_runner(); |
300 } | 198 } |
301 | 199 |
302 } // namespace fileapi | 200 } // namespace fileapi |
OLD | NEW |