Chromium Code Reviews| 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 "chromeos/dbus/flimflam_profile_client.h" | 5 #include "chromeos/dbus/flimflam_profile_client.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/message_loop.h" | 8 #include "base/message_loop.h" |
| 9 #include "base/stl_util.h" | |
| 9 #include "dbus/bus.h" | 10 #include "dbus/bus.h" |
| 10 #include "dbus/message.h" | 11 #include "dbus/message.h" |
| 11 #include "dbus/object_path.h" | 12 #include "dbus/object_path.h" |
| 12 #include "dbus/object_proxy.h" | 13 #include "dbus/object_proxy.h" |
| 13 #include "dbus/values_util.h" | 14 #include "dbus/values_util.h" |
| 14 #include "third_party/cros_system_api/dbus/service_constants.h" | 15 #include "third_party/cros_system_api/dbus/service_constants.h" |
| 15 | 16 |
| 16 namespace chromeos { | 17 namespace chromeos { |
| 17 | 18 |
| 18 namespace { | 19 namespace { |
| 19 | 20 |
| 20 // The FlimflamProfileClient implementation. | 21 // The FlimflamProfileClient implementation. |
| 21 class FlimflamProfileClientImpl : public FlimflamProfileClient { | 22 class FlimflamProfileClientImpl : public FlimflamProfileClient { |
| 22 public: | 23 public: |
| 23 explicit FlimflamProfileClientImpl(dbus::Bus* bus); | 24 explicit FlimflamProfileClientImpl(dbus::Bus* bus); |
| 24 | 25 |
| 25 // FlimflamProfileClient overrides: | 26 // FlimflamProfileClient overrides: |
| 26 virtual void SetPropertyChangedHandler( | 27 virtual void SetPropertyChangedHandler( |
| 28 const dbus::ObjectPath& profile_path, | |
| 27 const PropertyChangedHandler& handler) OVERRIDE; | 29 const PropertyChangedHandler& handler) OVERRIDE; |
| 28 virtual void ResetPropertyChangedHandler() OVERRIDE; | 30 virtual void ResetPropertyChangedHandler( |
| 29 virtual void GetProperties(const DictionaryValueCallback& callback) OVERRIDE; | 31 const dbus::ObjectPath& profile_path) OVERRIDE; |
| 30 virtual void GetEntry(const dbus::ObjectPath& path, | 32 virtual void GetProperties(const dbus::ObjectPath& profile_path, |
| 33 const DictionaryValueCallback& callback) OVERRIDE; | |
| 34 virtual void GetEntry(const dbus::ObjectPath& profile_path, | |
| 35 const std::string& entry_path, | |
| 31 const DictionaryValueCallback& callback) OVERRIDE; | 36 const DictionaryValueCallback& callback) OVERRIDE; |
| 32 virtual void DeleteEntry(const dbus::ObjectPath& path, | 37 virtual void DeleteEntry(const dbus::ObjectPath& profile_path, |
| 38 const std::string& entry_path, | |
| 33 const VoidCallback& callback) OVERRIDE; | 39 const VoidCallback& callback) OVERRIDE; |
| 34 | 40 |
| 35 private: | 41 private: |
| 42 typedef std::map<std::string, FlimflamClientHelper*> HelpersMap; | |
|
stevenjb
2012/04/12 17:34:36
s/HelpersMap/HelperMap/
hashimoto
2012/04/13 05:27:22
Done.
| |
| 43 | |
| 44 // Returns the corresponding FlimflamClientHelper for the profile. | |
| 45 FlimflamClientHelper* GetHelper(const dbus::ObjectPath& profile_path); | |
| 36 // Handles the result of signal connection setup. | 46 // Handles the result of signal connection setup. |
| 37 void OnSignalConnected(const std::string& interface, | 47 void OnSignalConnected(const std::string& interface, |
| 38 const std::string& signal, | 48 const std::string& signal, |
| 39 bool success); | 49 bool success); |
| 40 // Handles PropertyChanged signal. | 50 // Handles PropertyChanged signal. |
| 41 void OnPropertyChanged(dbus::Signal* signal); | 51 void OnPropertyChanged(dbus::Signal* signal); |
| 42 // Handles responses for methods without results. | 52 // Handles responses for methods without results. |
| 43 void OnVoidMethod(const VoidCallback& callback, dbus::Response* response); | 53 void OnVoidMethod(const VoidCallback& callback, dbus::Response* response); |
| 44 // Handles responses for methods with DictionaryValue results. | 54 // Handles responses for methods with DictionaryValue results. |
| 45 void OnDictionaryValueMethod(const DictionaryValueCallback& callback, | 55 void OnDictionaryValueMethod(const DictionaryValueCallback& callback, |
| 46 dbus::Response* response); | 56 dbus::Response* response); |
| 47 | 57 |
| 48 dbus::ObjectProxy* proxy_; | 58 dbus::Bus* bus_; |
| 49 FlimflamClientHelper helper_; | 59 HelpersMap helpers_; |
| 60 STLValueDeleter<HelpersMap> helpers_deleter_; | |
| 50 | 61 |
| 51 DISALLOW_COPY_AND_ASSIGN(FlimflamProfileClientImpl); | 62 DISALLOW_COPY_AND_ASSIGN(FlimflamProfileClientImpl); |
| 52 }; | 63 }; |
| 53 | 64 |
| 54 FlimflamProfileClientImpl::FlimflamProfileClientImpl(dbus::Bus* bus) | 65 FlimflamProfileClientImpl::FlimflamProfileClientImpl(dbus::Bus* bus) |
| 55 : proxy_(bus->GetObjectProxy( | 66 : bus_(bus), |
| 56 flimflam::kFlimflamServiceName, | 67 helpers_deleter_(&helpers_) { |
| 57 dbus::ObjectPath(flimflam::kFlimflamServicePath))), | 68 } |
| 58 helper_(proxy_) { | 69 |
| 59 helper_.MonitorPropertyChanged(flimflam::kFlimflamProfileInterface); | 70 FlimflamClientHelper* FlimflamProfileClientImpl::GetHelper( |
| 71 const dbus::ObjectPath& profile_path) { | |
| 72 HelpersMap::iterator it = helpers_.find(profile_path.value()); | |
| 73 if (it != helpers_.end()) | |
| 74 return it->second; | |
| 75 | |
| 76 // There is no helper for the profile, create it. | |
| 77 dbus::ObjectProxy* object_proxy = | |
| 78 bus_->GetObjectProxy(flimflam::kFlimflamServiceName, profile_path); | |
| 79 FlimflamClientHelper* helper = new FlimflamClientHelper(object_proxy); | |
| 80 helper->MonitorPropertyChanged(flimflam::kFlimflamProfileInterface); | |
| 81 helpers_.insert(HelpersMap::value_type(profile_path.value(), helper)); | |
| 82 return helper; | |
| 60 } | 83 } |
| 61 | 84 |
| 62 void FlimflamProfileClientImpl::SetPropertyChangedHandler( | 85 void FlimflamProfileClientImpl::SetPropertyChangedHandler( |
| 86 const dbus::ObjectPath& profile_path, | |
| 63 const PropertyChangedHandler& handler) { | 87 const PropertyChangedHandler& handler) { |
| 64 helper_.SetPropertyChangedHandler(handler); | 88 GetHelper(profile_path)->SetPropertyChangedHandler(handler); |
| 65 } | 89 } |
| 66 | 90 |
| 67 void FlimflamProfileClientImpl::ResetPropertyChangedHandler() { | 91 void FlimflamProfileClientImpl::ResetPropertyChangedHandler( |
| 68 helper_.ResetPropertyChangedHandler(); | 92 const dbus::ObjectPath& profile_path) { |
| 93 GetHelper(profile_path)->ResetPropertyChangedHandler(); | |
| 69 } | 94 } |
| 70 | 95 |
| 71 void FlimflamProfileClientImpl::GetProperties( | 96 void FlimflamProfileClientImpl::GetProperties( |
| 97 const dbus::ObjectPath& profile_path, | |
| 72 const DictionaryValueCallback& callback) { | 98 const DictionaryValueCallback& callback) { |
| 73 dbus::MethodCall method_call(flimflam::kFlimflamProfileInterface, | 99 dbus::MethodCall method_call(flimflam::kFlimflamProfileInterface, |
| 74 flimflam::kGetPropertiesFunction); | 100 flimflam::kGetPropertiesFunction); |
| 75 helper_.CallDictionaryValueMethod(&method_call, callback); | 101 GetHelper(profile_path)->CallDictionaryValueMethod(&method_call, callback); |
| 76 } | 102 } |
| 77 | 103 |
| 78 void FlimflamProfileClientImpl::GetEntry( | 104 void FlimflamProfileClientImpl::GetEntry( |
| 79 const dbus::ObjectPath& path, | 105 const dbus::ObjectPath& profile_path, |
| 106 const std::string& entry_path, | |
| 80 const DictionaryValueCallback& callback) { | 107 const DictionaryValueCallback& callback) { |
| 81 dbus::MethodCall method_call(flimflam::kFlimflamProfileInterface, | 108 dbus::MethodCall method_call(flimflam::kFlimflamProfileInterface, |
| 82 flimflam::kGetEntryFunction); | 109 flimflam::kGetEntryFunction); |
| 83 dbus::MessageWriter writer(&method_call); | 110 dbus::MessageWriter writer(&method_call); |
| 84 writer.AppendObjectPath(path); | 111 writer.AppendString(entry_path); |
| 85 helper_.CallDictionaryValueMethod(&method_call, callback); | 112 GetHelper(profile_path)->CallDictionaryValueMethod(&method_call, callback); |
| 86 } | 113 } |
| 87 | 114 |
| 88 void FlimflamProfileClientImpl::DeleteEntry(const dbus::ObjectPath& path, | 115 void FlimflamProfileClientImpl::DeleteEntry( |
| 89 const VoidCallback& callback) { | 116 const dbus::ObjectPath& profile_path, |
| 117 const std::string& entry_path, | |
| 118 const VoidCallback& callback) { | |
| 90 dbus::MethodCall method_call(flimflam::kFlimflamProfileInterface, | 119 dbus::MethodCall method_call(flimflam::kFlimflamProfileInterface, |
| 91 flimflam::kDeleteEntryFunction); | 120 flimflam::kDeleteEntryFunction); |
| 92 dbus::MessageWriter writer(&method_call); | 121 dbus::MessageWriter writer(&method_call); |
| 93 writer.AppendObjectPath(path); | 122 writer.AppendString(entry_path); |
| 94 helper_.CallVoidMethod(&method_call, callback); | 123 GetHelper(profile_path)->CallVoidMethod(&method_call, callback); |
| 95 } | 124 } |
| 96 | 125 |
| 97 // A stub implementation of FlimflamProfileClient. | 126 // A stub implementation of FlimflamProfileClient. |
| 98 class FlimflamProfileClientStubImpl : public FlimflamProfileClient { | 127 class FlimflamProfileClientStubImpl : public FlimflamProfileClient { |
| 99 public: | 128 public: |
| 100 FlimflamProfileClientStubImpl() : weak_ptr_factory_(this) {} | 129 FlimflamProfileClientStubImpl() : weak_ptr_factory_(this) {} |
| 101 | 130 |
| 102 virtual ~FlimflamProfileClientStubImpl() {} | 131 virtual ~FlimflamProfileClientStubImpl() {} |
| 103 | 132 |
| 104 // FlimflamProfileClient override. | 133 // FlimflamProfileClient override. |
| 105 virtual void SetPropertyChangedHandler( | 134 virtual void SetPropertyChangedHandler( |
| 135 const dbus::ObjectPath& profile_path, | |
| 106 const PropertyChangedHandler& handler) OVERRIDE {} | 136 const PropertyChangedHandler& handler) OVERRIDE {} |
| 107 | 137 |
| 108 // FlimflamProfileClient override. | 138 // FlimflamProfileClient override. |
| 109 virtual void ResetPropertyChangedHandler() OVERRIDE {} | 139 virtual void ResetPropertyChangedHandler( |
| 140 const dbus::ObjectPath& profile_path) OVERRIDE {} | |
| 110 | 141 |
| 111 // FlimflamProfileClient override. | 142 // FlimflamProfileClient override. |
| 112 virtual void GetProperties(const DictionaryValueCallback& callback) OVERRIDE { | 143 virtual void GetProperties(const dbus::ObjectPath& profile_path, |
| 144 const DictionaryValueCallback& callback) OVERRIDE { | |
| 113 MessageLoop::current()->PostTask( | 145 MessageLoop::current()->PostTask( |
| 114 FROM_HERE, | 146 FROM_HERE, |
| 115 base::Bind(&FlimflamProfileClientStubImpl::PassEmptyDictionaryValue, | 147 base::Bind(&FlimflamProfileClientStubImpl::PassEmptyDictionaryValue, |
| 116 weak_ptr_factory_.GetWeakPtr(), | 148 weak_ptr_factory_.GetWeakPtr(), |
| 117 callback)); | 149 callback)); |
| 118 } | 150 } |
| 119 | 151 |
| 120 // FlimflamProfileClient override. | 152 // FlimflamProfileClient override. |
| 121 virtual void GetEntry(const dbus::ObjectPath& path, | 153 virtual void GetEntry(const dbus::ObjectPath& profile_path, |
| 154 const std::string& entry_path, | |
| 122 const DictionaryValueCallback& callback) OVERRIDE { | 155 const DictionaryValueCallback& callback) OVERRIDE { |
| 123 MessageLoop::current()->PostTask( | 156 MessageLoop::current()->PostTask( |
| 124 FROM_HERE, | 157 FROM_HERE, |
| 125 base::Bind(&FlimflamProfileClientStubImpl::PassEmptyDictionaryValue, | 158 base::Bind(&FlimflamProfileClientStubImpl::PassEmptyDictionaryValue, |
| 126 weak_ptr_factory_.GetWeakPtr(), | 159 weak_ptr_factory_.GetWeakPtr(), |
| 127 callback)); | 160 callback)); |
| 128 } | 161 } |
| 129 | 162 |
| 130 // FlimflamProfileClient override. | 163 // FlimflamProfileClient override. |
| 131 virtual void DeleteEntry(const dbus::ObjectPath& path, | 164 virtual void DeleteEntry(const dbus::ObjectPath& profile_path, |
| 165 const std::string& entry_path, | |
| 132 const VoidCallback& callback) OVERRIDE { | 166 const VoidCallback& callback) OVERRIDE { |
| 133 MessageLoop::current()->PostTask(FROM_HERE, | 167 MessageLoop::current()->PostTask(FROM_HERE, |
| 134 base::Bind(callback, | 168 base::Bind(callback, |
| 135 DBUS_METHOD_CALL_SUCCESS)); | 169 DBUS_METHOD_CALL_SUCCESS)); |
| 136 } | 170 } |
| 137 | 171 |
| 138 private: | 172 private: |
| 139 void PassEmptyDictionaryValue(const DictionaryValueCallback& callback) const { | 173 void PassEmptyDictionaryValue(const DictionaryValueCallback& callback) const { |
| 140 base::DictionaryValue dictionary; | 174 base::DictionaryValue dictionary; |
| 141 callback.Run(DBUS_METHOD_CALL_SUCCESS, dictionary); | 175 callback.Run(DBUS_METHOD_CALL_SUCCESS, dictionary); |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 156 FlimflamProfileClient* FlimflamProfileClient::Create( | 190 FlimflamProfileClient* FlimflamProfileClient::Create( |
| 157 DBusClientImplementationType type, | 191 DBusClientImplementationType type, |
| 158 dbus::Bus* bus) { | 192 dbus::Bus* bus) { |
| 159 if (type == REAL_DBUS_CLIENT_IMPLEMENTATION) | 193 if (type == REAL_DBUS_CLIENT_IMPLEMENTATION) |
| 160 return new FlimflamProfileClientImpl(bus); | 194 return new FlimflamProfileClientImpl(bus); |
| 161 DCHECK_EQ(STUB_DBUS_CLIENT_IMPLEMENTATION, type); | 195 DCHECK_EQ(STUB_DBUS_CLIENT_IMPLEMENTATION, type); |
| 162 return new FlimflamProfileClientStubImpl(); | 196 return new FlimflamProfileClientStubImpl(); |
| 163 } | 197 } |
| 164 | 198 |
| 165 } // namespace chromeos | 199 } // namespace chromeos |
| OLD | NEW |