• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 COMPONENTS_WEBDATA_COMMON_WEB_DATA_REQUEST_MANAGER_H__
10 #define COMPONENTS_WEBDATA_COMMON_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 "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"
20 
21 class WebDataService;
22 class WebDataServiceConsumer;
23 class WebDataRequestManager;
24 
25 namespace base {
26 class MessageLoop;
27 }
28 
29 //////////////////////////////////////////////////////////////////////////////
30 //
31 // Webdata requests
32 //
33 // Every request is processed using a request object. The object contains
34 // both the request parameters and the results.
35 //////////////////////////////////////////////////////////////////////////////
36 class WebDataRequest {
37  public:
38   WebDataRequest(WebDataServiceConsumer* consumer,
39                  WebDataRequestManager* manager);
40 
41   virtual ~WebDataRequest();
42 
43   WebDataServiceBase::Handle GetHandle() const;
44 
45   // Retrieves the |consumer_| set in the constructor.
46   WebDataServiceConsumer* GetConsumer() const;
47 
48   // Retrieves the original message loop the of the request.
49   base::MessageLoop* GetMessageLoop() const;
50 
51   // Returns |true| if the request was cancelled via the |Cancel()| method.
52   bool IsCancelled() const;
53 
54   // This can be invoked from any thread. From this point we assume that
55   // our consumer_ reference is invalid.
56   void Cancel();
57 
58   // Invoked when the request has been completed.
59   void OnComplete();
60 
61   // The result is owned by the request.
62   void SetResult(scoped_ptr<WDTypedResult> r);
63 
64   // Transfers ownership pof result to caller. Should only be called once per
65   // result.
66   scoped_ptr<WDTypedResult> GetResult();
67 
68  private:
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_;
72 
73   // Tracks loop that the request originated on.
74   base::MessageLoop* message_loop_;
75 
76   // Identifier for this request.
77   WebDataServiceBase::Handle handle_;
78 
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_;
82   bool cancelled_;
83 
84   // The originator of the service request.
85   WebDataServiceConsumer* consumer_;
86 
87   scoped_ptr<WDTypedResult> result_;
88 
89   DISALLOW_COPY_AND_ASSIGN(WebDataRequest);
90 };
91 
92 //////////////////////////////////////////////////////////////////////////////
93 //
94 // Webdata Request Manager
95 //
96 // Tracks all WebDataRequests for a WebDataService.
97 //
98 // Note: This is an internal interface, not to be used outside of webdata/
99 //////////////////////////////////////////////////////////////////////////////
100 class WebDataRequestManager
101     : public base::RefCountedThreadSafe<WebDataRequestManager> {
102  public:
103   WebDataRequestManager();
104 
105   // Cancel any pending request.
106   void CancelRequest(WebDataServiceBase::Handle h);
107 
108   // Invoked by the WebDataService when |request| has been completed.
109   void RequestCompleted(scoped_ptr<WebDataRequest> request);
110 
111   // Register the request as a pending request.
112   void RegisterRequest(WebDataRequest* request);
113 
114   // Return the next request handle.
115   int GetNextRequestHandle();
116 
117  private:
118   friend class base::RefCountedThreadSafe<WebDataRequestManager>;
119 
120   ~WebDataRequestManager();
121 
122   // This will notify the consumer in whatever thread was used to create this
123   // request.
124   void RequestCompletedOnThread(scoped_ptr<WebDataRequest> request);
125 
126   // A lock to protect pending requests and next request handle.
127   base::Lock pending_lock_;
128 
129   // Next handle to be used for requests. Incremented for each use.
130   WebDataServiceBase::Handle next_request_handle_;
131 
132   typedef std::map<WebDataServiceBase::Handle, WebDataRequest*> RequestMap;
133   RequestMap pending_requests_;
134 
135   DISALLOW_COPY_AND_ASSIGN(WebDataRequestManager);
136 };
137 
138 #endif  // COMPONENTS_WEBDATA_COMMON_WEB_DATA_REQUEST_MANAGER_H__
139