summaryrefslogtreecommitdiffstats
path: root/webkit/browser/fileapi/file_system_operation_context.h
blob: 866ce69f535ab7770afdc03a28379abc8dfa2405 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef WEBKIT_BROWSER_FILEAPI_FILE_SYSTEM_OPERATION_CONTEXT_H_
#define WEBKIT_BROWSER_FILEAPI_FILE_SYSTEM_OPERATION_CONTEXT_H_

#include "base/files/file_path.h"
#include "base/supports_user_data.h"
#include "base/threading/thread_checker.h"
#include "webkit/browser/fileapi/task_runner_bound_observer_list.h"
#include "webkit/browser/webkit_storage_browser_export.h"
#include "webkit/common/quota/quota_types.h"

namespace base {
class SequencedTaskRunner;
}

namespace fileapi {

class FileSystemContext;

// A context class which is carried around by FileSystemOperation and
// its delegated tasks. It is valid to reuse one context instance across
// multiple operations as far as those operations are supposed to share
// the same context (e.g. use the same task runner, share the quota etc).
// Note that the remaining quota bytes (allowed_bytes_growth) may be
// updated during the execution of write operations.
class WEBKIT_STORAGE_BROWSER_EXPORT_PRIVATE FileSystemOperationContext
    : public base::SupportsUserData {
 public:
  explicit FileSystemOperationContext(FileSystemContext* context);

  // Specifies |task_runner| which the operation is performed on.
  FileSystemOperationContext(FileSystemContext* context,
                             base::SequencedTaskRunner* task_runner);

  virtual ~FileSystemOperationContext();

  FileSystemContext* file_system_context() const {
    return file_system_context_.get();
  }

  // Updates the current remaining quota.
  // This can be called to update the remaining quota during the operation.
  void set_allowed_bytes_growth(const int64& allowed_bytes_growth) {
    allowed_bytes_growth_ = allowed_bytes_growth;
  }

  // Returns the current remaining quota.
  int64 allowed_bytes_growth() const { return allowed_bytes_growth_; }
  quota::QuotaLimitType quota_limit_type() const { return quota_limit_type_; }
  base::SequencedTaskRunner* task_runner() const { return task_runner_.get(); }
  const base::FilePath& root_path() const { return root_path_; }

  ChangeObserverList* change_observers() { return &change_observers_; }
  UpdateObserverList* update_observers() { return &update_observers_; }

  // Following setters should be called only on the same thread as the
  // FileSystemOperationContext is created (i.e. are not supposed be updated
  // after the context's passed onto other task runners).
  void set_change_observers(const ChangeObserverList& list) {
    DCHECK(setter_thread_checker_.CalledOnValidThread());
    change_observers_ = list;
  }
  void set_update_observers(const UpdateObserverList& list) {
    DCHECK(setter_thread_checker_.CalledOnValidThread());
    update_observers_ = list;
  }
  void set_quota_limit_type(quota::QuotaLimitType limit_type) {
    DCHECK(setter_thread_checker_.CalledOnValidThread());
    quota_limit_type_ = limit_type;
  }
  void set_root_path(const base::FilePath& root_path) {
    DCHECK(setter_thread_checker_.CalledOnValidThread());
    root_path_ = root_path;
  }

  // Gets and sets value-type (or not-owned) variable as UserData.
  // (SetUserValue can be called only on the same thread as this context
  // is created as well as other setters.)
  template <typename T> T GetUserValue(const char* key) const {
    ValueAdapter<T>* v = static_cast<ValueAdapter<T>*>(GetUserData(key));
    return v ? v->value() : T();
  }
  template <typename T> void SetUserValue(const char* key, const T& value) {
    DCHECK(setter_thread_checker_.CalledOnValidThread());
    SetUserData(key, new ValueAdapter<T>(value));
  }

 private:
  // An adapter for setting a value-type (or not owned) data as UserData.
  template <typename T> class ValueAdapter
      : public base::SupportsUserData::Data {
   public:
    ValueAdapter(const T& value) : value_(value) {}
    const T& value() const { return value_; }
   private:
    T value_;
    DISALLOW_COPY_AND_ASSIGN(ValueAdapter);
  };

  scoped_refptr<FileSystemContext> file_system_context_;
  scoped_refptr<base::SequencedTaskRunner> task_runner_;

  // The current remaining quota, used by ObfuscatedFileUtil.
  int64 allowed_bytes_growth_;

  // The current quota limit type, used by ObfuscatedFileUtil.
  quota::QuotaLimitType quota_limit_type_;

  // Observers attached to this context.
  ChangeObserverList change_observers_;
  UpdateObserverList update_observers_;

  // Root path for the operation, used by LocalFileUtil.
  base::FilePath root_path_;

  // Used to check its setters are not called on arbitrary thread.
  base::ThreadChecker setter_thread_checker_;

  DISALLOW_COPY_AND_ASSIGN(FileSystemOperationContext);
};

}  // namespace fileapi

#endif  // WEBKIT_BROWSER_FILEAPI_FILE_SYSTEM_OPERATION_CONTEXT_H_