OLD | NEW |
---|---|
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef NET_URL_REQUEST_URL_FETCHER_RESPONSE_WRITER_H_ | 5 #ifndef NET_URL_REQUEST_URL_FETCHER_RESPONSE_WRITER_H_ |
6 #define NET_URL_REQUEST_URL_FETCHER_RESPONSE_WRITER_H_ | 6 #define NET_URL_REQUEST_URL_FETCHER_RESPONSE_WRITER_H_ |
7 | 7 |
8 #include "base/basictypes.h" | 8 #include "base/basictypes.h" |
9 #include "base/files/file_path.h" | 9 #include "base/files/file_path.h" |
10 #include "base/memory/ref_counted.h" | 10 #include "base/memory/ref_counted.h" |
11 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
12 #include "base/memory/weak_ptr.h" | 12 #include "base/memory/weak_ptr.h" |
13 #include "net/base/completion_callback.h" | 13 #include "net/base/completion_callback.h" |
14 #include "net/base/net_export.h" | |
14 | 15 |
15 namespace base { | 16 namespace base { |
16 class TaskRunner; | 17 class TaskRunner; |
17 } // namespace base | 18 } // namespace base |
18 | 19 |
19 namespace net { | 20 namespace net { |
20 | 21 |
21 class DrainableIOBuffer; | 22 class DrainableIOBuffer; |
22 class FileStream; | 23 class FileStream; |
23 class IOBuffer; | 24 class IOBuffer; |
24 class URLFetcherFileWriter; | 25 class URLFetcherFileWriter; |
25 class URLFetcherStringWriter; | 26 class URLFetcherStringWriter; |
26 | 27 |
27 // This class encapsulates all state involved in writing URLFetcher response | 28 // This class encapsulates all state involved in writing URLFetcher response |
28 // bytes to the destination. | 29 // bytes to the destination. |
29 class URLFetcherResponseWriter { | 30 class NET_EXPORT URLFetcherResponseWriter { |
30 public: | 31 public: |
31 virtual ~URLFetcherResponseWriter() {} | 32 virtual ~URLFetcherResponseWriter() {} |
32 | 33 |
33 // Initializes this instance. If ERR_IO_PENDING is returned, |callback| will | 34 // Initializes this instance. If ERR_IO_PENDING is returned, |callback| will |
34 // be run later with the result. Calling this method again after a | 35 // be run later with the result. Calling this method again after a |
35 // Initialize() success results in discarding already written data. | 36 // Initialize() success results in discarding already written data. |
36 virtual int Initialize(const CompletionCallback& callback) = 0; | 37 virtual int Initialize(const CompletionCallback& callback) = 0; |
37 | 38 |
38 // Writes |num_bytes| bytes in |buffer|, and returns the number of bytes | 39 // Writes |num_bytes| bytes in |buffer|, and returns the number of bytes |
39 // written or an error code. If ERR_IO_PENDING is returned, |callback| will be | 40 // written or an error code. If ERR_IO_PENDING is returned, |callback| will be |
40 // run later with the result. | 41 // run later with the result. |
41 virtual int Write(IOBuffer* buffer, | 42 virtual int Write(IOBuffer* buffer, |
42 int num_bytes, | 43 int num_bytes, |
43 const CompletionCallback& callback) = 0; | 44 const CompletionCallback& callback) = 0; |
44 | 45 |
45 // Finishes writing. If ERR_IO_PENDING is returned, |callback| will be run | 46 // Finishes writing. If ERR_IO_PENDING is returned, |callback| will be run |
46 // later with the result. | 47 // later with the result. |
47 virtual int Finish(const CompletionCallback& callback) = 0; | 48 virtual int Finish(const CompletionCallback& callback) = 0; |
48 | 49 |
49 // Returns this instance's pointer as URLFetcherStringWriter when possible. | 50 // Returns this instance's pointer as URLFetcherStringWriter when possible. |
50 virtual URLFetcherStringWriter* AsStringWriter(); | 51 virtual URLFetcherStringWriter* AsStringWriter(); |
51 | 52 |
52 // Returns this instance's pointer as URLFetcherFileWriter when possible. | 53 // Returns this instance's pointer as URLFetcherFileWriter when possible. |
53 virtual URLFetcherFileWriter* AsFileWriter(); | 54 virtual URLFetcherFileWriter* AsFileWriter(); |
54 }; | 55 }; |
55 | 56 |
56 // URLFetcherResponseWriter implementation for std::string. | 57 // URLFetcherResponseWriter implementation for std::string. |
57 class URLFetcherStringWriter : public URLFetcherResponseWriter { | 58 class NET_EXPORT URLFetcherStringWriter : public URLFetcherResponseWriter { |
58 public: | 59 public: |
59 URLFetcherStringWriter(); | 60 URLFetcherStringWriter(); |
60 virtual ~URLFetcherStringWriter(); | 61 virtual ~URLFetcherStringWriter(); |
61 | 62 |
62 const std::string& data() const { return data_; } | 63 const std::string& data() const { return data_; } |
63 | 64 |
64 // URLFetcherResponseWriter overrides: | 65 // URLFetcherResponseWriter overrides: |
65 virtual int Initialize(const CompletionCallback& callback) OVERRIDE; | 66 virtual int Initialize(const CompletionCallback& callback) OVERRIDE; |
66 virtual int Write(IOBuffer* buffer, | 67 virtual int Write(IOBuffer* buffer, |
67 int num_bytes, | 68 int num_bytes, |
68 const CompletionCallback& callback) OVERRIDE; | 69 const CompletionCallback& callback) OVERRIDE; |
69 virtual int Finish(const CompletionCallback& callback) OVERRIDE; | 70 virtual int Finish(const CompletionCallback& callback) OVERRIDE; |
70 virtual URLFetcherStringWriter* AsStringWriter() OVERRIDE; | 71 virtual URLFetcherStringWriter* AsStringWriter() OVERRIDE; |
71 | 72 |
72 private: | 73 private: |
73 std::string data_; | 74 std::string data_; |
74 | 75 |
75 DISALLOW_COPY_AND_ASSIGN(URLFetcherStringWriter); | 76 DISALLOW_COPY_AND_ASSIGN(URLFetcherStringWriter); |
76 }; | 77 }; |
77 | 78 |
78 // URLFetcherResponseWriter implementation for files. | 79 // URLFetcherResponseWriter implementation for files. |
79 class URLFetcherFileWriter : public URLFetcherResponseWriter { | 80 class NET_EXPORT URLFetcherFileWriter : public URLFetcherResponseWriter { |
wtc
2013/10/14 19:57:23
Nit: I understand why we need to export the base c
hashimoto
2013/10/15 01:00:35
By exporting these derived classes, URLFetcher use
| |
80 public: | 81 public: |
81 // |file_path| is used as the destination path. If |file_path| is empty, | 82 // |file_path| is used as the destination path. If |file_path| is empty, |
82 // Initialize() will create a temporary file. | 83 // Initialize() will create a temporary file. |
83 URLFetcherFileWriter(scoped_refptr<base::TaskRunner> file_task_runner, | 84 URLFetcherFileWriter(scoped_refptr<base::TaskRunner> file_task_runner, |
84 const base::FilePath& file_path); | 85 const base::FilePath& file_path); |
85 virtual ~URLFetcherFileWriter(); | 86 virtual ~URLFetcherFileWriter(); |
86 | 87 |
87 const base::FilePath& file_path() const { return file_path_; } | 88 const base::FilePath& file_path() const { return file_path_; } |
88 | 89 |
89 // URLFetcherResponseWriter overrides: | 90 // URLFetcherResponseWriter overrides: |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
131 bool owns_file_; | 132 bool owns_file_; |
132 | 133 |
133 scoped_ptr<FileStream> file_stream_; | 134 scoped_ptr<FileStream> file_stream_; |
134 | 135 |
135 DISALLOW_COPY_AND_ASSIGN(URLFetcherFileWriter); | 136 DISALLOW_COPY_AND_ASSIGN(URLFetcherFileWriter); |
136 }; | 137 }; |
137 | 138 |
138 } // namespace net | 139 } // namespace net |
139 | 140 |
140 #endif // NET_URL_REQUEST_URL_FETCHER_RESPONSE_WRITER_H_ | 141 #endif // NET_URL_REQUEST_URL_FETCHER_RESPONSE_WRITER_H_ |
OLD | NEW |