1// Copyright (c) 2012 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_REQUEST_FILE_JOB_H_
6#define NET_URL_REQUEST_URL_REQUEST_FILE_JOB_H_
7
8#include <string>
9#include <vector>
10
11#include "base/files/file_path.h"
12#include "base/memory/ref_counted.h"
13#include "base/memory/weak_ptr.h"
14#include "net/base/net_export.h"
15#include "net/http/http_byte_range.h"
16#include "net/url_request/url_request.h"
17#include "net/url_request/url_request_job.h"
18
19namespace base{
20struct PlatformFileInfo;
21class TaskRunner;
22}
23namespace file_util {
24struct FileInfo;
25}
26
27namespace net {
28
29class FileStream;
30
31// A request job that handles reading file URLs
32class NET_EXPORT URLRequestFileJob : public URLRequestJob {
33 public:
34  URLRequestFileJob(URLRequest* request,
35                    NetworkDelegate* network_delegate,
36                    const base::FilePath& file_path,
37                    const scoped_refptr<base::TaskRunner>& file_task_runner);
38
39  // URLRequestJob:
40  virtual void Start() OVERRIDE;
41  virtual void Kill() OVERRIDE;
42  virtual bool ReadRawData(IOBuffer* buf,
43                           int buf_size,
44                           int* bytes_read) OVERRIDE;
45  virtual bool IsRedirectResponse(GURL* location,
46                                  int* http_status_code) OVERRIDE;
47  virtual Filter* SetupFilter() const OVERRIDE;
48  virtual bool GetMimeType(std::string* mime_type) const OVERRIDE;
49  virtual void SetExtraRequestHeaders(
50      const HttpRequestHeaders& headers) OVERRIDE;
51
52 protected:
53  virtual ~URLRequestFileJob();
54
55  // The OS-specific full path name of the file
56  base::FilePath file_path_;
57
58 private:
59  // Meta information about the file. It's used as a member in the
60  // URLRequestFileJob and also passed between threads because disk access is
61  // necessary to obtain it.
62  struct FileMetaInfo {
63    FileMetaInfo();
64
65    // Size of the file.
66    int64 file_size;
67    // Mime type associated with the file.
68    std::string mime_type;
69    // Result returned from GetMimeTypeFromFile(), i.e. flag showing whether
70    // obtaining of the mime type was successful.
71    bool mime_type_result;
72    // Flag showing whether the file exists.
73    bool file_exists;
74    // Flag showing whether the file name actually refers to a directory.
75    bool is_directory;
76  };
77
78  // Fetches file info on a background thread.
79  static void FetchMetaInfo(const base::FilePath& file_path,
80                            FileMetaInfo* meta_info);
81
82  // Callback after fetching file info on a background thread.
83  void DidFetchMetaInfo(const FileMetaInfo* meta_info);
84
85  // Callback after opening file on a background thread.
86  void DidOpen(int result);
87
88  // Callback after seeking to the beginning of |byte_range_| in the file
89  // on a background thread.
90  void DidSeek(int64 result);
91
92  // Callback after data is asynchronously read from the file.
93  void DidRead(int result);
94
95  scoped_ptr<FileStream> stream_;
96  FileMetaInfo meta_info_;
97  const scoped_refptr<base::TaskRunner> file_task_runner_;
98
99  HttpByteRange byte_range_;
100  int64 remaining_bytes_;
101
102  base::WeakPtrFactory<URLRequestFileJob> weak_ptr_factory_;
103
104  DISALLOW_COPY_AND_ASSIGN(URLRequestFileJob);
105};
106
107}  // namespace net
108
109#endif  // NET_URL_REQUEST_URL_REQUEST_FILE_JOB_H_
110