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 // This class simulates what wininet does when a dns lookup fails. 5 6 #ifndef CHROME_BROWSER_AUTOMATION_URL_REQUEST_AUTOMATION_JOB_H_ 7 #define CHROME_BROWSER_AUTOMATION_URL_REQUEST_AUTOMATION_JOB_H_ 8 9 #include "base/memory/weak_ptr.h" 10 #include "chrome/common/ref_counted_util.h" 11 #include "net/url_request/url_request.h" 12 #include "net/url_request/url_request_job.h" 13 14 class AutomationResourceMessageFilter; 15 struct AutomationURLResponse; 16 17 namespace net { 18 class HttpResponseHeaders; 19 class HttpResponseInfo; 20 class HttpUserAgentSettings; 21 class HostPortPair; 22 } 23 24 namespace IPC { 25 class Message; 26 } 27 28 // net::URLRequestJob implementation that loads the resources using 29 // automation. 30 class URLRequestAutomationJob : public net::URLRequestJob { 31 public: 32 URLRequestAutomationJob( 33 net::URLRequest* request, 34 net::NetworkDelegate* network_delegate, 35 const net::HttpUserAgentSettings* http_user_agent_settings, 36 int tab, 37 int request_id, 38 AutomationResourceMessageFilter* filter, 39 bool is_pending); 40 41 // Register our factory for HTTP/HTTPs requests. 42 static void EnsureProtocolFactoryRegistered(); 43 44 static net::URLRequest::ProtocolFactory Factory; 45 46 // net::URLRequestJob methods. 47 virtual void Start(); 48 virtual void Kill(); 49 virtual bool GetMimeType(std::string* mime_type) const; 50 virtual bool GetCharset(std::string* charset); 51 virtual void GetResponseInfo(net::HttpResponseInfo* info); 52 virtual void GetLoadTimingInfo(net::LoadTimingInfo* load_timing_info) const; 53 virtual int GetResponseCode() const; 54 virtual bool IsRedirectResponse(GURL* location, int* http_status_code); 55 virtual net::UploadProgress GetUploadProgress() const; 56 virtual net::HostPortPair GetSocketAddress() const; 57 58 // Peek and process automation messages for URL requests. 59 static bool MayFilterMessage(const IPC::Message& message, int* request_id); 60 void OnMessage(const IPC::Message& message); 61 id()62 int id() const { 63 return id_; 64 } 65 request_id()66 int request_id() const { 67 return request_id_; 68 } 69 is_pending()70 bool is_pending() const { 71 return is_pending_; 72 } 73 message_filter()74 AutomationResourceMessageFilter* message_filter() const { 75 return message_filter_.get(); 76 } 77 78 // Resumes a job, which was waiting for the external host to connect to the 79 // automation channel. This is to ensure that this request gets routed to the 80 // external host. 81 void StartPendingJob(int new_tab_handle, 82 AutomationResourceMessageFilter* new_filter); 83 84 protected: 85 // Protected net::URLRequestJob override. 86 virtual bool ReadRawData(net::IOBuffer* buf, int buf_size, int* bytes_read); 87 88 void StartAsync(); 89 void Cleanup(); 90 void DisconnectFromMessageFilter(); 91 92 // IPC message handlers. 93 void OnRequestStarted(int id, const AutomationURLResponse& response); 94 void OnDataAvailable(int id, const std::string& bytes); 95 void OnRequestEnd(int id, const net::URLRequestStatus& status); 96 97 private: 98 virtual ~URLRequestAutomationJob(); 99 100 // Task which is scheduled in the URLRequestAutomationJob::ReadRawData 101 // function, which completes the job. 102 void NotifyJobCompletionTask(); 103 104 const net::HttpUserAgentSettings* http_user_agent_settings_; 105 int id_; 106 int tab_; 107 scoped_refptr<AutomationResourceMessageFilter> message_filter_; 108 109 scoped_refptr<net::IOBuffer> pending_buf_; 110 size_t pending_buf_size_; 111 112 std::string mime_type_; 113 scoped_refptr<net::HttpResponseHeaders> headers_; 114 std::string redirect_url_; 115 int redirect_status_; 116 int request_id_; 117 118 static int instance_count_; 119 120 static bool is_protocol_factory_registered_; 121 // The previous HTTP/HTTPs protocol factories. We pass unhandled 122 // requests off to these factories 123 static net::URLRequest::ProtocolFactory* old_http_factory_; 124 static net::URLRequest::ProtocolFactory* old_https_factory_; 125 126 // Set to true if the job is waiting for the external host to connect to the 127 // automation channel, which will be used for routing the network requests to 128 // the host. 129 bool is_pending_; 130 131 // Contains the request status code, which is eventually passed to the http 132 // stack when we receive a Read request for a completed job. 133 net::URLRequestStatus request_status_; 134 135 // Contains the ip address and port of the destination host. 136 net::HostPortPair socket_address_; 137 138 // Size of the upload data appended to the request. 139 uint64 upload_size_; 140 141 // When the request was sent out over automation. 142 base::TimeTicks request_start_; 143 144 // When the response headers arrived from automation. 145 base::TimeTicks receive_headers_end_; 146 147 base::WeakPtrFactory<URLRequestAutomationJob> weak_factory_; 148 149 DISALLOW_COPY_AND_ASSIGN(URLRequestAutomationJob); 150 }; 151 152 #endif // CHROME_BROWSER_AUTOMATION_URL_REQUEST_AUTOMATION_JOB_H_ 153