15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved. 25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be 35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file. 45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifndef NET_URL_REQUEST_URL_REQUEST_FILE_JOB_H_ 65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define NET_URL_REQUEST_URL_REQUEST_FILE_JOB_H_ 75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <string> 95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <vector> 105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/files/file_path.h" 123551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)#include "base/memory/ref_counted.h" 132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/memory/weak_ptr.h" 145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/base/net_export.h" 155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/http/http_byte_range.h" 165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/url_request/url_request.h" 175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/url_request/url_request_job.h" 185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace base{ 205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)struct PlatformFileInfo; 213551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)class TaskRunner; 225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace file_util { 245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)struct FileInfo; 255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace net { 285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class FileStream; 305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// A request job that handles reading file URLs 325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class NET_EXPORT URLRequestFileJob : public URLRequestJob { 335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public: 345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) URLRequestFileJob(URLRequest* request, 355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) NetworkDelegate* network_delegate, 363551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) const base::FilePath& file_path, 373551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) const scoped_refptr<base::TaskRunner>& file_task_runner); 385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // URLRequestJob: 405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual void Start() OVERRIDE; 415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual void Kill() OVERRIDE; 425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual bool ReadRawData(IOBuffer* buf, 435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) int buf_size, 445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) int* bytes_read) OVERRIDE; 455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual bool IsRedirectResponse(GURL* location, 465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) int* http_status_code) OVERRIDE; 475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual Filter* SetupFilter() const OVERRIDE; 485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual bool GetMimeType(std::string* mime_type) const OVERRIDE; 495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual void SetExtraRequestHeaders( 505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const HttpRequestHeaders& headers) OVERRIDE; 515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) protected: 535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual ~URLRequestFileJob(); 545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // The OS-specific full path name of the file 562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) base::FilePath file_path_; 575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private: 592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // Meta information about the file. It's used as a member in the 602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // URLRequestFileJob and also passed between threads because disk access is 612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // necessary to obtain it. 622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) struct FileMetaInfo { 632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) FileMetaInfo(); 642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // Size of the file. 662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) int64 file_size; 672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // Mime type associated with the file. 682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) std::string mime_type; 692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // Result returned from GetMimeTypeFromFile(), i.e. flag showing whether 702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // obtaining of the mime type was successful. 712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) bool mime_type_result; 722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // Flag showing whether the file exists. 732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) bool file_exists; 742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // Flag showing whether the file name actually refers to a directory. 752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) bool is_directory; 762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) }; 772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // Fetches file info on a background thread. 792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) static void FetchMetaInfo(const base::FilePath& file_path, 802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) FileMetaInfo* meta_info); 812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Callback after fetching file info on a background thread. 832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) void DidFetchMetaInfo(const FileMetaInfo* meta_info); 842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // Callback after opening file on a background thread. 862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) void DidOpen(int result); 872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // Callback after seeking to the beginning of |byte_range_| in the file 892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // on a background thread. 902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) void DidSeek(int64 result); 915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Callback after data is asynchronously read from the file. 935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) void DidRead(int result); 945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) scoped_ptr<FileStream> stream_; 962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) FileMetaInfo meta_info_; 973551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) const scoped_refptr<base::TaskRunner> file_task_runner_; 985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) HttpByteRange byte_range_; 1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) int64 remaining_bytes_; 1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) base::WeakPtrFactory<URLRequestFileJob> weak_ptr_factory_; 1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) DISALLOW_COPY_AND_ASSIGN(URLRequestFileJob); 1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}; 1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} // namespace net 1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif // NET_URL_REQUEST_URL_REQUEST_FILE_JOB_H_ 110