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
5#include "chrome/browser/extensions/autoupdate_interceptor.h"
6
7#include "base/file_util.h"
8#include "base/threading/thread_restrictions.h"
9#include "content/browser/browser_thread.h"
10#include "net/url_request/url_request_test_job.h"
11#include "testing/gtest/include/gtest/gtest.h"
12
13// This is a specialized version of net::URLRequestTestJob that lets us specify
14// response data and make sure the response code is 200, which the autoupdate
15// code relies on.
16class AutoUpdateTestRequestJob : public net::URLRequestTestJob {
17 public:
18  AutoUpdateTestRequestJob(net::URLRequest* request,
19                           const std::string& response_data)
20      : net::URLRequestTestJob(request,
21                               net::URLRequestTestJob::test_headers(),
22                               response_data,
23                               true) {
24  }
25
26  virtual int GetResponseCode() const { return 200; }
27
28 private:
29  ~AutoUpdateTestRequestJob() {}
30};
31
32
33AutoUpdateInterceptor::AutoUpdateInterceptor() {
34  net::URLRequest::RegisterRequestInterceptor(this);
35}
36
37AutoUpdateInterceptor::~AutoUpdateInterceptor() {
38  net::URLRequest::UnregisterRequestInterceptor(this);
39}
40
41net::URLRequestJob* AutoUpdateInterceptor::MaybeIntercept(
42    net::URLRequest* request) {
43  EXPECT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::IO));
44  if (request->url().scheme() != "http" ||
45      request->url().host() != "localhost") {
46      return NULL;
47  }
48
49  // It's ok to do a blocking disk access on this thread; this class
50  // is just used for tests.
51  base::ThreadRestrictions::ScopedAllowIO allow_io;
52
53  // Search for this request's url, ignoring any query parameters.
54  GURL url = request->url();
55  if (url.has_query()) {
56    GURL::Replacements replacements;
57    replacements.ClearQuery();
58    url = url.ReplaceComponents(replacements);
59  }
60  std::map<GURL, FilePath>::iterator i = responses_.find(url);
61  if (i == responses_.end()) {
62    return NULL;
63  }
64  std::string contents;
65  EXPECT_TRUE(file_util::ReadFileToString(i->second, &contents));
66
67  return new AutoUpdateTestRequestJob(request, contents);
68}
69
70
71void AutoUpdateInterceptor::SetResponse(const std::string url,
72                                        const FilePath& path) {
73  EXPECT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::IO));
74  // It's ok to do a blocking disk access on this thread; this class
75  // is just used for tests.
76  base::ThreadRestrictions::ScopedAllowIO allow_io;
77  GURL gurl(url);
78  EXPECT_EQ("http", gurl.scheme());
79  EXPECT_EQ("localhost", gurl.host());
80  EXPECT_TRUE(file_util::PathExists(path));
81  responses_[gurl] = path;
82}
83
84
85void AutoUpdateInterceptor::SetResponseOnIOThread(const std::string url,
86                                                  const FilePath& path) {
87  BrowserThread::PostTask(
88      BrowserThread::IO, FROM_HERE,
89      NewRunnableMethod(this, &AutoUpdateInterceptor::SetResponse, url, path));
90}
91