url_request_file_job.h revision a02191e04bc25c4935f804f2c080ae28663d096d
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  // The OS-specific full path name of the file
59  base::FilePath file_path_;
60
61 private:
62  // Meta information about the file. It's used as a member in the
63  // URLRequestFileJob and also passed between threads because disk access is
64  // necessary to obtain it.
65  struct FileMetaInfo {
66    FileMetaInfo();
67
68    // Size of the file.
69    int64 file_size;
70    // Mime type associated with the file.
71    std::string mime_type;
72    // Result returned from GetMimeTypeFromFile(), i.e. flag showing whether
73    // obtaining of the mime type was successful.
74    bool mime_type_result;
75    // Flag showing whether the file exists.
76    bool file_exists;
77    // Flag showing whether the file name actually refers to a directory.
78    bool is_directory;
79  };
80
81  // Fetches file info on a background thread.
82  static void FetchMetaInfo(const base::FilePath& file_path,
83                            FileMetaInfo* meta_info);
84
85  // Callback after fetching file info on a background thread.
86  void DidFetchMetaInfo(const FileMetaInfo* meta_info);
87
88  // Callback after opening file on a background thread.
89  void DidOpen(int result);
90
91  // Callback after seeking to the beginning of |byte_range_| in the file
92  // on a background thread.
93  void DidSeek(int64 result);
94
95  // Callback after data is asynchronously read from the file into |buf|.
96  void DidRead(scoped_refptr<net::IOBuffer> buf, int result);
97
98  scoped_ptr<FileStream> stream_;
99  FileMetaInfo meta_info_;
100  const scoped_refptr<base::TaskRunner> file_task_runner_;
101
102  HttpByteRange byte_range_;
103  int64 remaining_bytes_;
104
105  base::WeakPtrFactory<URLRequestFileJob> weak_ptr_factory_;
106
107  DISALLOW_COPY_AND_ASSIGN(URLRequestFileJob);
108};
109
110}  // namespace net
111
112#endif  // NET_URL_REQUEST_URL_REQUEST_FILE_JOB_H_
113