summaryrefslogtreecommitdiffstats
path: root/chrome/browser/extensions/autoupdate_interceptor.cc
blob: ef0091f9871e61286071635b9c5324c2f91f5229 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// Copyright (c) 2009 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/extensions/autoupdate_interceptor.h"

#include "base/file_util.h"
#include "base/thread_restrictions.h"
#include "chrome/browser/browser_thread.h"
#include "net/url_request/url_request_test_job.h"
#include "testing/gtest/include/gtest/gtest.h"

// This is a specialized version of URLRequestTestJob that lets us specify
// response data and make sure the response code is 200, which the autoupdate
// code relies on.
class AutoUpdateTestRequestJob : public URLRequestTestJob {
 public:
  AutoUpdateTestRequestJob(URLRequest* request,
    const std::string& response_data) : URLRequestTestJob(
    request, URLRequestTestJob::test_headers(), response_data, true) {}
  virtual int GetResponseCode() const { return 200; }

 private:
  ~AutoUpdateTestRequestJob() {}
};


AutoUpdateInterceptor::AutoUpdateInterceptor() {
  URLRequest::RegisterRequestInterceptor(this);
}

AutoUpdateInterceptor::~AutoUpdateInterceptor() {
  URLRequest::UnregisterRequestInterceptor(this);
}


URLRequestJob* AutoUpdateInterceptor::MaybeIntercept(URLRequest* request) {
  EXPECT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::IO));
  if (request->url().scheme() != "http" ||
      request->url().host() != "localhost") {
      return NULL;
  }

  // It's ok to do a blocking disk access on this thread; this class
  // is just used for tests.
  base::ThreadRestrictions::ScopedAllowIO allow_io;

  // Search for this request's url, ignoring any query parameters.
  GURL url = request->url();
  if (url.has_query()) {
    GURL::Replacements replacements;
    replacements.ClearQuery();
    url = url.ReplaceComponents(replacements);
  }
  std::map<GURL, FilePath>::iterator i = responses_.find(url);
  if (i == responses_.end()) {
    return NULL;
  }
  std::string contents;
  EXPECT_TRUE(file_util::ReadFileToString(i->second, &contents));

  return new AutoUpdateTestRequestJob(request, contents);
}


void AutoUpdateInterceptor::SetResponse(const std::string url,
                                        const FilePath& path) {
  EXPECT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::IO));
  // It's ok to do a blocking disk access on this thread; this class
  // is just used for tests.
  base::ThreadRestrictions::ScopedAllowIO allow_io;
  GURL gurl(url);
  EXPECT_EQ("http", gurl.scheme());
  EXPECT_EQ("localhost", gurl.host());
  EXPECT_TRUE(file_util::PathExists(path));
  responses_[gurl] = path;
}


void AutoUpdateInterceptor::SetResponseOnIOThread(const std::string url,
                                                  const FilePath& path) {
  BrowserThread::PostTask(
      BrowserThread::IO, FROM_HERE,
      NewRunnableMethod(this, &AutoUpdateInterceptor::SetResponse, url, path));
}