Ben Murdoch | c407dc5 | 2010-07-29 17:14:53 +0100 | [diff] [blame] | 1 | // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
Patrick Scott | c7f5f85 | 2010-02-04 10:37:17 -0500 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Patrick Scott | c7f5f85 | 2010-02-04 10:37:17 -0500 | [diff] [blame] | 5 | #include "net/url_request/url_request_job_tracker.h" |
| 6 | |
Kristian Monsen | 21d179b | 2011-05-11 20:53:37 +0100 | [diff] [blame] | 7 | #include <algorithm> |
| 8 | |
Patrick Scott | c7f5f85 | 2010-02-04 10:37:17 -0500 | [diff] [blame] | 9 | #include "base/logging.h" |
| 10 | #include "net/url_request/url_request_job.h" |
| 11 | |
Kristian Monsen | 21d179b | 2011-05-11 20:53:37 +0100 | [diff] [blame] | 12 | namespace net { |
| 13 | |
Patrick Scott | c7f5f85 | 2010-02-04 10:37:17 -0500 | [diff] [blame] | 14 | URLRequestJobTracker g_url_request_job_tracker; |
| 15 | |
| 16 | URLRequestJobTracker::URLRequestJobTracker() { |
| 17 | } |
| 18 | |
| 19 | URLRequestJobTracker::~URLRequestJobTracker() { |
Kristian Monsen | dc0f95d | 2011-06-09 11:47:42 +0100 | [diff] [blame] | 20 | DLOG_IF(WARNING, !active_jobs_.empty()) << |
Kristian Monsen | 21d179b | 2011-05-11 20:53:37 +0100 | [diff] [blame] | 21 | "Leaking " << active_jobs_.size() << " URLRequestJob object(s), this " |
| 22 | "could be because the URLRequest forgot to free it (bad), or if the " |
| 23 | "program was terminated while a request was active (normal)."; |
Patrick Scott | c7f5f85 | 2010-02-04 10:37:17 -0500 | [diff] [blame] | 24 | } |
| 25 | |
| 26 | void URLRequestJobTracker::AddNewJob(URLRequestJob* job) { |
| 27 | active_jobs_.push_back(job); |
| 28 | FOR_EACH_OBSERVER(JobObserver, observers_, OnJobAdded(job)); |
| 29 | } |
| 30 | |
| 31 | void URLRequestJobTracker::RemoveJob(URLRequestJob* job) { |
| 32 | JobList::iterator iter = std::find(active_jobs_.begin(), active_jobs_.end(), |
| 33 | job); |
| 34 | if (iter == active_jobs_.end()) { |
| 35 | NOTREACHED() << "Removing a non-active job"; |
| 36 | return; |
| 37 | } |
| 38 | active_jobs_.erase(iter); |
| 39 | |
| 40 | FOR_EACH_OBSERVER(JobObserver, observers_, OnJobRemoved(job)); |
| 41 | } |
| 42 | |
| 43 | void URLRequestJobTracker::OnJobDone(URLRequestJob* job, |
| 44 | const URLRequestStatus& status) { |
| 45 | FOR_EACH_OBSERVER(JobObserver, observers_, OnJobDone(job, status)); |
| 46 | } |
| 47 | |
| 48 | void URLRequestJobTracker::OnJobRedirect(URLRequestJob* job, |
| 49 | const GURL& location, |
| 50 | int status_code) { |
| 51 | FOR_EACH_OBSERVER(JobObserver, observers_, |
| 52 | OnJobRedirect(job, location, status_code)); |
| 53 | } |
| 54 | |
| 55 | void URLRequestJobTracker::OnBytesRead(URLRequestJob* job, |
Ben Murdoch | c407dc5 | 2010-07-29 17:14:53 +0100 | [diff] [blame] | 56 | const char* buf, |
Patrick Scott | c7f5f85 | 2010-02-04 10:37:17 -0500 | [diff] [blame] | 57 | int byte_count) { |
| 58 | FOR_EACH_OBSERVER(JobObserver, observers_, |
Ben Murdoch | c407dc5 | 2010-07-29 17:14:53 +0100 | [diff] [blame] | 59 | OnBytesRead(job, buf, byte_count)); |
Patrick Scott | c7f5f85 | 2010-02-04 10:37:17 -0500 | [diff] [blame] | 60 | } |
Kristian Monsen | 21d179b | 2011-05-11 20:53:37 +0100 | [diff] [blame] | 61 | |
| 62 | } // namespace net |