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.
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.
9 #ifndef COMPONENTS_WEBDATA_COMMON_WEB_DATA_REQUEST_MANAGER_H__
10 #define COMPONENTS_WEBDATA_COMMON_WEB_DATA_REQUEST_MANAGER_H__
14 #include "base/memory/ref_counted.h"
15 #include "base/synchronization/lock.h"
16 #include "components/webdata/common/web_database_service.h"
17 #include "components/webdata/common/web_data_results.h"
18 #include "components/webdata/common/web_data_service_base.h"
19 #include "components/webdata/common/web_data_service_consumer.h"
22 class WebDataServiceConsumer
;
23 class WebDataRequestManager
;
29 //////////////////////////////////////////////////////////////////////////////
33 // Every request is processed using a request object. The object contains
34 // both the request parameters and the results.
35 //////////////////////////////////////////////////////////////////////////////
36 class WebDataRequest
{
38 WebDataRequest(WebDataServiceConsumer
* consumer
,
39 WebDataRequestManager
* manager
);
41 virtual ~WebDataRequest();
43 WebDataServiceBase::Handle
GetHandle() const;
45 // Retrieves the |consumer_| set in the constructor.
46 WebDataServiceConsumer
* GetConsumer() const;
48 // Retrieves the original message loop the of the request.
49 base::MessageLoop
* GetMessageLoop() const;
51 // Returns |true| if the request was cancelled via the |Cancel()| method.
52 bool IsCancelled() const;
54 // This can be invoked from any thread. From this point we assume that
55 // our consumer_ reference is invalid.
58 // Invoked when the request has been completed.
61 // The result is owned by the request.
62 void SetResult(scoped_ptr
<WDTypedResult
> r
);
64 // Transfers ownership pof result to caller. Should only be called once per
66 scoped_ptr
<WDTypedResult
> GetResult();
69 // Used to notify manager if request is cancelled. Uses a raw ptr instead of
70 // a ref_ptr so that it can be set to NULL when a request is cancelled.
71 WebDataRequestManager
* manager_
;
73 // Tracks loop that the request originated on.
74 base::MessageLoop
* message_loop_
;
76 // Identifier for this request.
77 WebDataServiceBase::Handle handle_
;
79 // A lock to protect against simultaneous cancellations of the request.
80 // Cancellation affects both the |cancelled_| flag and |consumer_|.
81 mutable base::Lock cancel_lock_
;
84 // The originator of the service request.
85 WebDataServiceConsumer
* consumer_
;
87 scoped_ptr
<WDTypedResult
> result_
;
89 DISALLOW_COPY_AND_ASSIGN(WebDataRequest
);
92 //////////////////////////////////////////////////////////////////////////////
94 // Webdata Request Manager
96 // Tracks all WebDataRequests for a WebDataService.
98 // Note: This is an internal interface, not to be used outside of webdata/
99 //////////////////////////////////////////////////////////////////////////////
100 class WebDataRequestManager
101 : public base::RefCountedThreadSafe
<WebDataRequestManager
> {
103 WebDataRequestManager();
105 // Cancel any pending request.
106 void CancelRequest(WebDataServiceBase::Handle h
);
108 // Invoked by the WebDataService when |request| has been completed.
109 void RequestCompleted(scoped_ptr
<WebDataRequest
> request
);
111 // Register the request as a pending request.
112 void RegisterRequest(WebDataRequest
* request
);
114 // Return the next request handle.
115 int GetNextRequestHandle();
118 friend class base::RefCountedThreadSafe
<WebDataRequestManager
>;
120 ~WebDataRequestManager();
122 // This will notify the consumer in whatever thread was used to create this
124 void RequestCompletedOnThread(scoped_ptr
<WebDataRequest
> request
);
126 // A lock to protect pending requests and next request handle.
127 base::Lock pending_lock_
;
129 // Next handle to be used for requests. Incremented for each use.
130 WebDataServiceBase::Handle next_request_handle_
;
132 typedef std::map
<WebDataServiceBase::Handle
, WebDataRequest
*> RequestMap
;
133 RequestMap pending_requests_
;
135 DISALLOW_COPY_AND_ASSIGN(WebDataRequestManager
);
138 #endif // COMPONENTS_WEBDATA_COMMON_WEB_DATA_REQUEST_MANAGER_H__