| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium OS Authors. All rights reserved. | 1 // Copyright (c) 2009 The Chromium OS 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 CHROMEOS_PLATFORM_UPDATE_ENGINE_LIBCURL_HTTP_FETCHER_H__ | 5 #ifndef CHROMEOS_PLATFORM_UPDATE_ENGINE_LIBCURL_HTTP_FETCHER_H__ |
| 6 #define CHROMEOS_PLATFORM_UPDATE_ENGINE_LIBCURL_HTTP_FETCHER_H__ | 6 #define CHROMEOS_PLATFORM_UPDATE_ENGINE_LIBCURL_HTTP_FETCHER_H__ |
| 7 | 7 |
| 8 #include <map> | 8 #include <map> |
| 9 #include <string> | 9 #include <string> |
| 10 #include <curl/curl.h> | 10 #include <curl/curl.h> |
| (...skipping 15 matching lines...) Expand all Loading... |
| 26 : curl_multi_handle_(NULL), | 26 : curl_multi_handle_(NULL), |
| 27 curl_handle_(NULL), | 27 curl_handle_(NULL), |
| 28 timeout_source_(NULL), | 28 timeout_source_(NULL), |
| 29 transfer_in_progress_(false), | 29 transfer_in_progress_(false), |
| 30 transfer_size_(0), | 30 transfer_size_(0), |
| 31 bytes_downloaded_(0), | 31 bytes_downloaded_(0), |
| 32 resume_offset_(0), | 32 resume_offset_(0), |
| 33 retry_count_(0), | 33 retry_count_(0), |
| 34 retry_seconds_(60), | 34 retry_seconds_(60), |
| 35 idle_seconds_(1), | 35 idle_seconds_(1), |
| 36 force_connection_type_(false), |
| 37 forced_expensive_connection_(false), |
| 36 in_write_callback_(false), | 38 in_write_callback_(false), |
| 37 terminate_requested_(false) {} | 39 terminate_requested_(false) {} |
| 38 | 40 |
| 39 // Cleans up all internal state. Does not notify delegate | 41 // Cleans up all internal state. Does not notify delegate |
| 40 ~LibcurlHttpFetcher(); | 42 ~LibcurlHttpFetcher(); |
| 41 | 43 |
| 42 void SetOffset(off_t offset) { bytes_downloaded_ = offset; } | 44 void SetOffset(off_t offset) { bytes_downloaded_ = offset; } |
| 43 | 45 |
| 44 // Begins the transfer if it hasn't already begun. | 46 // Begins the transfer if it hasn't already begun. |
| 45 virtual void BeginTransfer(const std::string& url); | 47 virtual void BeginTransfer(const std::string& url); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 60 // primarily useful for testing. | 62 // primarily useful for testing. |
| 61 // From http://curl.haxx.se/libcurl/c/curl_multi_timeout.html: | 63 // From http://curl.haxx.se/libcurl/c/curl_multi_timeout.html: |
| 62 // if libcurl returns a -1 timeout here, it just means that libcurl | 64 // if libcurl returns a -1 timeout here, it just means that libcurl |
| 63 // currently has no stored timeout value. You must not wait too long | 65 // currently has no stored timeout value. You must not wait too long |
| 64 // (more than a few seconds perhaps) before you call | 66 // (more than a few seconds perhaps) before you call |
| 65 // curl_multi_perform() again. | 67 // curl_multi_perform() again. |
| 66 void set_idle_seconds(int seconds) { idle_seconds_ = seconds; } | 68 void set_idle_seconds(int seconds) { idle_seconds_ = seconds; } |
| 67 | 69 |
| 68 // Sets the retry timeout. Useful for testing. | 70 // Sets the retry timeout. Useful for testing. |
| 69 void set_retry_seconds(int seconds) { retry_seconds_ = seconds; } | 71 void set_retry_seconds(int seconds) { retry_seconds_ = seconds; } |
| 72 |
| 73 void SetConnectionAsExpensive(bool is_expensive) { |
| 74 force_connection_type_ = true; |
| 75 forced_expensive_connection_ = is_expensive; |
| 76 } |
| 70 | 77 |
| 71 private: | 78 private: |
| 72 // Asks libcurl for the http response code and stores it in the object. | 79 // Asks libcurl for the http response code and stores it in the object. |
| 73 void GetHttpResponseCode(); | 80 void GetHttpResponseCode(); |
| 74 | 81 |
| 75 // Resumes a transfer where it left off. This will use the | 82 // Resumes a transfer where it left off. This will use the |
| 76 // HTTP Range: header to make a new connection from where the last | 83 // HTTP Range: header to make a new connection from where the last |
| 77 // left off. | 84 // left off. |
| 78 virtual void ResumeTransfer(const std::string& url); | 85 virtual void ResumeTransfer(const std::string& url); |
| 79 | 86 |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 115 static size_t StaticLibcurlWrite(void *ptr, size_t size, | 122 static size_t StaticLibcurlWrite(void *ptr, size_t size, |
| 116 size_t nmemb, void *stream) { | 123 size_t nmemb, void *stream) { |
| 117 return reinterpret_cast<LibcurlHttpFetcher*>(stream)-> | 124 return reinterpret_cast<LibcurlHttpFetcher*>(stream)-> |
| 118 LibcurlWrite(ptr, size, nmemb); | 125 LibcurlWrite(ptr, size, nmemb); |
| 119 } | 126 } |
| 120 | 127 |
| 121 // Cleans up the following if they are non-null: | 128 // Cleans up the following if they are non-null: |
| 122 // curl(m) handles, io_channels_, timeout_source_. | 129 // curl(m) handles, io_channels_, timeout_source_. |
| 123 void CleanUp(); | 130 void CleanUp(); |
| 124 | 131 |
| 132 // Returns whether or not the current network connection is considered |
| 133 // expensive. |
| 134 bool ConnectionIsExpensive() const; |
| 135 |
| 125 // Handles for the libcurl library | 136 // Handles for the libcurl library |
| 126 CURLM *curl_multi_handle_; | 137 CURLM *curl_multi_handle_; |
| 127 CURL *curl_handle_; | 138 CURL *curl_handle_; |
| 128 | 139 |
| 129 // a list of all file descriptors that we're waiting on from the | 140 // a list of all file descriptors that we're waiting on from the |
| 130 // glib main loop | 141 // glib main loop |
| 131 typedef std::map<int, std::pair<GIOChannel*, guint> > IOChannels; | 142 typedef std::map<int, std::pair<GIOChannel*, guint> > IOChannels; |
| 132 IOChannels io_channels_; | 143 IOChannels io_channels_; |
| 133 | 144 |
| 134 // if non-NULL, a timer we're waiting on. glib main loop will call us back | 145 // if non-NULL, a timer we're waiting on. glib main loop will call us back |
| (...skipping 15 matching lines...) Expand all Loading... |
| 150 off_t resume_offset_; | 161 off_t resume_offset_; |
| 151 | 162 |
| 152 // Number of resumes performed. | 163 // Number of resumes performed. |
| 153 int retry_count_; | 164 int retry_count_; |
| 154 | 165 |
| 155 // Seconds to wait before retrying a resume. | 166 // Seconds to wait before retrying a resume. |
| 156 int retry_seconds_; | 167 int retry_seconds_; |
| 157 | 168 |
| 158 // Seconds to wait before asking libcurl to "perform". | 169 // Seconds to wait before asking libcurl to "perform". |
| 159 int idle_seconds_; | 170 int idle_seconds_; |
| 171 |
| 172 // If true, assume the network is expensive or not, according to |
| 173 // forced_expensive_connection_. (Useful for testing). |
| 174 bool force_connection_type_; |
| 175 bool forced_expensive_connection_; |
| 160 | 176 |
| 161 // If true, we are currently performing a write callback on the delegate. | 177 // If true, we are currently performing a write callback on the delegate. |
| 162 bool in_write_callback_; | 178 bool in_write_callback_; |
| 163 | 179 |
| 164 // We can't clean everything up while we're in a write callback, so | 180 // We can't clean everything up while we're in a write callback, so |
| 165 // if we get a terminate request, queue it until we can handle it. | 181 // if we get a terminate request, queue it until we can handle it. |
| 166 bool terminate_requested_; | 182 bool terminate_requested_; |
| 167 | 183 |
| 168 DISALLOW_COPY_AND_ASSIGN(LibcurlHttpFetcher); | 184 DISALLOW_COPY_AND_ASSIGN(LibcurlHttpFetcher); |
| 169 }; | 185 }; |
| 170 | 186 |
| 171 } // namespace chromeos_update_engine | 187 } // namespace chromeos_update_engine |
| 172 | 188 |
| 173 #endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_LIBCURL_HTTP_FETCHER_H__ | 189 #endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_LIBCURL_HTTP_FETCHER_H__ |
| OLD | NEW |