Home | History | Annotate | Download | only in download
      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 CONTENT_BROWSER_DOWNLOAD_SAVE_FILE_RESOURCE_HANDLER_H_
      6 #define CONTENT_BROWSER_DOWNLOAD_SAVE_FILE_RESOURCE_HANDLER_H_
      7 
      8 #include <string>
      9 
     10 #include "base/memory/ref_counted.h"
     11 #include "content/browser/loader/resource_handler.h"
     12 #include "url/gurl.h"
     13 
     14 namespace content {
     15 class SaveFileManager;
     16 
     17 // Forwards data to the save thread.
     18 class SaveFileResourceHandler : public ResourceHandler {
     19  public:
     20   SaveFileResourceHandler(int render_process_host_id,
     21                           int render_view_id,
     22                           const GURL& url,
     23                           SaveFileManager* manager);
     24   virtual ~SaveFileResourceHandler();
     25 
     26   // ResourceHandler Implementation:
     27   virtual bool OnUploadProgress(int request_id,
     28                                 uint64 position,
     29                                 uint64 size) OVERRIDE;
     30 
     31   // Saves the redirected URL to final_url_, we need to use the original
     32   // URL to match original request.
     33   virtual bool OnRequestRedirected(int request_id,
     34                                    const GURL& url,
     35                                    ResourceResponse* response,
     36                                    bool* defer) OVERRIDE;
     37 
     38   // Sends the download creation information to the download thread.
     39   virtual bool OnResponseStarted(int request_id,
     40                                  ResourceResponse* response,
     41                                  bool* defer) OVERRIDE;
     42 
     43   // Pass-through implementation.
     44   virtual bool OnWillStart(int request_id,
     45                            const GURL& url,
     46                            bool* defer) OVERRIDE;
     47 
     48   // Creates a new buffer, which will be handed to the download thread for file
     49   // writing and deletion.
     50   virtual bool OnWillRead(int request_id,
     51                           scoped_refptr<net::IOBuffer>* buf,
     52                           int* buf_size,
     53                           int min_size) OVERRIDE;
     54 
     55   // Passes the buffer to the download file writer.
     56   virtual bool OnReadCompleted(int request_id, int bytes_read,
     57                                bool* defer) OVERRIDE;
     58 
     59   virtual void OnResponseCompleted(int request_id,
     60                                    const net::URLRequestStatus& status,
     61                                    const std::string& security_info,
     62                                    bool* defer) OVERRIDE;
     63 
     64   // N/A to this flavor of SaveFileResourceHandler.
     65   virtual void OnDataDownloaded(int request_id, int bytes_downloaded) OVERRIDE;
     66 
     67   // If the content-length header is not present (or contains something other
     68   // than numbers), StringToInt64 returns 0, which indicates 'unknown size' and
     69   // is handled correctly by the SaveManager.
     70   void set_content_length(const std::string& content_length);
     71 
     72   void set_content_disposition(const std::string& content_disposition) {
     73     content_disposition_ = content_disposition;
     74   }
     75 
     76  private:
     77   int save_id_;
     78   int render_process_id_;
     79   int render_view_id_;
     80   scoped_refptr<net::IOBuffer> read_buffer_;
     81   std::string content_disposition_;
     82   GURL url_;
     83   GURL final_url_;
     84   int64 content_length_;
     85   SaveFileManager* save_manager_;
     86 
     87   static const int kReadBufSize = 32768;  // bytes
     88 
     89   DISALLOW_COPY_AND_ASSIGN(SaveFileResourceHandler);
     90 };
     91 
     92 }  // namespace content
     93 
     94 #endif  // CONTENT_BROWSER_DOWNLOAD_SAVE_FILE_RESOURCE_HANDLER_H_
     95