blob: 422a8a400adbfa76aeb5f17929c13b313260dbed [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
8#include "base/basictypes.h"
[email protected]d22bad42013-04-04 13:39:129#include "base/files/file_path.h"
[email protected]fb501bc2013-03-05 08:38:1510#include "base/memory/ref_counted.h"
[email protected]d22bad42013-04-04 13:39:1211#include "base/memory/scoped_ptr.h"
[email protected]fb501bc2013-03-05 08:38:1512#include "base/memory/weak_ptr.h"
[email protected]fb501bc2013-03-05 08:38:1513#include "net/base/completion_callback.h"
[email protected]86882402013-10-16 10:28:1014#include "net/base/net_export.h"
[email protected]fb501bc2013-03-05 08:38:1515
16namespace base {
[email protected]0b5ab5092013-11-12 01:09:5617class SequencedTaskRunner;
[email protected]fb501bc2013-03-05 08:38:1518} // namespace base
19
20namespace net {
21
22class DrainableIOBuffer;
[email protected]d22bad42013-04-04 13:39:1223class FileStream;
[email protected]fb501bc2013-03-05 08:38:1524class IOBuffer;
[email protected]9225b062013-10-11 06:02:4825class URLFetcherFileWriter;
26class URLFetcherStringWriter;
[email protected]fb501bc2013-03-05 08:38:1527
28// This class encapsulates all state involved in writing URLFetcher response
29// bytes to the destination.
[email protected]86882402013-10-16 10:28:1030class NET_EXPORT URLFetcherResponseWriter {
[email protected]fb501bc2013-03-05 08:38:1531 public:
32 virtual ~URLFetcherResponseWriter() {}
33
34 // Initializes this instance. If ERR_IO_PENDING is returned, |callback| will
[email protected]9225b062013-10-11 06:02:4835 // be run later with the result. Calling this method again after a
36 // Initialize() success results in discarding already written data.
[email protected]fb501bc2013-03-05 08:38:1537 virtual int Initialize(const CompletionCallback& callback) = 0;
38
39 // Writes |num_bytes| bytes in |buffer|, and returns the number of bytes
40 // written or an error code. If ERR_IO_PENDING is returned, |callback| will be
41 // run later with the result.
42 virtual int Write(IOBuffer* buffer,
43 int num_bytes,
44 const CompletionCallback& callback) = 0;
45
46 // Finishes writing. If ERR_IO_PENDING is returned, |callback| will be run
47 // later with the result.
48 virtual int Finish(const CompletionCallback& callback) = 0;
[email protected]9225b062013-10-11 06:02:4849
50 // Returns this instance's pointer as URLFetcherStringWriter when possible.
51 virtual URLFetcherStringWriter* AsStringWriter();
52
53 // Returns this instance's pointer as URLFetcherFileWriter when possible.
54 virtual URLFetcherFileWriter* AsFileWriter();
[email protected]fb501bc2013-03-05 08:38:1555};
56
57// URLFetcherResponseWriter implementation for std::string.
[email protected]86882402013-10-16 10:28:1058class NET_EXPORT URLFetcherStringWriter : public URLFetcherResponseWriter {
[email protected]fb501bc2013-03-05 08:38:1559 public:
[email protected]9225b062013-10-11 06:02:4860 URLFetcherStringWriter();
[email protected]fb501bc2013-03-05 08:38:1561 virtual ~URLFetcherStringWriter();
62
[email protected]9225b062013-10-11 06:02:4863 const std::string& data() const { return data_; }
64
[email protected]fb501bc2013-03-05 08:38:1565 // URLFetcherResponseWriter overrides:
66 virtual int Initialize(const CompletionCallback& callback) OVERRIDE;
67 virtual int Write(IOBuffer* buffer,
68 int num_bytes,
69 const CompletionCallback& callback) OVERRIDE;
70 virtual int Finish(const CompletionCallback& callback) OVERRIDE;
[email protected]9225b062013-10-11 06:02:4871 virtual URLFetcherStringWriter* AsStringWriter() OVERRIDE;
[email protected]fb501bc2013-03-05 08:38:1572
73 private:
[email protected]9225b062013-10-11 06:02:4874 std::string data_;
[email protected]fb501bc2013-03-05 08:38:1575
76 DISALLOW_COPY_AND_ASSIGN(URLFetcherStringWriter);
77};
78
79// URLFetcherResponseWriter implementation for files.
[email protected]86882402013-10-16 10:28:1080class NET_EXPORT URLFetcherFileWriter : public URLFetcherResponseWriter {
[email protected]fb501bc2013-03-05 08:38:1581 public:
[email protected]9225b062013-10-11 06:02:4882 // |file_path| is used as the destination path. If |file_path| is empty,
83 // Initialize() will create a temporary file.
[email protected]0b5ab5092013-11-12 01:09:5684 URLFetcherFileWriter(
85 scoped_refptr<base::SequencedTaskRunner> file_task_runner,
86 const base::FilePath& file_path);
[email protected]fb501bc2013-03-05 08:38:1587 virtual ~URLFetcherFileWriter();
88
[email protected]9225b062013-10-11 06:02:4889 const base::FilePath& file_path() const { return file_path_; }
[email protected]fb501bc2013-03-05 08:38:1590
91 // URLFetcherResponseWriter overrides:
92 virtual int Initialize(const CompletionCallback& callback) OVERRIDE;
93 virtual int Write(IOBuffer* buffer,
94 int num_bytes,
95 const CompletionCallback& callback) OVERRIDE;
96 virtual int Finish(const CompletionCallback& callback) OVERRIDE;
[email protected]9225b062013-10-11 06:02:4897 virtual URLFetcherFileWriter* AsFileWriter() OVERRIDE;
[email protected]fb501bc2013-03-05 08:38:1598
99 // Drops ownership of the file at |file_path_|.
100 // This class will not delete it or write to it again.
101 void DisownFile();
102
[email protected]9225b062013-10-11 06:02:48103 private:
104 // Called when a write has been done.
105 void DidWrite(const CompletionCallback& callback, int result);
[email protected]fb501bc2013-03-05 08:38:15106
107 // Closes the file if it is open and then delete it.
108 void CloseAndDeleteFile();
109
[email protected]fb501bc2013-03-05 08:38:15110 // Callback which gets the result of a temporary file creation.
111 void DidCreateTempFile(const CompletionCallback& callback,
[email protected]d22bad42013-04-04 13:39:12112 base::FilePath* temp_file_path,
113 bool success);
[email protected]fb501bc2013-03-05 08:38:15114
[email protected]d22bad42013-04-04 13:39:12115 // Callback which gets the result of FileStream::Open.
116 void DidOpenFile(const CompletionCallback& callback,
117 int result);
[email protected]fb501bc2013-03-05 08:38:15118
[email protected]c1d9cf742013-09-12 06:37:19119 // Callback which gets the result of closing a file.
120 void CloseComplete(const CompletionCallback& callback, int result);
121
[email protected]fb501bc2013-03-05 08:38:15122 // Callbacks are created for use with base::FileUtilProxy.
123 base::WeakPtrFactory<URLFetcherFileWriter> weak_factory_;
124
125 // Task runner on which file operations should happen.
[email protected]0b5ab5092013-11-12 01:09:56126 scoped_refptr<base::SequencedTaskRunner> file_task_runner_;
[email protected]fb501bc2013-03-05 08:38:15127
128 // Destination file path.
129 // Initialize() creates a temporary file if this variable is empty.
130 base::FilePath file_path_;
131
132 // True when this instance is responsible to delete the file at |file_path_|.
133 bool owns_file_;
134
[email protected]d22bad42013-04-04 13:39:12135 scoped_ptr<FileStream> file_stream_;
[email protected]fb501bc2013-03-05 08:38:15136
[email protected]fb501bc2013-03-05 08:38:15137 DISALLOW_COPY_AND_ASSIGN(URLFetcherFileWriter);
138};
139
140} // namespace net
141
142#endif // NET_URL_REQUEST_URL_FETCHER_RESPONSE_WRITER_H_