Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(233)

Side by Side Diff: components/webdata/common/web_data_request_manager.h

Issue 2845753002: Remove data race in WebDataRequest::CancelRequest. (Closed)
Patch Set: rebase Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | components/webdata/common/web_data_request_manager.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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;
25 class WebDataRequestManager; 26 class WebDataRequestManager;
26 27
27 ////////////////////////////////////////////////////////////////////////////// 28 //////////////////////////////////////////////////////////////////////////////
28 // 29 //
29 // WebData requests 30 // WebData requests
30 // 31 //
31 // Every request is processed using a request object. The object contains 32 // Every request is processed using a request object. The object contains
32 // both the request parameters and the results. 33 // both the request parameters and the results.
33 ////////////////////////////////////////////////////////////////////////////// 34 //////////////////////////////////////////////////////////////////////////////
34 class WebDataRequest { 35 class WebDataRequest {
35 public: 36 public:
36 virtual ~WebDataRequest(); 37 virtual ~WebDataRequest();
37 38
38 // Returns the identifier for this request. 39 // Returns the identifier for this request.
39 WebDataServiceBase::Handle GetHandle() const; 40 WebDataServiceBase::Handle GetHandle() const;
40 41
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();
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();
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();
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();
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_;
74 77
75 // The originator of the service request. 78 // The originator of the service request.
76 WebDataServiceConsumer* consumer_; 79 WebDataServiceConsumer* const consumer_;
77 80
78 // Identifier for this request. 81 // Identifier for this request.
79 WebDataServiceBase::Handle handle_; 82 const WebDataServiceBase::Handle handle_;
80 83
81 DISALLOW_COPY_AND_ASSIGN(WebDataRequest); 84 DISALLOW_COPY_AND_ASSIGN(WebDataRequest);
82 }; 85 };
83 86
84 ////////////////////////////////////////////////////////////////////////////// 87 //////////////////////////////////////////////////////////////////////////////
85 // 88 //
86 // WebData Request Manager 89 // WebData Request Manager
87 // 90 //
88 // Tracks all WebDataRequests for a WebDataService. 91 // Tracks all WebDataRequests for a WebDataService.
89 // 92 //
90 // Note: This is an internal interface, not to be used outside of webdata/ 93 // Note: This is an internal interface, not to be used outside of webdata/
91 ////////////////////////////////////////////////////////////////////////////// 94 //////////////////////////////////////////////////////////////////////////////
92 class WebDataRequestManager 95 class WebDataRequestManager
93 : public base::RefCountedThreadSafe<WebDataRequestManager> { 96 : public base::RefCountedThreadSafe<WebDataRequestManager> {
94 public: 97 public:
95 WebDataRequestManager(); 98 WebDataRequestManager();
96 99
97 // Factory function to create a new WebDataRequest. 100 // Factory function to create a new WebDataRequest.
98 std::unique_ptr<WebDataRequest> NewRequest(WebDataServiceConsumer* consumer); 101 std::unique_ptr<WebDataRequest> NewRequest(WebDataServiceConsumer* consumer);
99 102
100 // Cancel any pending request. 103 // Cancel any pending request.
101 void CancelRequest(WebDataServiceBase::Handle h); 104 void CancelRequest(WebDataServiceBase::Handle h);
102 105
103 // Invoked by the WebDataService when |request| has been completed. 106 // Invoked by the WebDataService when |request| has been completed.
104 void RequestCompleted(std::unique_ptr<WebDataRequest> request, 107 void RequestCompleted(std::unique_ptr<WebDataRequest> request,
105 std::unique_ptr<WDTypedResult> result); 108 std::unique_ptr<WDTypedResult> result);
106 109
107 // A debugging aid to assert that the pending_lock_ is held by the current
108 // thread.
109 void AssertLockedByCurrentThread() const;
110
111 private: 110 private:
112 friend class base::RefCountedThreadSafe<WebDataRequestManager>; 111 friend class base::RefCountedThreadSafe<WebDataRequestManager>;
113 112
114 ~WebDataRequestManager(); 113 ~WebDataRequestManager();
115 114
116 // This will notify the consumer in whatever thread was used to create this 115 // This will notify the consumer in whatever thread was used to create this
117 // request. 116 // request.
118 void RequestCompletedOnThread(std::unique_ptr<WebDataRequest> request, 117 void RequestCompletedOnThread(std::unique_ptr<WebDataRequest> request,
119 std::unique_ptr<WDTypedResult> result); 118 std::unique_ptr<WDTypedResult> result);
120 119
121 // A lock to protect pending requests and next request handle. 120 // A lock to protect pending requests and next request handle.
122 base::Lock pending_lock_; 121 base::Lock pending_lock_;
123 122
124 // Next handle to be used for requests. Incremented for each use. 123 // Next handle to be used for requests. Incremented for each use.
125 WebDataServiceBase::Handle next_request_handle_; 124 WebDataServiceBase::Handle next_request_handle_;
126 125
127 std::map<WebDataServiceBase::Handle, WebDataRequest*> pending_requests_; 126 std::map<WebDataServiceBase::Handle, WebDataRequest*> pending_requests_;
128 127
129 DISALLOW_COPY_AND_ASSIGN(WebDataRequestManager); 128 DISALLOW_COPY_AND_ASSIGN(WebDataRequestManager);
130 }; 129 };
131 130
132 #endif // COMPONENTS_WEBDATA_COMMON_WEB_DATA_REQUEST_MANAGER_H__ 131 #endif // COMPONENTS_WEBDATA_COMMON_WEB_DATA_REQUEST_MANAGER_H__
OLDNEW
« no previous file with comments | « no previous file | components/webdata/common/web_data_request_manager.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698