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

Side by Side Diff: services/preferences/public/cpp/pref_store_impl.cc

Issue 2635153002: Pref service: expose all read-only PrefStores through Mojo (Closed)
Patch Set: Merge Created 3 years, 9 months 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
(Empty)
1 // Copyright 2017 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 "services/preferences/public/cpp/pref_store_impl.h"
6
7 #include <memory>
8
9 #include "base/values.h"
10
11 namespace prefs {
12
13 // static
14 std::unique_ptr<PrefStoreImpl> PrefStoreImpl::Create(
15 mojom::PrefStoreRegistryPtr registry_ptr,
16 scoped_refptr<::PrefStore> pref_store,
17 PrefValueStore::PrefStoreType type) {
18 mojom::PrefStorePtr ptr;
19 auto impl = base::MakeUnique<PrefStoreImpl>(std::move(pref_store),
20 mojo::MakeRequest(&ptr));
21 registry_ptr->Register(type, std::move(ptr));
22 return impl;
23 }
24
25 PrefStoreImpl::PrefStoreImpl(scoped_refptr<::PrefStore> pref_store,
26 mojom::PrefStoreRequest request)
27 : backing_pref_store_(std::move(pref_store)),
28 initialized_(false),
29 binding_(this, std::move(request)) {
30 DCHECK(backing_pref_store_);
31 if (backing_pref_store_->IsInitializationComplete())
32 OnInitializationCompleted(true);
33 backing_pref_store_->AddObserver(this);
34 }
35
36 PrefStoreImpl::~PrefStoreImpl() {
37 backing_pref_store_->RemoveObserver(this);
38 }
39
40 void PrefStoreImpl::OnPrefValueChanged(const std::string& key) {
41 auto dictionary = base::MakeUnique<base::DictionaryValue>();
42 const base::Value* value = nullptr;
43 if (backing_pref_store_->GetValue(key, &value)) {
44 for (const auto& observer : observers_)
45 observer->OnPrefChanged(key, value->CreateDeepCopy());
46 } else {
47 for (const auto& observer : observers_)
48 observer->OnPrefChanged(key, base::Value::CreateNullValue());
Sam McNally 2017/03/03 03:47:58 nullptr?
tibell 2017/03/07 00:52:52 Done.
49 }
50 }
51
52 void PrefStoreImpl::OnInitializationCompleted(bool succeeded) {
53 // Some pref stores call this more than once. We just ignore all calls after
54 // the first.
55 if (initialized_)
56 DCHECK(succeeded);
57 initialized_ = succeeded;
58 for (const auto& observer : observers_)
59 observer->OnInitializationCompleted(succeeded);
60 }
61
62 void PrefStoreImpl::AddObserver(const AddObserverCallback& callback) {
63 mojom::PrefStoreObserverPtr observer_ptr;
64 auto request = mojo::MakeRequest(&observer_ptr);
65 observers_.push_back(std::move(observer_ptr));
66 mojom::PrefStoreConnectionPtr connection = mojom::PrefStoreConnection::New();
67 connection->observer = std::move(request);
68 connection->initial_prefs = backing_pref_store_->GetValues();
69 connection->is_initialized = backing_pref_store_->IsInitializationComplete();
70 callback.Run(std::move(connection));
Sam McNally 2017/03/03 03:47:58 Inline constructing |connection|.
tibell 2017/03/07 00:52:52 Done.
71 }
72
73 } // namespace prefs
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698