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/sandbox_quota_observer.h" | 5 #include "webkit/fileapi/sandbox_quota_observer.h" |
6 | 6 |
7 #include "base/sequenced_task_runner.h" | 7 #include "base/sequenced_task_runner.h" |
8 #include "webkit/fileapi/file_system_url.h" | 8 #include "webkit/fileapi/file_system_url.h" |
9 #include "webkit/fileapi/file_system_usage_cache.h" | 9 #include "webkit/fileapi/file_system_usage_cache.h" |
10 #include "webkit/fileapi/file_system_util.h" | 10 #include "webkit/fileapi/file_system_util.h" |
11 #include "webkit/fileapi/sandbox_mount_point_provider.h" | 11 #include "webkit/fileapi/sandbox_mount_point_provider.h" |
12 #include "webkit/quota/quota_client.h" | 12 #include "webkit/quota/quota_client.h" |
13 #include "webkit/quota/quota_manager.h" | 13 #include "webkit/quota/quota_manager.h" |
14 | 14 |
15 namespace fileapi { | 15 namespace fileapi { |
16 | 16 |
17 SandboxQuotaObserver::SandboxQuotaObserver( | 17 SandboxQuotaObserver::SandboxQuotaObserver( |
18 quota::QuotaManagerProxy* quota_manager_proxy, | 18 quota::QuotaManagerProxy* quota_manager_proxy, |
19 base::SequencedTaskRunner* update_notify_runner, | 19 base::SequencedTaskRunner* update_notify_runner, |
20 ObfuscatedFileUtil* sandbox_file_util) | 20 ObfuscatedFileUtil* sandbox_file_util) |
21 : quota_manager_proxy_(quota_manager_proxy), | 21 : quota_manager_proxy_(quota_manager_proxy), |
22 update_notify_runner_(update_notify_runner), | 22 update_notify_runner_(update_notify_runner), |
23 sandbox_file_util_(sandbox_file_util) {} | 23 sandbox_file_util_(sandbox_file_util), |
24 running_delayed_cache_update_(false), | |
25 weak_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) {} | |
24 | 26 |
25 SandboxQuotaObserver::~SandboxQuotaObserver() {} | 27 SandboxQuotaObserver::~SandboxQuotaObserver() {} |
26 | 28 |
27 void SandboxQuotaObserver::OnStartUpdate(const FileSystemURL& url) { | 29 void SandboxQuotaObserver::OnStartUpdate(const FileSystemURL& url) { |
28 DCHECK(SandboxMountPointProvider::CanHandleType(url.type())); | 30 DCHECK(SandboxMountPointProvider::CanHandleType(url.type())); |
29 DCHECK(update_notify_runner_->RunsTasksOnCurrentThread()); | 31 DCHECK(update_notify_runner_->RunsTasksOnCurrentThread()); |
30 FilePath usage_file_path = GetUsageCachePath(url); | 32 FilePath usage_file_path = GetUsageCachePath(url); |
31 if (usage_file_path.empty()) | 33 if (usage_file_path.empty()) |
32 return; | 34 return; |
33 FileSystemUsageCache::IncrementDirty(usage_file_path); | 35 FileSystemUsageCache::IncrementDirty(usage_file_path); |
34 } | 36 } |
35 | 37 |
36 void SandboxQuotaObserver::OnUpdate(const FileSystemURL& url, | 38 void SandboxQuotaObserver::OnUpdate(const FileSystemURL& url, |
37 int64 delta) { | 39 int64 delta) { |
38 DCHECK(SandboxMountPointProvider::CanHandleType(url.type())); | 40 DCHECK(SandboxMountPointProvider::CanHandleType(url.type())); |
39 DCHECK(update_notify_runner_->RunsTasksOnCurrentThread()); | 41 DCHECK(update_notify_runner_->RunsTasksOnCurrentThread()); |
42 | |
40 FilePath usage_file_path = GetUsageCachePath(url); | 43 FilePath usage_file_path = GetUsageCachePath(url); |
kinuko
2013/01/07 06:38:25
should we return if usage_file_path.empty() here t
tzik
2013/01/07 16:00:37
Done.
| |
41 if (usage_file_path.empty()) | 44 pending_update_notification_[usage_file_path] += delta; |
42 return; | 45 if (!running_delayed_cache_update_) { |
43 if (delta != 0) | 46 update_notify_runner_->PostTask(FROM_HERE, base::Bind( |
44 FileSystemUsageCache::AtomicUpdateUsageByDelta(usage_file_path, delta); | 47 &SandboxQuotaObserver::ApplyPendingUsageUpdate, |
48 weak_factory_.GetWeakPtr())); | |
49 running_delayed_cache_update_ = true; | |
50 } | |
51 | |
45 if (quota_manager_proxy_) { | 52 if (quota_manager_proxy_) { |
46 quota_manager_proxy_->NotifyStorageModified( | 53 quota_manager_proxy_->NotifyStorageModified( |
47 quota::QuotaClient::kFileSystem, | 54 quota::QuotaClient::kFileSystem, |
48 url.origin(), | 55 url.origin(), |
49 FileSystemTypeToQuotaStorageType(url.type()), | 56 FileSystemTypeToQuotaStorageType(url.type()), |
50 delta); | 57 delta); |
51 } | 58 } |
52 } | 59 } |
53 | 60 |
54 void SandboxQuotaObserver::OnEndUpdate(const FileSystemURL& url) { | 61 void SandboxQuotaObserver::OnEndUpdate(const FileSystemURL& url) { |
55 DCHECK(SandboxMountPointProvider::CanHandleType(url.type())); | 62 DCHECK(SandboxMountPointProvider::CanHandleType(url.type())); |
56 DCHECK(update_notify_runner_->RunsTasksOnCurrentThread()); | 63 DCHECK(update_notify_runner_->RunsTasksOnCurrentThread()); |
64 | |
57 FilePath usage_file_path = GetUsageCachePath(url); | 65 FilePath usage_file_path = GetUsageCachePath(url); |
58 if (usage_file_path.empty()) | 66 if (usage_file_path.empty()) |
59 return; | 67 return; |
68 | |
69 PendingUpdateNotificationMap::iterator found = | |
70 pending_update_notification_.find(usage_file_path); | |
71 if (found != pending_update_notification_.end()) { | |
72 UpdateUsageCacheFile(found->first, found->second); | |
73 pending_update_notification_.erase(found); | |
74 } | |
75 | |
60 FileSystemUsageCache::DecrementDirty(usage_file_path); | 76 FileSystemUsageCache::DecrementDirty(usage_file_path); |
61 } | 77 } |
62 | 78 |
63 void SandboxQuotaObserver::OnAccess(const FileSystemURL& url) { | 79 void SandboxQuotaObserver::OnAccess(const FileSystemURL& url) { |
64 DCHECK(SandboxMountPointProvider::CanHandleType(url.type())); | 80 DCHECK(SandboxMountPointProvider::CanHandleType(url.type())); |
65 if (quota_manager_proxy_) { | 81 if (quota_manager_proxy_) { |
66 quota_manager_proxy_->NotifyStorageAccessed( | 82 quota_manager_proxy_->NotifyStorageAccessed( |
67 quota::QuotaClient::kFileSystem, | 83 quota::QuotaClient::kFileSystem, |
68 url.origin(), | 84 url.origin(), |
69 FileSystemTypeToQuotaStorageType(url.type())); | 85 FileSystemTypeToQuotaStorageType(url.type())); |
70 } | 86 } |
71 } | 87 } |
72 | 88 |
73 FilePath SandboxQuotaObserver::GetUsageCachePath(const FileSystemURL& url) { | 89 FilePath SandboxQuotaObserver::GetUsageCachePath(const FileSystemURL& url) { |
74 DCHECK(sandbox_file_util_); | 90 DCHECK(sandbox_file_util_); |
75 base::PlatformFileError error = base::PLATFORM_FILE_OK; | 91 base::PlatformFileError error = base::PLATFORM_FILE_OK; |
76 FilePath path = SandboxMountPointProvider::GetUsageCachePathForOriginAndType( | 92 FilePath path = SandboxMountPointProvider::GetUsageCachePathForOriginAndType( |
77 sandbox_file_util_, url.origin(), url.type(), &error); | 93 sandbox_file_util_, url.origin(), url.type(), &error); |
78 if (error != base::PLATFORM_FILE_OK) { | 94 if (error != base::PLATFORM_FILE_OK) { |
79 LOG(WARNING) << "Could not get usage cache path for: " | 95 LOG(WARNING) << "Could not get usage cache path for: " |
80 << url.DebugString(); | 96 << url.DebugString(); |
81 return FilePath(); | 97 return FilePath(); |
82 } | 98 } |
83 return path; | 99 return path; |
84 } | 100 } |
85 | 101 |
102 void SandboxQuotaObserver::ApplyPendingUsageUpdate() { | |
103 for (PendingUpdateNotificationMap::iterator itr = | |
104 pending_update_notification_.begin(); | |
105 itr != pending_update_notification_.end(); | |
106 ++itr) { | |
107 UpdateUsageCacheFile(itr->first, itr->second); | |
108 } | |
109 pending_update_notification_.clear(); | |
110 running_delayed_cache_update_ = false; | |
111 } | |
112 | |
113 void SandboxQuotaObserver::UpdateUsageCacheFile( | |
114 const FilePath& usage_file_path, | |
115 int64 delta) { | |
116 if (!usage_file_path.empty() && delta != 0) | |
kinuko
2013/01/07 06:38:25
DCHECK(!usage_file_path.empty()) ?
tzik
2013/01/07 16:00:37
Done.
| |
117 FileSystemUsageCache::AtomicUpdateUsageByDelta(usage_file_path, delta); | |
118 } | |
119 | |
86 } // namespace fileapi | 120 } // namespace fileapi |
OLD | NEW |