Fix XmppLoginHandler to handle destruction from delegate methods.
[chromium-blink-merge.git] / dbus / test_service.h
blobde4aa720ac76a931ebf4811c89c966484aae037a
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 #ifndef DBUS_TEST_SERVICE_H_
6 #define DBUS_TEST_SERVICE_H_
8 #include "base/compiler_specific.h"
9 #include "base/memory/ref_counted.h"
10 #include "base/threading/thread.h"
11 #include "base/synchronization/waitable_event.h"
12 #include "dbus/bus.h"
13 #include "dbus/exported_object.h"
15 namespace base {
16 class SequencedTaskRunner;
19 namespace dbus {
21 class MethodCall;
22 class MessageWriter;
23 class Response;
25 // The test service is used for end-to-end tests. The service runs in a
26 // separate thread, so it does not interfere the test code that runs in
27 // the main thread.
29 // The test service exports an object with methods such as Echo() and
30 // SlowEcho(). The object has ability to send "Test" signal.
31 class TestService : public base::Thread {
32 public:
33 // Options for the test service.
34 struct Options {
35 Options();
36 ~Options();
38 // NULL by default (i.e. don't use the D-Bus thread).
39 scoped_refptr<base::SequencedTaskRunner> dbus_task_runner;
41 // Flags governing parameters of service ownership request.
42 Bus::ServiceOwnershipOptions request_ownership_options;
45 // The number of methods we'll export.
46 static const int kNumMethodsToExport;
48 explicit TestService(const Options& options);
49 ~TestService() override;
51 // Starts the service in a separate thread.
52 // Returns true if the thread is started successfully.
53 bool StartService();
55 // Waits until the service is started (i.e. all methods are exported).
56 // Returns true on success.
57 bool WaitUntilServiceIsStarted() WARN_UNUSED_RESULT;
59 // Shuts down the service and blocks until it's done.
60 void ShutdownAndBlock();
62 // Returns true if the bus has the D-Bus thread.
63 bool HasDBusThread();
65 // Sends "Test" signal with the given message from the exported object.
66 void SendTestSignal(const std::string& message);
68 // Sends "Test" signal with the given message from the root object ("/").
69 // This function emulates dbus-send's behavior.
70 void SendTestSignalFromRoot(const std::string& message);
72 // Request the ownership of a well-known name "TestService".
73 // |callback| will be called with the result when an ownership request is
74 // completed.
75 void RequestOwnership(base::Callback<void(bool)> callback);
77 // Release the ownership of the well-known name "TestService".
78 // |callback| will be called when the ownership has been released.
79 void ReleaseOwnership(base::Closure callback);
81 // Returns whether this instance has the name ownership or not.
82 bool has_ownership() const { return has_ownership_; }
84 private:
85 // Helper function for SendTestSignal().
86 void SendTestSignalInternal(const std::string& message);
88 // Helper function for SendTestSignalFromRoot.
89 void SendTestSignalFromRootInternal(const std::string& message);
91 // Helper function for ShutdownAndBlock().
92 void ShutdownAndBlockInternal();
94 // Called when an ownership request is completed.
95 // |callback| is the callback to be called with the result. |service_name| is
96 // the requested well-known bus name. |callback| and |service_name| are bound
97 // when the service requests the ownership. |success| is the result of the
98 // completed request, and is propagated to |callback|.
99 void OnOwnership(base::Callback<void(bool)> callback,
100 const std::string& service_name,
101 bool success);
103 // Called when a method is exported.
104 void OnExported(const std::string& interface_name,
105 const std::string& method_name,
106 bool success);
108 // base::Thread override.
109 void Run(base::MessageLoop* message_loop) override;
112 // Exported methods.
115 // Echos the text message received from the method call.
116 void Echo(MethodCall* method_call,
117 dbus::ExportedObject::ResponseSender response_sender);
119 // Echos the text message received from the method call, but sleeps for
120 // TestTimeouts::tiny_timeout_ms() before returning the response.
121 void SlowEcho(MethodCall* method_call,
122 dbus::ExportedObject::ResponseSender response_sender);
124 // Echos the text message received from the method call, but sends its
125 // response asynchronously after this callback has returned.
126 void AsyncEcho(MethodCall* method_call,
127 dbus::ExportedObject::ResponseSender response_sender);
129 // Returns NULL, instead of a valid Response.
130 void BrokenMethod(MethodCall* method_call,
131 dbus::ExportedObject::ResponseSender response_sender);
133 // Returns a set of property values for testing.
134 void GetAllProperties(MethodCall* method_call,
135 dbus::ExportedObject::ResponseSender response_sender);
137 // Returns a new value of 20 for the Version property when called.
138 void GetProperty(MethodCall* method_call,
139 dbus::ExportedObject::ResponseSender response_sender);
141 // Allows the name property to be changed, errors otherwise.
142 void SetProperty(MethodCall* method_call,
143 dbus::ExportedObject::ResponseSender response_sender);
145 // Performs an action for testing.
146 void PerformAction(MethodCall* method_call,
147 dbus::ExportedObject::ResponseSender response_sender);
149 // Object Manager: returns the set of objects and properties.
150 void GetManagedObjects(MethodCall* method_call,
151 dbus::ExportedObject::ResponseSender response_sender);
153 // Add a properties dictionary to a message writer.
154 void AddPropertiesToWriter(MessageWriter* writer);
156 // Add a new object to the manager.
157 void AddObject(const dbus::ObjectPath& object_path);
158 void AddObjectInternal(const dbus::ObjectPath& object_path);
160 // Remove an object from the manager.
161 void RemoveObject(const dbus::ObjectPath& object_path);
162 void RemoveObjectInternal(const dbus::ObjectPath& object_path);
164 // Sends a property changed signal for the name property.
165 void SendPropertyChangedSignal(const std::string& name);
167 // Helper function for SendPropertyChangedSignal().
168 void SendPropertyChangedSignalInternal(const std::string& name);
170 // Sends a property invalidated signal for the name property.
171 void SendPropertyInvalidatedSignal();
173 // Helper function for SendPropertyInvalidatedSignal().
174 void SendPropertyInvalidatedSignalInternal();
176 // Helper function for RequestOwnership().
177 void RequestOwnershipInternal(base::Callback<void(bool)> callback);
179 // Helper function for ReleaseOwnership().
180 void ReleaseOwnershipInternal(base::Closure callback);
182 // Configures the test service to send a PropertiesChanged signal for the
183 // "Name" property immediately after a call to GetManagedObjects.
184 void SetSendImmediatePropertiesChanged();
186 // Sends the response on completion of the performed action.
187 void PerformActionResponse(
188 MethodCall* method_call,
189 dbus::ExportedObject::ResponseSender response_sender);
191 // Re-requests ownership of the well-known name after releasing it.
192 void OwnershipReleased(
193 MethodCall* method_call,
194 dbus::ExportedObject::ResponseSender response_sender);
196 // Sends the action response after regaining the well-known name.
197 void OwnershipRegained(
198 MethodCall* method_call,
199 dbus::ExportedObject::ResponseSender response_sender,
200 bool success);
202 // Options to use when requesting service ownership.
203 Bus::ServiceOwnershipOptions request_ownership_options_;
205 scoped_refptr<base::SequencedTaskRunner> dbus_task_runner_;
206 base::WaitableEvent on_name_obtained_;
207 // The number of methods actually exported.
208 int num_exported_methods_;
210 // True if a PropertiesChanged signal for the "Name" property should be sent
211 // immediately following a call to GetManagedObjects.
212 bool send_immediate_properties_changed_;
214 // True iff this instance has successfully acquired the name ownership.
215 bool has_ownership_;
217 scoped_refptr<Bus> bus_;
218 ExportedObject* exported_object_;
219 ExportedObject* exported_object_manager_;
222 } // namespace dbus
224 #endif // DBUS_TEST_SERVICE_H_