blob: fc57c8aad89024150d21946bf0637791c67cd5a4 [file] [log] [blame]
[email protected]52c41b42014-03-14 17:56:481// 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]4e2dec32014-04-29 01:16:179#include "base/files/file_proxy.h"
[email protected]52c41b42014-03-14 17:56:4810#include "base/memory/ref_counted.h"
11#include "content/public/browser/browser_thread.h"
12#include "net/base/file_stream.h"
dmurphbff2e532015-01-23 09:18:5613#include "storage/browser/blob/shareable_file_reference.h"
[email protected]52c41b42014-03-14 17:56:4814
[email protected]cd501a72014-08-22 19:58:3115using storage::ShareableFileReference;
[email protected]52c41b42014-03-14 17:56:4816
17namespace content {
18
19namespace {
20
21void DidCreateTemporaryFile(
22 const CreateTemporaryFileStreamCallback& callback,
[email protected]4e2dec32014-04-29 01:16:1723 scoped_ptr<base::FileProxy> file_proxy,
[email protected]52c41b42014-03-14 17:56:4824 base::File::Error error_code,
[email protected]52c41b42014-03-14 17:56:4825 const base::FilePath& file_path) {
mostynb4c27d042015-03-18 21:47:4726 DCHECK_CURRENTLY_ON(BrowserThread::IO);
[email protected]52c41b42014-03-14 17:56:4827
[email protected]4e2dec32014-04-29 01:16:1728 if (!file_proxy->IsValid()) {
[email protected]52c41b42014-03-14 17:56:4829 callback.Run(error_code, scoped_ptr<net::FileStream>(), NULL);
30 return;
31 }
32
[email protected]671e95fd2014-04-30 11:21:3633 scoped_refptr<base::TaskRunner> task_runner =
34 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE);
35
[email protected]52c41b42014-03-14 17:56:4836 // 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]671e95fd2014-04-30 11:21:3641 task_runner.get());
[email protected]52c41b42014-03-14 17:56:4842
[email protected]4e2dec32014-04-29 01:16:1743 scoped_ptr<net::FileStream> file_stream(
[email protected]671e95fd2014-04-30 11:21:3644 new net::FileStream(file_proxy->TakeFile(), task_runner));
[email protected]52c41b42014-03-14 17:56:4845
dcheng67769df52014-08-26 19:43:5146 callback.Run(error_code, file_stream.Pass(), deletable_file.get());
[email protected]52c41b42014-03-14 17:56:4847}
48
49} // namespace
50
51void CreateTemporaryFileStream(
52 const CreateTemporaryFileStreamCallback& callback) {
mostynb4c27d042015-03-18 21:47:4753 DCHECK_CURRENTLY_ON(BrowserThread::IO);
[email protected]52c41b42014-03-14 17:56:4854
[email protected]4e2dec32014-04-29 01:16:1755 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]52c41b42014-03-14 17:56:4861}
62
63} // namespace content