| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "webkit/fileapi/file_writer_delegate.h" | 5 #include "webkit/fileapi/file_writer_delegate.h" |
| 6 | 6 |
| 7 #include "base/file_util_proxy.h" | 7 #include "base/file_util_proxy.h" |
| 8 #include "base/message_loop.h" | 8 #include "base/message_loop.h" |
| 9 #include "base/threading/thread_restrictions.h" | 9 #include "base/threading/thread_restrictions.h" |
| 10 #include "net/base/net_errors.h" | 10 #include "net/base/net_errors.h" |
| 11 #include "webkit/fileapi/file_system_context.h" | 11 #include "webkit/fileapi/file_system_context.h" |
| 12 #include "webkit/fileapi/file_system_operation.h" | 12 #include "webkit/fileapi/file_system_operation.h" |
| 13 #include "webkit/fileapi/file_system_operation_context.h" | 13 #include "webkit/fileapi/file_system_operation_context.h" |
| 14 #include "webkit/fileapi/file_system_path_manager.h" | 14 #include "webkit/fileapi/file_system_path_manager.h" |
| 15 #include "webkit/fileapi/file_system_quota_util.h" | 15 #include "webkit/fileapi/file_system_quota_util.h" |
| 16 #include "webkit/fileapi/quota_file_util.h" | 16 #include "webkit/fileapi/quota_file_util.h" |
| 17 | 17 |
| 18 namespace fileapi { | 18 namespace fileapi { |
| 19 | 19 |
| 20 static const int kReadBufSize = 32768; | 20 static const int kReadBufSize = 32768; |
| 21 | 21 |
| 22 namespace { | |
| 23 | |
| 24 typedef Callback2<base::PlatformFileError /* error code */, | |
| 25 const base::PlatformFileInfo& /* file_info */ | |
| 26 >::Type InitializeTaskCallback; | |
| 27 | |
| 28 class InitializeTask : public base::RefCountedThreadSafe<InitializeTask> { | |
| 29 public: | |
| 30 InitializeTask( | |
| 31 base::PlatformFile file, | |
| 32 FileSystemOperationContext* context, | |
| 33 InitializeTaskCallback* callback) | |
| 34 : origin_message_loop_proxy_( | |
| 35 base::MessageLoopProxy::CreateForCurrentThread()), | |
| 36 error_code_(base::PLATFORM_FILE_OK), | |
| 37 file_(file), | |
| 38 context_(*context), | |
| 39 callback_(callback) { | |
| 40 DCHECK(callback); | |
| 41 } | |
| 42 | |
| 43 bool Start(scoped_refptr<base::MessageLoopProxy> message_loop_proxy, | |
| 44 const tracked_objects::Location& from_here) { | |
| 45 return message_loop_proxy->PostTask(from_here, NewRunnableMethod(this, | |
| 46 &InitializeTask::ProcessOnTargetThread)); | |
| 47 } | |
| 48 | |
| 49 private: | |
| 50 friend class base::RefCountedThreadSafe<InitializeTask>; | |
| 51 | |
| 52 void RunCallback() { | |
| 53 callback_->Run(error_code_, file_info_); | |
| 54 delete callback_; | |
| 55 } | |
| 56 | |
| 57 void ProcessOnTargetThread() { | |
| 58 DCHECK(context_.file_system_context()); | |
| 59 FileSystemQuotaUtil* quota_util = context_.file_system_context()-> | |
| 60 GetQuotaUtil(context_.src_type()); | |
| 61 if (quota_util) { | |
| 62 DCHECK(quota_util->proxy()); | |
| 63 quota_util->proxy()->StartUpdateOrigin( | |
| 64 context_.src_origin_url(), context_.src_type()); | |
| 65 } | |
| 66 if (!base::GetPlatformFileInfo(file_, &file_info_)) | |
| 67 error_code_ = base::PLATFORM_FILE_ERROR_FAILED; | |
| 68 origin_message_loop_proxy_->PostTask(FROM_HERE, NewRunnableMethod(this, | |
| 69 &InitializeTask::RunCallback)); | |
| 70 } | |
| 71 | |
| 72 scoped_refptr<base::MessageLoopProxy> origin_message_loop_proxy_; | |
| 73 base::PlatformFileError error_code_; | |
| 74 | |
| 75 base::PlatformFile file_; | |
| 76 FileSystemOperationContext context_; | |
| 77 InitializeTaskCallback* callback_; | |
| 78 | |
| 79 base::PlatformFileInfo file_info_; | |
| 80 }; | |
| 81 | |
| 82 } // namespace (anonymous) | |
| 83 | |
| 84 FileWriterDelegate::FileWriterDelegate( | 22 FileWriterDelegate::FileWriterDelegate( |
| 85 FileSystemOperation* file_system_operation, int64 offset, | 23 FileSystemOperation* file_system_operation, int64 offset, |
| 86 scoped_refptr<base::MessageLoopProxy> proxy) | 24 scoped_refptr<base::MessageLoopProxy> proxy) |
| 87 : file_system_operation_(file_system_operation), | 25 : file_system_operation_(file_system_operation), |
| 88 file_(base::kInvalidPlatformFileValue), | 26 file_(base::kInvalidPlatformFileValue), |
| 89 offset_(offset), | 27 offset_(offset), |
| 90 proxy_(proxy), | 28 proxy_(proxy), |
| 91 bytes_written_backlog_(0), | 29 bytes_written_backlog_(0), |
| 92 bytes_written_(0), | 30 bytes_written_(0), |
| 93 bytes_read_(0), | 31 bytes_read_(0), |
| 94 total_bytes_written_(0), | 32 total_bytes_written_(0), |
| 95 allowed_bytes_to_write_(0), | 33 allowed_bytes_to_write_(0), |
| 96 io_buffer_(new net::IOBufferWithSize(kReadBufSize)), | 34 io_buffer_(new net::IOBufferWithSize(kReadBufSize)), |
| 97 io_callback_(ALLOW_THIS_IN_INITIALIZER_LIST(this), | 35 io_callback_(ALLOW_THIS_IN_INITIALIZER_LIST(this), |
| 98 &FileWriterDelegate::OnDataWritten), | 36 &FileWriterDelegate::OnDataWritten), |
| 99 method_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)), | 37 method_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)), |
| 100 callback_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { | 38 callback_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { |
| 101 } | 39 } |
| 102 | 40 |
| 103 FileWriterDelegate::~FileWriterDelegate() { | 41 FileWriterDelegate::~FileWriterDelegate() { |
| 104 } | 42 } |
| 105 | 43 |
| 106 void FileWriterDelegate::OnGetFileInfoAndCallStartUpdate( | 44 void FileWriterDelegate::Start(base::PlatformFile file, |
| 107 base::PlatformFileError error, | 45 int64 file_size, |
| 108 const base::PlatformFileInfo& file_info) { | 46 net::URLRequest* request) { |
| 109 if (error) { | 47 file_ = file; |
| 110 OnError(error); | 48 size_ = file_size; |
| 111 return; | 49 request_ = request; |
| 50 |
| 51 DCHECK(file_system_operation_); |
| 52 FileSystemQuotaUtil* quota_util = |
| 53 file_system_operation_->file_system_context()->GetQuotaUtil( |
| 54 file_system_operation_context()->src_type()); |
| 55 if (quota_util) { |
| 56 DCHECK(quota_util->proxy()); |
| 57 quota_util->proxy()->StartUpdateOrigin( |
| 58 file_system_operation_context()->src_origin_url(), |
| 59 file_system_operation_context()->src_type()); |
| 112 } | 60 } |
| 61 |
| 113 int64 allowed_bytes_growth = | 62 int64 allowed_bytes_growth = |
| 114 file_system_operation_context()->allowed_bytes_growth(); | 63 file_system_operation_context()->allowed_bytes_growth(); |
| 115 if (allowed_bytes_growth == QuotaFileUtil::kNoLimit || | 64 if (allowed_bytes_growth == QuotaFileUtil::kNoLimit || |
| 116 file_system_operation_->file_system_context()->IsStorageUnlimited( | 65 file_system_operation_->file_system_context()->IsStorageUnlimited( |
| 117 file_system_operation_context()->src_origin_url())) { | 66 file_system_operation_context()->src_origin_url())) { |
| 118 // TODO(kinuko): kNoLimit is kint64max therefore all the calculation/ | 67 // TODO(kinuko): kNoLimit is kint64max therefore all the calculation/ |
| 119 // comparison with the value should just work, but we should drop | 68 // comparison with the value should just work, but we should drop |
| 120 // such implicit assumption and should use an explicit boolean flag | 69 // such implicit assumption and should use an explicit boolean flag |
| 121 // or something. | 70 // or something. |
| 122 allowed_bytes_to_write_ = QuotaFileUtil::kNoLimit; | 71 allowed_bytes_to_write_ = QuotaFileUtil::kNoLimit; |
| 123 } else { | 72 } else { |
| 124 if (allowed_bytes_growth < 0) | 73 if (allowed_bytes_growth < 0) |
| 125 allowed_bytes_growth = 0; | 74 allowed_bytes_growth = 0; |
| 126 allowed_bytes_to_write_ = file_info.size - offset_ + allowed_bytes_growth; | 75 allowed_bytes_to_write_ = file_size - offset_ + allowed_bytes_growth; |
| 127 } | 76 } |
| 128 size_ = file_info.size; | |
| 129 file_stream_.reset(new net::FileStream(file_, | 77 file_stream_.reset(new net::FileStream(file_, |
| 130 base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_WRITE | | 78 base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_WRITE | |
| 131 base::PLATFORM_FILE_ASYNC)); | 79 base::PLATFORM_FILE_ASYNC)); |
| 132 request_->Start(); | 80 request_->Start(); |
| 133 } | 81 } |
| 134 | 82 |
| 135 void FileWriterDelegate::Start(base::PlatformFile file, | |
| 136 net::URLRequest* request) { | |
| 137 file_ = file; | |
| 138 request_ = request; | |
| 139 | |
| 140 scoped_refptr<InitializeTask> relay = new InitializeTask( | |
| 141 file_, file_system_operation_context(), | |
| 142 callback_factory_.NewCallback( | |
| 143 &FileWriterDelegate::OnGetFileInfoAndCallStartUpdate)); | |
| 144 relay->Start(proxy_, FROM_HERE); | |
| 145 } | |
| 146 | |
| 147 void FileWriterDelegate::OnReceivedRedirect( | 83 void FileWriterDelegate::OnReceivedRedirect( |
| 148 net::URLRequest* request, const GURL& new_url, bool* defer_redirect) { | 84 net::URLRequest* request, const GURL& new_url, bool* defer_redirect) { |
| 149 NOTREACHED(); | 85 NOTREACHED(); |
| 150 OnError(base::PLATFORM_FILE_ERROR_SECURITY); | 86 OnError(base::PLATFORM_FILE_ERROR_SECURITY); |
| 151 } | 87 } |
| 152 | 88 |
| 153 void FileWriterDelegate::OnAuthRequired( | 89 void FileWriterDelegate::OnAuthRequired( |
| 154 net::URLRequest* request, net::AuthChallengeInfo* auth_info) { | 90 net::URLRequest* request, net::AuthChallengeInfo* auth_info) { |
| 155 NOTREACHED(); | 91 NOTREACHED(); |
| 156 OnError(base::PLATFORM_FILE_ERROR_SECURITY); | 92 OnError(base::PLATFORM_FILE_ERROR_SECURITY); |
| (...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 320 | 256 |
| 321 FileSystemQuotaUtil* FileWriterDelegate::quota_util() const { | 257 FileSystemQuotaUtil* FileWriterDelegate::quota_util() const { |
| 322 DCHECK(file_system_operation_); | 258 DCHECK(file_system_operation_); |
| 323 DCHECK(file_system_operation_->file_system_context()); | 259 DCHECK(file_system_operation_->file_system_context()); |
| 324 DCHECK(file_system_operation_->file_system_operation_context()); | 260 DCHECK(file_system_operation_->file_system_operation_context()); |
| 325 return file_system_operation_->file_system_context()->GetQuotaUtil( | 261 return file_system_operation_->file_system_context()->GetQuotaUtil( |
| 326 file_system_operation_context()->src_type()); | 262 file_system_operation_context()->src_type()); |
| 327 } | 263 } |
| 328 | 264 |
| 329 } // namespace fileapi | 265 } // namespace fileapi |
| OLD | NEW |