Move VariationsRegistrySyncerWin to ChromeVariationsServiceClient
[chromium-blink-merge.git] / chromeos / dbus / shill_profile_client.cc
blob638b92f64a4769e4c214305224e814d824951963
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.
5 #include "chromeos/dbus/shill_profile_client.h"
7 #include "base/bind.h"
8 #include "base/containers/scoped_ptr_map.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/message_loop/message_loop.h"
11 #include "base/values.h"
12 #include "chromeos/dbus/dbus_thread_manager.h"
13 #include "chromeos/dbus/shill_property_changed_observer.h"
14 #include "dbus/bus.h"
15 #include "dbus/message.h"
16 #include "dbus/object_path.h"
17 #include "dbus/values_util.h"
18 #include "third_party/cros_system_api/dbus/service_constants.h"
20 namespace chromeos {
22 namespace {
24 const char kSharedProfilePath[] = "/profile/default";
26 class ShillProfileClientImpl : public ShillProfileClient {
27 public:
28 ShillProfileClientImpl();
30 void AddPropertyChangedObserver(
31 const dbus::ObjectPath& profile_path,
32 ShillPropertyChangedObserver* observer) override {
33 GetHelper(profile_path)->AddPropertyChangedObserver(observer);
36 void RemovePropertyChangedObserver(
37 const dbus::ObjectPath& profile_path,
38 ShillPropertyChangedObserver* observer) override {
39 GetHelper(profile_path)->RemovePropertyChangedObserver(observer);
42 void GetProperties(const dbus::ObjectPath& profile_path,
43 const DictionaryValueCallbackWithoutStatus& callback,
44 const ErrorCallback& error_callback) override;
45 void GetEntry(const dbus::ObjectPath& profile_path,
46 const std::string& entry_path,
47 const DictionaryValueCallbackWithoutStatus& callback,
48 const ErrorCallback& error_callback) override;
49 void DeleteEntry(const dbus::ObjectPath& profile_path,
50 const std::string& entry_path,
51 const base::Closure& callback,
52 const ErrorCallback& error_callback) override;
54 TestInterface* GetTestInterface() override { return NULL; }
56 protected:
57 void Init(dbus::Bus* bus) override { bus_ = bus; }
59 private:
60 typedef base::ScopedPtrMap<std::string, scoped_ptr<ShillClientHelper>>
61 HelperMap;
63 // Returns the corresponding ShillClientHelper for the profile.
64 ShillClientHelper* GetHelper(const dbus::ObjectPath& profile_path);
66 dbus::Bus* bus_;
67 HelperMap helpers_;
69 DISALLOW_COPY_AND_ASSIGN(ShillProfileClientImpl);
72 ShillProfileClientImpl::ShillProfileClientImpl() : bus_(NULL) {
75 ShillClientHelper* ShillProfileClientImpl::GetHelper(
76 const dbus::ObjectPath& profile_path) {
77 HelperMap::const_iterator it = helpers_.find(profile_path.value());
78 if (it != helpers_.end())
79 return it->second;
81 // There is no helper for the profile, create it.
82 dbus::ObjectProxy* object_proxy =
83 bus_->GetObjectProxy(shill::kFlimflamServiceName, profile_path);
84 scoped_ptr<ShillClientHelper> helper(new ShillClientHelper(object_proxy));
85 helper->MonitorPropertyChanged(shill::kFlimflamProfileInterface);
86 ShillClientHelper* helper_ptr = helper.get();
87 helpers_.insert(profile_path.value(), helper.Pass());
88 return helper_ptr;
91 void ShillProfileClientImpl::GetProperties(
92 const dbus::ObjectPath& profile_path,
93 const DictionaryValueCallbackWithoutStatus& callback,
94 const ErrorCallback& error_callback) {
95 dbus::MethodCall method_call(shill::kFlimflamProfileInterface,
96 shill::kGetPropertiesFunction);
97 GetHelper(profile_path)->CallDictionaryValueMethodWithErrorCallback(
98 &method_call, callback, error_callback);
101 void ShillProfileClientImpl::GetEntry(
102 const dbus::ObjectPath& profile_path,
103 const std::string& entry_path,
104 const DictionaryValueCallbackWithoutStatus& callback,
105 const ErrorCallback& error_callback) {
106 dbus::MethodCall method_call(shill::kFlimflamProfileInterface,
107 shill::kGetEntryFunction);
108 dbus::MessageWriter writer(&method_call);
109 writer.AppendString(entry_path);
110 GetHelper(profile_path)->CallDictionaryValueMethodWithErrorCallback(
111 &method_call, callback, error_callback);
114 void ShillProfileClientImpl::DeleteEntry(
115 const dbus::ObjectPath& profile_path,
116 const std::string& entry_path,
117 const base::Closure& callback,
118 const ErrorCallback& error_callback) {
119 dbus::MethodCall method_call(shill::kFlimflamProfileInterface,
120 shill::kDeleteEntryFunction);
121 dbus::MessageWriter writer(&method_call);
122 writer.AppendString(entry_path);
123 GetHelper(profile_path)->CallVoidMethodWithErrorCallback(
124 &method_call, callback, error_callback);
127 } // namespace
129 ShillProfileClient::ShillProfileClient() {}
131 ShillProfileClient::~ShillProfileClient() {}
133 // static
134 ShillProfileClient* ShillProfileClient::Create() {
135 return new ShillProfileClientImpl();
138 // static
139 std::string ShillProfileClient::GetSharedProfilePath() {
140 return std::string(kSharedProfilePath);
143 } // namespace chromeos