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