Home | History | Annotate | Download | only in extensions
      1 // Copyright (c) 2009 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 #ifndef CHROME_BROWSER_EXTENSIONS_AUTOUPDATE_INTERCEPTOR_H_
      6 #define CHROME_BROWSER_EXTENSIONS_AUTOUPDATE_INTERCEPTOR_H_
      7 #pragma once
      8 
      9 #include <map>
     10 #include <string>
     11 
     12 #include "googleurl/src/gurl.h"
     13 #include "net/url_request/url_request.h"
     14 
     15 // This url request interceptor lets us respond to localhost http request urls
     16 // with the contents of files on disk for use in tests.
     17 class AutoUpdateInterceptor
     18     : public net::URLRequest::Interceptor,
     19       public base::RefCountedThreadSafe<AutoUpdateInterceptor> {
     20  public:
     21   AutoUpdateInterceptor();
     22 
     23   // When computing matches, this ignores query parameters (since the autoupdate
     24   // fetch code appends a bunch of them to manifest fetches).
     25   virtual net::URLRequestJob* MaybeIntercept(net::URLRequest* request);
     26 
     27   // When requests for |url| arrive, respond with the contents of |path|. The
     28   // hostname of |url| must be "localhost" to avoid DNS lookups, and the scheme
     29   // must be "http" so MaybeIntercept can ignore "chrome" and other schemes.
     30   // Also, the match for |url| will ignore any query parameters.
     31   void SetResponse(const std::string url, const FilePath& path);
     32 
     33   // A helper function to call SetResponse on the I/O thread.
     34   void SetResponseOnIOThread(const std::string url, const FilePath& path);
     35 
     36  private:
     37   friend class base::RefCountedThreadSafe<AutoUpdateInterceptor>;
     38 
     39   virtual ~AutoUpdateInterceptor();
     40 
     41   std::map<GURL, FilePath> responses_;
     42 
     43   DISALLOW_COPY_AND_ASSIGN(AutoUpdateInterceptor);
     44 };
     45 
     46 #endif  // CHROME_BROWSER_EXTENSIONS_AUTOUPDATE_INTERCEPTOR_H_
     47