AU: Fix potential issues with premature destruction of HTTP fetchers.

This patch adds a new TransferTerminated callback to the
HttpFetcher class. It fixes two potential memory corruption
issues with premature destruction of HttpFetcher instances:

1. When MultiHttpFetcher completes a range, it terminates
the current fetcher and starts the next one, if any. Change
so that the next fetcher is started when the
TransferTerminated callback is received from the current
fetcher. This prevents the multi fetcher from sending a
TransferComplete/TransferTerminated callbacks before the
underlying fetcher is cleaned up, which may lead to the
fetchers being destroyed prematurely.

2. If the download action fails due to a failed write,
terminate the transfer and then wait for the transfer
terminated callback before notifying the action processor
that the action is complete. Otherwise, the action may get
destroyed before the transfer is actually terminated
possibly leading to memory corruption, etc.

Hopefully these changes fix crosbug.com/8798.

BUG=8798
TEST=unit tests, tested on device with write errors

Change-Id: If416b95625ab31662f2e1308df6bdd1757a2ad78

Review URL: http://codereview.chromium.org/5009009
diff --git a/mock_http_fetcher.cc b/mock_http_fetcher.cc
index eec550f..1a88b94 100644
--- a/mock_http_fetcher.cc
+++ b/mock_http_fetcher.cc
@@ -41,6 +41,11 @@
                                   data_.size() - sent_size_);
     CHECK(delegate_);
     delegate_->ReceivedBytes(this, &data_[sent_size_], chunk_size);
+    // We may get terminated in the callback.
+    if (sent_size_ == data_.size()) {
+      LOG(INFO) << "Terminated in the ReceivedBytes callback.";
+      return timeout_source_;
+    }
     sent_size_ += chunk_size;
     CHECK_LE(sent_size_, data_.size());
     if (sent_size_ == data_.size()) {
@@ -81,6 +86,7 @@
 // If the transfer is in progress, aborts the transfer early.
 // The transfer cannot be resumed.
 void MockHttpFetcher::TerminateTransfer() {
+  LOG(INFO) << "Terminating transfer.";
   sent_size_ = data_.size();
   // kill any timeout
   if (timeout_source_) {
@@ -88,6 +94,7 @@
     g_source_destroy(timeout_source_);
     timeout_source_ = NULL;
   }
+  delegate_->TransferTerminated(this);
 }
 
 void MockHttpFetcher::Pause() {