blob: 4d1751084821543e8968e245eaec4273175606e1 [file] [log] [blame]
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +01001// 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 WEBKIT_BROWSER_FILEAPI_FILE_SYSTEM_OPERATION_CONTEXT_H_
6#define WEBKIT_BROWSER_FILEAPI_FILE_SYSTEM_OPERATION_CONTEXT_H_
7
8#include "base/files/file_path.h"
9#include "base/supports_user_data.h"
10#include "base/threading/thread_checker.h"
11#include "webkit/browser/fileapi/task_runner_bound_observer_list.h"
Torne (Richard Coles)7d4cd472013-06-19 11:58:07 +010012#include "webkit/browser/webkit_storage_browser_export.h"
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +010013#include "webkit/common/quota/quota_types.h"
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +010014
15namespace base {
16class SequencedTaskRunner;
17}
18
19namespace fileapi {
20
21class FileSystemContext;
22
23// A context class which is carried around by FileSystemOperation and
24// its delegated tasks. It is valid to reuse one context instance across
25// multiple operations as far as those operations are supposed to share
26// the same context (e.g. use the same task runner, share the quota etc).
27// Note that the remaining quota bytes (allowed_bytes_growth) may be
28// updated during the execution of write operations.
Torne (Richard Coles)7d4cd472013-06-19 11:58:07 +010029class WEBKIT_STORAGE_BROWSER_EXPORT_PRIVATE FileSystemOperationContext
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +010030 : public base::SupportsUserData {
31 public:
32 explicit FileSystemOperationContext(FileSystemContext* context);
33
34 // Specifies |task_runner| which the operation is performed on.
Ben Murdoch7dbb3d52013-07-17 14:55:54 +010035 // The backend of |task_runner| must outlive the IO thread.
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +010036 FileSystemOperationContext(FileSystemContext* context,
37 base::SequencedTaskRunner* task_runner);
38
39 virtual ~FileSystemOperationContext();
40
41 FileSystemContext* file_system_context() const {
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +010042 return file_system_context_.get();
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +010043 }
44
45 // Updates the current remaining quota.
46 // This can be called to update the remaining quota during the operation.
47 void set_allowed_bytes_growth(const int64& allowed_bytes_growth) {
48 allowed_bytes_growth_ = allowed_bytes_growth;
49 }
50
51 // Returns the current remaining quota.
52 int64 allowed_bytes_growth() const { return allowed_bytes_growth_; }
53 quota::QuotaLimitType quota_limit_type() const { return quota_limit_type_; }
54 base::SequencedTaskRunner* task_runner() const { return task_runner_.get(); }
55 const base::FilePath& root_path() const { return root_path_; }
56
57 ChangeObserverList* change_observers() { return &change_observers_; }
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +010058 UpdateObserverList* update_observers() { return &update_observers_; }
59
60 // Following setters should be called only on the same thread as the
61 // FileSystemOperationContext is created (i.e. are not supposed be updated
62 // after the context's passed onto other task runners).
63 void set_change_observers(const ChangeObserverList& list) {
64 DCHECK(setter_thread_checker_.CalledOnValidThread());
65 change_observers_ = list;
66 }
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +010067 void set_update_observers(const UpdateObserverList& list) {
68 DCHECK(setter_thread_checker_.CalledOnValidThread());
69 update_observers_ = list;
70 }
71 void set_quota_limit_type(quota::QuotaLimitType limit_type) {
72 DCHECK(setter_thread_checker_.CalledOnValidThread());
73 quota_limit_type_ = limit_type;
74 }
75 void set_root_path(const base::FilePath& root_path) {
76 DCHECK(setter_thread_checker_.CalledOnValidThread());
77 root_path_ = root_path;
78 }
79
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +010080 private:
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +010081 scoped_refptr<FileSystemContext> file_system_context_;
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +010082 scoped_refptr<base::SequencedTaskRunner> task_runner_;
83
84 // The current remaining quota, used by ObfuscatedFileUtil.
85 int64 allowed_bytes_growth_;
86
87 // The current quota limit type, used by ObfuscatedFileUtil.
88 quota::QuotaLimitType quota_limit_type_;
89
90 // Observers attached to this context.
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +010091 ChangeObserverList change_observers_;
92 UpdateObserverList update_observers_;
93
94 // Root path for the operation, used by LocalFileUtil.
95 base::FilePath root_path_;
96
97 // Used to check its setters are not called on arbitrary thread.
98 base::ThreadChecker setter_thread_checker_;
99
100 DISALLOW_COPY_AND_ASSIGN(FileSystemOperationContext);
101};
102
103} // namespace fileapi
104
105#endif // WEBKIT_BROWSER_FILEAPI_FILE_SYSTEM_OPERATION_CONTEXT_H_