OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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/bind.h" | 7 #include "base/bind.h" |
8 #include "base/callback.h" | 8 #include "base/callback.h" |
9 #include "base/file_util_proxy.h" | 9 #include "base/file_util_proxy.h" |
10 #include "base/message_loop.h" | 10 #include "base/message_loop.h" |
| 11 #include "base/message_loop_proxy.h" |
11 #include "base/threading/thread_restrictions.h" | 12 #include "base/threading/thread_restrictions.h" |
12 #include "net/base/net_errors.h" | 13 #include "net/base/net_errors.h" |
13 #include "webkit/fileapi/file_system_context.h" | 14 #include "webkit/fileapi/file_system_context.h" |
14 #include "webkit/fileapi/file_system_operation.h" | 15 #include "webkit/fileapi/file_system_operation.h" |
15 #include "webkit/fileapi/file_system_operation_context.h" | 16 #include "webkit/fileapi/file_system_operation_context.h" |
16 #include "webkit/fileapi/file_system_quota_util.h" | 17 #include "webkit/fileapi/file_system_quota_util.h" |
17 #include "webkit/fileapi/quota_file_util.h" | 18 #include "webkit/fileapi/quota_file_util.h" |
18 | 19 |
19 namespace fileapi { | 20 namespace fileapi { |
20 | 21 |
21 static const int kReadBufSize = 32768; | 22 static const int kReadBufSize = 32768; |
22 | 23 |
23 namespace { | 24 namespace { |
24 | 25 |
25 typedef base::Callback<void(base::PlatformFileError /* error code */, | 26 typedef base::Callback<void(base::PlatformFileError /* error code */, |
26 const base::PlatformFileInfo& /* file_info */)> | 27 const base::PlatformFileInfo& /* file_info */)> |
27 InitializeTaskCallback; | 28 InitializeTaskCallback; |
28 | 29 |
29 class InitializeTask : public base::RefCountedThreadSafe<InitializeTask> { | 30 class InitializeTask : public base::RefCountedThreadSafe<InitializeTask> { |
30 public: | 31 public: |
31 InitializeTask( | 32 InitializeTask( |
32 base::PlatformFile file, | 33 base::PlatformFile file, |
33 const FileSystemPath& path, | 34 const FileSystemPath& path, |
34 FileSystemOperationContext* context, | 35 FileSystemOperationContext* context, |
35 const InitializeTaskCallback& callback) | 36 const InitializeTaskCallback& callback) |
36 : origin_message_loop_proxy_( | 37 : original_loop_(base::MessageLoopProxy::current()), |
37 base::MessageLoopProxy::current()), | |
38 error_code_(base::PLATFORM_FILE_OK), | 38 error_code_(base::PLATFORM_FILE_OK), |
39 file_(file), | 39 file_(file), |
40 path_(path), | 40 path_(path), |
41 context_(*context), | 41 context_(*context), |
42 callback_(callback) { | 42 callback_(callback) { |
43 DCHECK_EQ(false, callback.is_null()); | 43 DCHECK_EQ(false, callback.is_null()); |
44 } | 44 } |
45 | 45 |
46 bool Start(scoped_refptr<base::MessageLoopProxy> message_loop_proxy, | 46 bool Start(base::SequencedTaskRunner* task_runner, |
47 const tracked_objects::Location& from_here) { | 47 const tracked_objects::Location& from_here) { |
48 return message_loop_proxy->PostTask( | 48 return task_runner->PostTask( |
49 from_here, | 49 from_here, |
50 base::Bind(&InitializeTask::ProcessOnTargetThread, this)); | 50 base::Bind(&InitializeTask::ProcessOnTargetThread, this)); |
51 } | 51 } |
52 | 52 |
53 private: | 53 private: |
54 friend class base::RefCountedThreadSafe<InitializeTask>; | 54 friend class base::RefCountedThreadSafe<InitializeTask>; |
55 | 55 |
56 void RunCallback() { | 56 void RunCallback() { |
57 callback_.Run(error_code_, file_info_); | 57 callback_.Run(error_code_, file_info_); |
58 } | 58 } |
59 | 59 |
60 void ProcessOnTargetThread() { | 60 void ProcessOnTargetThread() { |
61 DCHECK(context_.file_system_context()); | 61 DCHECK(context_.file_system_context()); |
62 FileSystemQuotaUtil* quota_util = context_.file_system_context()-> | 62 FileSystemQuotaUtil* quota_util = context_.file_system_context()-> |
63 GetQuotaUtil(path_.type()); | 63 GetQuotaUtil(path_.type()); |
64 if (quota_util) { | 64 if (quota_util) { |
65 DCHECK(quota_util->proxy()); | 65 DCHECK(quota_util->proxy()); |
66 quota_util->proxy()->StartUpdateOrigin(path_.origin(), path_.type()); | 66 quota_util->proxy()->StartUpdateOrigin(path_.origin(), path_.type()); |
67 } | 67 } |
68 if (!base::GetPlatformFileInfo(file_, &file_info_)) | 68 if (!base::GetPlatformFileInfo(file_, &file_info_)) |
69 error_code_ = base::PLATFORM_FILE_ERROR_FAILED; | 69 error_code_ = base::PLATFORM_FILE_ERROR_FAILED; |
70 origin_message_loop_proxy_->PostTask( | 70 original_loop_->PostTask( |
71 FROM_HERE, | 71 FROM_HERE, |
72 base::Bind(&InitializeTask::RunCallback, this)); | 72 base::Bind(&InitializeTask::RunCallback, this)); |
73 } | 73 } |
74 | 74 |
75 scoped_refptr<base::MessageLoopProxy> origin_message_loop_proxy_; | 75 scoped_refptr<base::MessageLoopProxy> original_loop_; |
76 base::PlatformFileError error_code_; | 76 base::PlatformFileError error_code_; |
77 | 77 |
78 base::PlatformFile file_; | 78 base::PlatformFile file_; |
79 FileSystemPath path_; | 79 FileSystemPath path_; |
80 FileSystemOperationContext context_; | 80 FileSystemOperationContext context_; |
81 InitializeTaskCallback callback_; | 81 InitializeTaskCallback callback_; |
82 | 82 |
83 base::PlatformFileInfo file_info_; | 83 base::PlatformFileInfo file_info_; |
84 }; | 84 }; |
85 | 85 |
86 } // namespace (anonymous) | 86 } // namespace (anonymous) |
87 | 87 |
88 FileWriterDelegate::FileWriterDelegate( | 88 FileWriterDelegate::FileWriterDelegate( |
89 FileSystemOperation* file_system_operation, | 89 FileSystemOperation* file_system_operation, |
90 const FileSystemPath& path, | 90 const FileSystemPath& path, |
91 int64 offset, | 91 int64 offset, |
92 scoped_refptr<base::MessageLoopProxy> proxy) | 92 base::SequencedTaskRunner* task_runner) |
93 : file_system_operation_(file_system_operation), | 93 : file_system_operation_(file_system_operation), |
94 file_(base::kInvalidPlatformFileValue), | 94 file_(base::kInvalidPlatformFileValue), |
95 path_(path), | 95 path_(path), |
96 offset_(offset), | 96 offset_(offset), |
97 proxy_(proxy), | 97 task_runner_(task_runner), |
98 bytes_written_backlog_(0), | 98 bytes_written_backlog_(0), |
99 bytes_written_(0), | 99 bytes_written_(0), |
100 bytes_read_(0), | 100 bytes_read_(0), |
101 total_bytes_written_(0), | 101 total_bytes_written_(0), |
102 allowed_bytes_to_write_(0), | 102 allowed_bytes_to_write_(0), |
103 io_buffer_(new net::IOBufferWithSize(kReadBufSize)), | 103 io_buffer_(new net::IOBufferWithSize(kReadBufSize)), |
104 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) { | 104 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) { |
105 } | 105 } |
106 | 106 |
107 FileWriterDelegate::~FileWriterDelegate() { | 107 FileWriterDelegate::~FileWriterDelegate() { |
(...skipping 26 matching lines...) Expand all Loading... |
134 void FileWriterDelegate::Start(base::PlatformFile file, | 134 void FileWriterDelegate::Start(base::PlatformFile file, |
135 net::URLRequest* request) { | 135 net::URLRequest* request) { |
136 file_ = file; | 136 file_ = file; |
137 request_ = request; | 137 request_ = request; |
138 | 138 |
139 scoped_refptr<InitializeTask> relay = new InitializeTask( | 139 scoped_refptr<InitializeTask> relay = new InitializeTask( |
140 file_, path_, | 140 file_, path_, |
141 file_system_operation_context(), | 141 file_system_operation_context(), |
142 base::Bind(&FileWriterDelegate::OnGetFileInfoAndCallStartUpdate, | 142 base::Bind(&FileWriterDelegate::OnGetFileInfoAndCallStartUpdate, |
143 weak_factory_.GetWeakPtr())); | 143 weak_factory_.GetWeakPtr())); |
144 relay->Start(proxy_, FROM_HERE); | 144 relay->Start(task_runner_, FROM_HERE); |
145 } | 145 } |
146 | 146 |
147 void FileWriterDelegate::OnReceivedRedirect(net::URLRequest* request, | 147 void FileWriterDelegate::OnReceivedRedirect(net::URLRequest* request, |
148 const GURL& new_url, | 148 const GURL& new_url, |
149 bool* defer_redirect) { | 149 bool* defer_redirect) { |
150 NOTREACHED(); | 150 NOTREACHED(); |
151 OnError(base::PLATFORM_FILE_ERROR_SECURITY); | 151 OnError(base::PLATFORM_FILE_ERROR_SECURITY); |
152 } | 152 } |
153 | 153 |
154 void FileWriterDelegate::OnAuthRequired(net::URLRequest* request, | 154 void FileWriterDelegate::OnAuthRequired(net::URLRequest* request, |
(...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
319 | 319 |
320 FileSystemQuotaUtil* FileWriterDelegate::quota_util() const { | 320 FileSystemQuotaUtil* FileWriterDelegate::quota_util() const { |
321 DCHECK(file_system_operation_); | 321 DCHECK(file_system_operation_); |
322 DCHECK(file_system_operation_->file_system_context()); | 322 DCHECK(file_system_operation_->file_system_context()); |
323 DCHECK(file_system_operation_->file_system_operation_context()); | 323 DCHECK(file_system_operation_->file_system_operation_context()); |
324 return file_system_operation_->file_system_context()->GetQuotaUtil( | 324 return file_system_operation_->file_system_context()->GetQuotaUtil( |
325 path_.type()); | 325 path_.type()); |
326 } | 326 } |
327 | 327 |
328 } // namespace fileapi | 328 } // namespace fileapi |
OLD | NEW |