| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef CHROME_BROWSER_COMPONENT_UPDATER_COMPONENT_UPDATER_INTERCEPTOR_H_ | 5 #ifndef CHROME_BROWSER_COMPONENT_UPDATER_COMPONENT_UPDATER_INTERCEPTOR_H_ |
| 6 #define CHROME_BROWSER_COMPONENT_UPDATER_COMPONENT_UPDATER_INTERCEPTOR_H_ | 6 #define CHROME_BROWSER_COMPONENT_UPDATER_COMPONENT_UPDATER_INTERCEPTOR_H_ |
| 7 | 7 |
| 8 #include <map> | 8 #include <map> |
| 9 #include <string> | 9 #include <string> |
| 10 | 10 |
| 11 #include "base/compiler_specific.h" | 11 #include "base/compiler_specific.h" |
| 12 #include "base/file_path.h" | 12 #include "base/file_path.h" |
| 13 #include "base/memory/ref_counted.h" |
| 13 #include "googleurl/src/gurl.h" | 14 #include "googleurl/src/gurl.h" |
| 14 #include "net/url_request/url_request.h" | 15 #include "net/url_request/url_request_job_factory.h" |
| 15 | 16 |
| 16 #if !defined(UNIT_TEST) | 17 #if !defined(UNIT_TEST) |
| 17 #error "use this class only in unit tests" | 18 #error "use this class only in unit tests" |
| 18 #endif | 19 #endif |
| 19 | 20 |
| 20 // This url request interceptor lets us respond to localhost http request urls | 21 // This url request interceptor lets us respond to localhost http request urls |
| 21 // with the contents of files on disk for use in tests. | 22 // with the contents of files on disk for use in tests. |
| 22 class ComponentUpdateInterceptor | 23 class ComponentUpdateInterceptor |
| 23 : public net::URLRequest::Interceptor, | 24 : public net::URLRequestJobFactory::ProtocolHandler, |
| 24 public base::RefCountedThreadSafe<ComponentUpdateInterceptor> { | 25 public base::RefCountedThreadSafe<ComponentUpdateInterceptor> { |
| 25 public: | 26 public: |
| 26 ComponentUpdateInterceptor(); | 27 ComponentUpdateInterceptor(); |
| 27 | 28 |
| 28 // When requests for |url| arrive, respond with the contents of |path|. The | 29 // When requests for |url| arrive, respond with the contents of |path|. The |
| 29 // hostname of |url| must be "localhost" to avoid DNS lookups, and the scheme | 30 // hostname of |url| must be "localhost" to avoid DNS lookups, and the scheme |
| 30 // must be "http". | 31 // must be "http". |
| 31 void SetResponse(const std::string& url, | 32 void SetResponse(const std::string& url, |
| 32 const std::string& headers, | 33 const std::string& headers, |
| 33 const FilePath& path); | 34 const FilePath& path); |
| 34 | 35 |
| 35 // Returns how many requests have been issued that have a stored reply. | 36 // Returns how many requests have been issued that have a stored reply. |
| 36 int hit_count() const { return hit_count_; } | 37 int hit_count() const { return hit_count_; } |
| 37 | 38 |
| 38 private: | 39 private: |
| 39 // When computing matches, this ignores the query parameters of the url. | 40 // When computing matches, this ignores the query parameters of the url. |
| 40 virtual net::URLRequestJob* MaybeIntercept( | 41 virtual net::URLRequestJob* MaybeCreateJob( |
| 41 net::URLRequest* request, | 42 net::URLRequest* request, |
| 42 net::NetworkDelegate* network_delegate) OVERRIDE; | 43 net::NetworkDelegate* network_delegate) const OVERRIDE; |
| 43 | 44 |
| 44 friend class base::RefCountedThreadSafe<ComponentUpdateInterceptor>; | 45 friend class base::RefCountedThreadSafe<ComponentUpdateInterceptor>; |
| 45 | 46 |
| 46 virtual ~ComponentUpdateInterceptor(); | 47 virtual ~ComponentUpdateInterceptor(); |
| 48 void Register(); |
| 49 static void Unregister(); |
| 47 | 50 |
| 48 struct Response { | 51 struct Response { |
| 49 FilePath data_path; | 52 FilePath data_path; |
| 50 std::string headers; | 53 std::string headers; |
| 51 }; | 54 }; |
| 52 | 55 |
| 53 typedef std::map<GURL, Response> ResponseMap; | 56 typedef std::map<GURL, Response> ResponseMap; |
| 54 ResponseMap responses_; | 57 ResponseMap responses_; |
| 55 int hit_count_; | 58 mutable int hit_count_; |
| 56 | 59 |
| 57 DISALLOW_COPY_AND_ASSIGN(ComponentUpdateInterceptor); | 60 DISALLOW_COPY_AND_ASSIGN(ComponentUpdateInterceptor); |
| 58 }; | 61 }; |
| 59 | 62 |
| 60 #endif // CHROME_BROWSER_COMPONENT_UPDATER_COMPONENT_UPDATER_INTERCEPTOR_H_ | 63 #endif // CHROME_BROWSER_COMPONENT_UPDATER_COMPONENT_UPDATER_INTERCEPTOR_H_ |
| OLD | NEW |