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
5// A net::URLRequestJob class that simulates network errors (including https
6// related).
7// It is based on URLRequestMockHttpJob.
8
9#ifndef CHROME_BROWSER_NET_URL_REQUEST_MOCK_NET_ERROR_JOB_H_
10#define CHROME_BROWSER_NET_URL_REQUEST_MOCK_NET_ERROR_JOB_H_
11#pragma once
12
13#include "chrome/browser/net/url_request_mock_http_job.h"
14
15class URLRequestMockNetErrorJob : public URLRequestMockHTTPJob {
16 public:
17  URLRequestMockNetErrorJob(net::URLRequest* request,
18                            const std::vector<int>& errors,
19                            net::X509Certificate* ssl_cert,
20                            const FilePath& file_path);
21
22  virtual void Start();
23  virtual void ContinueDespiteLastError();
24
25  // Add the specified URL to the list of URLs that should be mocked.  When this
26  // URL is hit, the specified |errors| will be played.  If any of these errors
27  // is a cert error, |ssl_cert| will be used as the ssl cert when notifying of
28  // the error.  |ssl_cert| can be NULL if |errors| does not contain any cert
29  // errors. |base| is the location on disk where the file mocking the URL
30  // contents and http-headers should be retrieved from.
31  static void AddMockedURL(const GURL& url,
32                           const std::wstring& base,
33                           const std::vector<int>& errors,
34                           net::X509Certificate* ssl_cert);
35
36  // Removes the specified |url| from the list of mocked urls.
37  static void RemoveMockedURL(const GURL& url);
38
39 private:
40  ~URLRequestMockNetErrorJob();
41
42  static net::URLRequest::ProtocolFactory Factory;
43
44  void StartAsync();
45
46  // The errors to simulate.
47  std::vector<int> errors_;
48
49  // The certificate to use for SSL errors.
50  scoped_refptr<net::X509Certificate> ssl_cert_;
51
52  struct MockInfo;
53  typedef std::map<GURL, MockInfo> URLMockInfoMap;
54  static URLMockInfoMap url_mock_info_map_;
55
56  DISALLOW_COPY_AND_ASSIGN(URLRequestMockNetErrorJob);
57};
58
59#endif  // CHROME_BROWSER_NET_URL_REQUEST_MOCK_NET_ERROR_JOB_H_
60