url_request_file_job.h revision 3551c9c881056c480085172ff9840cab31610854
149683f3c961379fbc088871a5d6304950f1f1cbcJustin Holewinski// Copyright (c) 2012 The Chromium Authors. All rights reserved.
249683f3c961379fbc088871a5d6304950f1f1cbcJustin Holewinski// Use of this source code is governed by a BSD-style license that can be
349683f3c961379fbc088871a5d6304950f1f1cbcJustin Holewinski// found in the LICENSE file.
449683f3c961379fbc088871a5d6304950f1f1cbcJustin Holewinski
549683f3c961379fbc088871a5d6304950f1f1cbcJustin Holewinski#ifndef NET_URL_REQUEST_URL_REQUEST_FILE_JOB_H_
649683f3c961379fbc088871a5d6304950f1f1cbcJustin Holewinski#define NET_URL_REQUEST_URL_REQUEST_FILE_JOB_H_
749683f3c961379fbc088871a5d6304950f1f1cbcJustin Holewinski
849683f3c961379fbc088871a5d6304950f1f1cbcJustin Holewinski#include <string>
949683f3c961379fbc088871a5d6304950f1f1cbcJustin Holewinski#include <vector>
1049683f3c961379fbc088871a5d6304950f1f1cbcJustin Holewinski
1149683f3c961379fbc088871a5d6304950f1f1cbcJustin Holewinski#include "base/files/file_path.h"
1249683f3c961379fbc088871a5d6304950f1f1cbcJustin Holewinski#include "base/memory/ref_counted.h"
1349683f3c961379fbc088871a5d6304950f1f1cbcJustin Holewinski#include "base/memory/weak_ptr.h"
1449683f3c961379fbc088871a5d6304950f1f1cbcJustin Holewinski#include "net/base/net_export.h"
150b8c9a80f20772c3793201ab5b251d3520b9cea3Chandler Carruth#include "net/http/http_byte_range.h"
160b8c9a80f20772c3793201ab5b251d3520b9cea3Chandler Carruth#include "net/url_request/url_request.h"
17d04a8d4b33ff316ca4cf961e06c9e312eff8e64fChandler Carruth#include "net/url_request/url_request_job.h"
1849683f3c961379fbc088871a5d6304950f1f1cbcJustin Holewinski
1949683f3c961379fbc088871a5d6304950f1f1cbcJustin Holewinskinamespace base{
20d04a8d4b33ff316ca4cf961e06c9e312eff8e64fChandler Carruthstruct PlatformFileInfo;
2149683f3c961379fbc088871a5d6304950f1f1cbcJustin Holewinskiclass TaskRunner;
2249683f3c961379fbc088871a5d6304950f1f1cbcJustin Holewinski}
2349683f3c961379fbc088871a5d6304950f1f1cbcJustin Holewinskinamespace file_util {
2449683f3c961379fbc088871a5d6304950f1f1cbcJustin Holewinskistruct FileInfo;
2549683f3c961379fbc088871a5d6304950f1f1cbcJustin Holewinski}
2649683f3c961379fbc088871a5d6304950f1f1cbcJustin Holewinski
2749683f3c961379fbc088871a5d6304950f1f1cbcJustin Holewinskinamespace net {
283639ce2575660a0e6938d2e84e8bd9a738fd7051Justin Holewinski
293639ce2575660a0e6938d2e84e8bd9a738fd7051Justin Holewinskiclass FileStream;
303639ce2575660a0e6938d2e84e8bd9a738fd7051Justin Holewinski
313639ce2575660a0e6938d2e84e8bd9a738fd7051Justin Holewinski// A request job that handles reading file URLs
3249683f3c961379fbc088871a5d6304950f1f1cbcJustin Holewinskiclass NET_EXPORT URLRequestFileJob : public URLRequestJob {
3349683f3c961379fbc088871a5d6304950f1f1cbcJustin Holewinski public:
343639ce2575660a0e6938d2e84e8bd9a738fd7051Justin Holewinski  URLRequestFileJob(URLRequest* request,
3549683f3c961379fbc088871a5d6304950f1f1cbcJustin Holewinski                    NetworkDelegate* network_delegate,
363639ce2575660a0e6938d2e84e8bd9a738fd7051Justin Holewinski                    const base::FilePath& file_path,
373639ce2575660a0e6938d2e84e8bd9a738fd7051Justin Holewinski                    const scoped_refptr<base::TaskRunner>& file_task_runner);
3849683f3c961379fbc088871a5d6304950f1f1cbcJustin Holewinski
393639ce2575660a0e6938d2e84e8bd9a738fd7051Justin Holewinski  // URLRequestJob:
403639ce2575660a0e6938d2e84e8bd9a738fd7051Justin Holewinski  virtual void Start() OVERRIDE;
413639ce2575660a0e6938d2e84e8bd9a738fd7051Justin Holewinski  virtual void Kill() OVERRIDE;
423639ce2575660a0e6938d2e84e8bd9a738fd7051Justin Holewinski  virtual bool ReadRawData(IOBuffer* buf,
433639ce2575660a0e6938d2e84e8bd9a738fd7051Justin Holewinski                           int buf_size,
4449683f3c961379fbc088871a5d6304950f1f1cbcJustin Holewinski                           int* bytes_read) OVERRIDE;
45b9c26dcb2438266567ce94570bf294d00d10cc87Justin Holewinski  virtual bool IsRedirectResponse(GURL* location,
46b9c26dcb2438266567ce94570bf294d00d10cc87Justin Holewinski                                  int* http_status_code) OVERRIDE;
47b9c26dcb2438266567ce94570bf294d00d10cc87Justin Holewinski  virtual Filter* SetupFilter() const OVERRIDE;
48b9c26dcb2438266567ce94570bf294d00d10cc87Justin Holewinski  virtual bool GetMimeType(std::string* mime_type) const OVERRIDE;
49b9c26dcb2438266567ce94570bf294d00d10cc87Justin Holewinski  virtual void SetExtraRequestHeaders(
5049683f3c961379fbc088871a5d6304950f1f1cbcJustin Holewinski      const HttpRequestHeaders& headers) OVERRIDE;
5149683f3c961379fbc088871a5d6304950f1f1cbcJustin Holewinski
5249683f3c961379fbc088871a5d6304950f1f1cbcJustin Holewinski protected:
5349683f3c961379fbc088871a5d6304950f1f1cbcJustin Holewinski  virtual ~URLRequestFileJob();
5449683f3c961379fbc088871a5d6304950f1f1cbcJustin Holewinski
5549683f3c961379fbc088871a5d6304950f1f1cbcJustin Holewinski  // The OS-specific full path name of the file
5649683f3c961379fbc088871a5d6304950f1f1cbcJustin Holewinski  base::FilePath file_path_;
5749683f3c961379fbc088871a5d6304950f1f1cbcJustin Holewinski
5849683f3c961379fbc088871a5d6304950f1f1cbcJustin Holewinski private:
593639ce2575660a0e6938d2e84e8bd9a738fd7051Justin Holewinski  // Meta information about the file. It's used as a member in the
603639ce2575660a0e6938d2e84e8bd9a738fd7051Justin Holewinski  // URLRequestFileJob and also passed between threads because disk access is
6149683f3c961379fbc088871a5d6304950f1f1cbcJustin Holewinski  // necessary to obtain it.
6249683f3c961379fbc088871a5d6304950f1f1cbcJustin Holewinski  struct FileMetaInfo {
6349683f3c961379fbc088871a5d6304950f1f1cbcJustin Holewinski    FileMetaInfo();
6449683f3c961379fbc088871a5d6304950f1f1cbcJustin Holewinski
6549683f3c961379fbc088871a5d6304950f1f1cbcJustin Holewinski    // Size of the file.
6649683f3c961379fbc088871a5d6304950f1f1cbcJustin Holewinski    int64 file_size;
673639ce2575660a0e6938d2e84e8bd9a738fd7051Justin Holewinski    // Mime type associated with the file.
683639ce2575660a0e6938d2e84e8bd9a738fd7051Justin Holewinski    std::string mime_type;
693639ce2575660a0e6938d2e84e8bd9a738fd7051Justin Holewinski    // Result returned from GetMimeTypeFromFile(), i.e. flag showing whether
703639ce2575660a0e6938d2e84e8bd9a738fd7051Justin Holewinski    // obtaining of the mime type was successful.
713639ce2575660a0e6938d2e84e8bd9a738fd7051Justin Holewinski    bool mime_type_result;
723639ce2575660a0e6938d2e84e8bd9a738fd7051Justin Holewinski    // Flag showing whether the file exists.
7349683f3c961379fbc088871a5d6304950f1f1cbcJustin Holewinski    bool file_exists;
7449683f3c961379fbc088871a5d6304950f1f1cbcJustin Holewinski    // Flag showing whether the file name actually refers to a directory.
7549683f3c961379fbc088871a5d6304950f1f1cbcJustin Holewinski    bool is_directory;
76af878315192a9fa5b534364e327c24aeb8d73b5aBenjamin Kramer  };
77af878315192a9fa5b534364e327c24aeb8d73b5aBenjamin Kramer
7849683f3c961379fbc088871a5d6304950f1f1cbcJustin Holewinski  // Fetches file info on a background thread.
7949683f3c961379fbc088871a5d6304950f1f1cbcJustin Holewinski  static void FetchMetaInfo(const base::FilePath& file_path,
8049683f3c961379fbc088871a5d6304950f1f1cbcJustin Holewinski                            FileMetaInfo* meta_info);
8149683f3c961379fbc088871a5d6304950f1f1cbcJustin Holewinski
82b9c26dcb2438266567ce94570bf294d00d10cc87Justin Holewinski  // Callback after fetching file info on a background thread.
83b9c26dcb2438266567ce94570bf294d00d10cc87Justin Holewinski  void DidFetchMetaInfo(const FileMetaInfo* meta_info);
8449683f3c961379fbc088871a5d6304950f1f1cbcJustin Holewinski
8549683f3c961379fbc088871a5d6304950f1f1cbcJustin Holewinski  // Callback after opening file on a background thread.
8649683f3c961379fbc088871a5d6304950f1f1cbcJustin Holewinski  void DidOpen(int result);
8749683f3c961379fbc088871a5d6304950f1f1cbcJustin Holewinski
8849683f3c961379fbc088871a5d6304950f1f1cbcJustin Holewinski  // Callback after seeking to the beginning of |byte_range_| in the file
8949683f3c961379fbc088871a5d6304950f1f1cbcJustin Holewinski  // on a background thread.
9049683f3c961379fbc088871a5d6304950f1f1cbcJustin Holewinski  void DidSeek(int64 result);
9149683f3c961379fbc088871a5d6304950f1f1cbcJustin Holewinski
923639ce2575660a0e6938d2e84e8bd9a738fd7051Justin Holewinski  // Callback after data is asynchronously read from the file.
9349683f3c961379fbc088871a5d6304950f1f1cbcJustin Holewinski  void DidRead(int result);
9449683f3c961379fbc088871a5d6304950f1f1cbcJustin Holewinski
953639ce2575660a0e6938d2e84e8bd9a738fd7051Justin Holewinski  scoped_ptr<FileStream> stream_;
9649683f3c961379fbc088871a5d6304950f1f1cbcJustin Holewinski  FileMetaInfo meta_info_;
9749683f3c961379fbc088871a5d6304950f1f1cbcJustin Holewinski  const scoped_refptr<base::TaskRunner> file_task_runner_;
9849683f3c961379fbc088871a5d6304950f1f1cbcJustin Holewinski
9949683f3c961379fbc088871a5d6304950f1f1cbcJustin Holewinski  HttpByteRange byte_range_;
10049683f3c961379fbc088871a5d6304950f1f1cbcJustin Holewinski  int64 remaining_bytes_;
10149683f3c961379fbc088871a5d6304950f1f1cbcJustin Holewinski
10249683f3c961379fbc088871a5d6304950f1f1cbcJustin Holewinski  base::WeakPtrFactory<URLRequestFileJob> weak_ptr_factory_;
10349683f3c961379fbc088871a5d6304950f1f1cbcJustin Holewinski
10449683f3c961379fbc088871a5d6304950f1f1cbcJustin Holewinski  DISALLOW_COPY_AND_ASSIGN(URLRequestFileJob);
1057eacad03efda36e09ebd96e95d7891cadaaa9087Justin Holewinski};
1067eacad03efda36e09ebd96e95d7891cadaaa9087Justin Holewinski
1077eacad03efda36e09ebd96e95d7891cadaaa9087Justin Holewinski}  // namespace net
1087eacad03efda36e09ebd96e95d7891cadaaa9087Justin Holewinski
1097eacad03efda36e09ebd96e95d7891cadaaa9087Justin Holewinski#endif  // NET_URL_REQUEST_URL_REQUEST_FILE_JOB_H_
1107eacad03efda36e09ebd96e95d7891cadaaa9087Justin Holewinski