1c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
2c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// Use of this source code is governed by a BSD-style license that can be
3c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// found in the LICENSE file.
4c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
5c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#ifndef NET_URL_REQUEST_URL_REQUEST_JOB_TRACKER_H_
6c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#define NET_URL_REQUEST_URL_REQUEST_JOB_TRACKER_H_
7c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
8c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#include <vector>
9c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
10c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#include "base/observer_list.h"
11c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#include "net/url_request/url_request_status.h"
12c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
13c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scottclass URLRequestJob;
14c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scottclass GURL;
15c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
16c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// This class maintains a list of active URLRequestJobs for debugging purposes.
17c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// This allows us to warn on leaked jobs and also allows an observer to track
18c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// what is happening, for example, for the network status monitor.
19c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott//
20c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// NOTE: URLRequest is single-threaded, so this class should only be used on
21c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott// the same thread where all of the application's URLRequest calls are made.
22c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott//
23c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scottclass URLRequestJobTracker {
24c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott public:
25c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  typedef std::vector<URLRequestJob*> JobList;
26c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  typedef JobList::const_iterator JobIterator;
27c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
28c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  // The observer's methods are called on the thread that called AddObserver.
29c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  class JobObserver {
30c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott   public:
31c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott    // Called after the given job has been added to the list
32c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott    virtual void OnJobAdded(URLRequestJob* job) = 0;
33c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
34c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott    // Called after the given job has been removed from the list
35c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott    virtual void OnJobRemoved(URLRequestJob* job) = 0;
36c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
37c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott    // Called when the given job has completed, before notifying the request
38c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott    virtual void OnJobDone(URLRequestJob* job,
39c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott                           const URLRequestStatus& status) = 0;
40c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
41c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott    // Called when the given job is about to follow a redirect to the given
42c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott    // new URL. The redirect type is given in status_code
43c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott    virtual void OnJobRedirect(URLRequestJob* job, const GURL& location,
44c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott                               int status_code) = 0;
45c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
46c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott    // Called when a new chunk of bytes has been read for the given job. The
47c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott    // byte count is the number of bytes for that read event only.
48c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott    virtual void OnBytesRead(URLRequestJob* job, int byte_count) = 0;
49c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
50c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott    virtual ~JobObserver() {}
51c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  };
52c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
53c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  URLRequestJobTracker();
54c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  ~URLRequestJobTracker();
55c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
56c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  // adds or removes an observer from the list.  note, these methods should
57c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  // only be called on the same thread where URLRequest objects are used.
58c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  void AddObserver(JobObserver* observer) {
59c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott    observers_.AddObserver(observer);
60c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  }
61c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  void RemoveObserver(JobObserver* observer) {
62c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott    observers_.RemoveObserver(observer);
63c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  }
64c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
65c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  // adds or removes the job from the active list, should be called by the
66c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  // job constructor and destructor. Note: don't use "AddJob" since that
67c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  // is #defined by windows.h :(
68c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  void AddNewJob(URLRequestJob* job);
69c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  void RemoveJob(URLRequestJob* job);
70c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
71c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  // Job status change notifications
72c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  void OnJobDone(URLRequestJob* job, const URLRequestStatus& status);
73c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  void OnJobRedirect(URLRequestJob* job, const GURL& location,
74c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott                     int status_code);
75c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
76c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  // Bytes read notifications.
77c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  void OnBytesRead(URLRequestJob* job, int byte_count);
78c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
79c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  // allows iteration over all active jobs
80c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  JobIterator begin() const {
81c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott    return active_jobs_.begin();
82c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  }
83c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  JobIterator end() const {
84c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott    return active_jobs_.end();
85c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  }
86c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
87c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott private:
88c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  ObserverList<JobObserver> observers_;
89c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott  JobList active_jobs_;
90c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott};
91c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
92c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scottextern URLRequestJobTracker g_url_request_job_tracker;
93c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott
94c7f5f8508d98d5952d42ed7648c2a8f30a4da156Patrick Scott#endif  // NET_URL_REQUEST_URL_REQUEST_JOB_TRACKER_H_
95