| 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 #include "update_engine/mock_http_fetcher.h" | 5 #include "update_engine/mock_http_fetcher.h" |
| 6 #include <algorithm> | 6 #include <algorithm> |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 | 8 |
| 9 // This is a mac implementation of HttpFetcher which is useful for testing. | 9 // This is a mac implementation of HttpFetcher which is useful for testing. |
| 10 | 10 |
| (...skipping 23 matching lines...) Expand all Loading... |
| 34 SignalTransferComplete(); | 34 SignalTransferComplete(); |
| 35 return timeout_source_; | 35 return timeout_source_; |
| 36 } | 36 } |
| 37 | 37 |
| 38 CHECK_LT(sent_size_, data_.size()); | 38 CHECK_LT(sent_size_, data_.size()); |
| 39 if (!skip_delivery) { | 39 if (!skip_delivery) { |
| 40 const size_t chunk_size = min(kMockHttpFetcherChunkSize, | 40 const size_t chunk_size = min(kMockHttpFetcherChunkSize, |
| 41 data_.size() - sent_size_); | 41 data_.size() - sent_size_); |
| 42 CHECK(delegate_); | 42 CHECK(delegate_); |
| 43 delegate_->ReceivedBytes(this, &data_[sent_size_], chunk_size); | 43 delegate_->ReceivedBytes(this, &data_[sent_size_], chunk_size); |
| 44 // We may get terminated in the callback. |
| 45 if (sent_size_ == data_.size()) { |
| 46 LOG(INFO) << "Terminated in the ReceivedBytes callback."; |
| 47 return timeout_source_; |
| 48 } |
| 44 sent_size_ += chunk_size; | 49 sent_size_ += chunk_size; |
| 45 CHECK_LE(sent_size_, data_.size()); | 50 CHECK_LE(sent_size_, data_.size()); |
| 46 if (sent_size_ == data_.size()) { | 51 if (sent_size_ == data_.size()) { |
| 47 // We've sent all the data. Notify of success. | 52 // We've sent all the data. Notify of success. |
| 48 SignalTransferComplete(); | 53 SignalTransferComplete(); |
| 49 } | 54 } |
| 50 } | 55 } |
| 51 | 56 |
| 52 if (paused_) { | 57 if (paused_) { |
| 53 // If we're paused, we should return true if timeout_source_ is set, | 58 // If we're paused, we should return true if timeout_source_ is set, |
| (...skipping 20 matching lines...) Expand all Loading... |
| 74 bool ret = SendData(false); | 79 bool ret = SendData(false); |
| 75 if (false == ret) { | 80 if (false == ret) { |
| 76 timeout_source_ = NULL; | 81 timeout_source_ = NULL; |
| 77 } | 82 } |
| 78 return ret; | 83 return ret; |
| 79 } | 84 } |
| 80 | 85 |
| 81 // If the transfer is in progress, aborts the transfer early. | 86 // If the transfer is in progress, aborts the transfer early. |
| 82 // The transfer cannot be resumed. | 87 // The transfer cannot be resumed. |
| 83 void MockHttpFetcher::TerminateTransfer() { | 88 void MockHttpFetcher::TerminateTransfer() { |
| 89 LOG(INFO) << "Terminating transfer."; |
| 84 sent_size_ = data_.size(); | 90 sent_size_ = data_.size(); |
| 85 // kill any timeout | 91 // kill any timeout |
| 86 if (timeout_source_) { | 92 if (timeout_source_) { |
| 87 g_source_remove(timout_tag_); | 93 g_source_remove(timout_tag_); |
| 88 g_source_destroy(timeout_source_); | 94 g_source_destroy(timeout_source_); |
| 89 timeout_source_ = NULL; | 95 timeout_source_ = NULL; |
| 90 } | 96 } |
| 97 delegate_->TransferTerminated(this); |
| 91 } | 98 } |
| 92 | 99 |
| 93 void MockHttpFetcher::Pause() { | 100 void MockHttpFetcher::Pause() { |
| 94 CHECK(!paused_); | 101 CHECK(!paused_); |
| 95 paused_ = true; | 102 paused_ = true; |
| 96 if (timeout_source_) { | 103 if (timeout_source_) { |
| 97 g_source_remove(timout_tag_); | 104 g_source_remove(timout_tag_); |
| 98 g_source_destroy(timeout_source_); | 105 g_source_destroy(timeout_source_); |
| 99 timeout_source_ = NULL; | 106 timeout_source_ = NULL; |
| 100 } | 107 } |
| (...skipping 16 matching lines...) Expand all Loading... |
| 117 void MockHttpFetcher::SignalTransferComplete() { | 124 void MockHttpFetcher::SignalTransferComplete() { |
| 118 // If the transfer has been failed, the HTTP response code should be set | 125 // If the transfer has been failed, the HTTP response code should be set |
| 119 // already. | 126 // already. |
| 120 if (!fail_transfer_) { | 127 if (!fail_transfer_) { |
| 121 http_response_code_ = 200; | 128 http_response_code_ = 200; |
| 122 } | 129 } |
| 123 delegate_->TransferComplete(this, !fail_transfer_); | 130 delegate_->TransferComplete(this, !fail_transfer_); |
| 124 } | 131 } |
| 125 | 132 |
| 126 } // namespace chromeos_update_engine | 133 } // namespace chromeos_update_engine |
| OLD | NEW |