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 "base/file_util_proxy.h" | 5 #include "base/file_util_proxy.h" |
6 | 6 |
7 #include "base/message_loop_proxy.h" | 7 #include "base/message_loop_proxy.h" |
8 | 8 |
9 // TODO(jianli): Move the code from anonymous namespace to base namespace so | 9 // TODO(jianli): Move the code from anonymous namespace to base namespace so |
10 // that all of the base:: prefixes would be unnecessary. | 10 // that all of the base:: prefixes would be unnecessary. |
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
112 scoped_refptr<base::MessageLoopProxy> origin_message_loop_proxy_; | 112 scoped_refptr<base::MessageLoopProxy> origin_message_loop_proxy_; |
113 base::PlatformFileError error_code_; | 113 base::PlatformFileError error_code_; |
114 }; | 114 }; |
115 | 115 |
116 class RelayCreateOrOpen : public MessageLoopRelay { | 116 class RelayCreateOrOpen : public MessageLoopRelay { |
117 public: | 117 public: |
118 RelayCreateOrOpen( | 118 RelayCreateOrOpen( |
119 scoped_refptr<base::MessageLoopProxy> message_loop_proxy, | 119 scoped_refptr<base::MessageLoopProxy> message_loop_proxy, |
120 const FilePath& file_path, | 120 const FilePath& file_path, |
121 int file_flags, | 121 int file_flags, |
| 122 bool return_no_handle, |
122 base::FileUtilProxy::CreateOrOpenCallback* callback) | 123 base::FileUtilProxy::CreateOrOpenCallback* callback) |
123 : message_loop_proxy_(message_loop_proxy), | 124 : message_loop_proxy_(message_loop_proxy), |
124 file_path_(file_path), | 125 file_path_(file_path), |
125 file_flags_(file_flags), | 126 file_flags_(file_flags), |
126 callback_(callback), | 127 callback_(callback), |
127 file_handle_(base::kInvalidPlatformFileValue), | 128 file_handle_(base::kInvalidPlatformFileValue), |
| 129 return_no_handle_(return_no_handle), |
128 created_(false) { | 130 created_(false) { |
129 DCHECK(callback); | 131 DCHECK(callback); |
130 } | 132 } |
131 | 133 |
132 protected: | 134 protected: |
133 virtual ~RelayCreateOrOpen() { | 135 virtual ~RelayCreateOrOpen() { |
134 if (file_handle_ != base::kInvalidPlatformFileValue) | 136 if (file_handle_ != base::kInvalidPlatformFileValue) |
135 base::FileUtilProxy::Close(message_loop_proxy_, file_handle_, NULL); | 137 base::FileUtilProxy::Close(message_loop_proxy_, file_handle_, NULL); |
136 } | 138 } |
137 | 139 |
138 virtual void RunWork() { | 140 virtual void RunWork() { |
139 base::PlatformFileError error_code = base::PLATFORM_FILE_OK; | 141 base::PlatformFileError error_code = base::PLATFORM_FILE_OK; |
140 file_handle_ = base::CreatePlatformFile(file_path_, file_flags_, | 142 file_handle_ = base::CreatePlatformFile(file_path_, file_flags_, |
141 &created_, &error_code); | 143 &created_, &error_code); |
| 144 // If the return_no_handle is true the caller is not interested |
| 145 // in the file_handle_. Close it right now. |
| 146 if (return_no_handle_ && file_handle_ != base::kInvalidPlatformFileValue) { |
| 147 // We don't check the return value here. |
| 148 base::ClosePlatformFile(file_handle_); |
| 149 file_handle_ = base::kInvalidPlatformFileValue; |
| 150 } |
142 set_error_code(error_code); | 151 set_error_code(error_code); |
143 } | 152 } |
144 | 153 |
145 virtual void RunCallback() { | 154 virtual void RunCallback() { |
146 callback_->Run(error_code(), base::PassPlatformFile(&file_handle_), | 155 callback_->Run(error_code(), base::PassPlatformFile(&file_handle_), |
147 created_); | 156 created_); |
148 delete callback_; | 157 delete callback_; |
149 } | 158 } |
150 | 159 |
151 private: | 160 private: |
152 scoped_refptr<base::MessageLoopProxy> message_loop_proxy_; | 161 scoped_refptr<base::MessageLoopProxy> message_loop_proxy_; |
153 FilePath file_path_; | 162 FilePath file_path_; |
154 int file_flags_; | 163 int file_flags_; |
155 base::FileUtilProxy::CreateOrOpenCallback* callback_; | 164 base::FileUtilProxy::CreateOrOpenCallback* callback_; |
156 base::PlatformFile file_handle_; | 165 base::PlatformFile file_handle_; |
| 166 bool return_no_handle_; |
157 bool created_; | 167 bool created_; |
158 }; | 168 }; |
159 | 169 |
160 class RelayCreateTemporary : public MessageLoopRelay { | 170 class RelayCreateTemporary : public MessageLoopRelay { |
161 public: | 171 public: |
162 RelayCreateTemporary( | 172 RelayCreateTemporary( |
163 scoped_refptr<base::MessageLoopProxy> message_loop_proxy, | 173 scoped_refptr<base::MessageLoopProxy> message_loop_proxy, |
164 base::FileUtilProxy::CreateTemporaryCallback* callback) | 174 base::FileUtilProxy::CreateTemporaryCallback* callback) |
165 : message_loop_proxy_(message_loop_proxy), | 175 : message_loop_proxy_(message_loop_proxy), |
166 callback_(callback), | 176 callback_(callback), |
(...skipping 506 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
673 } // namespace | 683 } // namespace |
674 | 684 |
675 namespace base { | 685 namespace base { |
676 | 686 |
677 // static | 687 // static |
678 bool FileUtilProxy::CreateOrOpen( | 688 bool FileUtilProxy::CreateOrOpen( |
679 scoped_refptr<MessageLoopProxy> message_loop_proxy, | 689 scoped_refptr<MessageLoopProxy> message_loop_proxy, |
680 const FilePath& file_path, int file_flags, | 690 const FilePath& file_path, int file_flags, |
681 CreateOrOpenCallback* callback) { | 691 CreateOrOpenCallback* callback) { |
682 return Start(FROM_HERE, message_loop_proxy, new RelayCreateOrOpen( | 692 return Start(FROM_HERE, message_loop_proxy, new RelayCreateOrOpen( |
683 message_loop_proxy, file_path, file_flags, callback)); | 693 message_loop_proxy, file_path, file_flags, false /* return_no_handle */, |
| 694 callback)); |
684 } | 695 } |
685 | 696 |
686 // static | 697 // static |
| 698 bool FileUtilProxy::Create( |
| 699 scoped_refptr<MessageLoopProxy> message_loop_proxy, |
| 700 const FilePath& file_path, int file_flags, |
| 701 CreateOrOpenCallback* callback) { |
| 702 return Start(FROM_HERE, message_loop_proxy, new RelayCreateOrOpen( |
| 703 message_loop_proxy, file_path, file_flags, true /* return_no_handle */, |
| 704 callback)); |
| 705 } |
| 706 |
| 707 // static |
687 bool FileUtilProxy::CreateTemporary( | 708 bool FileUtilProxy::CreateTemporary( |
688 scoped_refptr<MessageLoopProxy> message_loop_proxy, | 709 scoped_refptr<MessageLoopProxy> message_loop_proxy, |
689 CreateTemporaryCallback* callback) { | 710 CreateTemporaryCallback* callback) { |
690 return Start(FROM_HERE, message_loop_proxy, | 711 return Start(FROM_HERE, message_loop_proxy, |
691 new RelayCreateTemporary(message_loop_proxy, callback)); | 712 new RelayCreateTemporary(message_loop_proxy, callback)); |
692 } | 713 } |
693 | 714 |
694 // static | 715 // static |
695 bool FileUtilProxy::CreateDirectory( | 716 bool FileUtilProxy::CreateDirectory( |
696 scoped_refptr<MessageLoopProxy> message_loop_proxy, | 717 scoped_refptr<MessageLoopProxy> message_loop_proxy, |
(...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
844 | 865 |
845 // static | 866 // static |
846 bool FileUtilProxy::Flush( | 867 bool FileUtilProxy::Flush( |
847 scoped_refptr<MessageLoopProxy> message_loop_proxy, | 868 scoped_refptr<MessageLoopProxy> message_loop_proxy, |
848 PlatformFile file, | 869 PlatformFile file, |
849 StatusCallback* callback) { | 870 StatusCallback* callback) { |
850 return Start(FROM_HERE, message_loop_proxy, new RelayFlush(file, callback)); | 871 return Start(FROM_HERE, message_loop_proxy, new RelayFlush(file, callback)); |
851 } | 872 } |
852 | 873 |
853 } // namespace base | 874 } // namespace base |
OLD | NEW |