blob: 0c4209a0dded91bbc2eca25844398d879023c093 [file] [log] [blame]
rspangler@google.com49fdf182009-10-10 00:57:34 +00001// 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
3// found in the LICENSE file.
4
5#include "glog/logging.h"
6#include "update_engine/mock_http_fetcher.h"
7
8// This is a mac implementation of HttpFetcher which is useful for testing.
9
10namespace chromeos_update_engine {
11
12MockHttpFetcher::~MockHttpFetcher() {
13 CHECK(!timeout_source_) << "Call TerminateTransfer() before dtor.";
14}
15
16void MockHttpFetcher::BeginTransfer(const std::string& url) {
17 if (sent_size_ < data_.size())
18 SendData(true);
19}
20
21// Returns false on one condition: If timeout_source_ was already set
22// and it needs to be deleted by the caller. If timeout_source_ is NULL
23// when this function is called, this function will always return true.
24bool MockHttpFetcher::SendData(bool skip_delivery) {
25 CHECK_LT(sent_size_, data_.size());
26 if (!skip_delivery) {
27 const size_t chunk_size = min(kMockHttpFetcherChunkSize,
28 data_.size() - sent_size_);
29 CHECK(delegate_);
30 delegate_->ReceivedBytes(this, &data_[sent_size_], chunk_size);
31 sent_size_ += chunk_size;
32 CHECK_LE(sent_size_, data_.size());
33 if (sent_size_ == data_.size()) {
34 // We've sent all the data. notify of success
35 delegate_->TransferComplete(this, true);
36 }
37 }
38
39 if (paused_) {
40 // If we're paused, we should return true if timeout_source_ is set,
41 // since we need the caller to delete it.
42 return timeout_source_;
43 }
44
45 if (timeout_source_) {
46 // we still need a timeout if there's more data to send
47 return sent_size_ < data_.size();
48 } else if (sent_size_ < data_.size()) {
49 // we don't have a timeout source and we need one
50 timeout_source_ = g_timeout_source_new(10);
51 CHECK(timeout_source_);
52 g_source_set_callback(timeout_source_, StaticTimeoutCallback, this,
53 NULL);
54 timout_tag_ = g_source_attach(timeout_source_, NULL);
55 }
56 return true;
57}
58
59bool MockHttpFetcher::TimeoutCallback() {
60 CHECK(!paused_);
61 bool ret = SendData(false);
62 if (false == ret) {
63 timeout_source_ = NULL;
64 }
65 return ret;
66}
67
68// If the transfer is in progress, aborts the transfer early.
69// The transfer cannot be resumed.
70void MockHttpFetcher::TerminateTransfer() {
71 sent_size_ = data_.size();
72 // kill any timeout
73 if (timeout_source_) {
74 g_source_remove(timout_tag_);
75 g_source_destroy(timeout_source_);
76 timeout_source_ = NULL;
77 }
78}
79
80void MockHttpFetcher::Pause() {
81 CHECK(!paused_);
82 paused_ = true;
83 if (timeout_source_) {
84 g_source_remove(timout_tag_);
85 g_source_destroy(timeout_source_);
86 timeout_source_ = NULL;
87 }
88
89}
90
91void MockHttpFetcher::Unpause() {
92 CHECK(paused_) << "You must pause before unpause.";
93 paused_ = false;
94 if (sent_size_ < data_.size()) {
95 SendData(false);
96 }
97}
98
99} // namespace chromeos_update_engine