| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/file_system_proxy.h" | 5 #include "base/file_util_proxy.h" |
| 6 | 6 |
| 7 #include "base/file_util.h" | 7 #include "base/file_util.h" |
| 8 #include "chrome/browser/chrome_thread_relay.h" | 8 #include "base/message_loop_proxy.h" |
| 9 | 9 |
| 10 namespace { | 10 namespace { |
| 11 | 11 |
| 12 class RelayCreateOrOpen : public ChromeThreadRelay { | 12 class MessageLoopRelay |
| 13 : public base::RefCountedThreadSafe<MessageLoopRelay> { |
| 14 public: |
| 15 MessageLoopRelay() |
| 16 : origin_message_loop_proxy_( |
| 17 base::MessageLoopProxy::CreateForCurrentThread()) { |
| 18 } |
| 19 |
| 20 void Start(scoped_refptr<base::MessageLoopProxy> message_loop_proxy, |
| 21 const tracked_objects::Location& from_here) { |
| 22 message_loop_proxy->PostTask( |
| 23 from_here, |
| 24 NewRunnableMethod(this, &MessageLoopRelay::ProcessOnTargetThread)); |
| 25 } |
| 26 |
| 27 protected: |
| 28 friend class base::RefCountedThreadSafe<MessageLoopRelay>; |
| 29 virtual ~MessageLoopRelay() {} |
| 30 |
| 31 // Called to perform work on the FILE thread. |
| 32 virtual void RunWork() = 0; |
| 33 |
| 34 // Called to notify the callback on the origin thread. |
| 35 virtual void RunCallback() = 0; |
| 36 |
| 37 private: |
| 38 void ProcessOnTargetThread() { |
| 39 RunWork(); |
| 40 origin_message_loop_proxy_->PostTask( |
| 41 FROM_HERE, |
| 42 NewRunnableMethod(this, &MessageLoopRelay::RunCallback)); |
| 43 } |
| 44 |
| 45 scoped_refptr<base::MessageLoopProxy> origin_message_loop_proxy_; |
| 46 }; |
| 47 |
| 48 class RelayCreateOrOpen : public MessageLoopRelay { |
| 13 public: | 49 public: |
| 14 RelayCreateOrOpen( | 50 RelayCreateOrOpen( |
| 51 scoped_refptr<base::MessageLoopProxy> message_loop_proxy, |
| 15 const FilePath& file_path, | 52 const FilePath& file_path, |
| 16 int file_flags, | 53 int file_flags, |
| 17 FileSystemProxy::CreateOrOpenCallback* callback) | 54 base::FileUtilProxy::CreateOrOpenCallback* callback) |
| 18 : file_path_(file_path), | 55 : message_loop_proxy_(message_loop_proxy), |
| 56 file_path_(file_path), |
| 19 file_flags_(file_flags), | 57 file_flags_(file_flags), |
| 20 callback_(callback), | 58 callback_(callback), |
| 21 file_handle_(base::kInvalidPlatformFileValue), | 59 file_handle_(base::kInvalidPlatformFileValue), |
| 22 created_(false) { | 60 created_(false) { |
| 23 DCHECK(callback); | 61 DCHECK(callback); |
| 24 } | 62 } |
| 25 | 63 |
| 26 protected: | 64 protected: |
| 27 virtual ~RelayCreateOrOpen() { | 65 virtual ~RelayCreateOrOpen() { |
| 28 if (file_handle_ != base::kInvalidPlatformFileValue) | 66 if (file_handle_ != base::kInvalidPlatformFileValue) |
| 29 FileSystemProxy::Close(file_handle_, NULL); | 67 base::FileUtilProxy::Close(message_loop_proxy_, file_handle_, NULL); |
| 30 } | 68 } |
| 31 | 69 |
| 32 virtual void RunWork() { | 70 virtual void RunWork() { |
| 33 file_handle_ = base::CreatePlatformFile(file_path_, file_flags_, &created_); | 71 file_handle_ = base::CreatePlatformFile(file_path_, file_flags_, &created_); |
| 34 } | 72 } |
| 35 | 73 |
| 36 virtual void RunCallback() { | 74 virtual void RunCallback() { |
| 37 callback_->Run(base::PassPlatformFile(&file_handle_), created_); | 75 callback_->Run(base::PassPlatformFile(&file_handle_), created_); |
| 38 delete callback_; | 76 delete callback_; |
| 39 } | 77 } |
| 40 | 78 |
| 41 private: | 79 private: |
| 80 scoped_refptr<base::MessageLoopProxy> message_loop_proxy_; |
| 42 FilePath file_path_; | 81 FilePath file_path_; |
| 43 int file_flags_; | 82 int file_flags_; |
| 44 FileSystemProxy::CreateOrOpenCallback* callback_; | 83 base::FileUtilProxy::CreateOrOpenCallback* callback_; |
| 45 base::PlatformFile file_handle_; | 84 base::PlatformFile file_handle_; |
| 46 bool created_; | 85 bool created_; |
| 47 }; | 86 }; |
| 48 | 87 |
| 49 class RelayCreateTemporary : public ChromeThreadRelay { | 88 class RelayCreateTemporary : public MessageLoopRelay { |
| 50 public: | 89 public: |
| 51 explicit RelayCreateTemporary( | 90 RelayCreateTemporary( |
| 52 FileSystemProxy::CreateTemporaryCallback* callback) | 91 scoped_refptr<base::MessageLoopProxy> message_loop_proxy, |
| 53 : callback_(callback), | 92 base::FileUtilProxy::CreateTemporaryCallback* callback) |
| 93 : message_loop_proxy_(message_loop_proxy), |
| 94 callback_(callback), |
| 54 file_handle_(base::kInvalidPlatformFileValue) { | 95 file_handle_(base::kInvalidPlatformFileValue) { |
| 55 DCHECK(callback); | 96 DCHECK(callback); |
| 56 } | 97 } |
| 57 | 98 |
| 58 protected: | 99 protected: |
| 59 virtual ~RelayCreateTemporary() { | 100 virtual ~RelayCreateTemporary() { |
| 60 if (file_handle_ != base::kInvalidPlatformFileValue) | 101 if (file_handle_ != base::kInvalidPlatformFileValue) |
| 61 FileSystemProxy::Close(file_handle_, NULL); | 102 base::FileUtilProxy::Close(message_loop_proxy_, file_handle_, NULL); |
| 62 } | 103 } |
| 63 | 104 |
| 64 virtual void RunWork() { | 105 virtual void RunWork() { |
| 65 // TODO(darin): file_util should have a variant of CreateTemporaryFile | 106 // TODO(darin): file_util should have a variant of CreateTemporaryFile |
| 66 // that returns a FilePath and a PlatformFile. | 107 // that returns a FilePath and a PlatformFile. |
| 67 file_util::CreateTemporaryFile(&file_path_); | 108 file_util::CreateTemporaryFile(&file_path_); |
| 68 | 109 |
| 69 // Use a fixed set of flags that are appropriate for writing to a temporary | 110 // Use a fixed set of flags that are appropriate for writing to a temporary |
| 70 // file from the IO thread using a net::FileStream. | 111 // file from the IO thread using a net::FileStream. |
| 71 int file_flags = | 112 int file_flags = |
| 72 base::PLATFORM_FILE_CREATE_ALWAYS | | 113 base::PLATFORM_FILE_CREATE_ALWAYS | |
| 73 base::PLATFORM_FILE_WRITE | | 114 base::PLATFORM_FILE_WRITE | |
| 74 base::PLATFORM_FILE_ASYNC | | 115 base::PLATFORM_FILE_ASYNC | |
| 75 base::PLATFORM_FILE_TEMPORARY; | 116 base::PLATFORM_FILE_TEMPORARY; |
| 76 file_handle_ = base::CreatePlatformFile(file_path_, file_flags, NULL); | 117 file_handle_ = base::CreatePlatformFile(file_path_, file_flags, NULL); |
| 77 } | 118 } |
| 78 | 119 |
| 79 virtual void RunCallback() { | 120 virtual void RunCallback() { |
| 80 callback_->Run(base::PassPlatformFile(&file_handle_), file_path_); | 121 callback_->Run(base::PassPlatformFile(&file_handle_), file_path_); |
| 81 delete callback_; | 122 delete callback_; |
| 82 } | 123 } |
| 83 | 124 |
| 84 private: | 125 private: |
| 85 FileSystemProxy::CreateTemporaryCallback* callback_; | 126 scoped_refptr<base::MessageLoopProxy> message_loop_proxy_; |
| 127 base::FileUtilProxy::CreateTemporaryCallback* callback_; |
| 86 base::PlatformFile file_handle_; | 128 base::PlatformFile file_handle_; |
| 87 FilePath file_path_; | 129 FilePath file_path_; |
| 88 }; | 130 }; |
| 89 | 131 |
| 90 class RelayWithStatusCallback : public ChromeThreadRelay { | 132 class RelayWithStatusCallback : public MessageLoopRelay { |
| 91 public: | 133 public: |
| 92 explicit RelayWithStatusCallback(FileSystemProxy::StatusCallback* callback) | 134 explicit RelayWithStatusCallback( |
| 135 base::FileUtilProxy::StatusCallback* callback) |
| 93 : callback_(callback), | 136 : callback_(callback), |
| 94 succeeded_(false) { | 137 succeeded_(false) { |
| 95 // It is OK for callback to be NULL. | 138 // It is OK for callback to be NULL. |
| 96 } | 139 } |
| 97 | 140 |
| 98 protected: | 141 protected: |
| 99 virtual void RunCallback() { | 142 virtual void RunCallback() { |
| 100 // The caller may not have been interested in the result. | 143 // The caller may not have been interested in the result. |
| 101 if (callback_) { | 144 if (callback_) { |
| 102 callback_->Run(succeeded_); | 145 callback_->Run(succeeded_); |
| 103 delete callback_; | 146 delete callback_; |
| 104 } | 147 } |
| 105 } | 148 } |
| 106 | 149 |
| 107 void SetStatus(bool succeeded) { succeeded_ = succeeded; } | 150 void SetStatus(bool succeeded) { succeeded_ = succeeded; } |
| 108 | 151 |
| 109 private: | 152 private: |
| 110 FileSystemProxy::StatusCallback* callback_; | 153 base::FileUtilProxy::StatusCallback* callback_; |
| 111 bool succeeded_; | 154 bool succeeded_; |
| 112 }; | 155 }; |
| 113 | 156 |
| 114 class RelayClose : public RelayWithStatusCallback { | 157 class RelayClose : public RelayWithStatusCallback { |
| 115 public: | 158 public: |
| 116 RelayClose(base::PlatformFile file_handle, | 159 RelayClose(base::PlatformFile file_handle, |
| 117 FileSystemProxy::StatusCallback* callback) | 160 base::FileUtilProxy::StatusCallback* callback) |
| 118 : RelayWithStatusCallback(callback), | 161 : RelayWithStatusCallback(callback), |
| 119 file_handle_(file_handle) { | 162 file_handle_(file_handle) { |
| 120 } | 163 } |
| 121 | 164 |
| 122 protected: | 165 protected: |
| 123 virtual void RunWork() { | 166 virtual void RunWork() { |
| 124 SetStatus(base::ClosePlatformFile(file_handle_)); | 167 SetStatus(base::ClosePlatformFile(file_handle_)); |
| 125 } | 168 } |
| 126 | 169 |
| 127 private: | 170 private: |
| 128 base::PlatformFile file_handle_; | 171 base::PlatformFile file_handle_; |
| 129 }; | 172 }; |
| 130 | 173 |
| 131 class RelayDelete : public RelayWithStatusCallback { | 174 class RelayDelete : public RelayWithStatusCallback { |
| 132 public: | 175 public: |
| 133 RelayDelete(const FilePath& file_path, | 176 RelayDelete(const FilePath& file_path, |
| 134 bool recursive, | 177 bool recursive, |
| 135 FileSystemProxy::StatusCallback* callback) | 178 base::FileUtilProxy::StatusCallback* callback) |
| 136 : RelayWithStatusCallback(callback), | 179 : RelayWithStatusCallback(callback), |
| 137 file_path_(file_path), | 180 file_path_(file_path), |
| 138 recursive_(recursive) { | 181 recursive_(recursive) { |
| 139 } | 182 } |
| 140 | 183 |
| 141 protected: | 184 protected: |
| 142 virtual void RunWork() { | 185 virtual void RunWork() { |
| 143 SetStatus(file_util::Delete(file_path_, recursive_)); | 186 SetStatus(file_util::Delete(file_path_, recursive_)); |
| 144 } | 187 } |
| 145 | 188 |
| 146 private: | 189 private: |
| 147 FilePath file_path_; | 190 FilePath file_path_; |
| 148 bool recursive_; | 191 bool recursive_; |
| 149 }; | 192 }; |
| 150 | 193 |
| 151 void Start(const tracked_objects::Location& from_here, | 194 void Start(const tracked_objects::Location& from_here, |
| 152 scoped_refptr<ChromeThreadRelay> relay) { | 195 scoped_refptr<base::MessageLoopProxy> message_loop_proxy, |
| 153 relay->Start(ChromeThread::FILE, from_here); | 196 scoped_refptr<MessageLoopRelay> relay) { |
| 197 relay->Start(message_loop_proxy, from_here); |
| 154 } | 198 } |
| 155 | 199 |
| 156 } // namespace | 200 } // namespace |
| 157 | 201 |
| 158 void FileSystemProxy::CreateOrOpen(const FilePath& file_path, int file_flags, | 202 namespace base { |
| 159 CreateOrOpenCallback* callback) { | 203 |
| 160 Start(FROM_HERE, new RelayCreateOrOpen(file_path, file_flags, callback)); | 204 // static |
| 205 void FileUtilProxy::CreateOrOpen( |
| 206 scoped_refptr<MessageLoopProxy> message_loop_proxy, |
| 207 const FilePath& file_path, int file_flags, |
| 208 CreateOrOpenCallback* callback) { |
| 209 Start(FROM_HERE, message_loop_proxy, new RelayCreateOrOpen( |
| 210 message_loop_proxy, file_path, file_flags, callback)); |
| 161 } | 211 } |
| 162 | 212 |
| 163 void FileSystemProxy::CreateTemporary(CreateTemporaryCallback* callback) { | 213 // static |
| 164 Start(FROM_HERE, new RelayCreateTemporary(callback)); | 214 void FileUtilProxy::CreateTemporary( |
| 215 scoped_refptr<MessageLoopProxy> message_loop_proxy, |
| 216 CreateTemporaryCallback* callback) { |
| 217 Start(FROM_HERE, message_loop_proxy, |
| 218 new RelayCreateTemporary(message_loop_proxy, callback)); |
| 165 } | 219 } |
| 166 | 220 |
| 167 void FileSystemProxy::Close(base::PlatformFile file_handle, | 221 // static |
| 222 void FileUtilProxy::Close(scoped_refptr<MessageLoopProxy> message_loop_proxy, |
| 223 base::PlatformFile file_handle, |
| 168 StatusCallback* callback) { | 224 StatusCallback* callback) { |
| 169 Start(FROM_HERE, new RelayClose(file_handle, callback)); | 225 Start(FROM_HERE, message_loop_proxy, new RelayClose(file_handle, callback)); |
| 170 } | 226 } |
| 171 | 227 |
| 172 void FileSystemProxy::Delete(const FilePath& file_path, | 228 // static |
| 229 void FileUtilProxy::Delete(scoped_refptr<MessageLoopProxy> message_loop_proxy, |
| 230 const FilePath& file_path, |
| 173 StatusCallback* callback) { | 231 StatusCallback* callback) { |
| 174 Start(FROM_HERE, new RelayDelete(file_path, false, callback)); | 232 Start(FROM_HERE, message_loop_proxy, |
| 233 new RelayDelete(file_path, false, callback)); |
| 175 } | 234 } |
| 176 | 235 |
| 177 void FileSystemProxy::RecursiveDelete(const FilePath& file_path, | 236 // static |
| 178 StatusCallback* callback) { | 237 void FileUtilProxy::RecursiveDelete( |
| 179 Start(FROM_HERE, new RelayDelete(file_path, true, callback)); | 238 scoped_refptr<MessageLoopProxy> message_loop_proxy, |
| 239 const FilePath& file_path, |
| 240 StatusCallback* callback) { |
| 241 Start(FROM_HERE, message_loop_proxy, |
| 242 new RelayDelete(file_path, true, callback)); |
| 180 } | 243 } |
| 244 |
| 245 } // namespace base |
| OLD | NEW |