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#include "chrome/browser/net/url_request_slow_http_job.h"
6
7#include "base/time.h"
8#include "base/utf_string_conversions.h"
9#include "net/url_request/url_request_filter.h"
10
11static const char kMockHostname[] = "mock.slow.http";
12
13FilePath URLRequestSlowHTTPJob::base_path_;
14
15// static
16const int URLRequestSlowHTTPJob::kDelayMs = 1000;
17
18using base::TimeDelta;
19
20/* static */
21net::URLRequestJob* URLRequestSlowHTTPJob::Factory(net::URLRequest* request,
22                                                   const std::string& scheme) {
23  return new URLRequestSlowHTTPJob(request,
24                                   GetOnDiskPath(base_path_, request, scheme));
25}
26
27/* static */
28void URLRequestSlowHTTPJob::AddUrlHandler(const FilePath& base_path) {
29  base_path_ = base_path;
30
31  // Add kMockHostname to net::URLRequestFilter.
32  net::URLRequestFilter* filter = net::URLRequestFilter::GetInstance();
33  filter->AddHostnameHandler("http", kMockHostname,
34                             URLRequestSlowHTTPJob::Factory);
35}
36
37/* static */
38GURL URLRequestSlowHTTPJob::GetMockUrl(const FilePath& path) {
39  std::string url = "http://";
40  url.append(kMockHostname);
41  url.append("/");
42  std::string path_str = path.MaybeAsASCII();
43  DCHECK(!path_str.empty());  // We only expect ASCII paths in tests.
44  url.append(path_str);
45  return GURL(url);
46}
47
48URLRequestSlowHTTPJob::URLRequestSlowHTTPJob(net::URLRequest* request,
49                                             const FilePath& file_path)
50    : URLRequestMockHTTPJob(request, file_path) { }
51
52void URLRequestSlowHTTPJob::Start() {
53  delay_timer_.Start(TimeDelta::FromMilliseconds(kDelayMs), this,
54                     &URLRequestSlowHTTPJob::RealStart);
55}
56
57URLRequestSlowHTTPJob::~URLRequestSlowHTTPJob() {
58}
59
60void URLRequestSlowHTTPJob::RealStart() {
61  URLRequestMockHTTPJob::Start();
62}
63