1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
|
// Copyright 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Chromium settings and storage represent user-selected preferences and
// information and MUST not be extracted, overwritten or modified except
// through Chromium defined APIs.
#ifndef COMPONENTS_WEBDATA_COMMON_WEB_DATA_REQUEST_MANAGER_H__
#define COMPONENTS_WEBDATA_COMMON_WEB_DATA_REQUEST_MANAGER_H__
#include <map>
#include "base/memory/ref_counted.h"
#include "base/synchronization/lock.h"
#include "components/webdata/common/web_database_service.h"
#include "components/webdata/common/web_data_results.h"
#include "components/webdata/common/web_data_service_base.h"
#include "components/webdata/common/web_data_service_consumer.h"
class WebDataServiceConsumer;
class WebDataRequestManager;
namespace base {
class MessageLoop;
}
//////////////////////////////////////////////////////////////////////////////
//
// Webdata requests
//
// Every request is processed using a request object. The object contains
// both the request parameters and the results.
//////////////////////////////////////////////////////////////////////////////
class WebDataRequest {
public:
WebDataRequest(WebDataServiceConsumer* consumer,
WebDataRequestManager* manager);
virtual ~WebDataRequest();
WebDataServiceBase::Handle GetHandle() const;
// Retrieves the |consumer_| set in the constructor.
WebDataServiceConsumer* GetConsumer() const;
// Retrieves the original message loop the of the request.
base::MessageLoop* GetMessageLoop() const;
// Returns |true| if the request was cancelled via the |Cancel()| method.
bool IsCancelled() const;
// This can be invoked from any thread. From this point we assume that
// our consumer_ reference is invalid.
void Cancel();
// Invoked when the request has been completed.
void OnComplete();
// The result is owned by the request.
void SetResult(scoped_ptr<WDTypedResult> r);
// Transfers ownership pof result to caller. Should only be called once per
// result.
scoped_ptr<WDTypedResult> GetResult();
private:
// Used to notify manager if request is cancelled. Uses a raw ptr instead of
// a ref_ptr so that it can be set to NULL when a request is cancelled.
WebDataRequestManager* manager_;
// Tracks loop that the request originated on.
base::MessageLoop* message_loop_;
// Identifier for this request.
WebDataServiceBase::Handle handle_;
// A lock to protect against simultaneous cancellations of the request.
// Cancellation affects both the |cancelled_| flag and |consumer_|.
mutable base::Lock cancel_lock_;
bool cancelled_;
// The originator of the service request.
WebDataServiceConsumer* consumer_;
scoped_ptr<WDTypedResult> result_;
DISALLOW_COPY_AND_ASSIGN(WebDataRequest);
};
//////////////////////////////////////////////////////////////////////////////
//
// Webdata Request Manager
//
// Tracks all WebDataRequests for a WebDataService.
//
// Note: This is an internal interface, not to be used outside of webdata/
//////////////////////////////////////////////////////////////////////////////
class WebDataRequestManager
: public base::RefCountedThreadSafe<WebDataRequestManager> {
public:
WebDataRequestManager();
// Cancel any pending request.
void CancelRequest(WebDataServiceBase::Handle h);
// Invoked by the WebDataService when |request| has been completed.
void RequestCompleted(scoped_ptr<WebDataRequest> request);
// Register the request as a pending request.
void RegisterRequest(WebDataRequest* request);
// Return the next request handle.
int GetNextRequestHandle();
private:
friend class base::RefCountedThreadSafe<WebDataRequestManager>;
~WebDataRequestManager();
// This will notify the consumer in whatever thread was used to create this
// request.
void RequestCompletedOnThread(scoped_ptr<WebDataRequest> request);
// A lock to protect pending requests and next request handle.
base::Lock pending_lock_;
// Next handle to be used for requests. Incremented for each use.
WebDataServiceBase::Handle next_request_handle_;
typedef std::map<WebDataServiceBase::Handle, WebDataRequest*> RequestMap;
RequestMap pending_requests_;
DISALLOW_COPY_AND_ASSIGN(WebDataRequestManager);
};
#endif // COMPONENTS_WEBDATA_COMMON_WEB_DATA_REQUEST_MANAGER_H__
|