Revert of Revert of Use BoringSSL in the implementation of ClearKey for Chromecast...
[chromium-blink-merge.git] / net / url_request / url_fetcher_response_writer.h
blobfb9952226fb6f010ba26af456f21b287cdb4347a
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 <string>
10 #include "base/basictypes.h"
11 #include "base/files/file_path.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/memory/weak_ptr.h"
15 #include "net/base/completion_callback.h"
16 #include "net/base/net_export.h"
18 namespace base {
19 class SequencedTaskRunner;
20 } // namespace base
22 namespace net {
24 class DrainableIOBuffer;
25 class FileStream;
26 class IOBuffer;
27 class URLFetcherFileWriter;
28 class URLFetcherStringWriter;
30 // This class encapsulates all state involved in writing URLFetcher response
31 // bytes to the destination.
32 class NET_EXPORT URLFetcherResponseWriter {
33 public:
34 virtual ~URLFetcherResponseWriter() {}
36 // Initializes this instance. If ERR_IO_PENDING is returned, |callback| will
37 // be run later with the result. Calling this method again after a
38 // Initialize() success results in discarding already written data.
39 virtual int Initialize(const CompletionCallback& callback) = 0;
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;
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;
52 // Returns this instance's pointer as URLFetcherStringWriter when possible.
53 virtual URLFetcherStringWriter* AsStringWriter();
55 // Returns this instance's pointer as URLFetcherFileWriter when possible.
56 virtual URLFetcherFileWriter* AsFileWriter();
59 // URLFetcherResponseWriter implementation for std::string.
60 class NET_EXPORT URLFetcherStringWriter : public URLFetcherResponseWriter {
61 public:
62 URLFetcherStringWriter();
63 ~URLFetcherStringWriter() override;
65 const std::string& data() const { return data_; }
67 // URLFetcherResponseWriter overrides:
68 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;
75 private:
76 std::string data_;
78 DISALLOW_COPY_AND_ASSIGN(URLFetcherStringWriter);
81 // URLFetcherResponseWriter implementation for files.
82 class NET_EXPORT URLFetcherFileWriter : public URLFetcherResponseWriter {
83 public:
84 // |file_path| is used as the destination path. If |file_path| is empty,
85 // Initialize() will create a temporary file.
86 URLFetcherFileWriter(
87 scoped_refptr<base::SequencedTaskRunner> file_task_runner,
88 const base::FilePath& file_path);
89 ~URLFetcherFileWriter() override;
91 const base::FilePath& file_path() const { return file_path_; }
93 // URLFetcherResponseWriter overrides:
94 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;
101 // Drops ownership of the file at |file_path_|.
102 // This class will not delete it or write to it again.
103 void DisownFile();
105 private:
106 // Called when a write has been done.
107 void DidWrite(const CompletionCallback& callback, int result);
109 // Closes the file if it is open and then delete it.
110 void CloseAndDeleteFile();
112 // Callback which gets the result of a temporary file creation.
113 void DidCreateTempFile(const CompletionCallback& callback,
114 base::FilePath* temp_file_path,
115 bool success);
117 // Callback which gets the result of FileStream::Open.
118 void DidOpenFile(const CompletionCallback& callback,
119 int result);
121 // Callback which gets the result of closing a file.
122 void CloseComplete(const CompletionCallback& callback, int result);
124 // Task runner on which file operations should happen.
125 scoped_refptr<base::SequencedTaskRunner> file_task_runner_;
127 // Destination file path.
128 // Initialize() creates a temporary file if this variable is empty.
129 base::FilePath file_path_;
131 // True when this instance is responsible to delete the file at |file_path_|.
132 bool owns_file_;
134 scoped_ptr<FileStream> file_stream_;
136 // Callbacks are created for use with base::FileUtilProxy.
137 base::WeakPtrFactory<URLFetcherFileWriter> weak_factory_;
139 DISALLOW_COPY_AND_ASSIGN(URLFetcherFileWriter);
142 } // namespace net
144 #endif // NET_URL_REQUEST_URL_FETCHER_RESPONSE_WRITER_H_