Add UMA stats for SBIRS StateStore loading
[chromium-blink-merge.git] / google_apis / drive / request_sender.h
blob6ace7976ff97d2201816224cfbc4a35d8d89b682
1 // Copyright (c) 2012 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.
5 #ifndef GOOGLE_APIS_DRIVE_REQUEST_SENDER_H_
6 #define GOOGLE_APIS_DRIVE_REQUEST_SENDER_H_
8 #include <set>
9 #include <string>
11 #include "base/basictypes.h"
12 #include "base/callback_forward.h"
13 #include "base/memory/ref_counted.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/memory/weak_ptr.h"
16 #include "base/threading/thread_checker.h"
17 #include "google_apis/drive/drive_api_error_codes.h"
19 namespace base {
20 class SequencedTaskRunner;
23 namespace net {
24 class URLRequestContextGetter;
27 namespace google_apis {
29 class AuthenticatedRequestInterface;
30 class AuthServiceInterface;
32 // Helper class that sends requests implementing
33 // AuthenticatedRequestInterface and handles retries and authentication.
34 class RequestSender {
35 public:
36 // |auth_service| is used for fetching OAuth tokens. It'll be owned by
37 // this RequestSender.
39 // |url_request_context_getter| is the context used to perform network
40 // requests from this RequestSender.
42 // |blocking_task_runner| is used for running blocking operation, e.g.,
43 // parsing JSON response from the server.
45 // |custom_user_agent| will be used for the User-Agent header in HTTP
46 // requests issued through the request sender if the value is not empty.
47 RequestSender(
48 AuthServiceInterface* auth_service,
49 net::URLRequestContextGetter* url_request_context_getter,
50 const scoped_refptr<base::SequencedTaskRunner>& blocking_task_runner,
51 const std::string& custom_user_agent);
52 ~RequestSender();
54 AuthServiceInterface* auth_service() { return auth_service_.get(); }
56 net::URLRequestContextGetter* url_request_context_getter() const {
57 return url_request_context_getter_.get();
60 base::SequencedTaskRunner* blocking_task_runner() const {
61 return blocking_task_runner_.get();
64 // Starts a request implementing the AuthenticatedRequestInterface
65 // interface, and makes the request retry upon authentication failures by
66 // calling back to RetryRequest. The |request| object is owned by this
67 // RequestSender. It will be deleted in RequestSender's destructor or
68 // in RequestFinished().
70 // Returns a closure to cancel the request. The closure cancels the request
71 // if it is in-flight, and does nothing if it is already terminated.
72 base::Closure StartRequestWithAuthRetry(
73 AuthenticatedRequestInterface* request);
75 // Notifies to this RequestSender that |request| has finished.
76 // TODO(kinaba): refactor the life time management and make this at private.
77 void RequestFinished(AuthenticatedRequestInterface* request);
79 private:
80 // Called when the access token is fetched.
81 void OnAccessTokenFetched(
82 const base::WeakPtr<AuthenticatedRequestInterface>& request,
83 DriveApiErrorCode error,
84 const std::string& access_token);
86 // Clears any authentication token and retries the request, which forces
87 // an authentication token refresh.
88 void RetryRequest(AuthenticatedRequestInterface* request);
90 // Cancels the request. Used for implementing the returned closure of
91 // StartRequestWithAuthRetry.
92 void CancelRequest(
93 const base::WeakPtr<AuthenticatedRequestInterface>& request);
95 scoped_ptr<AuthServiceInterface> auth_service_;
96 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter_;
97 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner_;
99 std::set<AuthenticatedRequestInterface*> in_flight_requests_;
100 const std::string custom_user_agent_;
102 base::ThreadChecker thread_checker_;
104 // Note: This should remain the last member so it'll be destroyed and
105 // invalidate its weak pointers before any other members are destroyed.
106 base::WeakPtrFactory<RequestSender> weak_ptr_factory_;
108 DISALLOW_COPY_AND_ASSIGN(RequestSender);
111 } // namespace google_apis
113 #endif // GOOGLE_APIS_DRIVE_REQUEST_SENDER_H_