| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "content/browser/loader/temporary_file_stream.h" | 5 #include "content/browser/loader/temporary_file_stream.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/files/file_util_proxy.h" | 9 #include "base/files/file_proxy.h" |
| 10 #include "base/memory/ref_counted.h" | 10 #include "base/memory/ref_counted.h" |
| 11 #include "content/public/browser/browser_thread.h" | 11 #include "content/public/browser/browser_thread.h" |
| 12 #include "net/base/file_stream.h" | 12 #include "net/base/file_stream.h" |
| 13 #include "webkit/common/blob/shareable_file_reference.h" | 13 #include "webkit/common/blob/shareable_file_reference.h" |
| 14 | 14 |
| 15 using webkit_blob::ShareableFileReference; | 15 using webkit_blob::ShareableFileReference; |
| 16 | 16 |
| 17 namespace content { | 17 namespace content { |
| 18 | 18 |
| 19 namespace { | 19 namespace { |
| 20 | 20 |
| 21 void DidCreateTemporaryFile( | 21 void DidCreateTemporaryFile( |
| 22 const CreateTemporaryFileStreamCallback& callback, | 22 const CreateTemporaryFileStreamCallback& callback, |
| 23 scoped_ptr<base::FileProxy> file_proxy, |
| 23 base::File::Error error_code, | 24 base::File::Error error_code, |
| 24 base::PassPlatformFile file_handle, | |
| 25 const base::FilePath& file_path) { | 25 const base::FilePath& file_path) { |
| 26 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 26 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 27 | 27 |
| 28 if (error_code != base::File::FILE_OK) { | 28 if (!file_proxy->IsValid()) { |
| 29 callback.Run(error_code, scoped_ptr<net::FileStream>(), NULL); | 29 callback.Run(error_code, scoped_ptr<net::FileStream>(), NULL); |
| 30 return; | 30 return; |
| 31 } | 31 } |
| 32 | 32 |
| 33 // Cancelled or not, create the deletable_file so the temporary is cleaned up. | 33 // Cancelled or not, create the deletable_file so the temporary is cleaned up. |
| 34 scoped_refptr<ShareableFileReference> deletable_file = | 34 scoped_refptr<ShareableFileReference> deletable_file = |
| 35 ShareableFileReference::GetOrCreate( | 35 ShareableFileReference::GetOrCreate( |
| 36 file_path, | 36 file_path, |
| 37 ShareableFileReference::DELETE_ON_FINAL_RELEASE, | 37 ShareableFileReference::DELETE_ON_FINAL_RELEASE, |
| 38 BrowserThread::GetMessageLoopProxyForThread( | 38 BrowserThread::GetMessageLoopProxyForThread( |
| 39 BrowserThread::FILE).get()); | 39 BrowserThread::FILE).get()); |
| 40 | 40 |
| 41 scoped_ptr<net::FileStream> file_stream(new net::FileStream( | 41 scoped_ptr<net::FileStream> file_stream( |
| 42 file_handle.ReleaseValue(), | 42 new net::FileStream(file_proxy->TakeFile())); |
| 43 base::PLATFORM_FILE_WRITE | base::PLATFORM_FILE_ASYNC)); | |
| 44 | 43 |
| 45 callback.Run(error_code, file_stream.Pass(), deletable_file); | 44 callback.Run(error_code, file_stream.Pass(), deletable_file); |
| 46 } | 45 } |
| 47 | 46 |
| 48 } // namespace | 47 } // namespace |
| 49 | 48 |
| 50 void CreateTemporaryFileStream( | 49 void CreateTemporaryFileStream( |
| 51 const CreateTemporaryFileStreamCallback& callback) { | 50 const CreateTemporaryFileStreamCallback& callback) { |
| 52 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 51 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 53 | 52 |
| 54 base::FileUtilProxy::CreateTemporary( | 53 scoped_ptr<base::FileProxy> file_proxy(new base::FileProxy( |
| 55 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE).get(), | 54 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE).get())); |
| 56 base::PLATFORM_FILE_ASYNC, | 55 base::FileProxy* proxy = file_proxy.get(); |
| 57 base::Bind(&DidCreateTemporaryFile, callback)); | 56 proxy->CreateTemporary( |
| 57 base::File::FLAG_ASYNC, |
| 58 base::Bind(&DidCreateTemporaryFile, callback, Passed(&file_proxy))); |
| 58 } | 59 } |
| 59 | 60 |
| 60 } // namespace content | 61 } // namespace content |
| OLD | NEW |