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
3 // found in the LICENSE file.
5 #include "storage/browser/fileapi/sandbox_quota_observer.h"
7 #include "base/sequenced_task_runner.h"
8 #include "storage/browser/fileapi/file_system_usage_cache.h"
9 #include "storage/browser/fileapi/sandbox_file_system_backend_delegate.h"
10 #include "storage/browser/fileapi/timed_task_helper.h"
11 #include "storage/browser/quota/quota_client.h"
12 #include "storage/browser/quota/quota_manager_proxy.h"
13 #include "storage/common/fileapi/file_system_util.h"
17 SandboxQuotaObserver::SandboxQuotaObserver(
18 storage::QuotaManagerProxy
* quota_manager_proxy
,
19 base::SequencedTaskRunner
* update_notify_runner
,
20 ObfuscatedFileUtil
* sandbox_file_util
,
21 FileSystemUsageCache
* file_system_usage_cache
)
22 : quota_manager_proxy_(quota_manager_proxy
),
23 update_notify_runner_(update_notify_runner
),
24 sandbox_file_util_(sandbox_file_util
),
25 file_system_usage_cache_(file_system_usage_cache
) {
28 SandboxQuotaObserver::~SandboxQuotaObserver() {}
30 void SandboxQuotaObserver::OnStartUpdate(const FileSystemURL
& url
) {
31 DCHECK(update_notify_runner_
->RunsTasksOnCurrentThread());
32 base::FilePath usage_file_path
= GetUsageCachePath(url
);
33 if (usage_file_path
.empty())
35 file_system_usage_cache_
->IncrementDirty(usage_file_path
);
38 void SandboxQuotaObserver::OnUpdate(const FileSystemURL
& url
,
40 DCHECK(update_notify_runner_
->RunsTasksOnCurrentThread());
42 if (quota_manager_proxy_
.get()) {
43 quota_manager_proxy_
->NotifyStorageModified(
44 storage::QuotaClient::kFileSystem
,
46 FileSystemTypeToQuotaStorageType(url
.type()),
50 base::FilePath usage_file_path
= GetUsageCachePath(url
);
51 if (usage_file_path
.empty())
54 pending_update_notification_
[usage_file_path
] += delta
;
55 if (!delayed_cache_update_helper_
) {
56 delayed_cache_update_helper_
.reset(
57 new TimedTaskHelper(update_notify_runner_
.get()));
58 delayed_cache_update_helper_
->Start(
60 base::TimeDelta(), // No delay.
61 base::Bind(&SandboxQuotaObserver::ApplyPendingUsageUpdate
,
62 base::Unretained(this)));
66 void SandboxQuotaObserver::OnEndUpdate(const FileSystemURL
& url
) {
67 DCHECK(update_notify_runner_
->RunsTasksOnCurrentThread());
69 base::FilePath usage_file_path
= GetUsageCachePath(url
);
70 if (usage_file_path
.empty())
73 PendingUpdateNotificationMap::iterator found
=
74 pending_update_notification_
.find(usage_file_path
);
75 if (found
!= pending_update_notification_
.end()) {
76 UpdateUsageCacheFile(found
->first
, found
->second
);
77 pending_update_notification_
.erase(found
);
80 file_system_usage_cache_
->DecrementDirty(usage_file_path
);
83 void SandboxQuotaObserver::OnAccess(const FileSystemURL
& url
) {
84 if (quota_manager_proxy_
.get()) {
85 quota_manager_proxy_
->NotifyStorageAccessed(
86 storage::QuotaClient::kFileSystem
,
88 FileSystemTypeToQuotaStorageType(url
.type()));
92 void SandboxQuotaObserver::SetUsageCacheEnabled(
96 if (quota_manager_proxy_
.get()) {
97 quota_manager_proxy_
->SetUsageCacheEnabled(
98 storage::QuotaClient::kFileSystem
,
100 FileSystemTypeToQuotaStorageType(type
),
105 base::FilePath
SandboxQuotaObserver::GetUsageCachePath(
106 const FileSystemURL
& url
) {
107 DCHECK(sandbox_file_util_
);
108 base::File::Error error
= base::File::FILE_OK
;
109 base::FilePath path
=
110 SandboxFileSystemBackendDelegate::GetUsageCachePathForOriginAndType(
111 sandbox_file_util_
, url
.origin(), url
.type(), &error
);
112 if (error
!= base::File::FILE_OK
) {
113 LOG(WARNING
) << "Could not get usage cache path for: "
114 << url
.DebugString();
115 return base::FilePath();
120 void SandboxQuotaObserver::ApplyPendingUsageUpdate() {
121 delayed_cache_update_helper_
.reset();
122 for (PendingUpdateNotificationMap::iterator itr
=
123 pending_update_notification_
.begin();
124 itr
!= pending_update_notification_
.end();
126 UpdateUsageCacheFile(itr
->first
, itr
->second
);
128 pending_update_notification_
.clear();
131 void SandboxQuotaObserver::UpdateUsageCacheFile(
132 const base::FilePath
& usage_file_path
,
134 DCHECK(!usage_file_path
.empty());
135 if (!usage_file_path
.empty() && delta
!= 0)
136 file_system_usage_cache_
->AtomicUpdateUsageByDelta(usage_file_path
, delta
);
139 } // namespace storage