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

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

Issue 2571923002: Fix threading issues in WebDataRequestManager. (Closed)
Patch Set: Created 4 years 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
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/macros.h" 15 #include "base/macros.h"
16 #include "base/memory/ref_counted.h" 16 #include "base/memory/ref_counted.h"
17 #include "base/single_thread_task_runner.h" 17 #include "base/single_thread_task_runner.h"
18 #include "base/synchronization/lock.h" 18 #include "base/synchronization/lock.h"
19 #include "components/webdata/common/web_data_results.h" 19 #include "components/webdata/common/web_data_results.h"
20 #include "components/webdata/common/web_data_service_base.h" 20 #include "components/webdata/common/web_data_service_base.h"
21 #include "components/webdata/common/web_data_service_consumer.h" 21 #include "components/webdata/common/web_data_service_consumer.h"
22 #include "components/webdata/common/web_database_service.h" 22 #include "components/webdata/common/web_database_service.h"
23 23
24 class WebDataServiceConsumer; 24 class WebDataServiceConsumer;
25 class WebDataRequestManager; 25 class WebDataRequestManager;
26 26
27 ////////////////////////////////////////////////////////////////////////////// 27 //////////////////////////////////////////////////////////////////////////////
28 // 28 //
29 // Webdata requests 29 // WebData requests
30 // 30 //
31 // Every request is processed using a request object. The object contains 31 // Every request is processed using a request object. The object contains
32 // both the request parameters and the results. 32 // both the request parameters and the results.
33 ////////////////////////////////////////////////////////////////////////////// 33 //////////////////////////////////////////////////////////////////////////////
34 class WebDataRequest { 34 class WebDataRequest {
35 public: 35 public:
36 WebDataRequest(WebDataServiceConsumer* consumer, 36 WebDataRequest(WebDataServiceConsumer* consumer,
37 WebDataRequestManager* manager); 37 WebDataRequestManager* manager);
38 38
39 virtual ~WebDataRequest(); 39 virtual ~WebDataRequest();
40 40
41 // Returns the identifier for this request.
41 WebDataServiceBase::Handle GetHandle() const; 42 WebDataServiceBase::Handle GetHandle() const;
42 43
44 // Returns |true| if the request was cancelled (or has already completed).
45 bool IsCancelled() const;
46
47 private:
48 // Internal debugging helper to assert that the manager pointer is valid and
49 // that the manager's lock is held by the current thread.
50 void AssertThreadSafe() const;
51
43 // Retrieves the |consumer_| set in the constructor. 52 // Retrieves the |consumer_| set in the constructor.
44 WebDataServiceConsumer* GetConsumer() const; 53 WebDataServiceConsumer* GetConsumer() const;
45 54
46 // Retrieves the original task runner of the request. 55 // Retrieves the original task runner of the request.
47 scoped_refptr<base::SingleThreadTaskRunner> GetTaskRunner() const; 56 scoped_refptr<base::SingleThreadTaskRunner> GetTaskRunner() const;
48 57
49 // Returns |true| if the request was cancelled via the |Cancel()| method. 58 // Marks the current request as inactive, either due to cancellation or
50 bool IsCancelled() const; 59 // completion.
51 60 void MarkAsInactive();
52 // This can be invoked from any thread. From this point we assume that
53 // our consumer_ reference is invalid.
54 void Cancel();
55
56 // Invoked when the request has been completed.
57 void OnComplete();
58 61
59 // The result is owned by the request. 62 // The result is owned by the request.
60 void SetResult(std::unique_ptr<WDTypedResult> r); 63 void SetResult(std::unique_ptr<WDTypedResult> r);
61 64
62 // Transfers ownership pof result to caller. Should only be called once per 65 // Transfers ownership of result to caller. Should only be called once per
63 // result. 66 // result.
64 std::unique_ptr<WDTypedResult> GetResult(); 67 std::unique_ptr<WDTypedResult> GetResult();
65 68
66 private:
67 // Used to notify manager if request is cancelled. Uses a raw ptr instead of
68 // a ref_ptr so that it can be set to NULL when a request is cancelled.
69 WebDataRequestManager* manager_;
70
71 // Tracks task runner that the request originated on. 69 // Tracks task runner that the request originated on.
72 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; 70 const scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
73 71
74 // Identifier for this request. 72 // Identifier for this request.
75 WebDataServiceBase::Handle handle_; 73 const WebDataServiceBase::Handle handle_;
76 74
77 // A lock to protect against simultaneous cancellations of the request. 75 // Used to notify manager if request is cancelled. Uses a raw ptr instead of
78 // Cancellation affects both the |cancelled_| flag and |consumer_|. 76 // a ref_ptr so that it can be set to NULL when a request is cancelled or
79 mutable base::Lock cancel_lock_; 77 // completed.
80 bool cancelled_; 78 WebDataRequestManager* manager_;
81 79
82 // The originator of the service request. 80 // The originator of the service request.
83 WebDataServiceConsumer* consumer_; 81 WebDataServiceConsumer* consumer_;
84 82
85 std::unique_ptr<WDTypedResult> result_; 83 std::unique_ptr<WDTypedResult> result_;
86 84
85 // For access to the web request mutable state under the manager's lock.
86 friend class WebDataRequestManager;
Mathieu 2016/12/14 01:13:46 I've seen those statements be right under the priv
Roger McFarlane (Chromium) 2016/12/14 14:02:49 Done.
87
87 DISALLOW_COPY_AND_ASSIGN(WebDataRequest); 88 DISALLOW_COPY_AND_ASSIGN(WebDataRequest);
88 }; 89 };
89 90
90 ////////////////////////////////////////////////////////////////////////////// 91 //////////////////////////////////////////////////////////////////////////////
91 // 92 //
92 // Webdata Request Manager 93 // WebData Request Manager
93 // 94 //
94 // Tracks all WebDataRequests for a WebDataService. 95 // Tracks all WebDataRequests for a WebDataService.
95 // 96 //
96 // Note: This is an internal interface, not to be used outside of webdata/ 97 // Note: This is an internal interface, not to be used outside of webdata/
97 ////////////////////////////////////////////////////////////////////////////// 98 //////////////////////////////////////////////////////////////////////////////
98 class WebDataRequestManager 99 class WebDataRequestManager
99 : public base::RefCountedThreadSafe<WebDataRequestManager> { 100 : public base::RefCountedThreadSafe<WebDataRequestManager> {
100 public: 101 public:
101 WebDataRequestManager(); 102 WebDataRequestManager();
102 103
103 // Cancel any pending request. 104 // Cancel any pending request.
104 void CancelRequest(WebDataServiceBase::Handle h); 105 void CancelRequest(WebDataServiceBase::Handle h);
105 106
106 // Invoked by the WebDataService when |request| has been completed. 107 // Invoked by the WebDataService when |request| has been completed.
107 void RequestCompleted(std::unique_ptr<WebDataRequest> request); 108 void RequestCompleted(std::unique_ptr<WebDataRequest> request,
109 std::unique_ptr<WDTypedResult> result);
108 110
109 // Register the request as a pending request. 111 // Register the request as a pending request.
110 void RegisterRequest(WebDataRequest* request); 112 void RegisterRequest(WebDataRequest* request);
111 113
112 // Return the next request handle. 114 // Return the next request handle.
113 int GetNextRequestHandle(); 115 int GetNextRequestHandle();
114 116
117 // A debugging aid to assert that the pending_lock_ is held by the current
118 // thread.
119 void AssertLockedByCurrentThread() const { pending_lock_.AssertAcquired(); }
120
115 private: 121 private:
116 friend class base::RefCountedThreadSafe<WebDataRequestManager>; 122 friend class base::RefCountedThreadSafe<WebDataRequestManager>;
117 123
118 ~WebDataRequestManager(); 124 ~WebDataRequestManager();
119 125
120 // This will notify the consumer in whatever thread was used to create this 126 // This will notify the consumer in whatever thread was used to create this
121 // request. 127 // request.
122 void RequestCompletedOnThread(std::unique_ptr<WebDataRequest> request); 128 void RequestCompletedOnThread(std::unique_ptr<WebDataRequest> request);
123 129
124 // A lock to protect pending requests and next request handle. 130 // A lock to protect pending requests and next request handle.
125 base::Lock pending_lock_; 131 base::Lock pending_lock_;
126 132
127 // Next handle to be used for requests. Incremented for each use. 133 // Next handle to be used for requests. Incremented for each use.
128 WebDataServiceBase::Handle next_request_handle_; 134 WebDataServiceBase::Handle next_request_handle_;
129 135
130 std::map<WebDataServiceBase::Handle, WebDataRequest*> pending_requests_; 136 std::map<WebDataServiceBase::Handle, WebDataRequest*> pending_requests_;
131 137
132 DISALLOW_COPY_AND_ASSIGN(WebDataRequestManager); 138 DISALLOW_COPY_AND_ASSIGN(WebDataRequestManager);
133 }; 139 };
134 140
135 #endif // COMPONENTS_WEBDATA_COMMON_WEB_DATA_REQUEST_MANAGER_H__ 141 #endif // COMPONENTS_WEBDATA_COMMON_WEB_DATA_REQUEST_MANAGER_H__
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698