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