file_stream_context.cc 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. // Copyright (c) 2012 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. #include "net/base/file_stream_context.h"
  5. #include <utility>
  6. #include "base/bind.h"
  7. #include "base/files/file_path.h"
  8. #include "base/location.h"
  9. #include "base/logging.h"
  10. #include "base/task/task_runner.h"
  11. #include "base/task/task_runner_util.h"
  12. #include "base/threading/thread_restrictions.h"
  13. #include "base/values.h"
  14. #include "build/build_config.h"
  15. #include "net/base/net_errors.h"
  16. #if BUILDFLAG(IS_ANDROID)
  17. #include "base/android/content_uri_utils.h"
  18. #endif
  19. namespace net {
  20. namespace {
  21. void CallInt64ToInt(CompletionOnceCallback callback, int64_t result) {
  22. std::move(callback).Run(static_cast<int>(result));
  23. }
  24. } // namespace
  25. FileStream::Context::IOResult::IOResult()
  26. : result(OK),
  27. os_error(0) {
  28. }
  29. FileStream::Context::IOResult::IOResult(int64_t result,
  30. logging::SystemErrorCode os_error)
  31. : result(result), os_error(os_error) {
  32. }
  33. // static
  34. FileStream::Context::IOResult FileStream::Context::IOResult::FromOSError(
  35. logging::SystemErrorCode os_error) {
  36. return IOResult(MapSystemError(os_error), os_error);
  37. }
  38. // ---------------------------------------------------------------------
  39. FileStream::Context::OpenResult::OpenResult() = default;
  40. FileStream::Context::OpenResult::OpenResult(base::File file,
  41. IOResult error_code)
  42. : file(std::move(file)), error_code(error_code) {}
  43. FileStream::Context::OpenResult::OpenResult(OpenResult&& other)
  44. : file(std::move(other.file)), error_code(other.error_code) {}
  45. FileStream::Context::OpenResult& FileStream::Context::OpenResult::operator=(
  46. OpenResult&& other) {
  47. file = std::move(other.file);
  48. error_code = other.error_code;
  49. return *this;
  50. }
  51. // ---------------------------------------------------------------------
  52. void FileStream::Context::Orphan() {
  53. DCHECK(!orphaned_);
  54. orphaned_ = true;
  55. if (!async_in_progress_) {
  56. CloseAndDelete();
  57. } else if (file_.IsValid()) {
  58. #if BUILDFLAG(IS_WIN)
  59. CancelIo(file_.GetPlatformFile());
  60. #endif
  61. }
  62. }
  63. void FileStream::Context::Open(const base::FilePath& path,
  64. int open_flags,
  65. CompletionOnceCallback callback) {
  66. DCHECK(!async_in_progress_);
  67. bool posted = base::PostTaskAndReplyWithResult(
  68. task_runner_.get(), FROM_HERE,
  69. base::BindOnce(&Context::OpenFileImpl, base::Unretained(this), path,
  70. open_flags),
  71. base::BindOnce(&Context::OnOpenCompleted, base::Unretained(this),
  72. std::move(callback)));
  73. DCHECK(posted);
  74. async_in_progress_ = true;
  75. }
  76. void FileStream::Context::Close(CompletionOnceCallback callback) {
  77. DCHECK(!async_in_progress_);
  78. bool posted = base::PostTaskAndReplyWithResult(
  79. task_runner_.get(), FROM_HERE,
  80. base::BindOnce(&Context::CloseFileImpl, base::Unretained(this)),
  81. base::BindOnce(&Context::OnAsyncCompleted, base::Unretained(this),
  82. IntToInt64(std::move(callback))));
  83. DCHECK(posted);
  84. async_in_progress_ = true;
  85. }
  86. void FileStream::Context::Seek(int64_t offset,
  87. Int64CompletionOnceCallback callback) {
  88. DCHECK(!async_in_progress_);
  89. if (offset < 0) {
  90. std::move(callback).Run(net::ERR_INVALID_ARGUMENT);
  91. return;
  92. }
  93. bool posted = base::PostTaskAndReplyWithResult(
  94. task_runner_.get(), FROM_HERE,
  95. base::BindOnce(&Context::SeekFileImpl, base::Unretained(this), offset),
  96. base::BindOnce(&Context::OnAsyncCompleted, base::Unretained(this),
  97. std::move(callback)));
  98. DCHECK(posted);
  99. async_in_progress_ = true;
  100. }
  101. void FileStream::Context::GetFileInfo(base::File::Info* file_info,
  102. CompletionOnceCallback callback) {
  103. base::PostTaskAndReplyWithResult(
  104. task_runner_.get(), FROM_HERE,
  105. base::BindOnce(&Context::GetFileInfoImpl, base::Unretained(this),
  106. base::Unretained(file_info)),
  107. base::BindOnce(&Context::OnAsyncCompleted, base::Unretained(this),
  108. IntToInt64(std::move(callback))));
  109. async_in_progress_ = true;
  110. }
  111. void FileStream::Context::Flush(CompletionOnceCallback callback) {
  112. DCHECK(!async_in_progress_);
  113. bool posted = base::PostTaskAndReplyWithResult(
  114. task_runner_.get(), FROM_HERE,
  115. base::BindOnce(&Context::FlushFileImpl, base::Unretained(this)),
  116. base::BindOnce(&Context::OnAsyncCompleted, base::Unretained(this),
  117. IntToInt64(std::move(callback))));
  118. DCHECK(posted);
  119. async_in_progress_ = true;
  120. }
  121. bool FileStream::Context::IsOpen() const {
  122. return file_.IsValid();
  123. }
  124. FileStream::Context::OpenResult FileStream::Context::OpenFileImpl(
  125. const base::FilePath& path, int open_flags) {
  126. #if BUILDFLAG(IS_POSIX)
  127. // Always use blocking IO.
  128. open_flags &= ~base::File::FLAG_ASYNC;
  129. #endif
  130. base::File file;
  131. #if BUILDFLAG(IS_ANDROID)
  132. if (path.IsContentUri()) {
  133. // Check that only Read flags are set.
  134. DCHECK_EQ(open_flags & ~base::File::FLAG_ASYNC,
  135. base::File::FLAG_OPEN | base::File::FLAG_READ);
  136. file = base::OpenContentUriForRead(path);
  137. } else {
  138. #endif // BUILDFLAG(IS_ANDROID)
  139. // FileStream::Context actually closes the file asynchronously,
  140. // independently from FileStream's destructor. It can cause problems for
  141. // users wanting to delete the file right after FileStream deletion. Thus
  142. // we are always adding SHARE_DELETE flag to accommodate such use case.
  143. // TODO(rvargas): This sounds like a bug, as deleting the file would
  144. // presumably happen on the wrong thread. There should be an async delete.
  145. open_flags |= base::File::FLAG_WIN_SHARE_DELETE;
  146. file.Initialize(path, open_flags);
  147. #if BUILDFLAG(IS_ANDROID)
  148. }
  149. #endif // BUILDFLAG(IS_ANDROID)
  150. if (!file.IsValid()) {
  151. return OpenResult(base::File(),
  152. IOResult::FromOSError(logging::GetLastSystemErrorCode()));
  153. }
  154. return OpenResult(std::move(file), IOResult(OK, 0));
  155. }
  156. FileStream::Context::IOResult FileStream::Context::GetFileInfoImpl(
  157. base::File::Info* file_info) {
  158. bool result = file_.GetInfo(file_info);
  159. if (!result)
  160. return IOResult::FromOSError(logging::GetLastSystemErrorCode());
  161. return IOResult(OK, 0);
  162. }
  163. FileStream::Context::IOResult FileStream::Context::CloseFileImpl() {
  164. file_.Close();
  165. return IOResult(OK, 0);
  166. }
  167. FileStream::Context::IOResult FileStream::Context::FlushFileImpl() {
  168. if (file_.Flush())
  169. return IOResult(OK, 0);
  170. return IOResult::FromOSError(logging::GetLastSystemErrorCode());
  171. }
  172. void FileStream::Context::OnOpenCompleted(CompletionOnceCallback callback,
  173. OpenResult open_result) {
  174. file_ = std::move(open_result.file);
  175. if (file_.IsValid() && !orphaned_)
  176. OnFileOpened();
  177. OnAsyncCompleted(IntToInt64(std::move(callback)), open_result.error_code);
  178. }
  179. void FileStream::Context::CloseAndDelete() {
  180. DCHECK(!async_in_progress_);
  181. if (file_.IsValid()) {
  182. bool posted = task_runner_.get()->PostTask(
  183. FROM_HERE, base::BindOnce(base::IgnoreResult(&Context::CloseFileImpl),
  184. base::Owned(this)));
  185. DCHECK(posted);
  186. } else {
  187. delete this;
  188. }
  189. }
  190. Int64CompletionOnceCallback FileStream::Context::IntToInt64(
  191. CompletionOnceCallback callback) {
  192. return base::BindOnce(&CallInt64ToInt, std::move(callback));
  193. }
  194. void FileStream::Context::OnAsyncCompleted(Int64CompletionOnceCallback callback,
  195. const IOResult& result) {
  196. // Reset this before Run() as Run() may issue a new async operation. Also it
  197. // should be reset before Close() because it shouldn't run if any async
  198. // operation is in progress.
  199. async_in_progress_ = false;
  200. if (orphaned_) {
  201. CloseAndDelete();
  202. } else {
  203. std::move(callback).Run(result.result);
  204. }
  205. }
  206. } // namespace net