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"
21 class WebDataServiceConsumer
;
22 class WebDataRequestManager
;
28 //////////////////////////////////////////////////////////////////////////////
32 // Every request is processed using a request object. The object contains
33 // both the request parameters and the results.
34 //////////////////////////////////////////////////////////////////////////////
35 class WebDataRequest
{
37 WebDataRequest(WebDataServiceConsumer
* consumer
,
38 WebDataRequestManager
* manager
);
40 virtual ~WebDataRequest();
42 WebDataServiceBase::Handle
GetHandle() const;
44 // Retrieves the |consumer_| set in the constructor.
45 WebDataServiceConsumer
* GetConsumer() const;
47 // Retrieves the original message loop the of the request.
48 base::MessageLoop
* GetMessageLoop() const;
50 // Returns |true| if the request was cancelled via the |Cancel()| method.
51 bool IsCancelled() const;
53 // This can be invoked from any thread. From this point we assume that
54 // our consumer_ reference is invalid.
57 // Invoked when the request has been completed.
60 // The result is owned by the request.
61 void SetResult(scoped_ptr
<WDTypedResult
> r
);
63 // Transfers ownership pof result to caller. Should only be called once per
65 scoped_ptr
<WDTypedResult
> GetResult();
68 // Used to notify manager if request is cancelled. Uses a raw ptr instead of
69 // a ref_ptr so that it can be set to NULL when a request is cancelled.
70 WebDataRequestManager
* manager_
;
72 // Tracks loop that the request originated on.
73 base::MessageLoop
* message_loop_
;
75 // Identifier for this request.
76 WebDataServiceBase::Handle handle_
;
78 // A lock to protect against simultaneous cancellations of the request.
79 // Cancellation affects both the |cancelled_| flag and |consumer_|.
80 mutable base::Lock cancel_lock_
;
83 // The originator of the service request.
84 WebDataServiceConsumer
* consumer_
;
86 scoped_ptr
<WDTypedResult
> result_
;
88 DISALLOW_COPY_AND_ASSIGN(WebDataRequest
);
91 //////////////////////////////////////////////////////////////////////////////
93 // Webdata Request Manager
95 // Tracks all WebDataRequests for a WebDataService.
97 // Note: This is an internal interface, not to be used outside of webdata/
98 //////////////////////////////////////////////////////////////////////////////
99 class WebDataRequestManager
100 : public base::RefCountedThreadSafe
<WebDataRequestManager
> {
102 WebDataRequestManager();
104 // Cancel any pending request.
105 void CancelRequest(WebDataServiceBase::Handle h
);
107 // Invoked by the WebDataService when |request| has been completed.
108 void RequestCompleted(scoped_ptr
<WebDataRequest
> request
);
110 // Register the request as a pending request.
111 void RegisterRequest(WebDataRequest
* request
);
113 // Return the next request handle.
114 int GetNextRequestHandle();
117 friend class base::RefCountedThreadSafe
<WebDataRequestManager
>;
119 ~WebDataRequestManager();
121 // This will notify the consumer in whatever thread was used to create this
123 void RequestCompletedOnThread(scoped_ptr
<WebDataRequest
> request
);
125 // A lock to protect pending requests and next request handle.
126 base::Lock pending_lock_
;
128 // Next handle to be used for requests. Incremented for each use.
129 WebDataServiceBase::Handle next_request_handle_
;
131 typedef std::map
<WebDataServiceBase::Handle
, WebDataRequest
*> RequestMap
;
132 RequestMap pending_requests_
;
134 DISALLOW_COPY_AND_ASSIGN(WebDataRequestManager
);
137 #endif // COMPONENTS_WEBDATA_COMMON_WEB_DATA_REQUEST_MANAGER_H__