OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 #include "components/prefs/json_pref_store.h" | 5 #include "components/prefs/json_pref_store.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include <algorithm> | 9 #include <algorithm> |
10 #include <utility> | 10 #include <utility> |
(...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
165 alternate_path_(pref_alternate_filename), | 165 alternate_path_(pref_alternate_filename), |
166 sequenced_task_runner_(sequenced_task_runner), | 166 sequenced_task_runner_(sequenced_task_runner), |
167 prefs_(new base::DictionaryValue()), | 167 prefs_(new base::DictionaryValue()), |
168 read_only_(false), | 168 read_only_(false), |
169 writer_(pref_filename, sequenced_task_runner), | 169 writer_(pref_filename, sequenced_task_runner), |
170 pref_filter_(std::move(pref_filter)), | 170 pref_filter_(std::move(pref_filter)), |
171 initialized_(false), | 171 initialized_(false), |
172 filtering_in_progress_(false), | 172 filtering_in_progress_(false), |
173 pending_lossy_write_(false), | 173 pending_lossy_write_(false), |
174 read_error_(PREF_READ_ERROR_NONE), | 174 read_error_(PREF_READ_ERROR_NONE), |
175 has_pending_successful_write_reply_(false), | 175 has_pending_write_reply_(false), |
176 has_pending_write_callbacks_(false), | |
177 write_count_histogram_(writer_.commit_interval(), path_) { | 176 write_count_histogram_(writer_.commit_interval(), path_) { |
178 DCHECK(!path_.empty()); | 177 DCHECK(!path_.empty()); |
179 } | 178 } |
180 | 179 |
181 bool JsonPrefStore::GetValue(const std::string& key, | 180 bool JsonPrefStore::GetValue(const std::string& key, |
182 const base::Value** result) const { | 181 const base::Value** result) const { |
183 DCHECK(CalledOnValidThread()); | 182 DCHECK(CalledOnValidThread()); |
184 | 183 |
185 base::Value* tmp = nullptr; | 184 base::Value* tmp = nullptr; |
186 if (!prefs_->Get(key, &tmp)) | 185 if (!prefs_->Get(key, &tmp)) |
(...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
323 | 322 |
324 FOR_EACH_OBSERVER(PrefStore::Observer, observers_, OnPrefValueChanged(key)); | 323 FOR_EACH_OBSERVER(PrefStore::Observer, observers_, OnPrefValueChanged(key)); |
325 | 324 |
326 ScheduleWrite(flags); | 325 ScheduleWrite(flags); |
327 } | 326 } |
328 | 327 |
329 void JsonPrefStore::RunOrScheduleNextSuccessfulWriteCallback( | 328 void JsonPrefStore::RunOrScheduleNextSuccessfulWriteCallback( |
330 bool write_success) { | 329 bool write_success) { |
331 DCHECK(CalledOnValidThread()); | 330 DCHECK(CalledOnValidThread()); |
332 | 331 |
333 has_pending_write_callbacks_ = false; | 332 has_pending_write_reply_ = false; |
334 if (has_pending_successful_write_reply_) { | 333 if (!on_next_successful_write_reply_.is_null()) { |
335 has_pending_successful_write_reply_ = false; | 334 base::Closure on_successful_write = on_next_successful_write_reply_; |
proberge
2016/10/04 20:17:58
is this fine? I need to clear on_next_successful_r
gab
2016/10/04 20:25:13
Yes except do:
base::Closure on_successful_write
proberge
2016/10/04 20:40:49
Done.
| |
335 on_next_successful_write_reply_ = base::Closure(); | |
gab
2016/10/04 20:25:40
And of course the move makes this line not be requ
proberge
2016/10/04 20:40:49
Done.
| |
336 if (write_success) { | 336 if (write_success) { |
337 on_next_successful_write_reply_.Run(); | 337 on_successful_write.Run(); |
338 } else { | 338 } else { |
339 RegisterOnNextSuccessfulWriteReply(on_next_successful_write_reply_); | 339 RegisterOnNextSuccessfulWriteReply(on_successful_write); |
340 } | 340 } |
341 } | 341 } |
342 } | 342 } |
343 | 343 |
344 // static | 344 // static |
345 void JsonPrefStore::PostWriteCallback( | 345 void JsonPrefStore::PostWriteCallback( |
346 const base::Callback<void(bool success)>& on_next_write_reply, | 346 const base::Callback<void(bool success)>& on_next_write_reply, |
347 const base::Callback<void(bool success)>& on_next_write_callback, | 347 const base::Callback<void(bool success)>& on_next_write_callback, |
348 scoped_refptr<base::SequencedTaskRunner> reply_task_runner, | 348 scoped_refptr<base::SequencedTaskRunner> reply_task_runner, |
349 bool write_success) { | 349 bool write_success) { |
350 if (!on_next_write_callback.is_null()) | 350 if (!on_next_write_callback.is_null()) |
351 on_next_write_callback.Run(write_success); | 351 on_next_write_callback.Run(write_success); |
352 | 352 |
353 // We can't run |on_next_write_reply| on the current thread. Bounce back to | 353 // We can't run |on_next_write_reply| on the current thread. Bounce back to |
354 // the |reply_task_runner| which is the correct sequenced thread. | 354 // the |reply_task_runner| which is the correct sequenced thread. |
355 reply_task_runner->PostTask(FROM_HERE, | 355 reply_task_runner->PostTask(FROM_HERE, |
356 base::Bind(on_next_write_reply, write_success)); | 356 base::Bind(on_next_write_reply, write_success)); |
357 } | 357 } |
358 | 358 |
359 void JsonPrefStore::RegisterOnNextSuccessfulWriteReply( | 359 void JsonPrefStore::RegisterOnNextSuccessfulWriteReply( |
360 const base::Closure& on_next_successful_write_reply) { | 360 const base::Closure& on_next_successful_write_reply) { |
361 DCHECK(CalledOnValidThread()); | 361 DCHECK(CalledOnValidThread()); |
362 DCHECK(!has_pending_successful_write_reply_); | 362 DCHECK(on_next_successful_write_reply_.is_null()); |
363 | 363 |
364 has_pending_successful_write_reply_ = true; | |
365 on_next_successful_write_reply_ = on_next_successful_write_reply; | 364 on_next_successful_write_reply_ = on_next_successful_write_reply; |
366 | 365 |
367 // If there are pending callbacks, avoid erasing them; the reply will be used | 366 // If there are pending callbacks, avoid erasing them; the reply will be used |
368 // as we set |on_next_successful_write_reply_|. Otherwise, setup a reply with | 367 // as we set |on_next_successful_write_reply_|. Otherwise, setup a reply with |
369 // an empty callback. | 368 // an empty callback. |
370 if (!has_pending_write_callbacks_) { | 369 if (!has_pending_write_reply_) { |
370 has_pending_write_reply_ = true; | |
371 writer_.RegisterOnNextWriteCallbacks( | 371 writer_.RegisterOnNextWriteCallbacks( |
372 base::Closure(), | 372 base::Closure(), |
373 base::Bind( | 373 base::Bind( |
374 &PostWriteCallback, | 374 &PostWriteCallback, |
375 base::Bind(&JsonPrefStore::RunOrScheduleNextSuccessfulWriteCallback, | 375 base::Bind(&JsonPrefStore::RunOrScheduleNextSuccessfulWriteCallback, |
376 AsWeakPtr()), | 376 AsWeakPtr()), |
377 base::Callback<void(bool success)>(), | 377 base::Callback<void(bool success)>(), |
378 base::SequencedTaskRunnerHandle::Get())); | 378 base::SequencedTaskRunnerHandle::Get())); |
379 } | 379 } |
380 } | 380 } |
381 | 381 |
382 void JsonPrefStore::RegisterOnNextWriteSynchronousCallbacks( | 382 void JsonPrefStore::RegisterOnNextWriteSynchronousCallbacks( |
383 OnWriteCallbackPair callbacks) { | 383 OnWriteCallbackPair callbacks) { |
384 DCHECK(CalledOnValidThread()); | 384 DCHECK(CalledOnValidThread()); |
385 DCHECK(!has_pending_write_callbacks_); | |
386 | 385 |
387 has_pending_write_callbacks_ = true; | 386 has_pending_write_reply_ = true; |
388 | 387 |
389 writer_.RegisterOnNextWriteCallbacks( | 388 writer_.RegisterOnNextWriteCallbacks( |
390 callbacks.first, | 389 callbacks.first, |
391 base::Bind( | 390 base::Bind( |
392 &PostWriteCallback, | 391 &PostWriteCallback, |
393 base::Bind(&JsonPrefStore::RunOrScheduleNextSuccessfulWriteCallback, | 392 base::Bind(&JsonPrefStore::RunOrScheduleNextSuccessfulWriteCallback, |
394 AsWeakPtr()), | 393 AsWeakPtr()), |
395 callbacks.second, base::SequencedTaskRunnerHandle::Get())); | 394 callbacks.second, base::SequencedTaskRunnerHandle::Get())); |
gab
2016/10/04 20:25:13
nit: Just noticed while reading this closely that
proberge
2016/10/04 20:40:49
Done.
| |
396 } | 395 } |
397 | 396 |
398 void JsonPrefStore::ClearMutableValues() { | 397 void JsonPrefStore::ClearMutableValues() { |
399 NOTIMPLEMENTED(); | 398 NOTIMPLEMENTED(); |
400 } | 399 } |
401 | 400 |
402 void JsonPrefStore::OnFileRead(std::unique_ptr<ReadResult> read_result) { | 401 void JsonPrefStore::OnFileRead(std::unique_ptr<ReadResult> read_result) { |
403 DCHECK(CalledOnValidThread()); | 402 DCHECK(CalledOnValidThread()); |
404 | 403 |
405 DCHECK(read_result); | 404 DCHECK(read_result); |
(...skipping 196 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
602 DCHECK_EQ(31, num_buckets); | 601 DCHECK_EQ(31, num_buckets); |
603 | 602 |
604 // The histogram below is an expansion of the UMA_HISTOGRAM_CUSTOM_COUNTS | 603 // The histogram below is an expansion of the UMA_HISTOGRAM_CUSTOM_COUNTS |
605 // macro adapted to allow for a dynamically suffixed histogram name. | 604 // macro adapted to allow for a dynamically suffixed histogram name. |
606 // Note: The factory creates and owns the histogram. | 605 // Note: The factory creates and owns the histogram. |
607 base::HistogramBase* histogram = base::Histogram::FactoryGet( | 606 base::HistogramBase* histogram = base::Histogram::FactoryGet( |
608 histogram_name, min_value, max_value, num_buckets, | 607 histogram_name, min_value, max_value, num_buckets, |
609 base::HistogramBase::kUmaTargetedHistogramFlag); | 608 base::HistogramBase::kUmaTargetedHistogramFlag); |
610 return histogram; | 609 return histogram; |
611 } | 610 } |
OLD | NEW |