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