• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2012 The Chromium Authors
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_TEST_URL_REQUEST_URL_REQUEST_TEST_JOB_BACKED_BY_FILE_H_
6 #define NET_TEST_URL_REQUEST_URL_REQUEST_TEST_JOB_BACKED_BY_FILE_H_
7 
8 #include <stdint.h>
9 
10 #include <memory>
11 #include <string>
12 #include <vector>
13 
14 #include "base/files/file_path.h"
15 #include "base/memory/scoped_refptr.h"
16 #include "base/memory/weak_ptr.h"
17 #include "net/base/net_errors.h"
18 #include "net/http/http_byte_range.h"
19 #include "net/url_request/url_request.h"
20 #include "net/url_request/url_request_job.h"
21 
22 namespace base {
23 class TaskRunner;
24 }
25 
26 namespace net {
27 
28 class FileStream;
29 
30 // A request job for testing that reads the response body from a file. Used to
31 // be used in production for file URLs, but not only used in tests, as the
32 // parent class of URLRequestMockHttpJob and TestURLRequestJob.
33 //
34 // TODO(mmenke): Consider merging those classes with this one. Could also
35 // simplify the logic a bit.
36 class URLRequestTestJobBackedByFile : public URLRequestJob {
37  public:
38   URLRequestTestJobBackedByFile(
39       URLRequest* request,
40       const base::FilePath& file_path,
41       const scoped_refptr<base::TaskRunner>& file_task_runner);
42 
43   URLRequestTestJobBackedByFile(const URLRequestTestJobBackedByFile&) = delete;
44   URLRequestTestJobBackedByFile& operator=(
45       const URLRequestTestJobBackedByFile&) = delete;
46 
47   // URLRequestJob:
48   void Start() override;
49   void Kill() override;
50   int ReadRawData(IOBuffer* buf, int buf_size) override;
51   bool GetMimeType(std::string* mime_type) const override;
52   void SetExtraRequestHeaders(const HttpRequestHeaders& headers) override;
ShouldServeMimeTypeAsContentTypeHeader()53   void ShouldServeMimeTypeAsContentTypeHeader() {
54     serve_mime_type_as_content_type_ = true;
55   }
56   void GetResponseInfo(HttpResponseInfo* info) override;
57 
58   // An interface for subclasses who wish to monitor read operations.
59   //
60   // |result| is the net::Error code resulting from attempting to open the file.
61   // Called before OnSeekComplete, only called if the request advanced to the
62   // point the file was opened, without being canceled.
63   virtual void OnOpenComplete(int result);
64   // Called at most once.  On success, |result| is the non-negative offset into
65   // the file that the request will read from.  On seek failure, it's a negative
66   // net:Error code.
67   virtual void OnSeekComplete(int64_t result);
68   // Called once per read attempt.  |buf| contains the read data, if any.
69   // |result| is the number of read bytes.  0 (net::OK) indicates EOF, negative
70   // numbers indicate it's a net::Error code.
71   virtual void OnReadComplete(IOBuffer* buf, int result);
72 
73  protected:
74   ~URLRequestTestJobBackedByFile() override;
75 
76   // URLRequestJob implementation.
77   std::unique_ptr<SourceStream> SetUpSourceStream() override;
78 
remaining_bytes()79   int64_t remaining_bytes() const { return remaining_bytes_; }
80 
81   // The OS-specific full path name of the file
82   base::FilePath file_path_;
83 
84  private:
85   // Meta information about the file. It's used as a member in the
86   // URLRequestTestJobBackedByFile and also passed between threads because disk
87   // access is necessary to obtain it.
88   struct FileMetaInfo {
89     FileMetaInfo();
90 
91     // Size of the file.
92     int64_t file_size = 0;
93     // Mime type associated with the file.
94     std::string mime_type;
95     // Result returned from GetMimeTypeFromFile(), i.e. flag showing whether
96     // obtaining of the mime type was successful.
97     bool mime_type_result = false;
98     // Flag showing whether the file exists.
99     bool file_exists = false;
100     // Flag showing whether the file name actually refers to a directory.
101     bool is_directory = false;
102     // Absolute path of the file (i.e. symbolic link is resolved).
103     base::FilePath absolute_path;
104   };
105 
106   // Fetches file info on a background thread.
107   static std::unique_ptr<FileMetaInfo> FetchMetaInfo(
108       const base::FilePath& file_path);
109 
110   // Callback after fetching file info on a background thread.
111   void DidFetchMetaInfo(std::unique_ptr<FileMetaInfo> meta_info);
112 
113   // Callback after opening file on a background thread.
114   void DidOpen(int result);
115 
116   // Callback after seeking to the beginning of |byte_range_| in the file
117   // on a background thread.
118   void DidSeek(int64_t result);
119 
120   // Callback after data is asynchronously read from the file into |buf|.
121   void DidRead(scoped_refptr<IOBuffer> buf, int result);
122 
123   std::unique_ptr<FileStream> stream_;
124   FileMetaInfo meta_info_;
125   const scoped_refptr<base::TaskRunner> file_task_runner_;
126 
127   std::vector<HttpByteRange> byte_ranges_;
128   HttpByteRange byte_range_;
129   int64_t remaining_bytes_ = 0;
130   bool serve_mime_type_as_content_type_ = false;
131 
132   Error range_parse_result_ = OK;
133 
134   base::WeakPtrFactory<URLRequestTestJobBackedByFile> weak_ptr_factory_{this};
135 };
136 
137 }  // namespace net
138 
139 #endif  // NET_TEST_URL_REQUEST_URL_REQUEST_TEST_JOB_BACKED_BY_FILE_H_
140