[email protected] | 52c41b4 | 2014-03-14 17:56:48 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "content/browser/loader/temporary_file_stream.h" |
| 6 | |
| 7 | #include "base/bind.h" |
| 8 | #include "base/callback.h" |
[email protected] | 4e2dec3 | 2014-04-29 01:16:17 | [diff] [blame] | 9 | #include "base/files/file_proxy.h" |
[email protected] | 52c41b4 | 2014-03-14 17:56:48 | [diff] [blame] | 10 | #include "base/memory/ref_counted.h" |
| 11 | #include "content/public/browser/browser_thread.h" |
| 12 | #include "net/base/file_stream.h" |
dmurph | bff2e53 | 2015-01-23 09:18:56 | [diff] [blame] | 13 | #include "storage/browser/blob/shareable_file_reference.h" |
[email protected] | 52c41b4 | 2014-03-14 17:56:48 | [diff] [blame] | 14 | |
[email protected] | cd501a7 | 2014-08-22 19:58:31 | [diff] [blame] | 15 | using storage::ShareableFileReference; |
[email protected] | 52c41b4 | 2014-03-14 17:56:48 | [diff] [blame] | 16 | |
| 17 | namespace content { |
| 18 | |
| 19 | namespace { |
| 20 | |
| 21 | void DidCreateTemporaryFile( |
| 22 | const CreateTemporaryFileStreamCallback& callback, |
[email protected] | 4e2dec3 | 2014-04-29 01:16:17 | [diff] [blame] | 23 | scoped_ptr<base::FileProxy> file_proxy, |
[email protected] | 52c41b4 | 2014-03-14 17:56:48 | [diff] [blame] | 24 | base::File::Error error_code, |
[email protected] | 52c41b4 | 2014-03-14 17:56:48 | [diff] [blame] | 25 | const base::FilePath& file_path) { |
mostynb | 4c27d04 | 2015-03-18 21:47:47 | [diff] [blame] | 26 | DCHECK_CURRENTLY_ON(BrowserThread::IO); |
[email protected] | 52c41b4 | 2014-03-14 17:56:48 | [diff] [blame] | 27 | |
[email protected] | 4e2dec3 | 2014-04-29 01:16:17 | [diff] [blame] | 28 | if (!file_proxy->IsValid()) { |
[email protected] | 52c41b4 | 2014-03-14 17:56:48 | [diff] [blame] | 29 | callback.Run(error_code, scoped_ptr<net::FileStream>(), NULL); |
| 30 | return; |
| 31 | } |
| 32 | |
[email protected] | 671e95fd | 2014-04-30 11:21:36 | [diff] [blame] | 33 | scoped_refptr<base::TaskRunner> task_runner = |
| 34 | BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE); |
| 35 | |
[email protected] | 52c41b4 | 2014-03-14 17:56:48 | [diff] [blame] | 36 | // Cancelled or not, create the deletable_file so the temporary is cleaned up. |
| 37 | scoped_refptr<ShareableFileReference> deletable_file = |
| 38 | ShareableFileReference::GetOrCreate( |
| 39 | file_path, |
| 40 | ShareableFileReference::DELETE_ON_FINAL_RELEASE, |
[email protected] | 671e95fd | 2014-04-30 11:21:36 | [diff] [blame] | 41 | task_runner.get()); |
[email protected] | 52c41b4 | 2014-03-14 17:56:48 | [diff] [blame] | 42 | |
[email protected] | 4e2dec3 | 2014-04-29 01:16:17 | [diff] [blame] | 43 | scoped_ptr<net::FileStream> file_stream( |
[email protected] | 671e95fd | 2014-04-30 11:21:36 | [diff] [blame] | 44 | new net::FileStream(file_proxy->TakeFile(), task_runner)); |
[email protected] | 52c41b4 | 2014-03-14 17:56:48 | [diff] [blame] | 45 | |
dcheng | 67769df5 | 2014-08-26 19:43:51 | [diff] [blame] | 46 | callback.Run(error_code, file_stream.Pass(), deletable_file.get()); |
[email protected] | 52c41b4 | 2014-03-14 17:56:48 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | } // namespace |
| 50 | |
| 51 | void CreateTemporaryFileStream( |
| 52 | const CreateTemporaryFileStreamCallback& callback) { |
mostynb | 4c27d04 | 2015-03-18 21:47:47 | [diff] [blame] | 53 | DCHECK_CURRENTLY_ON(BrowserThread::IO); |
[email protected] | 52c41b4 | 2014-03-14 17:56:48 | [diff] [blame] | 54 | |
[email protected] | 4e2dec3 | 2014-04-29 01:16:17 | [diff] [blame] | 55 | scoped_ptr<base::FileProxy> file_proxy(new base::FileProxy( |
| 56 | BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE).get())); |
| 57 | base::FileProxy* proxy = file_proxy.get(); |
| 58 | proxy->CreateTemporary( |
| 59 | base::File::FLAG_ASYNC, |
| 60 | base::Bind(&DidCreateTemporaryFile, callback, Passed(&file_proxy))); |
[email protected] | 52c41b4 | 2014-03-14 17:56:48 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | } // namespace content |