Home | History | Annotate | Download | only in url_request
      1 // Copyright (c) 2010 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_DIR_JOB_H_
      6 #define NET_URL_REQUEST_URL_REQUEST_FILE_DIR_JOB_H_
      7 #pragma once
      8 
      9 #include <string>
     10 
     11 #include "base/file_path.h"
     12 #include "base/file_util.h"
     13 #include "base/task.h"
     14 #include "net/base/directory_lister.h"
     15 #include "net/url_request/url_request_job.h"
     16 
     17 namespace net {
     18 
     19 class URLRequestFileDirJob
     20   : public URLRequestJob,
     21     public DirectoryLister::DirectoryListerDelegate {
     22  public:
     23   URLRequestFileDirJob(URLRequest* request, const FilePath& dir_path);
     24 
     25   bool list_complete() const { return list_complete_; }
     26 
     27   virtual void StartAsync();
     28 
     29   // Overridden from URLRequestJob:
     30   virtual void Start();
     31   virtual void Kill();
     32   virtual bool ReadRawData(IOBuffer* buf, int buf_size, int *bytes_read);
     33   virtual bool GetMimeType(std::string* mime_type) const;
     34   virtual bool GetCharset(std::string* charset);
     35 
     36   // Overridden from DirectoryLister::DirectoryListerDelegate:
     37   virtual void OnListFile(
     38       const DirectoryLister::DirectoryListerData& data);
     39   virtual void OnListDone(int error);
     40 
     41  private:
     42   virtual ~URLRequestFileDirJob();
     43 
     44   void CloseLister();
     45 
     46   // When we have data and a read has been pending, this function
     47   // will fill the response buffer and notify the request
     48   // appropriately.
     49   void CompleteRead();
     50 
     51   // Fills a buffer with the output.
     52   bool FillReadBuffer(char *buf, int buf_size, int *bytes_read);
     53 
     54   scoped_refptr<DirectoryLister> lister_;
     55   FilePath dir_path_;
     56   std::string data_;
     57   bool canceled_;
     58 
     59   // Indicates whether we have the complete list of the dir
     60   bool list_complete_;
     61 
     62   // Indicates whether we have written the HTML header
     63   bool wrote_header_;
     64 
     65   // To simulate Async IO, we hold onto the Reader's buffer while
     66   // we wait for IO to complete.  When done, we fill the buffer
     67   // manually.
     68   bool read_pending_;
     69   scoped_refptr<IOBuffer> read_buffer_;
     70   int read_buffer_length_;
     71   ScopedRunnableMethodFactory<URLRequestFileDirJob> method_factory_;
     72 
     73   DISALLOW_COPY_AND_ASSIGN(URLRequestFileDirJob);
     74 };
     75 
     76 }  // namespace net
     77 
     78 #endif  // NET_URL_REQUEST_URL_REQUEST_FILE_DIR_JOB_H_
     79