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.
5 #ifndef NET_URL_REQUEST_URL_REQUEST_FILE_JOB_H_
6 #define NET_URL_REQUEST_URL_REQUEST_FILE_JOB_H_
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"
30 // A request job that handles reading file URLs
31 class NET_EXPORT URLRequestFileJob
: public URLRequestJob
{
33 URLRequestFileJob(URLRequest
* request
,
34 NetworkDelegate
* network_delegate
,
35 const base::FilePath
& file_path
,
36 const scoped_refptr
<base::TaskRunner
>& file_task_runner
);
39 virtual void Start() OVERRIDE
;
40 virtual void Kill() OVERRIDE
;
41 virtual bool ReadRawData(IOBuffer
* buf
,
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
;
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
);
56 virtual ~URLRequestFileJob();
58 int64
remaining_bytes() const { return remaining_bytes_
; }
60 // The OS-specific full path name of the file
61 base::FilePath file_path_
;
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.
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.
79 // Flag showing whether the file name actually refers to a directory.
83 // Fetches file info on a background thread.
84 static void FetchMetaInfo(const base::FilePath
& file_path
,
85 FileMetaInfo
* meta_info
);
87 // Callback after fetching file info on a background thread.
88 void DidFetchMetaInfo(const FileMetaInfo
* meta_info
);
90 // Callback after opening file on a background thread.
91 void DidOpen(int result
);
93 // Callback after seeking to the beginning of |byte_range_| in the file
94 // on a background thread.
95 void DidSeek(int64 result
);
97 // Callback after data is asynchronously read from the file into |buf|.
98 void DidRead(scoped_refptr
<net::IOBuffer
> buf
, int result
);
100 scoped_ptr
<FileStream
> stream_
;
101 FileMetaInfo meta_info_
;
102 const scoped_refptr
<base::TaskRunner
> file_task_runner_
;
104 HttpByteRange byte_range_
;
105 int64 remaining_bytes_
;
107 base::WeakPtrFactory
<URLRequestFileJob
> weak_ptr_factory_
;
109 DISALLOW_COPY_AND_ASSIGN(URLRequestFileJob
);
114 #endif // NET_URL_REQUEST_URL_REQUEST_FILE_JOB_H_