url_request_job_factory.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. #ifndef NET_URL_REQUEST_URL_REQUEST_JOB_FACTORY_H_
  5. #define NET_URL_REQUEST_URL_REQUEST_JOB_FACTORY_H_
  6. #include <map>
  7. #include <memory>
  8. #include <string>
  9. #include "base/compiler_specific.h"
  10. #include "base/threading/thread_checker.h"
  11. #include "net/base/net_export.h"
  12. class GURL;
  13. namespace net {
  14. class URLRequest;
  15. class URLRequestInterceptor;
  16. class URLRequestJob;
  17. // Creates URLRequestJobs for URLRequests. Internally uses a mapping of schemes
  18. // to ProtocolHandlers, which handle the actual requests.
  19. class NET_EXPORT URLRequestJobFactory {
  20. public:
  21. class NET_EXPORT ProtocolHandler {
  22. public:
  23. virtual ~ProtocolHandler();
  24. // Creates a URLRequestJob for the particular protocol. Never returns
  25. // nullptr.
  26. virtual std::unique_ptr<URLRequestJob> CreateJob(
  27. URLRequest* request) const = 0;
  28. // Indicates if it should be safe to redirect to |location|. Should handle
  29. // protocols handled by MaybeCreateJob().
  30. virtual bool IsSafeRedirectTarget(const GURL& location) const;
  31. };
  32. URLRequestJobFactory();
  33. URLRequestJobFactory(const URLRequestJobFactory&) = delete;
  34. URLRequestJobFactory& operator=(const URLRequestJobFactory&) = delete;
  35. virtual ~URLRequestJobFactory();
  36. // Sets the ProtocolHandler for a scheme. Returns true on success, false on
  37. // failure (a ProtocolHandler already exists for |scheme|).
  38. bool SetProtocolHandler(const std::string& scheme,
  39. std::unique_ptr<ProtocolHandler> protocol_handler);
  40. // Creates a URLRequestJob for |request|. Returns a URLRequestJob that fails
  41. // with net::Error code if unable to handle request->url().
  42. //
  43. // Virtual for tests.
  44. virtual std::unique_ptr<URLRequestJob> CreateJob(URLRequest* request) const;
  45. // Returns true if it's safe to redirect to |location|.
  46. //
  47. // Virtual for tests.
  48. virtual bool IsSafeRedirectTarget(const GURL& location) const;
  49. protected:
  50. // Protected for (test-only) subclasses.
  51. THREAD_CHECKER(thread_checker_);
  52. private:
  53. // For testing only.
  54. friend class URLRequestFilter;
  55. using ProtocolHandlerMap =
  56. std::map<std::string, std::unique_ptr<ProtocolHandler>>;
  57. // Sets a global URLRequestInterceptor for testing purposes. The interceptor
  58. // is given the chance to intercept any request before the corresponding
  59. // ProtocolHandler. If an interceptor is set, the old interceptor must be
  60. // cleared before setting a new one.
  61. static void SetInterceptorForTesting(URLRequestInterceptor* interceptor);
  62. ProtocolHandlerMap protocol_handler_map_;
  63. };
  64. } // namespace net
  65. #endif // NET_URL_REQUEST_URL_REQUEST_JOB_FACTORY_H_