Cast: Stop logging kVideoFrameSentToEncoder and rename a couple events.
[chromium-blink-merge.git] / net / url_request / url_fetcher_response_writer.h
blob422a8a400adbfa76aeb5f17929c13b313260dbed
1 // 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.
5 #ifndef NET_URL_REQUEST_URL_FETCHER_RESPONSE_WRITER_H_
6 #define NET_URL_REQUEST_URL_FETCHER_RESPONSE_WRITER_H_
8 #include "base/basictypes.h"
9 #include "base/files/file_path.h"
10 #include "base/memory/ref_counted.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/memory/weak_ptr.h"
13 #include "net/base/completion_callback.h"
14 #include "net/base/net_export.h"
16 namespace base {
17 class SequencedTaskRunner;
18 } // namespace base
20 namespace net {
22 class DrainableIOBuffer;
23 class FileStream;
24 class IOBuffer;
25 class URLFetcherFileWriter;
26 class URLFetcherStringWriter;
28 // This class encapsulates all state involved in writing URLFetcher response
29 // bytes to the destination.
30 class NET_EXPORT URLFetcherResponseWriter {
31 public:
32 virtual ~URLFetcherResponseWriter() {}
34 // Initializes this instance. If ERR_IO_PENDING is returned, |callback| will
35 // be run later with the result. Calling this method again after a
36 // Initialize() success results in discarding already written data.
37 virtual int Initialize(const CompletionCallback& callback) = 0;
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;
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;
50 // Returns this instance's pointer as URLFetcherStringWriter when possible.
51 virtual URLFetcherStringWriter* AsStringWriter();
53 // Returns this instance's pointer as URLFetcherFileWriter when possible.
54 virtual URLFetcherFileWriter* AsFileWriter();
57 // URLFetcherResponseWriter implementation for std::string.
58 class NET_EXPORT URLFetcherStringWriter : public URLFetcherResponseWriter {
59 public:
60 URLFetcherStringWriter();
61 virtual ~URLFetcherStringWriter();
63 const std::string& data() const { return data_; }
65 // 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;
71 virtual URLFetcherStringWriter* AsStringWriter() OVERRIDE;
73 private:
74 std::string data_;
76 DISALLOW_COPY_AND_ASSIGN(URLFetcherStringWriter);
79 // URLFetcherResponseWriter implementation for files.
80 class NET_EXPORT URLFetcherFileWriter : public URLFetcherResponseWriter {
81 public:
82 // |file_path| is used as the destination path. If |file_path| is empty,
83 // Initialize() will create a temporary file.
84 URLFetcherFileWriter(
85 scoped_refptr<base::SequencedTaskRunner> file_task_runner,
86 const base::FilePath& file_path);
87 virtual ~URLFetcherFileWriter();
89 const base::FilePath& file_path() const { return file_path_; }
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;
97 virtual URLFetcherFileWriter* AsFileWriter() OVERRIDE;
99 // Drops ownership of the file at |file_path_|.
100 // This class will not delete it or write to it again.
101 void DisownFile();
103 private:
104 // Called when a write has been done.
105 void DidWrite(const CompletionCallback& callback, int result);
107 // Closes the file if it is open and then delete it.
108 void CloseAndDeleteFile();
110 // Callback which gets the result of a temporary file creation.
111 void DidCreateTempFile(const CompletionCallback& callback,
112 base::FilePath* temp_file_path,
113 bool success);
115 // Callback which gets the result of FileStream::Open.
116 void DidOpenFile(const CompletionCallback& callback,
117 int result);
119 // Callback which gets the result of closing a file.
120 void CloseComplete(const CompletionCallback& callback, int result);
122 // Callbacks are created for use with base::FileUtilProxy.
123 base::WeakPtrFactory<URLFetcherFileWriter> weak_factory_;
125 // Task runner on which file operations should happen.
126 scoped_refptr<base::SequencedTaskRunner> file_task_runner_;
128 // Destination file path.
129 // Initialize() creates a temporary file if this variable is empty.
130 base::FilePath file_path_;
132 // True when this instance is responsible to delete the file at |file_path_|.
133 bool owns_file_;
135 scoped_ptr<FileStream> file_stream_;
137 DISALLOW_COPY_AND_ASSIGN(URLFetcherFileWriter);
140 } // namespace net
142 #endif // NET_URL_REQUEST_URL_FETCHER_RESPONSE_WRITER_H_