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.
4
5#ifndef STORAGE_BROWSER_FILEAPI_SANDBOX_QUOTA_OBSERVER_H_
6#define STORAGE_BROWSER_FILEAPI_SANDBOX_QUOTA_OBSERVER_H_
7
8#include <map>
9
10#include "base/basictypes.h"
11#include "base/compiler_specific.h"
12#include "base/files/file_path.h"
13#include "base/memory/ref_counted.h"
14#include "base/memory/scoped_ptr.h"
15#include "base/memory/weak_ptr.h"
16#include "storage/browser/fileapi/file_observers.h"
17#include "storage/browser/fileapi/file_system_url.h"
18
19namespace base {
20class SequencedTaskRunner;
21}
22
23namespace storage {
24class QuotaManagerProxy;
25}
26
27namespace storage {
28
29class FileSystemUsageCache;
30class FileSystemURL;
31class TimedTaskHelper;
32class ObfuscatedFileUtil;
33
34class SandboxQuotaObserver
35    : public FileUpdateObserver,
36      public FileAccessObserver {
37 public:
38  typedef std::map<base::FilePath, int64> PendingUpdateNotificationMap;
39
40  SandboxQuotaObserver(storage::QuotaManagerProxy* quota_manager_proxy,
41                       base::SequencedTaskRunner* update_notify_runner,
42                       ObfuscatedFileUtil* sandbox_file_util,
43                       FileSystemUsageCache* file_system_usage_cache_);
44  virtual ~SandboxQuotaObserver();
45
46  // FileUpdateObserver overrides.
47  virtual void OnStartUpdate(const FileSystemURL& url) OVERRIDE;
48  virtual void OnUpdate(const FileSystemURL& url, int64 delta) OVERRIDE;
49  virtual void OnEndUpdate(const FileSystemURL& url) OVERRIDE;
50
51  // FileAccessObserver overrides.
52  virtual void OnAccess(const FileSystemURL& url) OVERRIDE;
53
54  void SetUsageCacheEnabled(const GURL& origin,
55                            FileSystemType type,
56                            bool enabled);
57
58 private:
59  void ApplyPendingUsageUpdate();
60  void UpdateUsageCacheFile(const base::FilePath& usage_file_path, int64 delta);
61
62  base::FilePath GetUsageCachePath(const FileSystemURL& url);
63
64  scoped_refptr<storage::QuotaManagerProxy> quota_manager_proxy_;
65  scoped_refptr<base::SequencedTaskRunner> update_notify_runner_;
66
67  // Not owned; sandbox_file_util_ should have identical lifetime with this.
68  ObfuscatedFileUtil* sandbox_file_util_;
69
70  // Not owned; file_system_usage_cache_ should have longer lifetime than this.
71  FileSystemUsageCache* file_system_usage_cache_;
72
73  PendingUpdateNotificationMap pending_update_notification_;
74  scoped_ptr<TimedTaskHelper> delayed_cache_update_helper_;
75
76  DISALLOW_COPY_AND_ASSIGN(SandboxQuotaObserver);
77};
78
79}  // namespace storage
80
81#endif  // STORAGE_BROWSER_FILEAPI_SANDBOX_QUOTA_OBSERVER_H_
82