| 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 NET_PROXY_PROXY_SCRIPT_FETCHER_IMPL_H_ | 5 #ifndef NET_PROXY_PROXY_SCRIPT_FETCHER_IMPL_H_ |
| 6 #define NET_PROXY_PROXY_SCRIPT_FETCHER_IMPL_H_ | 6 #define NET_PROXY_PROXY_SCRIPT_FETCHER_IMPL_H_ |
| 7 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
| 10 #include "base/ref_counted.h" | 10 #include "base/ref_counted.h" |
| 11 #include "base/scoped_ptr.h" | 11 #include "base/scoped_ptr.h" |
| 12 #include "base/string16.h" | 12 #include "base/string16.h" |
| 13 #include "base/task.h" | 13 #include "base/task.h" |
| 14 #include "base/time.h" | 14 #include "base/time.h" |
| 15 #include "net/proxy/proxy_script_fetcher.h" | 15 #include "net/proxy/proxy_script_fetcher.h" |
| 16 #include "net/url_request/url_request.h" | 16 #include "net/url_request/url_request.h" |
| 17 | 17 |
| 18 class GURL; | 18 class GURL; |
| 19 class X509Certificate; | 19 class X509Certificate; |
| 20 | 20 |
| 21 namespace net { | 21 namespace net { |
| 22 | 22 |
| 23 class URLRequestContext; | 23 class URLRequestContext; |
| 24 | 24 |
| 25 // Implementation of ProxyScriptFetcher that downloads scripts using the | 25 // Implementation of ProxyScriptFetcher that downloads scripts using the |
| 26 // specified request context. | 26 // specified request context. |
| 27 class ProxyScriptFetcherImpl : public ProxyScriptFetcher, | 27 class ProxyScriptFetcherImpl : public ProxyScriptFetcher, |
| 28 public net::URLRequest::Delegate { | 28 public URLRequest::Delegate { |
| 29 public: | 29 public: |
| 30 // Creates a ProxyScriptFetcher that issues requests through | 30 // Creates a ProxyScriptFetcher that issues requests through |
| 31 // |url_request_context|. |url_request_context| must remain valid for the | 31 // |url_request_context|. |url_request_context| must remain valid for the |
| 32 // lifetime of ProxyScriptFetcherImpl. | 32 // lifetime of ProxyScriptFetcherImpl. |
| 33 // Note that while a request is in progress, we will be holding a reference | 33 // Note that while a request is in progress, we will be holding a reference |
| 34 // to |url_request_context|. Be careful not to create cycles between the | 34 // to |url_request_context|. Be careful not to create cycles between the |
| 35 // fetcher and the context; you can break such cycles by calling Cancel(). | 35 // fetcher and the context; you can break such cycles by calling Cancel(). |
| 36 explicit ProxyScriptFetcherImpl(net::URLRequestContext* url_request_context); | 36 explicit ProxyScriptFetcherImpl(URLRequestContext* url_request_context); |
| 37 | 37 |
| 38 virtual ~ProxyScriptFetcherImpl(); | 38 virtual ~ProxyScriptFetcherImpl(); |
| 39 | 39 |
| 40 // ProxyScriptFetcher methods: | 40 // ProxyScriptFetcher methods: |
| 41 | 41 |
| 42 virtual int Fetch(const GURL& url, string16* text, | 42 virtual int Fetch(const GURL& url, string16* text, |
| 43 CompletionCallback* callback); | 43 CompletionCallback* callback); |
| 44 virtual void Cancel(); | 44 virtual void Cancel(); |
| 45 virtual net::URLRequestContext* GetRequestContext(); | 45 virtual URLRequestContext* GetRequestContext(); |
| 46 | 46 |
| 47 // net::URLRequest::Delegate methods: | 47 // URLRequest::Delegate methods: |
| 48 virtual void OnAuthRequired(net::URLRequest* request, | 48 virtual void OnAuthRequired(URLRequest* request, |
| 49 AuthChallengeInfo* auth_info); | 49 AuthChallengeInfo* auth_info); |
| 50 virtual void OnSSLCertificateError(net::URLRequest* request, int cert_error, | 50 virtual void OnSSLCertificateError(URLRequest* request, int cert_error, |
| 51 X509Certificate* cert); | 51 X509Certificate* cert); |
| 52 virtual void OnResponseStarted(net::URLRequest* request); | 52 virtual void OnResponseStarted(URLRequest* request); |
| 53 virtual void OnReadCompleted(net::URLRequest* request, int num_bytes); | 53 virtual void OnReadCompleted(URLRequest* request, int num_bytes); |
| 54 virtual void OnResponseCompleted(net::URLRequest* request); | 54 virtual void OnResponseCompleted(URLRequest* request); |
| 55 | 55 |
| 56 // Used by unit-tests to modify the default limits. | 56 // Used by unit-tests to modify the default limits. |
| 57 base::TimeDelta SetTimeoutConstraint(base::TimeDelta timeout); | 57 base::TimeDelta SetTimeoutConstraint(base::TimeDelta timeout); |
| 58 size_t SetSizeConstraint(size_t size_bytes); | 58 size_t SetSizeConstraint(size_t size_bytes); |
| 59 | 59 |
| 60 private: | 60 private: |
| 61 // Read more bytes from the response. | 61 // Read more bytes from the response. |
| 62 void ReadBody(net::URLRequest* request); | 62 void ReadBody(URLRequest* request); |
| 63 | 63 |
| 64 // Handles a response from Read(). Returns true if we should continue trying | 64 // Handles a response from Read(). Returns true if we should continue trying |
| 65 // to read. |num_bytes| is 0 for EOF, and < 0 on errors. | 65 // to read. |num_bytes| is 0 for EOF, and < 0 on errors. |
| 66 bool ConsumeBytesRead(net::URLRequest* request, int num_bytes); | 66 bool ConsumeBytesRead(URLRequest* request, int num_bytes); |
| 67 | 67 |
| 68 // Called once the request has completed to notify the caller of | 68 // Called once the request has completed to notify the caller of |
| 69 // |response_code_| and |response_text_|. | 69 // |response_code_| and |response_text_|. |
| 70 void FetchCompleted(); | 70 void FetchCompleted(); |
| 71 | 71 |
| 72 // Clear out the state for the current request. | 72 // Clear out the state for the current request. |
| 73 void ResetCurRequestState(); | 73 void ResetCurRequestState(); |
| 74 | 74 |
| 75 // Callback for time-out task of request with id |id|. | 75 // Callback for time-out task of request with id |id|. |
| 76 void OnTimeout(int id); | 76 void OnTimeout(int id); |
| 77 | 77 |
| 78 // Factory for creating the time-out task. This takes care of revoking | 78 // Factory for creating the time-out task. This takes care of revoking |
| 79 // outstanding tasks when |this| is deleted. | 79 // outstanding tasks when |this| is deleted. |
| 80 ScopedRunnableMethodFactory<ProxyScriptFetcherImpl> task_factory_; | 80 ScopedRunnableMethodFactory<ProxyScriptFetcherImpl> task_factory_; |
| 81 | 81 |
| 82 // The context used for making network requests. | 82 // The context used for making network requests. |
| 83 net::URLRequestContext* url_request_context_; | 83 URLRequestContext* url_request_context_; |
| 84 | 84 |
| 85 // Buffer that net::URLRequest writes into. | 85 // Buffer that URLRequest writes into. |
| 86 enum { kBufSize = 4096 }; | 86 enum { kBufSize = 4096 }; |
| 87 scoped_refptr<net::IOBuffer> buf_; | 87 scoped_refptr<IOBuffer> buf_; |
| 88 | 88 |
| 89 // The next ID to use for |cur_request_| (monotonically increasing). | 89 // The next ID to use for |cur_request_| (monotonically increasing). |
| 90 int next_id_; | 90 int next_id_; |
| 91 | 91 |
| 92 // The current (in progress) request, or NULL. | 92 // The current (in progress) request, or NULL. |
| 93 scoped_ptr<net::URLRequest> cur_request_; | 93 scoped_ptr<URLRequest> cur_request_; |
| 94 | 94 |
| 95 // State for current request (only valid when |cur_request_| is not NULL): | 95 // State for current request (only valid when |cur_request_| is not NULL): |
| 96 | 96 |
| 97 // Unique ID for the current request. | 97 // Unique ID for the current request. |
| 98 int cur_request_id_; | 98 int cur_request_id_; |
| 99 | 99 |
| 100 // Callback to invoke on completion of the fetch. | 100 // Callback to invoke on completion of the fetch. |
| 101 CompletionCallback* callback_; | 101 CompletionCallback* callback_; |
| 102 | 102 |
| 103 // Holds the error condition that was hit on the current request, or OK. | 103 // Holds the error condition that was hit on the current request, or OK. |
| (...skipping 11 matching lines...) Expand all Loading... |
| 115 | 115 |
| 116 // The maximum amount of time to wait for download to complete. | 116 // The maximum amount of time to wait for download to complete. |
| 117 base::TimeDelta max_duration_; | 117 base::TimeDelta max_duration_; |
| 118 | 118 |
| 119 DISALLOW_COPY_AND_ASSIGN(ProxyScriptFetcherImpl); | 119 DISALLOW_COPY_AND_ASSIGN(ProxyScriptFetcherImpl); |
| 120 }; | 120 }; |
| 121 | 121 |
| 122 } // namespace net | 122 } // namespace net |
| 123 | 123 |
| 124 #endif // NET_PROXY_PROXY_SCRIPT_FETCHER_IMPL_H_ | 124 #endif // NET_PROXY_PROXY_SCRIPT_FETCHER_IMPL_H_ |
| OLD | NEW |