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