Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(585)

Side by Side Diff: net/url_request/url_request.h

Issue 67019: URLRequest::Interceptor enhancements... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 11 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. 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 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_H_ 5 #ifndef NET_URL_REQUEST_URL_REQUEST_H_
6 #define NET_URL_REQUEST_URL_REQUEST_H_ 6 #define NET_URL_REQUEST_URL_REQUEST_H_
7 7
8 #include <map>
8 #include <string> 9 #include <string>
9 #include <vector> 10 #include <vector>
10 11
11 #include "base/file_path.h" 12 #include "base/file_path.h"
13 #include "base/linked_ptr.h"
12 #include "base/logging.h" 14 #include "base/logging.h"
13 #include "base/ref_counted.h" 15 #include "base/ref_counted.h"
16 #include "base/scoped_ptr.h"
14 #include "googleurl/src/gurl.h" 17 #include "googleurl/src/gurl.h"
15 #include "net/base/load_states.h" 18 #include "net/base/load_states.h"
16 #include "net/http/http_response_info.h" 19 #include "net/http/http_response_info.h"
17 #include "net/url_request/url_request_status.h" 20 #include "net/url_request/url_request_status.h"
18 21
19 namespace base { 22 namespace base {
20 class Time; 23 class Time;
21 } // namespace base 24 } // namespace base
22 25
23 namespace net { 26 namespace net {
(...skipping 17 matching lines...) Expand all
41 // consumer, and the instance is not required to live on the heap or be 44 // consumer, and the instance is not required to live on the heap or be
42 // allocated in any special way. It is also valid to delete an URLRequest 45 // allocated in any special way. It is also valid to delete an URLRequest
43 // object during the handling of a callback to its delegate. Of course, once 46 // object during the handling of a callback to its delegate. Of course, once
44 // the URLRequest is deleted, no further callbacks to its delegate will occur. 47 // the URLRequest is deleted, no further callbacks to its delegate will occur.
45 // 48 //
46 // NOTE: All usage of all instances of this class should be on the same thread. 49 // NOTE: All usage of all instances of this class should be on the same thread.
47 // 50 //
48 class URLRequest { 51 class URLRequest {
49 public: 52 public:
50 // Derive from this class and add your own data members to associate extra 53 // Derive from this class and add your own data members to associate extra
51 // information with a URLRequest. Use user_data() and set_user_data() 54 // information with a URLRequest. Use GetUserData(key) and SetUserData()
52 class UserData { 55 class UserData {
53 public: 56 public:
54 UserData() {} 57 UserData() {}
55 virtual ~UserData() {} 58 virtual ~UserData() {}
56 }; 59 };
57 60
58 // Callback function implemented by protocol handlers to create new jobs. 61 // Callback function implemented by protocol handlers to create new jobs.
59 // The factory may return NULL to indicate an error, which will cause other 62 // The factory may return NULL to indicate an error, which will cause other
60 // factories to be queried. If no factory handles the request, then the 63 // factories to be queried. If no factory handles the request, then the
61 // default job will be used. 64 // default job will be used.
62 typedef URLRequestJob* (ProtocolFactory)(URLRequest* request, 65 typedef URLRequestJob* (ProtocolFactory)(URLRequest* request,
63 const std::string& scheme); 66 const std::string& scheme);
64 67
65 // This class handles network interception. Use with 68 // This class handles network interception. Use with
66 // (Un)RegisterRequestInterceptor. 69 // (Un)RegisterRequestInterceptor.
67 class Interceptor { 70 class Interceptor {
68 public: 71 public:
69 virtual ~Interceptor() {} 72 virtual ~Interceptor() {}
70 73
71 // Called for every request made. Should return a new job to handle the 74 // Called for every request made. Should return a new job to handle the
72 // request if it should be intercepted, or NULL to allow the request to 75 // request if it should be intercepted, or NULL to allow the request to
73 // be handled in the normal manner. 76 // be handled in the normal manner.
74 virtual URLRequestJob* MaybeIntercept(URLRequest* request) = 0; 77 virtual URLRequestJob* MaybeIntercept(URLRequest* request) = 0;
78
79 // Called after having received a redirect response, but prior to the
80 // the request delegate being informed of the redirect. Can return a new
81 // job to replace the existing job if it should be intercepted, or NULL
82 // to allow the normal handling to continue. If a new job is provided,
83 // the delegate never sees the original redirect response, instead the
84 // response produced by the intercept job will be returned.
85 virtual URLRequestJob* MaybeInterceptRedirect(URLRequest* request,
86 const GURL& location) {
87 return NULL;
88 }
89
90 // Called after having received a final response, but prior to the
91 // the request delegate being informed of the response. This is also
92 // called when there is no server response at all to allow interception
93 // on dns or network errors. Can return a new job to replace the existing
94 // job if it should be intercepted, or NULL to allow the normal handling to
95 // continue. If a new job is provided, the delegate never sees the original
96 // response, instead the response produced by the intercept job will be
97 // returned.
98 virtual URLRequestJob* MaybeInterceptResponse(URLRequest* request) {
99 return NULL;
100 }
75 }; 101 };
76 102
77 // The delegate's methods are called from the message loop of the thread 103 // The delegate's methods are called from the message loop of the thread
78 // on which the request's Start() method is called. See above for the 104 // on which the request's Start() method is called. See above for the
79 // ordering of callbacks. 105 // ordering of callbacks.
80 // 106 //
81 // The callbacks will be called in the following order: 107 // The callbacks will be called in the following order:
82 // Start() 108 // Start()
83 // - OnReceivedRedirect* (zero or more calls, for the number of redirects) 109 // - OnReceivedRedirect* (zero or more calls, for the number of redirects)
84 // - OnAuthRequired* (zero or more calls, for the number of 110 // - OnAuthRequired* (zero or more calls, for the number of
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
150 }; 176 };
151 177
152 // Initialize an URL request. 178 // Initialize an URL request.
153 URLRequest(const GURL& url, Delegate* delegate); 179 URLRequest(const GURL& url, Delegate* delegate);
154 180
155 // If destroyed after Start() has been called but while IO is pending, 181 // If destroyed after Start() has been called but while IO is pending,
156 // then the request will be effectively canceled and the delegate 182 // then the request will be effectively canceled and the delegate
157 // will not have any more of its methods called. 183 // will not have any more of its methods called.
158 ~URLRequest(); 184 ~URLRequest();
159 185
160 // The user data allows the owner to associate data with this request. 186 // The user data allows the clients to associate data with this request.
161 // This request will TAKE OWNERSHIP of the given pointer, and will delete 187 // Multiple user data values can be stored under different keys.
162 // the object if it is changed or the request is destroyed. 188 // This request will TAKE OWNERSHIP of the given data pointer, and will
163 UserData* user_data() const { 189 // delete the object if it is changed or the request is destroyed.
164 return user_data_; 190 UserData* GetUserData(void* key) const;
165 } 191 void SetUserData(void* key, UserData* data);
166 void set_user_data(UserData* user_data) {
167 if (user_data_)
168 delete user_data_;
169 user_data_ = user_data;
170 }
171 192
172 // Registers a new protocol handler for the given scheme. If the scheme is 193 // Registers a new protocol handler for the given scheme. If the scheme is
173 // already handled, this will overwrite the given factory. To delete the 194 // already handled, this will overwrite the given factory. To delete the
174 // protocol factory, use NULL for the factory BUT this WILL NOT put back 195 // protocol factory, use NULL for the factory BUT this WILL NOT put back
175 // any previously registered protocol factory. It will have returned 196 // any previously registered protocol factory. It will have returned
176 // the previously registered factory (or NULL if none is registered) when 197 // the previously registered factory (or NULL if none is registered) when
177 // the scheme was first registered so that the caller can manually put it 198 // the scheme was first registered so that the caller can manually put it
178 // back if desired. 199 // back if desired.
179 // 200 //
180 // The scheme must be all-lowercase ASCII. See the ProtocolFactory 201 // The scheme must be all-lowercase ASCII. See the ProtocolFactory
(...skipping 262 matching lines...) Expand 10 before | Expand all | Expand 10 after
443 // Allow the URLRequestJob class to control the is_pending() flag. 464 // Allow the URLRequestJob class to control the is_pending() flag.
444 void set_is_pending(bool value) { is_pending_ = value; } 465 void set_is_pending(bool value) { is_pending_ = value; }
445 466
446 // Allow the URLRequestJob class to set our status too 467 // Allow the URLRequestJob class to set our status too
447 void set_status(const URLRequestStatus& value) { status_ = value; } 468 void set_status(const URLRequestStatus& value) { status_ = value; }
448 469
449 // Allow the URLRequestJob to redirect this request. Returns net::OK if 470 // Allow the URLRequestJob to redirect this request. Returns net::OK if
450 // successful, otherwise an error code is returned. 471 // successful, otherwise an error code is returned.
451 int Redirect(const GURL& location, int http_status_code); 472 int Redirect(const GURL& location, int http_status_code);
452 473
474 // Called by URLRequestJob to allow interception when a redirect occurs.
475 void ReceivedRedirect(const GURL& location);
476
477 // Called by URLRequestJob to allow interception when the final response
478 // occurs.
479 void ResponseStarted();
480
481 // Allow an interceptor's URLRequestJob to restart this request.
482 // Should only be called if the original job has not started a resposne.
483 void Restart();
484
453 private: 485 private:
454 friend class URLRequestJob; 486 friend class URLRequestJob;
455 487
488 void StartJob(URLRequestJob* job);
489 void RestartWithJob(URLRequestJob *job);
490
456 // Detaches the job from this request in preparation for this object going 491 // Detaches the job from this request in preparation for this object going
457 // away or the job being replaced. The job will not call us back when it has 492 // away or the job being replaced. The job will not call us back when it has
458 // been orphaned. 493 // been orphaned.
459 void OrphanJob(); 494 void OrphanJob();
460 495
461 // Cancels the request and set the error and ssl info for this request to the 496 // Cancels the request and set the error and ssl info for this request to the
462 // passed values. 497 // passed values.
463 void DoCancel(int os_error, const net::SSLInfo& ssl_info); 498 void DoCancel(int os_error, const net::SSLInfo& ssl_info);
464 499
465 // Discard headers which have meaning in POST (Content-Length, Content-Type, 500 // Discard headers which have meaning in POST (Content-Length, Content-Type,
(...skipping 23 matching lines...) Expand all
489 URLRequestStatus status_; 524 URLRequestStatus status_;
490 525
491 // The HTTP response info, lazily initialized. 526 // The HTTP response info, lazily initialized.
492 net::HttpResponseInfo response_info_; 527 net::HttpResponseInfo response_info_;
493 528
494 // Tells us whether the job is outstanding. This is true from the time 529 // Tells us whether the job is outstanding. This is true from the time
495 // Start() is called to the time we dispatch RequestComplete and indicates 530 // Start() is called to the time we dispatch RequestComplete and indicates
496 // whether the job is active. 531 // whether the job is active.
497 bool is_pending_; 532 bool is_pending_;
498 533
499 // Externally-defined data associated with this request 534 // Externally-defined data accessible by key
500 UserData* user_data_; 535 typedef std::map<void*, linked_ptr<UserData> > UserDataMap;
536 UserDataMap user_data_;
501 537
502 // Whether to enable performance profiling on the job serving this request. 538 // Whether to enable performance profiling on the job serving this request.
503 bool enable_profiling_; 539 bool enable_profiling_;
504 540
505 // Number of times we're willing to redirect. Used to guard against 541 // Number of times we're willing to redirect. Used to guard against
506 // infinite redirects. 542 // infinite redirects.
507 int redirect_limit_; 543 int redirect_limit_;
508 544
509 // Contextual information used for this request (can be NULL). 545 // Contextual information used for this request (can be NULL).
510 scoped_refptr<URLRequestContext> context_; 546 scoped_refptr<URLRequestContext> context_;
(...skipping 28 matching lines...) Expand all
539 #define URLREQUEST_COUNT_DTOR() url_request_metrics.object_count-- 575 #define URLREQUEST_COUNT_DTOR() url_request_metrics.object_count--
540 576
541 #else // disable leak checking in release builds... 577 #else // disable leak checking in release builds...
542 578
543 #define URLREQUEST_COUNT_CTOR() 579 #define URLREQUEST_COUNT_CTOR()
544 #define URLREQUEST_COUNT_DTOR() 580 #define URLREQUEST_COUNT_DTOR()
545 581
546 #endif // #ifndef NDEBUG 582 #endif // #ifndef NDEBUG
547 583
548 #endif // NET_URL_REQUEST_URL_REQUEST_H_ 584 #endif // NET_URL_REQUEST_URL_REQUEST_H_
OLDNEW
« no previous file with comments | « chrome/browser/renderer_host/resource_dispatcher_host.cc ('k') | net/url_request/url_request.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698