blob: 891123a6433b2f7c52573f2d5b56ae1f99ce9f97 [file] [log] [blame]
[email protected]fb501bc2013-03-05 08:38:151// Copyright (c) 2013 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#ifndef NET_URL_REQUEST_URL_FETCHER_RESPONSE_WRITER_H_
6#define NET_URL_REQUEST_URL_FETCHER_RESPONSE_WRITER_H_
7
[email protected]fe552402014-05-21 01:58:298#include <string>
9
[email protected]d22bad42013-04-04 13:39:1210#include "base/files/file_path.h"
Avi Drissman13fc8932015-12-20 04:40:4611#include "base/macros.h"
[email protected]fb501bc2013-03-05 08:38:1512#include "base/memory/ref_counted.h"
[email protected]d22bad42013-04-04 13:39:1213#include "base/memory/scoped_ptr.h"
[email protected]fb501bc2013-03-05 08:38:1514#include "base/memory/weak_ptr.h"
[email protected]fb501bc2013-03-05 08:38:1515#include "net/base/completion_callback.h"
[email protected]86882402013-10-16 10:28:1016#include "net/base/net_export.h"
[email protected]fb501bc2013-03-05 08:38:1517
18namespace base {
[email protected]0b5ab5092013-11-12 01:09:5619class SequencedTaskRunner;
[email protected]fb501bc2013-03-05 08:38:1520} // namespace base
21
22namespace net {
23
24class DrainableIOBuffer;
[email protected]d22bad42013-04-04 13:39:1225class FileStream;
[email protected]fb501bc2013-03-05 08:38:1526class IOBuffer;
[email protected]9225b062013-10-11 06:02:4827class URLFetcherFileWriter;
28class URLFetcherStringWriter;
[email protected]fb501bc2013-03-05 08:38:1529
30// This class encapsulates all state involved in writing URLFetcher response
31// bytes to the destination.
[email protected]86882402013-10-16 10:28:1032class NET_EXPORT URLFetcherResponseWriter {
[email protected]fb501bc2013-03-05 08:38:1533 public:
34 virtual ~URLFetcherResponseWriter() {}
35
36 // Initializes this instance. If ERR_IO_PENDING is returned, |callback| will
[email protected]9225b062013-10-11 06:02:4837 // be run later with the result. Calling this method again after a
38 // Initialize() success results in discarding already written data.
[email protected]fb501bc2013-03-05 08:38:1539 virtual int Initialize(const CompletionCallback& callback) = 0;
40
41 // Writes |num_bytes| bytes in |buffer|, and returns the number of bytes
42 // written or an error code. If ERR_IO_PENDING is returned, |callback| will be
43 // run later with the result.
44 virtual int Write(IOBuffer* buffer,
45 int num_bytes,
46 const CompletionCallback& callback) = 0;
47
48 // Finishes writing. If ERR_IO_PENDING is returned, |callback| will be run
49 // later with the result.
50 virtual int Finish(const CompletionCallback& callback) = 0;
[email protected]9225b062013-10-11 06:02:4851
52 // Returns this instance's pointer as URLFetcherStringWriter when possible.
53 virtual URLFetcherStringWriter* AsStringWriter();
54
55 // Returns this instance's pointer as URLFetcherFileWriter when possible.
56 virtual URLFetcherFileWriter* AsFileWriter();
[email protected]fb501bc2013-03-05 08:38:1557};
58
59// URLFetcherResponseWriter implementation for std::string.
[email protected]86882402013-10-16 10:28:1060class NET_EXPORT URLFetcherStringWriter : public URLFetcherResponseWriter {
[email protected]fb501bc2013-03-05 08:38:1561 public:
[email protected]9225b062013-10-11 06:02:4862 URLFetcherStringWriter();
dchengb03027d2014-10-21 12:00:2063 ~URLFetcherStringWriter() override;
[email protected]fb501bc2013-03-05 08:38:1564
[email protected]9225b062013-10-11 06:02:4865 const std::string& data() const { return data_; }
66
[email protected]fb501bc2013-03-05 08:38:1567 // URLFetcherResponseWriter overrides:
dchengb03027d2014-10-21 12:00:2068 int Initialize(const CompletionCallback& callback) override;
69 int Write(IOBuffer* buffer,
70 int num_bytes,
71 const CompletionCallback& callback) override;
72 int Finish(const CompletionCallback& callback) override;
73 URLFetcherStringWriter* AsStringWriter() override;
[email protected]fb501bc2013-03-05 08:38:1574
75 private:
[email protected]9225b062013-10-11 06:02:4876 std::string data_;
[email protected]fb501bc2013-03-05 08:38:1577
78 DISALLOW_COPY_AND_ASSIGN(URLFetcherStringWriter);
79};
80
81// URLFetcherResponseWriter implementation for files.
[email protected]86882402013-10-16 10:28:1082class NET_EXPORT URLFetcherFileWriter : public URLFetcherResponseWriter {
[email protected]fb501bc2013-03-05 08:38:1583 public:
[email protected]9225b062013-10-11 06:02:4884 // |file_path| is used as the destination path. If |file_path| is empty,
85 // Initialize() will create a temporary file.
[email protected]0b5ab5092013-11-12 01:09:5686 URLFetcherFileWriter(
87 scoped_refptr<base::SequencedTaskRunner> file_task_runner,
88 const base::FilePath& file_path);
dchengb03027d2014-10-21 12:00:2089 ~URLFetcherFileWriter() override;
[email protected]fb501bc2013-03-05 08:38:1590
[email protected]9225b062013-10-11 06:02:4891 const base::FilePath& file_path() const { return file_path_; }
[email protected]fb501bc2013-03-05 08:38:1592
93 // URLFetcherResponseWriter overrides:
dchengb03027d2014-10-21 12:00:2094 int Initialize(const CompletionCallback& callback) override;
95 int Write(IOBuffer* buffer,
96 int num_bytes,
97 const CompletionCallback& callback) override;
98 int Finish(const CompletionCallback& callback) override;
99 URLFetcherFileWriter* AsFileWriter() override;
[email protected]fb501bc2013-03-05 08:38:15100
101 // Drops ownership of the file at |file_path_|.
102 // This class will not delete it or write to it again.
103 void DisownFile();
104
[email protected]9225b062013-10-11 06:02:48105 private:
106 // Called when a write has been done.
107 void DidWrite(const CompletionCallback& callback, int result);
[email protected]fb501bc2013-03-05 08:38:15108
109 // Closes the file if it is open and then delete it.
110 void CloseAndDeleteFile();
111
[email protected]fb501bc2013-03-05 08:38:15112 // Callback which gets the result of a temporary file creation.
113 void DidCreateTempFile(const CompletionCallback& callback,
[email protected]d22bad42013-04-04 13:39:12114 base::FilePath* temp_file_path,
115 bool success);
[email protected]fb501bc2013-03-05 08:38:15116
[email protected]d22bad42013-04-04 13:39:12117 // Callback which gets the result of FileStream::Open.
118 void DidOpenFile(const CompletionCallback& callback,
119 int result);
[email protected]fb501bc2013-03-05 08:38:15120
[email protected]c1d9cf742013-09-12 06:37:19121 // Callback which gets the result of closing a file.
122 void CloseComplete(const CompletionCallback& callback, int result);
123
[email protected]fb501bc2013-03-05 08:38:15124 // Task runner on which file operations should happen.
[email protected]0b5ab5092013-11-12 01:09:56125 scoped_refptr<base::SequencedTaskRunner> file_task_runner_;
[email protected]fb501bc2013-03-05 08:38:15126
127 // Destination file path.
128 // Initialize() creates a temporary file if this variable is empty.
129 base::FilePath file_path_;
130
131 // True when this instance is responsible to delete the file at |file_path_|.
132 bool owns_file_;
133
[email protected]d22bad42013-04-04 13:39:12134 scoped_ptr<FileStream> file_stream_;
[email protected]fb501bc2013-03-05 08:38:15135
[email protected]09812102014-05-24 00:04:11136 // Callbacks are created for use with base::FileUtilProxy.
137 base::WeakPtrFactory<URLFetcherFileWriter> weak_factory_;
138
[email protected]fb501bc2013-03-05 08:38:15139 DISALLOW_COPY_AND_ASSIGN(URLFetcherFileWriter);
140};
141
142} // namespace net
143
144#endif // NET_URL_REQUEST_URL_FETCHER_RESPONSE_WRITER_H_