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