| OLD | NEW |
| (Empty) |
| 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 | |
| 5 #include "webkit/support/simple_file_writer.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/location.h" | |
| 9 #include "base/logging.h" | |
| 10 #include "base/message_loop/message_loop_proxy.h" | |
| 11 #include "net/url_request/url_request_context.h" | |
| 12 #include "webkit/browser/fileapi/file_system_context.h" | |
| 13 #include "webkit/browser/fileapi/file_system_operation_runner.h" | |
| 14 #include "webkit/browser/fileapi/file_system_url.h" | |
| 15 #include "webkit/common/fileapi/file_system_types.h" | |
| 16 #include "webkit/glue/webkit_glue.h" | |
| 17 #include "webkit/support/simple_resource_loader_bridge.h" | |
| 18 | |
| 19 using fileapi::FileSystemURL; | |
| 20 using fileapi::FileSystemContext; | |
| 21 using fileapi::FileSystemOperationRunner; | |
| 22 using fileapi::WebFileWriterBase; | |
| 23 using WebKit::WebFileWriterClient; | |
| 24 using WebKit::WebString; | |
| 25 using WebKit::WebURL; | |
| 26 | |
| 27 net::URLRequestContext* SimpleFileWriter::request_context_ = NULL; | |
| 28 | |
| 29 // Helper class to proxy to write and truncate calls to the IO thread, | |
| 30 // and to proxy the results back to the main thead. There is a one-to-one | |
| 31 // relationship between SimpleFileWriters and IOThreadBackends. | |
| 32 class SimpleFileWriter::IOThreadProxy | |
| 33 : public base::RefCountedThreadSafe<SimpleFileWriter::IOThreadProxy> { | |
| 34 public: | |
| 35 IOThreadProxy(const base::WeakPtr<SimpleFileWriter>& simple_writer, | |
| 36 FileSystemContext* file_system_context) | |
| 37 : simple_writer_(simple_writer), | |
| 38 operation_id_(FileSystemOperationRunner::kErrorOperationID), | |
| 39 file_system_context_(file_system_context) { | |
| 40 // The IO thread needs to be running for this class to work. | |
| 41 SimpleResourceLoaderBridge::EnsureIOThread(); | |
| 42 io_thread_ = SimpleResourceLoaderBridge::GetIoThread(); | |
| 43 main_thread_ = base::MessageLoopProxy::current(); | |
| 44 } | |
| 45 | |
| 46 void Truncate(const FileSystemURL& url, int64 offset) { | |
| 47 if (!io_thread_->BelongsToCurrentThread()) { | |
| 48 io_thread_->PostTask( | |
| 49 FROM_HERE, | |
| 50 base::Bind(&IOThreadProxy::Truncate, this, url, offset)); | |
| 51 return; | |
| 52 } | |
| 53 if (FailIfNotWritable(url)) | |
| 54 return; | |
| 55 DCHECK_EQ(FileSystemOperationRunner::kErrorOperationID, operation_id_); | |
| 56 operation_id_ = file_system_context_->operation_runner()->Truncate( | |
| 57 url, offset, base::Bind(&IOThreadProxy::DidFinish, this)); | |
| 58 } | |
| 59 | |
| 60 void Write(const FileSystemURL& url, const GURL& blob_url, int64 offset) { | |
| 61 if (!io_thread_->BelongsToCurrentThread()) { | |
| 62 io_thread_->PostTask( | |
| 63 FROM_HERE, | |
| 64 base::Bind(&IOThreadProxy::Write, this, url, blob_url, offset)); | |
| 65 return; | |
| 66 } | |
| 67 if (FailIfNotWritable(url)) | |
| 68 return; | |
| 69 DCHECK(request_context_); | |
| 70 DCHECK_EQ(FileSystemOperationRunner::kErrorOperationID, operation_id_); | |
| 71 operation_id_ = file_system_context_->operation_runner()->Write( | |
| 72 request_context_, url, blob_url, offset, | |
| 73 base::Bind(&IOThreadProxy::DidWrite, this)); | |
| 74 } | |
| 75 | |
| 76 void Cancel() { | |
| 77 if (!io_thread_->BelongsToCurrentThread()) { | |
| 78 io_thread_->PostTask( | |
| 79 FROM_HERE, | |
| 80 base::Bind(&IOThreadProxy::Cancel, this)); | |
| 81 return; | |
| 82 } | |
| 83 if (operation_id_ == FileSystemOperationRunner::kErrorOperationID) { | |
| 84 DidFailOnMainThread(base::PLATFORM_FILE_ERROR_INVALID_OPERATION); | |
| 85 return; | |
| 86 } | |
| 87 file_system_context_->operation_runner()->Cancel( | |
| 88 operation_id_, base::Bind(&IOThreadProxy::DidFinish, this)); | |
| 89 } | |
| 90 | |
| 91 private: | |
| 92 friend class base::RefCountedThreadSafe<IOThreadProxy>; | |
| 93 virtual ~IOThreadProxy() {} | |
| 94 | |
| 95 // Returns true if it is not writable. | |
| 96 bool FailIfNotWritable(const FileSystemURL& url) { | |
| 97 if (url.type() == fileapi::kFileSystemTypeDragged) { | |
| 98 // Write is not allowed in isolate file system in SimpleFileWriter. | |
| 99 DidFailOnMainThread(base::PLATFORM_FILE_ERROR_SECURITY); | |
| 100 return true; | |
| 101 } | |
| 102 return false; | |
| 103 } | |
| 104 | |
| 105 void DidSucceedOnMainThread() { | |
| 106 if (!main_thread_->BelongsToCurrentThread()) { | |
| 107 main_thread_->PostTask( | |
| 108 FROM_HERE, | |
| 109 base::Bind(&IOThreadProxy::DidSucceedOnMainThread, this)); | |
| 110 return; | |
| 111 } | |
| 112 if (simple_writer_.get()) | |
| 113 simple_writer_->DidSucceed(); | |
| 114 } | |
| 115 | |
| 116 void DidFailOnMainThread(base::PlatformFileError error_code) { | |
| 117 if (!main_thread_->BelongsToCurrentThread()) { | |
| 118 main_thread_->PostTask( | |
| 119 FROM_HERE, | |
| 120 base::Bind(&IOThreadProxy::DidFailOnMainThread, this, error_code)); | |
| 121 return; | |
| 122 } | |
| 123 if (simple_writer_.get()) | |
| 124 simple_writer_->DidFail(error_code); | |
| 125 } | |
| 126 | |
| 127 void DidWriteOnMainThread(int64 bytes, bool complete) { | |
| 128 if (!main_thread_->BelongsToCurrentThread()) { | |
| 129 main_thread_->PostTask( | |
| 130 FROM_HERE, | |
| 131 base::Bind(&IOThreadProxy::DidWriteOnMainThread, | |
| 132 this, bytes, complete)); | |
| 133 return; | |
| 134 } | |
| 135 if (simple_writer_.get()) | |
| 136 simple_writer_->DidWrite(bytes, complete); | |
| 137 } | |
| 138 | |
| 139 void ClearOperation() { | |
| 140 DCHECK(io_thread_->BelongsToCurrentThread()); | |
| 141 operation_id_ = FileSystemOperationRunner::kErrorOperationID; | |
| 142 } | |
| 143 | |
| 144 void DidFinish(base::PlatformFileError result) { | |
| 145 if (result == base::PLATFORM_FILE_OK) | |
| 146 DidSucceedOnMainThread(); | |
| 147 else | |
| 148 DidFailOnMainThread(result); | |
| 149 ClearOperation(); | |
| 150 } | |
| 151 | |
| 152 void DidWrite(base::PlatformFileError result, int64 bytes, bool complete) { | |
| 153 if (result == base::PLATFORM_FILE_OK) { | |
| 154 DidWriteOnMainThread(bytes, complete); | |
| 155 if (complete) | |
| 156 ClearOperation(); | |
| 157 } else { | |
| 158 DidFailOnMainThread(result); | |
| 159 ClearOperation(); | |
| 160 } | |
| 161 } | |
| 162 | |
| 163 scoped_refptr<base::MessageLoopProxy> io_thread_; | |
| 164 scoped_refptr<base::MessageLoopProxy> main_thread_; | |
| 165 | |
| 166 // Only used on the main thread. | |
| 167 base::WeakPtr<SimpleFileWriter> simple_writer_; | |
| 168 | |
| 169 // Only used on the io thread. | |
| 170 FileSystemOperationRunner::OperationID operation_id_; | |
| 171 | |
| 172 scoped_refptr<FileSystemContext> file_system_context_; | |
| 173 }; | |
| 174 | |
| 175 | |
| 176 SimpleFileWriter::SimpleFileWriter( | |
| 177 const GURL& path, | |
| 178 WebFileWriterClient* client, | |
| 179 FileSystemContext* file_system_context) | |
| 180 : WebFileWriterBase(path, client), | |
| 181 file_system_context_(file_system_context), | |
| 182 io_thread_proxy_(new IOThreadProxy(AsWeakPtr(), file_system_context)) { | |
| 183 } | |
| 184 | |
| 185 SimpleFileWriter::~SimpleFileWriter() { | |
| 186 } | |
| 187 | |
| 188 void SimpleFileWriter::DoTruncate(const GURL& path, int64 offset) { | |
| 189 FileSystemURL url = file_system_context_->CrackURL(path); | |
| 190 io_thread_proxy_->Truncate(url, offset); | |
| 191 } | |
| 192 | |
| 193 void SimpleFileWriter::DoWrite( | |
| 194 const GURL& path, const GURL& blob_url, int64 offset) { | |
| 195 FileSystemURL url = file_system_context_->CrackURL(path); | |
| 196 io_thread_proxy_->Write(url, blob_url, offset); | |
| 197 } | |
| 198 | |
| 199 void SimpleFileWriter::DoCancel() { | |
| 200 io_thread_proxy_->Cancel(); | |
| 201 } | |
| OLD | NEW |