| 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 #ifndef CHROMEOS_DBUS_SHILL_CLIENT_HELPER_H_ | 5 #ifndef CHROMEOS_DBUS_SHILL_CLIENT_HELPER_H_ |
| 6 #define CHROMEOS_DBUS_SHILL_CLIENT_HELPER_H_ | 6 #define CHROMEOS_DBUS_SHILL_CLIENT_HELPER_H_ |
| 7 | 7 |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
| (...skipping 23 matching lines...) Expand all Loading... |
| 34 class Response; | 34 class Response; |
| 35 class Signal; | 35 class Signal; |
| 36 | 36 |
| 37 } // namespace dbus | 37 } // namespace dbus |
| 38 | 38 |
| 39 namespace chromeos { | 39 namespace chromeos { |
| 40 | 40 |
| 41 // A class to help implement Shill clients. | 41 // A class to help implement Shill clients. |
| 42 class ShillClientHelper { | 42 class ShillClientHelper { |
| 43 public: | 43 public: |
| 44 class RefHolder; |
| 45 |
| 44 // A callback to handle PropertyChanged signals. | 46 // A callback to handle PropertyChanged signals. |
| 45 typedef base::Callback<void(const std::string& name, | 47 typedef base::Callback<void(const std::string& name, |
| 46 const base::Value& value)> PropertyChangedHandler; | 48 const base::Value& value)> PropertyChangedHandler; |
| 47 | 49 |
| 48 // A callback to handle responses for methods with DictionaryValue results. | 50 // A callback to handle responses for methods with DictionaryValue results. |
| 49 typedef base::Callback<void( | 51 typedef base::Callback<void( |
| 50 DBusMethodCallStatus call_status, | 52 DBusMethodCallStatus call_status, |
| 51 const base::DictionaryValue& result)> DictionaryValueCallback; | 53 const base::DictionaryValue& result)> DictionaryValueCallback; |
| 52 | 54 |
| 53 // A callback to handle responses for methods with DictionaryValue results. | 55 // A callback to handle responses for methods with DictionaryValue results. |
| 54 // This is used by CallDictionaryValueMethodWithErrorCallback. | 56 // This is used by CallDictionaryValueMethodWithErrorCallback. |
| 55 typedef base::Callback<void(const base::DictionaryValue& result)> | 57 typedef base::Callback<void(const base::DictionaryValue& result)> |
| 56 DictionaryValueCallbackWithoutStatus; | 58 DictionaryValueCallbackWithoutStatus; |
| 57 | 59 |
| 58 // A callback to handle responses of methods returning a ListValue. | 60 // A callback to handle responses of methods returning a ListValue. |
| 59 typedef base::Callback<void(const base::ListValue& result)> ListValueCallback; | 61 typedef base::Callback<void(const base::ListValue& result)> ListValueCallback; |
| 60 | 62 |
| 61 // A callback to handle errors for method call. | 63 // A callback to handle errors for method call. |
| 62 typedef base::Callback<void(const std::string& error_name, | 64 typedef base::Callback<void(const std::string& error_name, |
| 63 const std::string& error_message)> ErrorCallback; | 65 const std::string& error_message)> ErrorCallback; |
| 64 | 66 |
| 65 // A callback that handles responses for methods with string results. | 67 // A callback that handles responses for methods with string results. |
| 66 typedef base::Callback<void(const std::string& result)> StringCallback; | 68 typedef base::Callback<void(const std::string& result)> StringCallback; |
| 67 | 69 |
| 68 // A callback that handles responses for methods with boolean results. | 70 // A callback that handles responses for methods with boolean results. |
| 69 typedef base::Callback<void(bool result)> BooleanCallback; | 71 typedef base::Callback<void(bool result)> BooleanCallback; |
| 70 | 72 |
| 73 // Callback used to notify owner when this can be safely released. |
| 74 typedef base::Callback<void(ShillClientHelper* helper)> ReleasedCallback; |
| 71 | 75 |
| 72 ShillClientHelper(dbus::Bus* bus, dbus::ObjectProxy* proxy); | 76 explicit ShillClientHelper(dbus::ObjectProxy* proxy); |
| 73 | 77 |
| 74 virtual ~ShillClientHelper(); | 78 virtual ~ShillClientHelper(); |
| 75 | 79 |
| 80 // Sets |released_callback_|. This is optional and should only be called at |
| 81 // most once. |
| 82 void SetReleasedCallback(ReleasedCallback callback); |
| 83 |
| 76 // Adds an |observer| of the PropertyChanged signal. | 84 // Adds an |observer| of the PropertyChanged signal. |
| 77 void AddPropertyChangedObserver(ShillPropertyChangedObserver* observer); | 85 void AddPropertyChangedObserver(ShillPropertyChangedObserver* observer); |
| 78 | 86 |
| 79 // Removes an |observer| of the PropertyChanged signal. | 87 // Removes an |observer| of the PropertyChanged signal. |
| 80 void RemovePropertyChangedObserver(ShillPropertyChangedObserver* observer); | 88 void RemovePropertyChangedObserver(ShillPropertyChangedObserver* observer); |
| 81 | 89 |
| 82 // Starts monitoring PropertyChanged signal. If there aren't observers for the | 90 // Starts monitoring PropertyChanged signal. If there aren't observers for the |
| 83 // PropertyChanged signal, the actual monitoring will be delayed until the | 91 // PropertyChanged signal, the actual monitoring will be delayed until the |
| 84 // first observer is added. | 92 // first observer is added. |
| 85 void MonitorPropertyChanged(const std::string& interface_name); | 93 void MonitorPropertyChanged(const std::string& interface_name); |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 124 dbus::MethodCall* method_call, | 132 dbus::MethodCall* method_call, |
| 125 const DictionaryValueCallbackWithoutStatus& callback, | 133 const DictionaryValueCallbackWithoutStatus& callback, |
| 126 const ErrorCallback& error_callback); | 134 const ErrorCallback& error_callback); |
| 127 | 135 |
| 128 // Calls a method with a boolean array result with error callback. | 136 // Calls a method with a boolean array result with error callback. |
| 129 void CallListValueMethodWithErrorCallback( | 137 void CallListValueMethodWithErrorCallback( |
| 130 dbus::MethodCall* method_call, | 138 dbus::MethodCall* method_call, |
| 131 const ListValueCallback& callback, | 139 const ListValueCallback& callback, |
| 132 const ErrorCallback& error_callback); | 140 const ErrorCallback& error_callback); |
| 133 | 141 |
| 142 const dbus::ObjectProxy* object_proxy() const { return proxy_; } |
| 143 |
| 134 // Appends the value (basic types and string-to-string dictionary) to the | 144 // Appends the value (basic types and string-to-string dictionary) to the |
| 135 // writer as a variant. | 145 // writer as a variant. |
| 136 static void AppendValueDataAsVariant(dbus::MessageWriter* writer, | 146 static void AppendValueDataAsVariant(dbus::MessageWriter* writer, |
| 137 const base::Value& value); | 147 const base::Value& value); |
| 138 | 148 |
| 139 // Appends a string-to-variant dictionary to the writer. | 149 // Appends a string-to-variant dictionary to the writer. |
| 140 static void AppendServicePropertiesDictionary( | 150 static void AppendServicePropertiesDictionary( |
| 141 dbus::MessageWriter* writer, | 151 dbus::MessageWriter* writer, |
| 142 const base::DictionaryValue& dictionary); | 152 const base::DictionaryValue& dictionary); |
| 143 | 153 |
| 154 protected: |
| 155 // Reference / Ownership management. If the number of active refs (observers |
| 156 // + in-progress method calls) becomes 0, |released_callback_| (if set) will |
| 157 // be called. |
| 158 void AddRef(); |
| 159 void Release(); |
| 160 |
| 144 private: | 161 private: |
| 145 // Starts monitoring PropertyChanged signal. | 162 // Starts monitoring PropertyChanged signal. |
| 146 void MonitorPropertyChangedInternal(const std::string& interface_name); | 163 void MonitorPropertyChangedInternal(const std::string& interface_name); |
| 147 | 164 |
| 148 // Handles the result of signal connection setup. | 165 // Handles the result of signal connection setup. |
| 149 void OnSignalConnected(const std::string& interface, | 166 void OnSignalConnected(const std::string& interface, |
| 150 const std::string& signal, | 167 const std::string& signal, |
| 151 bool success); | 168 bool success); |
| 152 | 169 |
| 153 // Handles PropertyChanged signal. | 170 // Handles PropertyChanged signal. |
| 154 void OnPropertyChanged(dbus::Signal* signal); | 171 void OnPropertyChanged(dbus::Signal* signal); |
| 155 | 172 |
| 156 dbus::ObjectProxy* proxy_; | 173 dbus::ObjectProxy* proxy_; |
| 174 ReleasedCallback released_callback_; |
| 175 int active_refs_; |
| 157 PropertyChangedHandler property_changed_handler_; | 176 PropertyChangedHandler property_changed_handler_; |
| 158 ObserverList<ShillPropertyChangedObserver, true /* check_empty */> | 177 ObserverList<ShillPropertyChangedObserver, true /* check_empty */> |
| 159 observer_list_; | 178 observer_list_; |
| 160 std::vector<std::string> interfaces_to_be_monitored_; | 179 std::vector<std::string> interfaces_to_be_monitored_; |
| 161 | 180 |
| 162 // Note: This should remain the last member so it'll be destroyed and | 181 // Note: This should remain the last member so it'll be destroyed and |
| 163 // invalidate its weak pointers before any other members are destroyed. | 182 // invalidate its weak pointers before any other members are destroyed. |
| 164 base::WeakPtrFactory<ShillClientHelper> weak_ptr_factory_; | 183 base::WeakPtrFactory<ShillClientHelper> weak_ptr_factory_; |
| 165 | 184 |
| 166 DISALLOW_COPY_AND_ASSIGN(ShillClientHelper); | 185 DISALLOW_COPY_AND_ASSIGN(ShillClientHelper); |
| 167 }; | 186 }; |
| 168 | 187 |
| 169 } // namespace chromeos | 188 } // namespace chromeos |
| 170 | 189 |
| 171 #endif // CHROMEOS_DBUS_SHILL_CLIENT_HELPER_H_ | 190 #endif // CHROMEOS_DBUS_SHILL_CLIENT_HELPER_H_ |
| OLD | NEW |