OLD | NEW |
| (Empty) |
1 // Copyright (c) 2010 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 #include "chrome/browser/file_system_proxy.h" | |
6 | |
7 #include "base/file_util.h" | |
8 #include "chrome/browser/chrome_thread_relay.h" | |
9 | |
10 namespace { | |
11 | |
12 class RelayCreateOrOpen : public ChromeThreadRelay { | |
13 public: | |
14 RelayCreateOrOpen( | |
15 const FilePath& file_path, | |
16 int file_flags, | |
17 FileSystemProxy::CreateOrOpenCallback* callback) | |
18 : file_path_(file_path), | |
19 file_flags_(file_flags), | |
20 callback_(callback), | |
21 file_handle_(base::kInvalidPlatformFileValue), | |
22 created_(false) { | |
23 DCHECK(callback); | |
24 } | |
25 | |
26 protected: | |
27 virtual ~RelayCreateOrOpen() { | |
28 if (file_handle_ != base::kInvalidPlatformFileValue) | |
29 FileSystemProxy::Close(file_handle_, NULL); | |
30 } | |
31 | |
32 virtual void RunWork() { | |
33 file_handle_ = base::CreatePlatformFile(file_path_, file_flags_, &created_); | |
34 } | |
35 | |
36 virtual void RunCallback() { | |
37 callback_->Run(base::PassPlatformFile(&file_handle_), created_); | |
38 delete callback_; | |
39 } | |
40 | |
41 private: | |
42 FilePath file_path_; | |
43 int file_flags_; | |
44 FileSystemProxy::CreateOrOpenCallback* callback_; | |
45 base::PlatformFile file_handle_; | |
46 bool created_; | |
47 }; | |
48 | |
49 class RelayCreateTemporary : public ChromeThreadRelay { | |
50 public: | |
51 explicit RelayCreateTemporary( | |
52 FileSystemProxy::CreateTemporaryCallback* callback) | |
53 : callback_(callback), | |
54 file_handle_(base::kInvalidPlatformFileValue) { | |
55 DCHECK(callback); | |
56 } | |
57 | |
58 protected: | |
59 virtual ~RelayCreateTemporary() { | |
60 if (file_handle_ != base::kInvalidPlatformFileValue) | |
61 FileSystemProxy::Close(file_handle_, NULL); | |
62 } | |
63 | |
64 virtual void RunWork() { | |
65 // TODO(darin): file_util should have a variant of CreateTemporaryFile | |
66 // that returns a FilePath and a PlatformFile. | |
67 file_util::CreateTemporaryFile(&file_path_); | |
68 | |
69 // Use a fixed set of flags that are appropriate for writing to a temporary | |
70 // file from the IO thread using a net::FileStream. | |
71 int file_flags = | |
72 base::PLATFORM_FILE_CREATE_ALWAYS | | |
73 base::PLATFORM_FILE_WRITE | | |
74 base::PLATFORM_FILE_ASYNC | | |
75 base::PLATFORM_FILE_TEMPORARY; | |
76 file_handle_ = base::CreatePlatformFile(file_path_, file_flags, NULL); | |
77 } | |
78 | |
79 virtual void RunCallback() { | |
80 callback_->Run(base::PassPlatformFile(&file_handle_), file_path_); | |
81 delete callback_; | |
82 } | |
83 | |
84 private: | |
85 FileSystemProxy::CreateTemporaryCallback* callback_; | |
86 base::PlatformFile file_handle_; | |
87 FilePath file_path_; | |
88 }; | |
89 | |
90 class RelayWithStatusCallback : public ChromeThreadRelay { | |
91 public: | |
92 explicit RelayWithStatusCallback(FileSystemProxy::StatusCallback* callback) | |
93 : callback_(callback), | |
94 succeeded_(false) { | |
95 // It is OK for callback to be NULL. | |
96 } | |
97 | |
98 protected: | |
99 virtual void RunCallback() { | |
100 // The caller may not have been interested in the result. | |
101 if (callback_) { | |
102 callback_->Run(succeeded_); | |
103 delete callback_; | |
104 } | |
105 } | |
106 | |
107 void SetStatus(bool succeeded) { succeeded_ = succeeded; } | |
108 | |
109 private: | |
110 FileSystemProxy::StatusCallback* callback_; | |
111 bool succeeded_; | |
112 }; | |
113 | |
114 class RelayClose : public RelayWithStatusCallback { | |
115 public: | |
116 RelayClose(base::PlatformFile file_handle, | |
117 FileSystemProxy::StatusCallback* callback) | |
118 : RelayWithStatusCallback(callback), | |
119 file_handle_(file_handle) { | |
120 } | |
121 | |
122 protected: | |
123 virtual void RunWork() { | |
124 SetStatus(base::ClosePlatformFile(file_handle_)); | |
125 } | |
126 | |
127 private: | |
128 base::PlatformFile file_handle_; | |
129 }; | |
130 | |
131 class RelayDelete : public RelayWithStatusCallback { | |
132 public: | |
133 RelayDelete(const FilePath& file_path, | |
134 bool recursive, | |
135 FileSystemProxy::StatusCallback* callback) | |
136 : RelayWithStatusCallback(callback), | |
137 file_path_(file_path), | |
138 recursive_(recursive) { | |
139 } | |
140 | |
141 protected: | |
142 virtual void RunWork() { | |
143 SetStatus(file_util::Delete(file_path_, recursive_)); | |
144 } | |
145 | |
146 private: | |
147 FilePath file_path_; | |
148 bool recursive_; | |
149 }; | |
150 | |
151 void Start(const tracked_objects::Location& from_here, | |
152 scoped_refptr<ChromeThreadRelay> relay) { | |
153 relay->Start(ChromeThread::FILE, from_here); | |
154 } | |
155 | |
156 } // namespace | |
157 | |
158 void FileSystemProxy::CreateOrOpen(const FilePath& file_path, int file_flags, | |
159 CreateOrOpenCallback* callback) { | |
160 Start(FROM_HERE, new RelayCreateOrOpen(file_path, file_flags, callback)); | |
161 } | |
162 | |
163 void FileSystemProxy::CreateTemporary(CreateTemporaryCallback* callback) { | |
164 Start(FROM_HERE, new RelayCreateTemporary(callback)); | |
165 } | |
166 | |
167 void FileSystemProxy::Close(base::PlatformFile file_handle, | |
168 StatusCallback* callback) { | |
169 Start(FROM_HERE, new RelayClose(file_handle, callback)); | |
170 } | |
171 | |
172 void FileSystemProxy::Delete(const FilePath& file_path, | |
173 StatusCallback* callback) { | |
174 Start(FROM_HERE, new RelayDelete(file_path, false, callback)); | |
175 } | |
176 | |
177 void FileSystemProxy::RecursiveDelete(const FilePath& file_path, | |
178 StatusCallback* callback) { | |
179 Start(FROM_HERE, new RelayDelete(file_path, true, callback)); | |
180 } | |
OLD | NEW |