| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 CHROME_BROWSER_EXTENSIONS_UPDATER_REQUEST_QUEUE_H_ | 5 #ifndef EXTENSIONS_BROWSER_UPDATER_REQUEST_QUEUE_H_ |
| 6 #define CHROME_BROWSER_EXTENSIONS_UPDATER_REQUEST_QUEUE_H_ | 6 #define EXTENSIONS_BROWSER_UPDATER_REQUEST_QUEUE_H_ |
| 7 | 7 |
| 8 #include <deque> | 8 #include <deque> |
| 9 #include <utility> | 9 #include <utility> |
| 10 | 10 |
| 11 #include "base/callback.h" | 11 #include "base/callback.h" |
| 12 #include "base/memory/linked_ptr.h" | 12 #include "base/memory/linked_ptr.h" |
| 13 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
| 14 #include "base/time/time.h" | 14 #include "base/time/time.h" |
| 15 #include "base/timer/timer.h" | 15 #include "base/timer/timer.h" |
| 16 #include "net/base/backoff_entry.h" | 16 #include "net/base/backoff_entry.h" |
| 17 | 17 |
| 18 namespace extensions { | 18 namespace extensions { |
| 19 | 19 |
| 20 // This class keeps track of a queue of requests, and contains the logic to | 20 // This class keeps track of a queue of requests, and contains the logic to |
| 21 // retry requests with some backoff policy. Each request has a | 21 // retry requests with some backoff policy. Each request has a |
| 22 // net::BackoffEntry instance associated with it. | 22 // net::BackoffEntry instance associated with it. |
| 23 // | 23 // |
| 24 // The general flow when using this class would be something like this: | 24 // The general flow when using this class would be something like this: |
| 25 // - requests are queued up by calling ScheduleRequest. | 25 // - requests are queued up by calling ScheduleRequest. |
| 26 // - when a request is ready to be executed, RequestQueue removes the | 26 // - when a request is ready to be executed, RequestQueue removes the |
| 27 // request from the queue, assigns it as active request, and calls | 27 // request from the queue, assigns it as active request, and calls |
| 28 // the callback that was passed to the constructor. | 28 // the callback that was passed to the constructor. |
| 29 // - (optionally) when a request has completed unsuccessfully call | 29 // - (optionally) when a request has completed unsuccessfully call |
| 30 // RetryRequest to put the request back in the queue, using the | 30 // RetryRequest to put the request back in the queue, using the |
| 31 // backoff policy and minimum backoff delay to determine when to | 31 // backoff policy and minimum backoff delay to determine when to |
| 32 // next schedule this request. | 32 // next schedule this request. |
| 33 // - call reset_active_request() to indicate that the active request has | 33 // - call reset_active_request() to indicate that the active request has |
| 34 // been dealt with. | 34 // been dealt with. |
| 35 // - call StartNextRequest to schedule the next pending request (if any). | 35 // - call StartNextRequest to schedule the next pending request (if any). |
| 36 template<typename T> | 36 template <typename T> |
| 37 class RequestQueue { | 37 class RequestQueue { |
| 38 public: | 38 public: |
| 39 class iterator; | 39 class iterator; |
| 40 | 40 |
| 41 RequestQueue(const net::BackoffEntry::Policy* backoff_policy, | 41 RequestQueue(const net::BackoffEntry::Policy* backoff_policy, |
| 42 const base::Closure& start_request_callback); | 42 const base::Closure& start_request_callback); |
| 43 ~RequestQueue(); | 43 ~RequestQueue(); |
| 44 | 44 |
| 45 // Returns the request that is currently being processed. | 45 // Returns the request that is currently being processed. |
| 46 T* active_request(); | 46 T* active_request(); |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 81 | 81 |
| 82 private: | 82 private: |
| 83 struct Request { | 83 struct Request { |
| 84 Request(net::BackoffEntry* backoff_entry, T* request) | 84 Request(net::BackoffEntry* backoff_entry, T* request) |
| 85 : backoff_entry(backoff_entry), request(request) {} | 85 : backoff_entry(backoff_entry), request(request) {} |
| 86 linked_ptr<net::BackoffEntry> backoff_entry; | 86 linked_ptr<net::BackoffEntry> backoff_entry; |
| 87 linked_ptr<T> request; | 87 linked_ptr<T> request; |
| 88 }; | 88 }; |
| 89 | 89 |
| 90 // Compares the release time of two pending requests. | 90 // Compares the release time of two pending requests. |
| 91 static bool CompareRequests(const Request& a, | 91 static bool CompareRequests(const Request& a, const Request& b); |
| 92 const Request& b); | |
| 93 | 92 |
| 94 // Pushes a request with a given backoff entry onto the queue. | 93 // Pushes a request with a given backoff entry onto the queue. |
| 95 void PushImpl(scoped_ptr<T> request, | 94 void PushImpl(scoped_ptr<T> request, |
| 96 scoped_ptr<net::BackoffEntry> backoff_entry); | 95 scoped_ptr<net::BackoffEntry> backoff_entry); |
| 97 | 96 |
| 98 // The backoff policy used to determine backoff delays. | 97 // The backoff policy used to determine backoff delays. |
| 99 const net::BackoffEntry::Policy* backoff_policy_; | 98 const net::BackoffEntry::Policy* backoff_policy_; |
| 100 | 99 |
| 101 // Callback to call when a new request has become the active request. | 100 // Callback to call when a new request has become the active request. |
| 102 base::Closure start_request_callback_; | 101 base::Closure start_request_callback_; |
| 103 | 102 |
| 104 // Priority queue of pending requests. Not using std::priority_queue since | 103 // Priority queue of pending requests. Not using std::priority_queue since |
| 105 // the code needs to be able to iterate over all pending requests. | 104 // the code needs to be able to iterate over all pending requests. |
| 106 std::deque<Request> pending_requests_; | 105 std::deque<Request> pending_requests_; |
| 107 | 106 |
| 108 // Active request and its associated backoff entry. | 107 // Active request and its associated backoff entry. |
| 109 scoped_ptr<T> active_request_; | 108 scoped_ptr<T> active_request_; |
| 110 scoped_ptr<net::BackoffEntry> active_backoff_entry_; | 109 scoped_ptr<net::BackoffEntry> active_backoff_entry_; |
| 111 | 110 |
| 112 // Timer to schedule calls to StartNextRequest, if the first pending request | 111 // Timer to schedule calls to StartNextRequest, if the first pending request |
| 113 // hasn't passed its release time yet. | 112 // hasn't passed its release time yet. |
| 114 base::Timer timer_; | 113 base::Timer timer_; |
| 115 }; | 114 }; |
| 116 | 115 |
| 117 // Iterator class that wraps a std::deque<> iterator, only giving access to the | 116 // Iterator class that wraps a std::deque<> iterator, only giving access to the |
| 118 // actual request part of each item. | 117 // actual request part of each item. |
| 119 template<typename T> | 118 template <typename T> |
| 120 class RequestQueue<T>::iterator { | 119 class RequestQueue<T>::iterator { |
| 121 public: | 120 public: |
| 122 iterator() {} | 121 iterator() {} |
| 123 | 122 |
| 124 T* operator*() { return it_->request.get(); } | 123 T* operator*() { return it_->request.get(); } |
| 125 T* operator->() { return it_->request.get(); } | 124 T* operator->() { return it_->request.get(); } |
| 126 iterator& operator++() { | 125 iterator& operator++() { |
| 127 ++it_; | 126 ++it_; |
| 128 return *this; | 127 return *this; |
| 129 } | 128 } |
| 130 bool operator!=(const iterator& b) const { | 129 bool operator!=(const iterator& b) const { return it_ != b.it_; } |
| 131 return it_ != b.it_; | |
| 132 } | |
| 133 | 130 |
| 134 private: | 131 private: |
| 135 friend class RequestQueue<T>; | 132 friend class RequestQueue<T>; |
| 136 typedef std::deque<typename RequestQueue<T>::Request> Container; | 133 typedef std::deque<typename RequestQueue<T>::Request> Container; |
| 137 | 134 |
| 138 explicit iterator(const typename Container::iterator& it) | 135 explicit iterator(const typename Container::iterator& it) : it_(it) {} |
| 139 : it_(it) {} | |
| 140 | 136 |
| 141 typename Container::iterator it_; | 137 typename Container::iterator it_; |
| 142 }; | 138 }; |
| 143 | 139 |
| 144 | |
| 145 } // namespace extensions | 140 } // namespace extensions |
| 146 | 141 |
| 147 #endif // CHROME_BROWSER_EXTENSIONS_UPDATER_REQUEST_QUEUE_H_ | 142 #endif // EXTENSIONS_BROWSER_UPDATER_REQUEST_QUEUE_H_ |
| OLD | NEW |