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