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