Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 // Chromium settings and storage represent user-selected preferences and | 5 // Chromium settings and storage represent user-selected preferences and |
| 6 // information and MUST not be extracted, overwritten or modified except | 6 // information and MUST not be extracted, overwritten or modified except |
| 7 // through Chromium defined APIs. | 7 // through Chromium defined APIs. |
| 8 | 8 |
| 9 #ifndef COMPONENTS_WEBDATA_COMMON_WEB_DATA_REQUEST_MANAGER_H__ | 9 #ifndef COMPONENTS_WEBDATA_COMMON_WEB_DATA_REQUEST_MANAGER_H__ |
| 10 #define COMPONENTS_WEBDATA_COMMON_WEB_DATA_REQUEST_MANAGER_H__ | 10 #define COMPONENTS_WEBDATA_COMMON_WEB_DATA_REQUEST_MANAGER_H__ |
| 11 | 11 |
| 12 #include <map> | 12 #include <map> |
| 13 #include <memory> | 13 #include <memory> |
| 14 | 14 |
| 15 #include "base/atomicops.h" | |
| 15 #include "base/macros.h" | 16 #include "base/macros.h" |
| 16 #include "base/memory/ref_counted.h" | 17 #include "base/memory/ref_counted.h" |
| 17 #include "base/single_thread_task_runner.h" | 18 #include "base/single_thread_task_runner.h" |
| 18 #include "base/synchronization/lock.h" | 19 #include "base/synchronization/lock.h" |
| 19 #include "components/webdata/common/web_data_results.h" | 20 #include "components/webdata/common/web_data_results.h" |
| 20 #include "components/webdata/common/web_data_service_base.h" | 21 #include "components/webdata/common/web_data_service_base.h" |
| 21 #include "components/webdata/common/web_data_service_consumer.h" | 22 #include "components/webdata/common/web_data_service_consumer.h" |
| 22 #include "components/webdata/common/web_database_service.h" | 23 #include "components/webdata/common/web_database_service.h" |
| 23 | 24 |
| 24 class WebDataServiceConsumer; | 25 class WebDataServiceConsumer; |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 41 // Returns |true| if the request is active and |false| if the request has been | 42 // Returns |true| if the request is active and |false| if the request has been |
| 42 // cancelled or has already completed. | 43 // cancelled or has already completed. |
| 43 bool IsActive() const; | 44 bool IsActive() const; |
| 44 | 45 |
| 45 private: | 46 private: |
| 46 // For access to the web request mutable state under the manager's lock. | 47 // For access to the web request mutable state under the manager's lock. |
| 47 friend class WebDataRequestManager; | 48 friend class WebDataRequestManager; |
| 48 | 49 |
| 49 // Private constructor called for WebDataRequestManager::NewRequest. | 50 // Private constructor called for WebDataRequestManager::NewRequest. |
| 50 WebDataRequest(WebDataRequestManager* manager, | 51 WebDataRequest(WebDataRequestManager* manager, |
| 51 WebDataServiceConsumer* consumer); | 52 WebDataServiceConsumer* consumer, |
| 53 WebDataServiceBase::Handle handle); | |
| 52 | 54 |
| 53 // Internal debugging helper to assert that the request is active and that the | 55 // Retrieves the manager set in the constructor, if the request is still |
| 54 // manager's lock is held by the current thread. | 56 // active, or nullptr if the request is inactive. The returned value may |
| 55 void AssertThreadSafe() const; | 57 // change between calls. |
| 58 WebDataRequestManager* GetManager() const; | |
|
Peter Kasting
2017/04/28 06:36:41
Const methods should not return non-const pointers
Roger McFarlane (Chromium)
2017/04/28 15:06:13
Removed const qualifiers as needed.
| |
| 56 | 59 |
| 57 // Retrieves the |consumer_| set in the constructor. | 60 // Retrieves the |consumer_| set in the constructor. |
| 58 WebDataServiceConsumer* GetConsumer() const; | 61 WebDataServiceConsumer* GetConsumer() const; |
| 59 | 62 |
| 60 // Retrieves the original task runner of the request. | 63 // Retrieves the original task runner of the request. |
| 61 scoped_refptr<base::SingleThreadTaskRunner> GetTaskRunner() const; | 64 scoped_refptr<base::SingleThreadTaskRunner> GetTaskRunner() const; |
| 62 | 65 |
| 63 // Marks the current request as inactive, either due to cancellation or | 66 // Marks the current request as inactive, either due to cancellation or |
| 64 // completion. | 67 // completion. |
| 65 void MarkAsInactive(); | 68 void MarkAsInactive(); |
| 66 | 69 |
| 67 // Tracks task runner that the request originated on. | 70 // Tracks task runner that the request originated on. |
| 68 const scoped_refptr<base::SingleThreadTaskRunner> task_runner_; | 71 const scoped_refptr<base::SingleThreadTaskRunner> task_runner_; |
| 69 | 72 |
| 70 // Used to notify manager if request is cancelled. Uses a raw ptr instead of | 73 // The manager associated with this request. This is stored as a raw (untyped) |
| 71 // a ref_ptr so that it can be set to null when a request is cancelled or | 74 // pointer value because it does double duty as the flag indicating whether or |
| 72 // completed. | 75 // not this request is active (non-nullptr => active). |
| 73 WebDataRequestManager* manager_; | 76 base::subtle::AtomicWord atomic_manager_; |
| 77 static_assert(sizeof(atomic_manager_) == sizeof(WebDataRequestManager*), | |
| 78 "size mismatch"); | |
| 74 | 79 |
| 75 // The originator of the service request. | 80 // The originator of the service request. |
| 76 WebDataServiceConsumer* consumer_; | 81 WebDataServiceConsumer* const consumer_; |
| 77 | 82 |
| 78 // Identifier for this request. | 83 // Identifier for this request. |
| 79 WebDataServiceBase::Handle handle_; | 84 const WebDataServiceBase::Handle handle_; |
| 80 | 85 |
| 81 DISALLOW_COPY_AND_ASSIGN(WebDataRequest); | 86 DISALLOW_COPY_AND_ASSIGN(WebDataRequest); |
| 82 }; | 87 }; |
| 83 | 88 |
| 84 ////////////////////////////////////////////////////////////////////////////// | 89 ////////////////////////////////////////////////////////////////////////////// |
| 85 // | 90 // |
| 86 // WebData Request Manager | 91 // WebData Request Manager |
| 87 // | 92 // |
| 88 // Tracks all WebDataRequests for a WebDataService. | 93 // Tracks all WebDataRequests for a WebDataService. |
| 89 // | 94 // |
| 90 // Note: This is an internal interface, not to be used outside of webdata/ | 95 // Note: This is an internal interface, not to be used outside of webdata/ |
| 91 ////////////////////////////////////////////////////////////////////////////// | 96 ////////////////////////////////////////////////////////////////////////////// |
| 92 class WebDataRequestManager | 97 class WebDataRequestManager |
| 93 : public base::RefCountedThreadSafe<WebDataRequestManager> { | 98 : public base::RefCountedThreadSafe<WebDataRequestManager> { |
| 94 public: | 99 public: |
| 95 WebDataRequestManager(); | 100 WebDataRequestManager(); |
| 96 | 101 |
| 97 // Factory function to create a new WebDataRequest. | 102 // Factory function to create a new WebDataRequest. |
| 98 std::unique_ptr<WebDataRequest> NewRequest(WebDataServiceConsumer* consumer); | 103 std::unique_ptr<WebDataRequest> NewRequest(WebDataServiceConsumer* consumer); |
| 99 | 104 |
| 100 // Cancel any pending request. | 105 // Cancel any pending request. |
| 101 void CancelRequest(WebDataServiceBase::Handle h); | 106 void CancelRequest(WebDataServiceBase::Handle h); |
| 102 | 107 |
| 103 // Invoked by the WebDataService when |request| has been completed. | 108 // Invoked by the WebDataService when |request| has been completed. |
| 104 void RequestCompleted(std::unique_ptr<WebDataRequest> request, | 109 void RequestCompleted(std::unique_ptr<WebDataRequest> request, |
| 105 std::unique_ptr<WDTypedResult> result); | 110 std::unique_ptr<WDTypedResult> result); |
| 106 | 111 |
| 107 // A debugging aid to assert that the pending_lock_ is held by the current | |
| 108 // thread. | |
| 109 void AssertLockedByCurrentThread() const; | |
| 110 | |
| 111 private: | 112 private: |
| 112 friend class base::RefCountedThreadSafe<WebDataRequestManager>; | 113 friend class base::RefCountedThreadSafe<WebDataRequestManager>; |
| 113 | 114 |
| 114 ~WebDataRequestManager(); | 115 ~WebDataRequestManager(); |
| 115 | 116 |
| 116 // This will notify the consumer in whatever thread was used to create this | 117 // This will notify the consumer in whatever thread was used to create this |
| 117 // request. | 118 // request. |
| 118 void RequestCompletedOnThread(std::unique_ptr<WebDataRequest> request, | 119 void RequestCompletedOnThread(std::unique_ptr<WebDataRequest> request, |
| 119 std::unique_ptr<WDTypedResult> result); | 120 std::unique_ptr<WDTypedResult> result); |
| 120 | 121 |
| 121 // A lock to protect pending requests and next request handle. | 122 // A lock to protect pending requests and next request handle. |
| 122 base::Lock pending_lock_; | 123 base::Lock pending_lock_; |
| 123 | 124 |
| 124 // Next handle to be used for requests. Incremented for each use. | 125 // Next handle to be used for requests. Incremented for each use. |
| 125 WebDataServiceBase::Handle next_request_handle_; | 126 WebDataServiceBase::Handle next_request_handle_; |
| 126 | 127 |
| 127 std::map<WebDataServiceBase::Handle, WebDataRequest*> pending_requests_; | 128 std::map<WebDataServiceBase::Handle, WebDataRequest*> pending_requests_; |
| 128 | 129 |
| 129 DISALLOW_COPY_AND_ASSIGN(WebDataRequestManager); | 130 DISALLOW_COPY_AND_ASSIGN(WebDataRequestManager); |
| 130 }; | 131 }; |
| 131 | 132 |
| 132 #endif // COMPONENTS_WEBDATA_COMMON_WEB_DATA_REQUEST_MANAGER_H__ | 133 #endif // COMPONENTS_WEBDATA_COMMON_WEB_DATA_REQUEST_MANAGER_H__ |
| OLD | NEW |