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

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

Issue 2571923002: Fix threading issues in WebDataRequestManager. (Closed)
Patch Set: Simplify handling of result and add request factory func 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,
37 WebDataRequestManager* manager);
38
39 virtual ~WebDataRequest(); 36 virtual ~WebDataRequest();
40 37
38 // Returns the identifier for this request.
41 WebDataServiceBase::Handle GetHandle() const; 39 WebDataServiceBase::Handle GetHandle() const;
42 40
41 // Returns |true| if the request is active and |false| if the request has been
42 // was cancelled or has already completed.
Peter Kasting 2016/12/15 01:05:34 Nit: "has been was cancelled"
Roger McFarlane (Chromium) 2016/12/15 07:41:45 Done.
43 bool IsActive() const;
44
45 private:
46 // For access to the web request mutable state under the manager's lock.
47 friend class WebDataRequestManager;
48
49 // Private constructor called for WebDataRequestManager::NewRequest.
50 WebDataRequest(WebDataRequestManager* manager,
51 WebDataServiceConsumer* consumer);
52
53 // Internal debugging helper to assert that the request is active and that the
54 // manager's lock is held by the current thread.
55 void AssertThreadSafe() const;
56
43 // Retrieves the |consumer_| set in the constructor. 57 // Retrieves the |consumer_| set in the constructor.
44 WebDataServiceConsumer* GetConsumer() const; 58 WebDataServiceConsumer* GetConsumer() const;
45 59
46 // Retrieves the original task runner of the request. 60 // Retrieves the original task runner of the request.
47 scoped_refptr<base::SingleThreadTaskRunner> GetTaskRunner() const; 61 scoped_refptr<base::SingleThreadTaskRunner> GetTaskRunner() const;
48 62
49 // Returns |true| if the request was cancelled via the |Cancel()| method. 63 // Marks the current request as inactive, either due to cancellation or
50 bool IsCancelled() const; 64 // completion.
51 65 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
59 // The result is owned by the request.
60 void SetResult(std::unique_ptr<WDTypedResult> r);
61
62 // Transfers ownership pof result to caller. Should only be called once per
63 // result.
64 std::unique_ptr<WDTypedResult> GetResult();
65
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 66
71 // Tracks task runner that the request originated on. 67 // Tracks task runner that the request originated on.
72 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; 68 const scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
73 69
74 // Identifier for this request. 70 // Used to notify manager if request is cancelled. Uses a raw ptr instead of
75 WebDataServiceBase::Handle handle_; 71 // a ref_ptr so that it can be set to NULL when a request is cancelled or
Peter Kasting 2016/12/15 01:05:34 Nit: NULL -> null
Roger McFarlane (Chromium) 2016/12/15 07:41:45 Done.
76 72 // completed.
77 // A lock to protect against simultaneous cancellations of the request. 73 WebDataRequestManager* manager_;
78 // Cancellation affects both the |cancelled_| flag and |consumer_|.
79 mutable base::Lock cancel_lock_;
80 bool cancelled_;
81 74
82 // The originator of the service request. 75 // The originator of the service request.
83 WebDataServiceConsumer* consumer_; 76 WebDataServiceConsumer* consumer_;
84 77
85 std::unique_ptr<WDTypedResult> result_; 78 // Identifier for this request.
79 WebDataServiceBase::Handle handle_;
86 80
87 DISALLOW_COPY_AND_ASSIGN(WebDataRequest); 81 DISALLOW_COPY_AND_ASSIGN(WebDataRequest);
88 }; 82 };
89 83
90 ////////////////////////////////////////////////////////////////////////////// 84 //////////////////////////////////////////////////////////////////////////////
91 // 85 //
92 // Webdata Request Manager 86 // WebData Request Manager
93 // 87 //
94 // Tracks all WebDataRequests for a WebDataService. 88 // Tracks all WebDataRequests for a WebDataService.
95 // 89 //
96 // Note: This is an internal interface, not to be used outside of webdata/ 90 // Note: This is an internal interface, not to be used outside of webdata/
97 ////////////////////////////////////////////////////////////////////////////// 91 //////////////////////////////////////////////////////////////////////////////
98 class WebDataRequestManager 92 class WebDataRequestManager
99 : public base::RefCountedThreadSafe<WebDataRequestManager> { 93 : public base::RefCountedThreadSafe<WebDataRequestManager> {
100 public: 94 public:
101 WebDataRequestManager(); 95 WebDataRequestManager();
102 96
97 // Factory function to create a new WebDataRequest.
98 std::unique_ptr<WebDataRequest> NewRequest(WebDataServiceConsumer* consumer);
99
103 // Cancel any pending request. 100 // Cancel any pending request.
104 void CancelRequest(WebDataServiceBase::Handle h); 101 void CancelRequest(WebDataServiceBase::Handle h);
105 102
106 // Invoked by the WebDataService when |request| has been completed. 103 // Invoked by the WebDataService when |request| has been completed.
107 void RequestCompleted(std::unique_ptr<WebDataRequest> request); 104 void RequestCompleted(std::unique_ptr<WebDataRequest> request,
105 std::unique_ptr<WDTypedResult> result);
108 106
109 // Register the request as a pending request. 107 // A debugging aid to assert that the pending_lock_ is held by the current
110 void RegisterRequest(WebDataRequest* request); 108 // thread.
111 109 void AssertLockedByCurrentThread() const { pending_lock_.AssertAcquired(); }
Peter Kasting 2016/12/15 01:05:34 Nit: Avoid inlining non-"unix_hacker()" functions
Roger McFarlane (Chromium) 2016/12/15 07:41:45 Done.
112 // Return the next request handle.
113 int GetNextRequestHandle();
114 110
115 private: 111 private:
116 friend class base::RefCountedThreadSafe<WebDataRequestManager>; 112 friend class base::RefCountedThreadSafe<WebDataRequestManager>;
117 113
118 ~WebDataRequestManager(); 114 ~WebDataRequestManager();
119 115
120 // This will notify the consumer in whatever thread was used to create this 116 // This will notify the consumer in whatever thread was used to create this
121 // request. 117 // request.
122 void RequestCompletedOnThread(std::unique_ptr<WebDataRequest> request); 118 void RequestCompletedOnThread(std::unique_ptr<WebDataRequest> request,
119 std::unique_ptr<WDTypedResult> result);
123 120
124 // A lock to protect pending requests and next request handle. 121 // A lock to protect pending requests and next request handle.
125 base::Lock pending_lock_; 122 base::Lock pending_lock_;
126 123
127 // Next handle to be used for requests. Incremented for each use. 124 // Next handle to be used for requests. Incremented for each use.
128 WebDataServiceBase::Handle next_request_handle_; 125 WebDataServiceBase::Handle next_request_handle_;
129 126
130 std::map<WebDataServiceBase::Handle, WebDataRequest*> pending_requests_; 127 std::map<WebDataServiceBase::Handle, WebDataRequest*> pending_requests_;
131 128
132 DISALLOW_COPY_AND_ASSIGN(WebDataRequestManager); 129 DISALLOW_COPY_AND_ASSIGN(WebDataRequestManager);
133 }; 130 };
134 131
135 #endif // COMPONENTS_WEBDATA_COMMON_WEB_DATA_REQUEST_MANAGER_H__ 132 #endif // COMPONENTS_WEBDATA_COMMON_WEB_DATA_REQUEST_MANAGER_H__
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698