web_data_request_manager.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. // Chromium settings and storage represent user-selected preferences and
  5. // information and MUST not be extracted, overwritten or modified except
  6. // through Chromium defined APIs.
  7. #ifndef COMPONENTS_WEBDATA_COMMON_WEB_DATA_REQUEST_MANAGER_H__
  8. #define COMPONENTS_WEBDATA_COMMON_WEB_DATA_REQUEST_MANAGER_H__
  9. #include <map>
  10. #include <memory>
  11. #include "base/atomicops.h"
  12. #include "base/memory/ref_counted.h"
  13. #include "base/memory/weak_ptr.h"
  14. #include "base/synchronization/lock.h"
  15. #include "base/task/sequenced_task_runner.h"
  16. #include "components/webdata/common/web_data_results.h"
  17. #include "components/webdata/common/web_data_service_base.h"
  18. #include "components/webdata/common/web_data_service_consumer.h"
  19. #include "components/webdata/common/web_database_service.h"
  20. class WebDataServiceConsumer;
  21. class WebDataRequestManager;
  22. //////////////////////////////////////////////////////////////////////////////
  23. //
  24. // WebData requests
  25. //
  26. // Every request is processed using a request object. The object contains
  27. // both the request parameters and the results.
  28. //////////////////////////////////////////////////////////////////////////////
  29. class WebDataRequest {
  30. public:
  31. WebDataRequest(const WebDataRequest&) = delete;
  32. WebDataRequest& operator=(const WebDataRequest&) = delete;
  33. virtual ~WebDataRequest();
  34. // Returns the identifier for this request.
  35. WebDataServiceBase::Handle GetHandle() const;
  36. // Returns |true| if the request is active and |false| if the request has been
  37. // cancelled or has already completed.
  38. bool IsActive();
  39. private:
  40. // For access to the web request mutable state under the manager's lock.
  41. friend class WebDataRequestManager;
  42. // Private constructor called for WebDataRequestManager::NewRequest.
  43. WebDataRequest(WebDataRequestManager* manager,
  44. WebDataServiceConsumer* consumer,
  45. WebDataServiceBase::Handle handle);
  46. // Retrieves the manager set in the constructor, if the request is still
  47. // active, or nullptr if the request is inactive. The returned value may
  48. // change between calls.
  49. WebDataRequestManager* GetManager();
  50. // Retrieves the |consumer_| set in the constructor.
  51. WebDataServiceConsumer* GetConsumer();
  52. // Retrieves the original task runner of the request. This may be null if the
  53. // original task was not posted as a sequenced task.
  54. scoped_refptr<base::SequencedTaskRunner> GetTaskRunner();
  55. // Marks the current request as inactive, either due to cancellation or
  56. // completion.
  57. void MarkAsInactive();
  58. // Tracks task runner that the request originated on.
  59. const scoped_refptr<base::SequencedTaskRunner> task_runner_;
  60. // The manager associated with this request. This is stored as a raw (untyped)
  61. // pointer value because it does double duty as the flag indicating whether or
  62. // not this request is active (non-nullptr => active).
  63. base::subtle::AtomicWord atomic_manager_;
  64. // The originator of the service request.
  65. base::WeakPtr<WebDataServiceConsumer> consumer_;
  66. // Identifier for this request.
  67. const WebDataServiceBase::Handle handle_;
  68. };
  69. //////////////////////////////////////////////////////////////////////////////
  70. //
  71. // WebData Request Manager
  72. //
  73. // Tracks all WebDataRequests for a WebDataService.
  74. //
  75. // Note: This is an internal interface, not to be used outside of webdata/
  76. //////////////////////////////////////////////////////////////////////////////
  77. class WebDataRequestManager
  78. : public base::RefCountedThreadSafe<WebDataRequestManager> {
  79. public:
  80. WebDataRequestManager();
  81. WebDataRequestManager(const WebDataRequestManager&) = delete;
  82. WebDataRequestManager& operator=(const WebDataRequestManager&) = delete;
  83. // Factory function to create a new WebDataRequest.
  84. // Retrieves a WeakPtr to the |consumer| so that |consumer| does not have to
  85. // outlive the WebDataRequestManager.
  86. std::unique_ptr<WebDataRequest> NewRequest(WebDataServiceConsumer* consumer);
  87. // Cancel any pending request.
  88. void CancelRequest(WebDataServiceBase::Handle h);
  89. // Invoked by the WebDataService when |request| has been completed.
  90. void RequestCompleted(std::unique_ptr<WebDataRequest> request,
  91. std::unique_ptr<WDTypedResult> result);
  92. private:
  93. friend class base::RefCountedThreadSafe<WebDataRequestManager>;
  94. ~WebDataRequestManager();
  95. // This will notify the consumer in whatever thread was used to create this
  96. // request.
  97. void RequestCompletedOnThread(std::unique_ptr<WebDataRequest> request,
  98. std::unique_ptr<WDTypedResult> result);
  99. // A lock to protect pending requests and next request handle.
  100. base::Lock pending_lock_;
  101. // Next handle to be used for requests. Incremented for each use.
  102. WebDataServiceBase::Handle next_request_handle_;
  103. std::map<WebDataServiceBase::Handle, WebDataRequest*> pending_requests_;
  104. };
  105. #endif // COMPONENTS_WEBDATA_COMMON_WEB_DATA_REQUEST_MANAGER_H__