[email protected] | 83ab4a28 | 2012-07-12 18:19:45 | [diff] [blame] | 1 | // Copyright (c) 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 | |
| 5 | #include "content/browser/histogram_synchronizer.h" |
| 6 | |
fdoray | 0c755de | 2016-10-19 15:28:44 | [diff] [blame] | 7 | #include <utility> |
| 8 | |
[email protected] | 83ab4a28 | 2012-07-12 18:19:45 | [diff] [blame] | 9 | #include "base/bind.h" |
| 10 | #include "base/lazy_instance.h" |
skyostil | 95082a6 | 2015-06-05 19:53:07 | [diff] [blame] | 11 | #include "base/location.h" |
[email protected] | 83ab4a28 | 2012-07-12 18:19:45 | [diff] [blame] | 12 | #include "base/logging.h" |
[email protected] | 6afa90f | 2013-10-23 01:16:04 | [diff] [blame] | 13 | #include "base/metrics/histogram_delta_serialization.h" |
asvitkine | 3033081 | 2016-08-30 04:01:08 | [diff] [blame] | 14 | #include "base/metrics/histogram_macros.h" |
[email protected] | c50c21d | 2013-01-11 21:52:44 | [diff] [blame] | 15 | #include "base/pickle.h" |
skyostil | 95082a6 | 2015-06-05 19:53:07 | [diff] [blame] | 16 | #include "base/single_thread_task_runner.h" |
Eric Seckler | 8652dcd5 | 2018-09-20 10:42:28 | [diff] [blame] | 17 | #include "base/task/post_task.h" |
[email protected] | 83ab4a28 | 2012-07-12 18:19:45 | [diff] [blame] | 18 | #include "base/threading/thread.h" |
| 19 | #include "base/threading/thread_restrictions.h" |
| 20 | #include "content/browser/histogram_controller.h" |
Eric Seckler | 8652dcd5 | 2018-09-20 10:42:28 | [diff] [blame] | 21 | #include "content/public/browser/browser_task_traits.h" |
[email protected] | 83ab4a28 | 2012-07-12 18:19:45 | [diff] [blame] | 22 | #include "content/public/browser/browser_thread.h" |
| 23 | #include "content/public/browser/histogram_fetcher.h" |
| 24 | #include "content/public/common/content_constants.h" |
| 25 | |
Daniel Bratell | b8d076c | 2018-01-03 10:41:47 | [diff] [blame] | 26 | namespace content { |
| 27 | |
[email protected] | 83ab4a28 | 2012-07-12 18:19:45 | [diff] [blame] | 28 | using base::Time; |
| 29 | using base::TimeDelta; |
| 30 | using base::TimeTicks; |
| 31 | |
| 32 | namespace { |
| 33 | |
| 34 | // Negative numbers are never used as sequence numbers. We explicitly pick a |
| 35 | // negative number that is "so negative" that even when we add one (as is done |
| 36 | // when we generated the next sequence number) that it will still be negative. |
| 37 | // We have code that handles wrapping around on an overflow into negative |
| 38 | // territory. |
| 39 | static const int kNeverUsableSequenceNumber = -2; |
| 40 | |
| 41 | } // anonymous namespace |
| 42 | |
[email protected] | 83ab4a28 | 2012-07-12 18:19:45 | [diff] [blame] | 43 | // The "RequestContext" structure describes an individual request received from |
| 44 | // the UI. All methods are accessible on UI thread. |
| 45 | class HistogramSynchronizer::RequestContext { |
| 46 | public: |
| 47 | // A map from sequence_number_ to the actual RequestContexts. |
| 48 | typedef std::map<int, RequestContext*> RequestContextMap; |
| 49 | |
Makoto Shimazu | 51176e6 | 2019-10-10 14:43:17 | [diff] [blame] | 50 | RequestContext(base::OnceClosure callback, int sequence_number) |
| 51 | : callback_(std::move(callback)), |
[email protected] | 83ab4a28 | 2012-07-12 18:19:45 | [diff] [blame] | 52 | sequence_number_(sequence_number), |
| 53 | received_process_group_count_(0), |
Makoto Shimazu | 51176e6 | 2019-10-10 14:43:17 | [diff] [blame] | 54 | processes_pending_(0) {} |
[email protected] | 83ab4a28 | 2012-07-12 18:19:45 | [diff] [blame] | 55 | ~RequestContext() {} |
| 56 | |
| 57 | void SetReceivedProcessGroupCount(bool done) { |
mostynb | 4c27d04 | 2015-03-18 21:47:47 | [diff] [blame] | 58 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
[email protected] | 83ab4a28 | 2012-07-12 18:19:45 | [diff] [blame] | 59 | received_process_group_count_ = done; |
| 60 | } |
| 61 | |
| 62 | // Methods for book keeping of processes_pending_. |
| 63 | void AddProcessesPending(int processes_pending) { |
mostynb | 4c27d04 | 2015-03-18 21:47:47 | [diff] [blame] | 64 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
[email protected] | 83ab4a28 | 2012-07-12 18:19:45 | [diff] [blame] | 65 | processes_pending_ += processes_pending; |
| 66 | } |
| 67 | |
| 68 | void DecrementProcessesPending() { |
mostynb | 4c27d04 | 2015-03-18 21:47:47 | [diff] [blame] | 69 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
[email protected] | 83ab4a28 | 2012-07-12 18:19:45 | [diff] [blame] | 70 | --processes_pending_; |
| 71 | } |
| 72 | |
| 73 | // Records that we are waiting for one less histogram data from a process for |
| 74 | // the given sequence number. If |received_process_group_count_| and |
| 75 | // |processes_pending_| are zero, then delete the current object by calling |
| 76 | // Unregister. |
| 77 | void DeleteIfAllDone() { |
mostynb | 4c27d04 | 2015-03-18 21:47:47 | [diff] [blame] | 78 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
[email protected] | 83ab4a28 | 2012-07-12 18:19:45 | [diff] [blame] | 79 | |
| 80 | if (processes_pending_ <= 0 && received_process_group_count_) |
| 81 | RequestContext::Unregister(sequence_number_); |
| 82 | } |
| 83 | |
| 84 | // Register |callback| in |outstanding_requests_| map for the given |
| 85 | // |sequence_number|. |
Makoto Shimazu | 51176e6 | 2019-10-10 14:43:17 | [diff] [blame] | 86 | static void Register(base::OnceClosure callback, int sequence_number) { |
mostynb | 4c27d04 | 2015-03-18 21:47:47 | [diff] [blame] | 87 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
[email protected] | 83ab4a28 | 2012-07-12 18:19:45 | [diff] [blame] | 88 | |
Makoto Shimazu | 51176e6 | 2019-10-10 14:43:17 | [diff] [blame] | 89 | RequestContext* request = |
| 90 | new RequestContext(std::move(callback), sequence_number); |
[email protected] | 83ab4a28 | 2012-07-12 18:19:45 | [diff] [blame] | 91 | outstanding_requests_.Get()[sequence_number] = request; |
| 92 | } |
| 93 | |
| 94 | // Find the |RequestContext| in |outstanding_requests_| map for the given |
| 95 | // |sequence_number|. |
| 96 | static RequestContext* GetRequestContext(int sequence_number) { |
mostynb | 4c27d04 | 2015-03-18 21:47:47 | [diff] [blame] | 97 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
[email protected] | 83ab4a28 | 2012-07-12 18:19:45 | [diff] [blame] | 98 | |
jdoerrie | 55ec69d | 2018-10-08 13:34:46 | [diff] [blame] | 99 | auto it = outstanding_requests_.Get().find(sequence_number); |
[email protected] | 83ab4a28 | 2012-07-12 18:19:45 | [diff] [blame] | 100 | if (it == outstanding_requests_.Get().end()) |
Ivan Kotenkov | 2c0d2bb3 | 2017-11-01 15:41:28 | [diff] [blame] | 101 | return nullptr; |
[email protected] | 83ab4a28 | 2012-07-12 18:19:45 | [diff] [blame] | 102 | |
| 103 | RequestContext* request = it->second; |
| 104 | DCHECK_EQ(sequence_number, request->sequence_number_); |
| 105 | return request; |
| 106 | } |
| 107 | |
| 108 | // Delete the entry for the given |sequence_number| from |
| 109 | // |outstanding_requests_| map. This method is called when all changes have |
| 110 | // been acquired, or when the wait time expires (whichever is sooner). |
| 111 | static void Unregister(int sequence_number) { |
mostynb | 4c27d04 | 2015-03-18 21:47:47 | [diff] [blame] | 112 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
[email protected] | 83ab4a28 | 2012-07-12 18:19:45 | [diff] [blame] | 113 | |
jdoerrie | 55ec69d | 2018-10-08 13:34:46 | [diff] [blame] | 114 | auto it = outstanding_requests_.Get().find(sequence_number); |
[email protected] | 83ab4a28 | 2012-07-12 18:19:45 | [diff] [blame] | 115 | if (it == outstanding_requests_.Get().end()) |
| 116 | return; |
| 117 | |
| 118 | RequestContext* request = it->second; |
| 119 | DCHECK_EQ(sequence_number, request->sequence_number_); |
| 120 | bool received_process_group_count = request->received_process_group_count_; |
| 121 | int unresponsive_processes = request->processes_pending_; |
| 122 | |
Makoto Shimazu | 51176e6 | 2019-10-10 14:43:17 | [diff] [blame] | 123 | std::move(request->callback_).Run(); |
[email protected] | 83ab4a28 | 2012-07-12 18:19:45 | [diff] [blame] | 124 | |
| 125 | delete request; |
| 126 | outstanding_requests_.Get().erase(it); |
| 127 | |
| 128 | UMA_HISTOGRAM_BOOLEAN("Histogram.ReceivedProcessGroupCount", |
| 129 | received_process_group_count); |
Steven Holte | 9592222 | 2018-09-14 20:06:23 | [diff] [blame] | 130 | UMA_HISTOGRAM_COUNTS_1M("Histogram.PendingProcessNotResponding", |
| 131 | unresponsive_processes); |
[email protected] | 83ab4a28 | 2012-07-12 18:19:45 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | // Delete all the entries in |outstanding_requests_| map. |
| 135 | static void OnShutdown() { |
| 136 | // Just in case we have any pending tasks, clear them out. |
| 137 | while (!outstanding_requests_.Get().empty()) { |
jdoerrie | 55ec69d | 2018-10-08 13:34:46 | [diff] [blame] | 138 | auto it = outstanding_requests_.Get().begin(); |
[email protected] | 83ab4a28 | 2012-07-12 18:19:45 | [diff] [blame] | 139 | delete it->second; |
| 140 | outstanding_requests_.Get().erase(it); |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | // Requests are made to asynchronously send data to the |callback_|. |
Makoto Shimazu | 51176e6 | 2019-10-10 14:43:17 | [diff] [blame] | 145 | base::OnceClosure callback_; |
[email protected] | 83ab4a28 | 2012-07-12 18:19:45 | [diff] [blame] | 146 | |
| 147 | // The sequence number used by the most recent update request to contact all |
| 148 | // processes. |
| 149 | int sequence_number_; |
| 150 | |
| 151 | // Indicates if we have received all pending processes count. |
| 152 | bool received_process_group_count_; |
| 153 | |
| 154 | // The number of pending processes (all renderer processes and browser child |
| 155 | // processes) that have not yet responded to requests. |
| 156 | int processes_pending_; |
| 157 | |
| 158 | // Map of all outstanding RequestContexts, from sequence_number_ to |
| 159 | // RequestContext. |
| 160 | static base::LazyInstance<RequestContextMap>::Leaky outstanding_requests_; |
| 161 | }; |
| 162 | |
| 163 | // static |
| 164 | base::LazyInstance |
| 165 | <HistogramSynchronizer::RequestContext::RequestContextMap>::Leaky |
| 166 | HistogramSynchronizer::RequestContext::outstanding_requests_ = |
| 167 | LAZY_INSTANCE_INITIALIZER; |
| 168 | |
| 169 | HistogramSynchronizer::HistogramSynchronizer() |
| 170 | : lock_(), |
[email protected] | 83ab4a28 | 2012-07-12 18:19:45 | [diff] [blame] | 171 | last_used_sequence_number_(kNeverUsableSequenceNumber), |
| 172 | async_sequence_number_(kNeverUsableSequenceNumber) { |
[email protected] | 4648832 | 2012-10-30 03:22:20 | [diff] [blame] | 173 | HistogramController::GetInstance()->Register(this); |
[email protected] | 83ab4a28 | 2012-07-12 18:19:45 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | HistogramSynchronizer::~HistogramSynchronizer() { |
| 177 | RequestContext::OnShutdown(); |
| 178 | |
| 179 | // Just in case we have any pending tasks, clear them out. |
fdoray | 0c755de | 2016-10-19 15:28:44 | [diff] [blame] | 180 | SetTaskRunnerAndCallback(nullptr, base::Closure()); |
[email protected] | 83ab4a28 | 2012-07-12 18:19:45 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | HistogramSynchronizer* HistogramSynchronizer::GetInstance() { |
olli.raula | 36aa8be | 2015-09-10 11:14:22 | [diff] [blame] | 184 | return base::Singleton< |
| 185 | HistogramSynchronizer, |
| 186 | base::LeakySingletonTraits<HistogramSynchronizer>>::get(); |
[email protected] | 83ab4a28 | 2012-07-12 18:19:45 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | // static |
| 190 | void HistogramSynchronizer::FetchHistograms() { |
| 191 | if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { |
Sami Kyostila | 8e4d5a9 | 2019-08-02 12:45:05 | [diff] [blame] | 192 | base::PostTask(FROM_HERE, {BrowserThread::UI}, |
| 193 | base::BindOnce(&HistogramSynchronizer::FetchHistograms)); |
[email protected] | 83ab4a28 | 2012-07-12 18:19:45 | [diff] [blame] | 194 | return; |
| 195 | } |
mostynb | 4c27d04 | 2015-03-18 21:47:47 | [diff] [blame] | 196 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
[email protected] | 83ab4a28 | 2012-07-12 18:19:45 | [diff] [blame] | 197 | |
| 198 | HistogramSynchronizer* current_synchronizer = |
| 199 | HistogramSynchronizer::GetInstance(); |
Ivan Kotenkov | 2c0d2bb3 | 2017-11-01 15:41:28 | [diff] [blame] | 200 | if (current_synchronizer == nullptr) |
[email protected] | 83ab4a28 | 2012-07-12 18:19:45 | [diff] [blame] | 201 | return; |
| 202 | |
| 203 | current_synchronizer->RegisterAndNotifyAllProcesses( |
| 204 | HistogramSynchronizer::UNKNOWN, |
| 205 | base::TimeDelta::FromMinutes(1)); |
| 206 | } |
| 207 | |
fdoray | 0c755de | 2016-10-19 15:28:44 | [diff] [blame] | 208 | void FetchHistogramsAsynchronously(scoped_refptr<base::TaskRunner> task_runner, |
[email protected] | 83ab4a28 | 2012-07-12 18:19:45 | [diff] [blame] | 209 | const base::Closure& callback, |
| 210 | base::TimeDelta wait_time) { |
fdoray | 0c755de | 2016-10-19 15:28:44 | [diff] [blame] | 211 | HistogramSynchronizer::FetchHistogramsAsynchronously(std::move(task_runner), |
| 212 | callback, wait_time); |
[email protected] | 83ab4a28 | 2012-07-12 18:19:45 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | // static |
| 216 | void HistogramSynchronizer::FetchHistogramsAsynchronously( |
fdoray | 0c755de | 2016-10-19 15:28:44 | [diff] [blame] | 217 | scoped_refptr<base::TaskRunner> task_runner, |
[email protected] | 83ab4a28 | 2012-07-12 18:19:45 | [diff] [blame] | 218 | const base::Closure& callback, |
| 219 | base::TimeDelta wait_time) { |
fdoray | 0c755de | 2016-10-19 15:28:44 | [diff] [blame] | 220 | DCHECK(task_runner); |
[email protected] | 83ab4a28 | 2012-07-12 18:19:45 | [diff] [blame] | 221 | DCHECK(!callback.is_null()); |
| 222 | |
| 223 | HistogramSynchronizer* current_synchronizer = |
| 224 | HistogramSynchronizer::GetInstance(); |
fdoray | 0c755de | 2016-10-19 15:28:44 | [diff] [blame] | 225 | current_synchronizer->SetTaskRunnerAndCallback(std::move(task_runner), |
| 226 | callback); |
[email protected] | 83ab4a28 | 2012-07-12 18:19:45 | [diff] [blame] | 227 | |
| 228 | current_synchronizer->RegisterAndNotifyAllProcesses( |
| 229 | HistogramSynchronizer::ASYNC_HISTOGRAMS, wait_time); |
| 230 | } |
| 231 | |
| 232 | void HistogramSynchronizer::RegisterAndNotifyAllProcesses( |
| 233 | ProcessHistogramRequester requester, |
| 234 | base::TimeDelta wait_time) { |
mostynb | 4c27d04 | 2015-03-18 21:47:47 | [diff] [blame] | 235 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
[email protected] | 83ab4a28 | 2012-07-12 18:19:45 | [diff] [blame] | 236 | |
| 237 | int sequence_number = GetNextAvailableSequenceNumber(requester); |
| 238 | |
Makoto Shimazu | 51176e6 | 2019-10-10 14:43:17 | [diff] [blame] | 239 | base::OnceClosure callback = base::BindOnce( |
[email protected] | 83ab4a28 | 2012-07-12 18:19:45 | [diff] [blame] | 240 | &HistogramSynchronizer::ForceHistogramSynchronizationDoneCallback, |
Makoto Shimazu | 51176e6 | 2019-10-10 14:43:17 | [diff] [blame] | 241 | base::Unretained(this), sequence_number); |
[email protected] | 83ab4a28 | 2012-07-12 18:19:45 | [diff] [blame] | 242 | |
Tommy Nyquist | 4b749d0 | 2018-03-20 21:46:29 | [diff] [blame] | 243 | RequestContext::Register(std::move(callback), sequence_number); |
[email protected] | 83ab4a28 | 2012-07-12 18:19:45 | [diff] [blame] | 244 | |
| 245 | // Get histogram data from renderer and browser child processes. |
[email protected] | 4648832 | 2012-10-30 03:22:20 | [diff] [blame] | 246 | HistogramController::GetInstance()->GetHistogramData(sequence_number); |
[email protected] | 83ab4a28 | 2012-07-12 18:19:45 | [diff] [blame] | 247 | |
| 248 | // Post a task that would be called after waiting for wait_time. This acts |
| 249 | // as a watchdog, to cancel the requests for non-responsive processes. |
Sami Kyostila | 8e4d5a9 | 2019-08-02 12:45:05 | [diff] [blame] | 250 | base::PostDelayedTask( |
Eric Seckler | 8652dcd5 | 2018-09-20 10:42:28 | [diff] [blame] | 251 | FROM_HERE, {BrowserThread::UI}, |
tzik | e2aca99 | 2017-09-05 08:50:54 | [diff] [blame] | 252 | base::BindOnce(&RequestContext::Unregister, sequence_number), wait_time); |
[email protected] | 83ab4a28 | 2012-07-12 18:19:45 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | void HistogramSynchronizer::OnPendingProcesses(int sequence_number, |
| 256 | int pending_processes, |
| 257 | bool end) { |
mostynb | 4c27d04 | 2015-03-18 21:47:47 | [diff] [blame] | 258 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
[email protected] | 83ab4a28 | 2012-07-12 18:19:45 | [diff] [blame] | 259 | |
| 260 | RequestContext* request = RequestContext::GetRequestContext(sequence_number); |
| 261 | if (!request) |
| 262 | return; |
| 263 | request->AddProcessesPending(pending_processes); |
| 264 | request->SetReceivedProcessGroupCount(end); |
| 265 | request->DeleteIfAllDone(); |
| 266 | } |
| 267 | |
| 268 | void HistogramSynchronizer::OnHistogramDataCollected( |
| 269 | int sequence_number, |
| 270 | const std::vector<std::string>& pickled_histograms) { |
mostynb | 4c27d04 | 2015-03-18 21:47:47 | [diff] [blame] | 271 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
[email protected] | 83ab4a28 | 2012-07-12 18:19:45 | [diff] [blame] | 272 | |
[email protected] | 6afa90f | 2013-10-23 01:16:04 | [diff] [blame] | 273 | base::HistogramDeltaSerialization::DeserializeAndAddSamples( |
| 274 | pickled_histograms); |
| 275 | |
[email protected] | 83ab4a28 | 2012-07-12 18:19:45 | [diff] [blame] | 276 | RequestContext* request = RequestContext::GetRequestContext(sequence_number); |
[email protected] | 83ab4a28 | 2012-07-12 18:19:45 | [diff] [blame] | 277 | if (!request) |
| 278 | return; |
| 279 | |
| 280 | // Delete request if we have heard back from all child processes. |
| 281 | request->DecrementProcessesPending(); |
| 282 | request->DeleteIfAllDone(); |
| 283 | } |
| 284 | |
fdoray | 0c755de | 2016-10-19 15:28:44 | [diff] [blame] | 285 | void HistogramSynchronizer::SetTaskRunnerAndCallback( |
| 286 | scoped_refptr<base::TaskRunner> task_runner, |
[email protected] | 83ab4a28 | 2012-07-12 18:19:45 | [diff] [blame] | 287 | const base::Closure& callback) { |
| 288 | base::Closure old_callback; |
fdoray | 0c755de | 2016-10-19 15:28:44 | [diff] [blame] | 289 | scoped_refptr<base::TaskRunner> old_task_runner; |
[email protected] | 83ab4a28 | 2012-07-12 18:19:45 | [diff] [blame] | 290 | { |
| 291 | base::AutoLock auto_lock(lock_); |
| 292 | old_callback = callback_; |
| 293 | callback_ = callback; |
fdoray | 0c755de | 2016-10-19 15:28:44 | [diff] [blame] | 294 | old_task_runner = std::move(callback_task_runner_); |
| 295 | callback_task_runner_ = std::move(task_runner); |
[email protected] | 83ab4a28 | 2012-07-12 18:19:45 | [diff] [blame] | 296 | // Prevent premature calling of our new callbacks. |
| 297 | async_sequence_number_ = kNeverUsableSequenceNumber; |
| 298 | } |
| 299 | // Just in case there was a task pending.... |
Tommy Nyquist | 4b749d0 | 2018-03-20 21:46:29 | [diff] [blame] | 300 | InternalPostTask(std::move(old_task_runner), std::move(old_callback)); |
[email protected] | 83ab4a28 | 2012-07-12 18:19:45 | [diff] [blame] | 301 | } |
| 302 | |
| 303 | void HistogramSynchronizer::ForceHistogramSynchronizationDoneCallback( |
| 304 | int sequence_number) { |
| 305 | base::Closure callback; |
fdoray | 0c755de | 2016-10-19 15:28:44 | [diff] [blame] | 306 | scoped_refptr<base::TaskRunner> task_runner; |
[email protected] | 83ab4a28 | 2012-07-12 18:19:45 | [diff] [blame] | 307 | { |
| 308 | base::AutoLock lock(lock_); |
| 309 | if (sequence_number != async_sequence_number_) |
| 310 | return; |
| 311 | callback = callback_; |
fdoray | 0c755de | 2016-10-19 15:28:44 | [diff] [blame] | 312 | task_runner = std::move(callback_task_runner_); |
[email protected] | 83ab4a28 | 2012-07-12 18:19:45 | [diff] [blame] | 313 | callback_.Reset(); |
[email protected] | 83ab4a28 | 2012-07-12 18:19:45 | [diff] [blame] | 314 | } |
Tommy Nyquist | 4b749d0 | 2018-03-20 21:46:29 | [diff] [blame] | 315 | InternalPostTask(std::move(task_runner), std::move(callback)); |
[email protected] | 83ab4a28 | 2012-07-12 18:19:45 | [diff] [blame] | 316 | } |
| 317 | |
fdoray | 0c755de | 2016-10-19 15:28:44 | [diff] [blame] | 318 | void HistogramSynchronizer::InternalPostTask( |
| 319 | scoped_refptr<base::TaskRunner> task_runner, |
| 320 | const base::Closure& callback) { |
| 321 | if (callback.is_null() || !task_runner) |
[email protected] | 83ab4a28 | 2012-07-12 18:19:45 | [diff] [blame] | 322 | return; |
fdoray | 0c755de | 2016-10-19 15:28:44 | [diff] [blame] | 323 | task_runner->PostTask(FROM_HERE, callback); |
[email protected] | 83ab4a28 | 2012-07-12 18:19:45 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | int HistogramSynchronizer::GetNextAvailableSequenceNumber( |
| 327 | ProcessHistogramRequester requester) { |
| 328 | base::AutoLock auto_lock(lock_); |
| 329 | ++last_used_sequence_number_; |
| 330 | // Watch out for wrapping to a negative number. |
| 331 | if (last_used_sequence_number_ < 0) { |
| 332 | // Bypass the reserved number, which is used when a renderer spontaneously |
| 333 | // decides to send some histogram data. |
| 334 | last_used_sequence_number_ = |
| 335 | kHistogramSynchronizerReservedSequenceNumber + 1; |
| 336 | } |
| 337 | DCHECK_NE(last_used_sequence_number_, |
| 338 | kHistogramSynchronizerReservedSequenceNumber); |
| 339 | if (requester == ASYNC_HISTOGRAMS) |
| 340 | async_sequence_number_ = last_used_sequence_number_; |
| 341 | return last_used_sequence_number_; |
| 342 | } |
| 343 | |
| 344 | } // namespace content |