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