Home | History | Annotate | Download | only in chrome_frame
      1 // Copyright (c) 2011 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 CHROME_FRAME_PLUGIN_URL_REQUEST_H_
      6 #define CHROME_FRAME_PLUGIN_URL_REQUEST_H_
      7 
      8 #include <string>
      9 #include <vector>
     10 
     11 #include "base/memory/ref_counted.h"
     12 #include "base/time/time.h"
     13 #include "base/win/scoped_comptr.h"
     14 #include "chrome_frame/chrome_frame_delegate.h"
     15 #include "chrome_frame/urlmon_upload_data_stream.h"
     16 #include "ipc/ipc_message.h"
     17 #include "net/base/host_port_pair.h"
     18 #include "net/base/upload_data.h"
     19 #include "net/url_request/url_request_status.h"
     20 #include "webkit/common/resource_type.h"
     21 
     22 class PluginUrlRequest;
     23 class PluginUrlRequestDelegate;
     24 class PluginUrlRequestManager;
     25 
     26 class DECLSPEC_NOVTABLE PluginUrlRequestDelegate {  // NOLINT
     27  public:
     28   virtual void OnResponseStarted(
     29       int request_id, const char* mime_type, const char* headers, int size,
     30       base::Time last_modified, const std::string& redirect_url,
     31       int redirect_status, const net::HostPortPair& socket_address,
     32       uint64 upload_size) = 0;
     33   virtual void OnReadComplete(int request_id, const std::string& data) = 0;
     34   virtual void OnResponseEnd(int request_id,
     35                              const net::URLRequestStatus& status) = 0;
     36   virtual void AddPrivacyDataForUrl(const std::string& url,
     37                                     const std::string& policy_ref,
     38                                     int32 flags) {}
     39  protected:
     40   PluginUrlRequestDelegate() {}
     41   ~PluginUrlRequestDelegate() {}
     42 };
     43 
     44 class DECLSPEC_NOVTABLE PluginUrlRequestManager {  // NOLINT
     45  public:
     46   PluginUrlRequestManager() : delegate_(NULL), enable_frame_busting_(true) {}
     47   virtual ~PluginUrlRequestManager() {}
     48 
     49   void set_frame_busting(bool enable) {
     50     enable_frame_busting_ = enable;
     51   }
     52 
     53   virtual void set_delegate(PluginUrlRequestDelegate* delegate) {
     54     delegate_ = delegate;
     55   }
     56 
     57   enum ThreadSafeFlags {
     58     NOT_THREADSAFE               = 0x00,
     59     START_REQUEST_THREADSAFE     = 0x01,
     60     STOP_REQUEST_THREADSAFE      = 0x02,
     61     READ_REQUEST_THREADSAFE      = 0x04,
     62     DOWNLOAD_REQUEST_THREADSAFE  = 0x08,
     63     COOKIE_REQUEST_THREADSAFE    = 0x10
     64   };
     65   virtual ThreadSafeFlags GetThreadSafeFlags() = 0;
     66 
     67   // These are called directly from Automation Client when network related
     68   // automation messages are received from Chrome.
     69   // Strip 'tab' handle and forward to the virtual methods implemented by
     70   // derived classes.
     71   void StartUrlRequest(int request_id,
     72                        const AutomationURLRequest& request_info) {
     73     StartRequest(request_id, request_info);
     74   }
     75 
     76   void ReadUrlRequest(int request_id, int bytes_to_read) {
     77     ReadRequest(request_id, bytes_to_read);
     78   }
     79 
     80   void EndUrlRequest(int request_id, const net::URLRequestStatus& s) {
     81     EndRequest(request_id);
     82   }
     83 
     84   void DownloadUrlRequestInHost(int request_id) {
     85     DownloadRequestInHost(request_id);
     86   }
     87 
     88   void StopAllRequests() {
     89     StopAll();
     90   }
     91 
     92  protected:
     93   PluginUrlRequestDelegate* delegate_;
     94   bool enable_frame_busting_;
     95 
     96  private:
     97   virtual void StartRequest(
     98       int request_id, const AutomationURLRequest& request_info) = 0;
     99   virtual void ReadRequest(int request_id, int bytes_to_read) = 0;
    100   virtual void EndRequest(int request_id) = 0;
    101   virtual void DownloadRequestInHost(int request_id) = 0;
    102   virtual void StopAll() = 0;
    103 };
    104 
    105 // Used as base class. Holds Url request properties (url, method, referrer..)
    106 class PluginUrlRequest {
    107  public:
    108   PluginUrlRequest();
    109   ~PluginUrlRequest();
    110 
    111   bool Initialize(PluginUrlRequestDelegate* delegate,
    112       int remote_request_id, const std::string& url, const std::string& method,
    113       const std::string& referrer, const std::string& extra_headers,
    114       net::UploadData* upload_data, ResourceType::Type resource_type,
    115       bool enable_frame_busting_, int load_flags);
    116 
    117   // Accessors.
    118   int id() const {
    119     return remote_request_id_;
    120   }
    121 
    122   const std::string& url() const {
    123     return url_;
    124   }
    125 
    126   const std::string& method() const {
    127     return method_;
    128   }
    129 
    130   const std::string& referrer() const {
    131     return referrer_;
    132   }
    133 
    134   const std::string& extra_headers() const {
    135     return extra_headers_;
    136   }
    137 
    138   uint64 post_data_len() const {
    139     return post_data_len_;
    140   }
    141 
    142   bool is_chunked_upload() const {
    143     return is_chunked_upload_;
    144   }
    145 
    146  protected:
    147   HRESULT get_upload_data(IStream** ret) {
    148     DCHECK(ret);
    149     if (!upload_data_.get())
    150       return S_FALSE;
    151     *ret = upload_data_.get();
    152     (*ret)->AddRef();
    153     return S_OK;
    154   }
    155 
    156   void set_url(const std::string& url) {
    157     url_ = url;
    158   }
    159 
    160   void SendData();
    161   bool enable_frame_busting_;
    162 
    163   PluginUrlRequestDelegate* delegate_;
    164   int remote_request_id_;
    165   uint64 post_data_len_;
    166   std::string url_;
    167   std::string method_;
    168   std::string referrer_;
    169   std::string extra_headers_;
    170   ResourceType::Type resource_type_;
    171   int load_flags_;
    172   base::win::ScopedComPtr<IStream> upload_data_;
    173   bool is_chunked_upload_;
    174   // Contains the ip address and port of the destination host.
    175   net::HostPortPair socket_address_;
    176 };
    177 
    178 #endif  // CHROME_FRAME_PLUGIN_URL_REQUEST_H_
    179