summaryrefslogtreecommitdiffstats
path: root/chrome/browser/file_system_proxy.cc
blob: 1cf7b5505730a367bc88eb4ab5663e92e66520be (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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
// Copyright (c) 2010 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.

#include "chrome/browser/file_system_proxy.h"

#include "base/file_util.h"
#include "chrome/browser/chrome_thread_relay.h"

namespace {

class RelayCreateOrOpen : public ChromeThreadRelay {
 public:
  RelayCreateOrOpen(
      const FilePath& file_path,
      int file_flags,
      FileSystemProxy::CreateOrOpenCallback* callback)
      : file_path_(file_path),
        file_flags_(file_flags),
        callback_(callback),
        file_handle_(base::kInvalidPlatformFileValue),
        created_(false) {
    DCHECK(callback);
  }

 protected:
  virtual ~RelayCreateOrOpen() {
    if (file_handle_ != base::kInvalidPlatformFileValue)
      FileSystemProxy::Close(file_handle_, NULL);
  }

  virtual void RunWork() {
    file_handle_ = base::CreatePlatformFile(file_path_, file_flags_, &created_);
  }

  virtual void RunCallback() {
    callback_->Run(base::PassPlatformFile(&file_handle_), created_);
    delete callback_;
  }

 private:
  FilePath file_path_;
  int file_flags_;
  FileSystemProxy::CreateOrOpenCallback* callback_;
  base::PlatformFile file_handle_;
  bool created_;
};

class RelayCreateTemporary : public ChromeThreadRelay {
 public:
  explicit RelayCreateTemporary(
      FileSystemProxy::CreateTemporaryCallback* callback)
      : callback_(callback),
        file_handle_(base::kInvalidPlatformFileValue) {
    DCHECK(callback);
  }

 protected:
  virtual ~RelayCreateTemporary() {
    if (file_handle_ != base::kInvalidPlatformFileValue)
      FileSystemProxy::Close(file_handle_, NULL);
  }

  virtual void RunWork() {
    // TODO(darin): file_util should have a variant of CreateTemporaryFile
    // that returns a FilePath and a PlatformFile.
    file_util::CreateTemporaryFile(&file_path_);

    // Use a fixed set of flags that are appropriate for writing to a temporary
    // file from the IO thread using a net::FileStream.
    int file_flags =
        base::PLATFORM_FILE_CREATE_ALWAYS |
        base::PLATFORM_FILE_WRITE |
        base::PLATFORM_FILE_ASYNC |
        base::PLATFORM_FILE_TEMPORARY;
    file_handle_ = base::CreatePlatformFile(file_path_, file_flags, NULL);
  }

  virtual void RunCallback() {
    callback_->Run(base::PassPlatformFile(&file_handle_), file_path_);
    delete callback_;
  }

 private:
  FileSystemProxy::CreateTemporaryCallback* callback_;
  base::PlatformFile file_handle_;
  FilePath file_path_;
};

class RelayWithStatusCallback : public ChromeThreadRelay {
 public:
  explicit RelayWithStatusCallback(FileSystemProxy::StatusCallback* callback)
      : callback_(callback),
        succeeded_(false) {
    // It is OK for callback to be NULL.
  }

 protected:
  virtual void RunCallback() {
    // The caller may not have been interested in the result.
    if (callback_) {
      callback_->Run(succeeded_);
      delete callback_;
    }
  }

  void SetStatus(bool succeeded) { succeeded_ = succeeded; }

 private:
  FileSystemProxy::StatusCallback* callback_;
  bool succeeded_;
};

class RelayClose : public RelayWithStatusCallback {
 public:
  RelayClose(base::PlatformFile file_handle,
             FileSystemProxy::StatusCallback* callback)
      : RelayWithStatusCallback(callback),
        file_handle_(file_handle) {
  }

 protected:
  virtual void RunWork() {
    SetStatus(base::ClosePlatformFile(file_handle_));
  }

 private:
  base::PlatformFile file_handle_;
};

class RelayDelete : public RelayWithStatusCallback {
 public:
  RelayDelete(const FilePath& file_path,
              bool recursive,
              FileSystemProxy::StatusCallback* callback)
      : RelayWithStatusCallback(callback),
        file_path_(file_path),
        recursive_(recursive) {
  }

 protected:
  virtual void RunWork() {
    SetStatus(file_util::Delete(file_path_, recursive_));
  }

 private:
  FilePath file_path_;
  bool recursive_;
};

void Start(const tracked_objects::Location& from_here,
           scoped_refptr<ChromeThreadRelay> relay) {
  relay->Start(ChromeThread::FILE, from_here);
}

}  // namespace

void FileSystemProxy::CreateOrOpen(const FilePath& file_path, int file_flags,
                                   CreateOrOpenCallback* callback) {
  Start(FROM_HERE, new RelayCreateOrOpen(file_path, file_flags, callback));
}

void FileSystemProxy::CreateTemporary(CreateTemporaryCallback* callback) {
  Start(FROM_HERE, new RelayCreateTemporary(callback));
}

void FileSystemProxy::Close(base::PlatformFile file_handle,
                            StatusCallback* callback) {
  Start(FROM_HERE, new RelayClose(file_handle, callback));
}

void FileSystemProxy::Delete(const FilePath& file_path,
                             StatusCallback* callback) {
  Start(FROM_HERE, new RelayDelete(file_path, false, callback));
}

void FileSystemProxy::RecursiveDelete(const FilePath& file_path,
                                      StatusCallback* callback) {
  Start(FROM_HERE, new RelayDelete(file_path, true, callback));
}