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 void Start() override
;
41 bool ReadRawData(IOBuffer
* buf
, int buf_size
, int* bytes_read
) override
;
42 bool IsRedirectResponse(GURL
* location
, int* http_status_code
) override
;
43 Filter
* SetupFilter() const override
;
44 bool GetMimeType(std::string
* mime_type
) const override
;
45 void SetExtraRequestHeaders(const HttpRequestHeaders
& headers
) override
;
47 // An interface for subclasses who wish to monitor read operations.
48 virtual void OnSeekComplete(int64 result
);
49 virtual void OnReadComplete(net::IOBuffer
* buf
, int result
);
52 ~URLRequestFileJob() override
;
54 int64
remaining_bytes() const { return remaining_bytes_
; }
56 // The OS-specific full path name of the file
57 base::FilePath file_path_
;
60 // Meta information about the file. It's used as a member in the
61 // URLRequestFileJob and also passed between threads because disk access is
62 // necessary to obtain it.
68 // Mime type associated with the file.
69 std::string mime_type
;
70 // Result returned from GetMimeTypeFromFile(), i.e. flag showing whether
71 // obtaining of the mime type was successful.
72 bool mime_type_result
;
73 // Flag showing whether the file exists.
75 // Flag showing whether the file name actually refers to a directory.
79 // Fetches file info on a background thread.
80 static void FetchMetaInfo(const base::FilePath
& file_path
,
81 FileMetaInfo
* meta_info
);
83 // Callback after fetching file info on a background thread.
84 void DidFetchMetaInfo(const FileMetaInfo
* meta_info
);
86 // Callback after opening file on a background thread.
87 void DidOpen(int result
);
89 // Callback after seeking to the beginning of |byte_range_| in the file
90 // on a background thread.
91 void DidSeek(int64 result
);
93 // Callback after data is asynchronously read from the file into |buf|.
94 void DidRead(scoped_refptr
<net::IOBuffer
> buf
, int result
);
96 scoped_ptr
<FileStream
> stream_
;
97 FileMetaInfo meta_info_
;
98 const scoped_refptr
<base::TaskRunner
> file_task_runner_
;
100 HttpByteRange byte_range_
;
101 int64 remaining_bytes_
;
103 base::WeakPtrFactory
<URLRequestFileJob
> weak_ptr_factory_
;
105 DISALLOW_COPY_AND_ASSIGN(URLRequestFileJob
);
110 #endif // NET_URL_REQUEST_URL_REQUEST_FILE_JOB_H_