Blink roll 25b6bd3a7a131ffe68d809546ad1a20707915cdc:3a503f41ae42e5b79cfcd2ff10e65afde...
[chromium-blink-merge.git] / content / browser / loader / temporary_file_stream.cc
blobe904d88e107ae58a5795eb29f342d63150acdf44
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.
5 #include "content/browser/loader/temporary_file_stream.h"
7 #include "base/bind.h"
8 #include "base/callback.h"
9 #include "base/files/file_proxy.h"
10 #include "base/memory/ref_counted.h"
11 #include "content/public/browser/browser_thread.h"
12 #include "net/base/file_stream.h"
13 #include "storage/common/blob/shareable_file_reference.h"
15 using storage::ShareableFileReference;
17 namespace content {
19 namespace {
21 void DidCreateTemporaryFile(
22 const CreateTemporaryFileStreamCallback& callback,
23 scoped_ptr<base::FileProxy> file_proxy,
24 base::File::Error error_code,
25 const base::FilePath& file_path) {
26 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
28 if (!file_proxy->IsValid()) {
29 callback.Run(error_code, scoped_ptr<net::FileStream>(), NULL);
30 return;
33 scoped_refptr<base::TaskRunner> task_runner =
34 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE);
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,
41 task_runner.get());
43 scoped_ptr<net::FileStream> file_stream(
44 new net::FileStream(file_proxy->TakeFile(), task_runner));
46 callback.Run(error_code, file_stream.Pass(), deletable_file.get());
49 } // namespace
51 void CreateTemporaryFileStream(
52 const CreateTemporaryFileStreamCallback& callback) {
53 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
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)));
63 } // namespace content