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_URLMON_URL_REQUEST_H_
      6 #define CHROME_FRAME_URLMON_URL_REQUEST_H_
      7 
      8 #include <urlmon.h>
      9 #include <atlbase.h>
     10 #include <atlcom.h>
     11 #include <map>
     12 #include <string>
     13 
     14 #include "chrome_frame/plugin_url_request.h"
     15 #include "chrome_frame/urlmon_moniker.h"
     16 #include "chrome_frame/utils.h"
     17 
     18 namespace base {
     19 class MessageLoop;
     20 class Thread;
     21 }
     22 
     23 class UrlmonUrlRequest;
     24 
     25 class UrlmonUrlRequestManager
     26     : public PluginUrlRequestManager,
     27       public PluginUrlRequestDelegate {
     28  public:
     29   // Contains the privacy information for all requests issued by this instance.
     30   struct PrivacyInfo {
     31    public:
     32     struct PrivacyEntry {
     33       PrivacyEntry() : flags(0) {}
     34       std::wstring policy_ref;
     35       int32 flags;
     36     };
     37 
     38     typedef std::map<std::wstring, PrivacyEntry> PrivacyRecords;
     39 
     40     PrivacyInfo() : privacy_impacted(false) {}
     41 
     42     bool privacy_impacted;
     43     PrivacyRecords privacy_records;
     44   };
     45 
     46   UrlmonUrlRequestManager();
     47   ~UrlmonUrlRequestManager();
     48 
     49   // Use specific bind context when Chrome request this url.
     50   // Used from ChromeActiveDocument's implementation of IPersistMoniker::Load().
     51   void SetInfoForUrl(const std::wstring& url,
     52                      IMoniker* moniker, LPBC bind_context);
     53 
     54   // Returns a copy of the url privacy information for this instance.
     55   PrivacyInfo privacy_info() {
     56     return privacy_info_;
     57   }
     58 
     59   virtual void AddPrivacyDataForUrl(const std::string& url,
     60                                     const std::string& policy_ref,
     61                                     int32 flags);
     62 
     63   // This function passes the window on which notifications are to be fired.
     64   void put_notification_window(HWND window) {
     65     notification_window_ = window;
     66   }
     67 
     68   // This function passes information on whether ChromeFrame is running in
     69   // privileged mode.
     70   void set_privileged_mode(bool privileged_mode) {
     71     privileged_mode_ = privileged_mode;
     72   }
     73 
     74   void set_container(IUnknown* container) {
     75     container_ = container;
     76   }
     77 
     78  private:
     79   friend class base::MessageLoop;
     80 
     81   // PluginUrlRequestManager implementation.
     82   virtual PluginUrlRequestManager::ThreadSafeFlags GetThreadSafeFlags();
     83   virtual void StartRequest(int request_id,
     84                             const AutomationURLRequest& request_info);
     85   virtual void ReadRequest(int request_id, int bytes_to_read);
     86   virtual void EndRequest(int request_id);
     87   virtual void DownloadRequestInHost(int request_id);
     88   virtual void StopAll();
     89 
     90   // PluginUrlRequestDelegate implementation
     91   virtual void OnResponseStarted(
     92       int request_id, const char* mime_type, const char* headers, int size,
     93       base::Time last_modified, const std::string& redirect_url,
     94       int redirect_status, const net::HostPortPair& socket_address,
     95       uint64 upload_size);
     96   virtual void OnReadComplete(int request_id, const std::string& data);
     97   virtual void OnResponseEnd(int request_id,
     98                              const net::URLRequestStatus& status);
     99 
    100   // This method is passed as a callback to UrlmonUrlRequest::TerminateBind.
    101   // We simply forward moniker and bind_ctx to host ActiveX/ActiveDocument,
    102   // so it may start NavigateWithBindContext.
    103   void BindTerminated(IMoniker* moniker, IBindCtx* bind_ctx,
    104                       IStream* post_data, const char* request_headers);
    105 
    106   // Helper function to initiate a download request in the host.
    107   void DownloadRequestInHostHelper(UrlmonUrlRequest* request);
    108 
    109   // Map for (request_id <-> UrlmonUrlRequest)
    110   typedef std::map<int, scoped_refptr<UrlmonUrlRequest> > RequestMap;
    111   RequestMap request_map_;
    112   RequestMap background_request_map_;
    113 
    114   // The caller is responsible for acquiring any locks needed to access the
    115   // request map.
    116   scoped_refptr<UrlmonUrlRequest> LookupRequest(int request_id,
    117                                                 RequestMap* request_map);
    118   // The background_request_map_ is referenced from multiple threads. Lock to
    119   // synchronize access.
    120   base::Lock background_resource_map_lock_;
    121 
    122   // Helper function to stop all requests in the request map.
    123   void StopAllRequestsHelper(RequestMap* request_map,
    124                              base::Lock* request_map_lock);
    125   // Helper function for initiating a new IE request.
    126   void StartRequestHelper(int request_id,
    127                           const AutomationURLRequest& request_info,
    128                           RequestMap* request_map,
    129                           base::Lock* request_map_lock);
    130 
    131   scoped_refptr<UrlmonUrlRequest> pending_request_;
    132   scoped_ptr<base::Thread> background_thread_;
    133 
    134   bool stopping_;
    135 
    136   // Controls whether we download subresources on the page in a background
    137   // worker thread.
    138   bool background_worker_thread_enabled_;
    139 
    140   PrivacyInfo privacy_info_;
    141   // The window to be used to fire notifications on.
    142   HWND notification_window_;
    143   // Set to true if the ChromeFrame instance is running in privileged mode.
    144   bool privileged_mode_;
    145   // A pointer to the containing object. We maintain a weak reference to avoid
    146   // lifetime issues.
    147   IUnknown* container_;
    148 };
    149 
    150 #endif  // CHROME_FRAME_URLMON_URL_REQUEST_H_
    151