blob: adfcbb803491a8fe50579705f256ce96a525e93d [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]fb501bc2013-03-05 08:38:1510#include "base/basictypes.h"
[email protected]d22bad42013-04-04 13:39:1211#include "base/files/file_path.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();
[email protected]fb501bc2013-03-05 08:38:1563 virtual ~URLFetcherStringWriter();
64
[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:
68 virtual int Initialize(const CompletionCallback& callback) OVERRIDE;
69 virtual int Write(IOBuffer* buffer,
70 int num_bytes,
71 const CompletionCallback& callback) OVERRIDE;
72 virtual int Finish(const CompletionCallback& callback) OVERRIDE;
[email protected]9225b062013-10-11 06:02:4873 virtual 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);
[email protected]fb501bc2013-03-05 08:38:1589 virtual ~URLFetcherFileWriter();
90
[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:
94 virtual int Initialize(const CompletionCallback& callback) OVERRIDE;
95 virtual int Write(IOBuffer* buffer,
96 int num_bytes,
97 const CompletionCallback& callback) OVERRIDE;
98 virtual int Finish(const CompletionCallback& callback) OVERRIDE;
[email protected]9225b062013-10-11 06:02:4899 virtual 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 // Callbacks are created for use with base::FileUtilProxy.
125 base::WeakPtrFactory<URLFetcherFileWriter> weak_factory_;
126
127 // Task runner on which file operations should happen.
[email protected]0b5ab5092013-11-12 01:09:56128 scoped_refptr<base::SequencedTaskRunner> file_task_runner_;
[email protected]fb501bc2013-03-05 08:38:15129
130 // Destination file path.
131 // Initialize() creates a temporary file if this variable is empty.
132 base::FilePath file_path_;
133
134 // True when this instance is responsible to delete the file at |file_path_|.
135 bool owns_file_;
136
[email protected]d22bad42013-04-04 13:39:12137 scoped_ptr<FileStream> file_stream_;
[email protected]fb501bc2013-03-05 08:38:15138
[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_