Drive: Add BatchableRequest subclass.
[chromium-blink-merge.git] / ui / app_list / search / dictionary_data_store.cc
blob45f53ca6edb58101530b4cc03cf1e5416218c06c
1 // Copyright 2013 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.
5 #include "ui/app_list/search/dictionary_data_store.h"
7 #include "base/callback.h"
8 #include "base/json/json_file_value_serializer.h"
9 #include "base/json/json_string_value_serializer.h"
10 #include "base/logging.h"
11 #include "base/sequenced_task_runner.h"
12 #include "base/strings/string_number_conversions.h"
13 #include "base/task_runner_util.h"
14 #include "base/threading/sequenced_worker_pool.h"
15 #include "base/values.h"
17 namespace app_list {
19 DictionaryDataStore::DictionaryDataStore(const base::FilePath& data_file,
20 base::SequencedWorkerPool* worker_pool)
21 : data_file_(data_file), worker_pool_(worker_pool) {
22 std::string token("app-launcher-data-store");
23 token.append(data_file.AsUTF8Unsafe());
25 // Uses a SKIP_ON_SHUTDOWN file task runner because losing a couple
26 // associations is better than blocking shutdown.
27 file_task_runner_ = worker_pool->GetSequencedTaskRunnerWithShutdownBehavior(
28 worker_pool->GetNamedSequenceToken(token),
29 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN);
30 writer_.reset(
31 new base::ImportantFileWriter(data_file, file_task_runner_.get()));
33 cached_dict_.reset(new base::DictionaryValue);
36 DictionaryDataStore::~DictionaryDataStore() {
37 Flush(OnFlushedCallback());
40 void DictionaryDataStore::Flush(const OnFlushedCallback& on_flushed) {
41 if (writer_->HasPendingWrite())
42 writer_->DoScheduledWrite();
44 if (on_flushed.is_null())
45 return;
47 file_task_runner_->PostTaskAndReply(
48 FROM_HERE, base::Bind(&base::DoNothing), on_flushed);
51 void DictionaryDataStore::Load(
52 const DictionaryDataStore::OnLoadedCallback& on_loaded) {
53 base::PostTaskAndReplyWithResult(
54 file_task_runner_.get(),
55 FROM_HERE,
56 base::Bind(&DictionaryDataStore::LoadOnBlockingPool, this),
57 on_loaded);
60 void DictionaryDataStore::ScheduleWrite() {
61 writer_->ScheduleWrite(this);
64 scoped_ptr<base::DictionaryValue> DictionaryDataStore::LoadOnBlockingPool() {
65 DCHECK(worker_pool_->RunsTasksOnCurrentThread());
67 int error_code = JSONFileValueDeserializer::JSON_NO_ERROR;
68 std::string error_message;
69 JSONFileValueDeserializer deserializer(data_file_);
70 base::Value* value = deserializer.Deserialize(&error_code, &error_message);
71 base::DictionaryValue* dict_value = NULL;
72 if (error_code != JSONFileValueDeserializer::JSON_NO_ERROR || !value ||
73 !value->GetAsDictionary(&dict_value) || !dict_value) {
74 return nullptr;
77 base::DictionaryValue* return_dict = dict_value->DeepCopy();
78 cached_dict_.reset(dict_value);
79 return make_scoped_ptr(return_dict);
82 bool DictionaryDataStore::SerializeData(std::string* data) {
83 JSONStringValueSerializer serializer(data);
84 serializer.set_pretty_print(true);
85 return serializer.Serialize(*cached_dict_.get());
88 } // namespace app_list