OLD | NEW |
(Empty) | |
| 1 // Copyright 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. |
| 4 |
| 5 // Chromium settings and storage represent user-selected preferences and |
| 6 // information and MUST not be extracted, overwritten or modified except |
| 7 // through Chromium defined APIs. |
| 8 |
| 9 #ifndef CHROME_BROWSER_WEBDATA_WEB_DATA_REQUEST_MANAGER_H__ |
| 10 #define CHROME_BROWSER_WEBDATA_WEB_DATA_REQUEST_MANAGER_H__ |
| 11 |
| 12 #include <map> |
| 13 |
| 14 #include "base/memory/ref_counted.h" |
| 15 #include "base/synchronization/lock.h" |
| 16 #include "chrome/browser/api/webdata/web_data_results.h" |
| 17 #include "chrome/browser/api/webdata/web_data_service_base.h" |
| 18 #include "chrome/browser/api/webdata/web_data_service_consumer.h" |
| 19 |
| 20 class MessageLoop; |
| 21 class WebDataService; |
| 22 class WebDataServiceConsumer; |
| 23 class WebDataRequestManager; |
| 24 |
| 25 ////////////////////////////////////////////////////////////////////////////// |
| 26 // |
| 27 // Webdata requests |
| 28 // |
| 29 // Every request is processed using a request object. The object contains |
| 30 // both the request parameters and the results. |
| 31 ////////////////////////////////////////////////////////////////////////////// |
| 32 class WebDataRequest { |
| 33 public: |
| 34 WebDataRequest(WebDataService* service, |
| 35 WebDataServiceConsumer* consumer, |
| 36 WebDataRequestManager* manager); |
| 37 |
| 38 virtual ~WebDataRequest(); |
| 39 |
| 40 WebDataServiceBase::Handle GetHandle() const; |
| 41 |
| 42 // Retrieves the |consumer_| set in the constructor. |
| 43 WebDataServiceConsumer* GetConsumer() const; |
| 44 |
| 45 // Returns |true| if the request was cancelled via the |Cancel()| method. |
| 46 bool IsCancelled() const; |
| 47 |
| 48 // This can be invoked from any thread. From this point we assume that |
| 49 // our consumer_ reference is invalid. |
| 50 void Cancel(); |
| 51 |
| 52 // Invoked by the service when this request has been completed. |
| 53 // This will notify the service in whatever thread was used to create this |
| 54 // request. |
| 55 void RequestComplete(); |
| 56 |
| 57 // The result is owned by the request. |
| 58 void SetResult(WDTypedResult* r); |
| 59 const WDTypedResult* GetResult() const; |
| 60 |
| 61 private: |
| 62 // Used to notify service of request completion. |
| 63 scoped_refptr<WebDataService> service_; |
| 64 |
| 65 // Tracks loop that the request originated on. |
| 66 MessageLoop* message_loop_; |
| 67 |
| 68 // Identifier for this request. |
| 69 WebDataServiceBase::Handle handle_; |
| 70 |
| 71 // A lock to protect against simultaneous cancellations of the request. |
| 72 // Cancellation affects both the |cancelled_| flag and |consumer_|. |
| 73 mutable base::Lock cancel_lock_; |
| 74 bool cancelled_; |
| 75 |
| 76 // The originator of the service request. |
| 77 WebDataServiceConsumer* consumer_; |
| 78 |
| 79 WDTypedResult* result_; |
| 80 |
| 81 DISALLOW_COPY_AND_ASSIGN(WebDataRequest); |
| 82 }; |
| 83 |
| 84 ////////////////////////////////////////////////////////////////////////////// |
| 85 // |
| 86 // Webdata request templates |
| 87 // |
| 88 // Internally we use instances of the following template to represent |
| 89 // requests. |
| 90 ////////////////////////////////////////////////////////////////////////////// |
| 91 |
| 92 template <class T> |
| 93 class GenericRequest : public WebDataRequest { |
| 94 public: |
| 95 GenericRequest(WebDataService* service, |
| 96 WebDataServiceConsumer* consumer, |
| 97 WebDataRequestManager* manager, |
| 98 const T& arg) |
| 99 : WebDataRequest(service, consumer, manager), |
| 100 arg_(arg) { |
| 101 } |
| 102 |
| 103 virtual ~GenericRequest() { |
| 104 } |
| 105 |
| 106 const T& arg() const { return arg_; } |
| 107 |
| 108 private: |
| 109 T arg_; |
| 110 }; |
| 111 |
| 112 template <class T, class U> |
| 113 class GenericRequest2 : public WebDataRequest { |
| 114 public: |
| 115 GenericRequest2(WebDataService* service, |
| 116 WebDataServiceConsumer* consumer, |
| 117 WebDataRequestManager* manager, |
| 118 const T& arg1, |
| 119 const U& arg2) |
| 120 : WebDataRequest(service, consumer, manager), |
| 121 arg1_(arg1), |
| 122 arg2_(arg2) { |
| 123 } |
| 124 |
| 125 virtual ~GenericRequest2() { } |
| 126 |
| 127 const T& arg1() const { return arg1_; } |
| 128 |
| 129 const U& arg2() const { return arg2_; } |
| 130 |
| 131 private: |
| 132 T arg1_; |
| 133 U arg2_; |
| 134 }; |
| 135 |
| 136 ////////////////////////////////////////////////////////////////////////////// |
| 137 // |
| 138 // Webdata Request Manager |
| 139 // |
| 140 // Tracks all WebDataRequests for a WebDataService. |
| 141 // |
| 142 // Note: This is an internal interface, not to be used outside of webdata/ |
| 143 ////////////////////////////////////////////////////////////////////////////// |
| 144 class WebDataRequestManager { |
| 145 public: |
| 146 WebDataRequestManager(); |
| 147 |
| 148 ~WebDataRequestManager(); |
| 149 |
| 150 // Cancel any pending request. |
| 151 void CancelRequest(WebDataServiceBase::Handle h); |
| 152 |
| 153 // Invoked by request implementations when a request has been processed. |
| 154 void RequestCompleted(WebDataServiceBase::Handle h); |
| 155 |
| 156 // Register the request as a pending request. |
| 157 void RegisterRequest(WebDataRequest* request); |
| 158 |
| 159 // Return the next request handle. |
| 160 int GetNextRequestHandle(); |
| 161 |
| 162 private: |
| 163 // A lock to protect pending requests and next request handle. |
| 164 base::Lock pending_lock_; |
| 165 |
| 166 // Next handle to be used for requests. Incremented for each use. |
| 167 WebDataServiceBase::Handle next_request_handle_; |
| 168 |
| 169 typedef std::map<WebDataServiceBase::Handle, WebDataRequest*> RequestMap; |
| 170 RequestMap pending_requests_; |
| 171 |
| 172 DISALLOW_COPY_AND_ASSIGN(WebDataRequestManager); |
| 173 }; |
| 174 |
| 175 #endif // CHROME_BROWSER_WEBDATA_WEB_DATA_REQUEST_MANAGER_H__ |
OLD | NEW |