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