summaryrefslogtreecommitdiffstats
path: root/content/browser/loader/temporary_file_stream.cc
blob: 5979bd9986e5b228847cc6380b080fc1df634d1e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "content/browser/loader/temporary_file_stream.h"

#include "base/bind.h"
#include "base/callback.h"
#include "base/files/file_util_proxy.h"
#include "base/memory/ref_counted.h"
#include "content/public/browser/browser_thread.h"
#include "net/base/file_stream.h"
#include "webkit/common/blob/shareable_file_reference.h"

using webkit_blob::ShareableFileReference;

namespace content {

namespace {

void DidCreateTemporaryFile(
    const CreateTemporaryFileStreamCallback& callback,
    base::File::Error error_code,
    base::PassPlatformFile file_handle,
    const base::FilePath& file_path) {
  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));

  if (error_code != base::File::FILE_OK) {
    callback.Run(error_code, scoped_ptr<net::FileStream>(), NULL);
    return;
  }

  // Cancelled or not, create the deletable_file so the temporary is cleaned up.
  scoped_refptr<ShareableFileReference> deletable_file =
      ShareableFileReference::GetOrCreate(
          file_path,
          ShareableFileReference::DELETE_ON_FINAL_RELEASE,
          BrowserThread::GetMessageLoopProxyForThread(
              BrowserThread::FILE).get());

  scoped_ptr<net::FileStream> file_stream(new net::FileStream(
      file_handle.ReleaseValue(),
      base::PLATFORM_FILE_WRITE | base::PLATFORM_FILE_ASYNC));

  callback.Run(error_code, file_stream.Pass(), deletable_file);
}

}  // namespace

void CreateTemporaryFileStream(
    const CreateTemporaryFileStreamCallback& callback) {
  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));

  base::FileUtilProxy::CreateTemporary(
      BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE).get(),
      base::PLATFORM_FILE_ASYNC,
      base::Bind(&DidCreateTemporaryFile, callback));
}

}  // namespace content