AU: Don't use network on expensive connection types
Specifically:
- FlimFlam proxy class to query the current network status and find
out if it's expensive.
- Dbus Interface, so dbus can be mocked.
- Libcurl change to redirect the URL if we are on an expensive
connection. This may seem hacky, but the reason I avoided retooling
the whole class is that we may decide that some network usage is
okay on pricy connections. Perhaps it's okay to throttle. So, for
now this is a more minimal change.
BUG=chromium-os:2397
TEST=Unit tests, tested that flimflam proxy works on the device.
Review URL: http://codereview.chromium.org/4029002
Change-Id: Ic4dcde1ca863bda890bc46a55c552e2b32d9433d
diff --git a/libcurl_http_fetcher.cc b/libcurl_http_fetcher.cc
index c846812..9cacf86 100644
--- a/libcurl_http_fetcher.cc
+++ b/libcurl_http_fetcher.cc
@@ -3,11 +3,19 @@
// found in the LICENSE file.
#include "update_engine/libcurl_http_fetcher.h"
+
#include <algorithm>
-#include "base/logging.h"
+#include <string>
+
+#include <base/logging.h>
+
+#include "update_engine/dbus_interface.h"
+#include "update_engine/flimflam_proxy.h"
+#include "update_engine/utils.h"
using std::max;
using std::make_pair;
+using std::string;
// This is a concrete implementation of HttpFetcher that uses libcurl to do the
// http work.
@@ -17,12 +25,24 @@
namespace {
const int kMaxRetriesCount = 20;
const char kCACertificatesPath[] = "/usr/share/update_engine/ca-certificates";
-}
+} // namespace {}
LibcurlHttpFetcher::~LibcurlHttpFetcher() {
CleanUp();
}
+// On error, returns false.
+bool LibcurlHttpFetcher::ConnectionIsExpensive() const {
+ if (force_connection_type_)
+ return forced_expensive_connection_;
+ NetworkConnectionType type;
+ ConcreteDbusGlib dbus_iface;
+ TEST_AND_RETURN_FALSE(FlimFlamProxy::GetConnectionType(&dbus_iface, &type));
+ LOG(INFO) << "We are connected via "
+ << FlimFlamProxy::StringForConnectionType(type);
+ return FlimFlamProxy::IsExpensiveConnectionType(type);
+}
+
void LibcurlHttpFetcher::ResumeTransfer(const std::string& url) {
LOG(INFO) << "Starting/Resuming transfer";
CHECK(!transfer_in_progress_);
@@ -54,7 +74,18 @@
CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_WRITEDATA, this), CURLE_OK);
CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_WRITEFUNCTION,
StaticLibcurlWrite), CURLE_OK);
- CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_URL, url_.c_str()), CURLE_OK);
+
+ string url_to_use(url_);
+ if (ConnectionIsExpensive()) {
+ LOG(INFO) << "Not initiating HTTP connection b/c we are on an expensive"
+ << " connection";
+ url_to_use = ""; // Sabotage the URL
+ }
+
+ CHECK_EQ(curl_easy_setopt(curl_handle_,
+ CURLOPT_URL,
+ url_to_use.c_str()),
+ CURLE_OK);
// If the connection drops under 10 bytes/sec for 3 minutes, reconnect.
CHECK_EQ(curl_easy_setopt(curl_handle_, CURLOPT_LOW_SPEED_LIMIT, 10),