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