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

Side by Side Diff: dbus/test_service.cc

Issue 125673003: dbus: Add comments about the right way to expose methods (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 11 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 | Annotate | Revision Log
« no previous file with comments | « dbus/test_service.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 "dbus/test_service.h" 5 #include "dbus/test_service.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/test/test_timeouts.h" 8 #include "base/test/test_timeouts.h"
9 #include "base/threading/platform_thread.h" 9 #include "base/threading/platform_thread.h"
10 #include "dbus/bus.h" 10 #include "dbus/bus.h"
(...skipping 20 matching lines...) Expand all
31 : request_ownership_options(Bus::REQUIRE_PRIMARY) { 31 : request_ownership_options(Bus::REQUIRE_PRIMARY) {
32 } 32 }
33 33
34 TestService::Options::~Options() { 34 TestService::Options::~Options() {
35 } 35 }
36 36
37 TestService::TestService(const Options& options) 37 TestService::TestService(const Options& options)
38 : base::Thread("TestService"), 38 : base::Thread("TestService"),
39 request_ownership_options_(options.request_ownership_options), 39 request_ownership_options_(options.request_ownership_options),
40 dbus_task_runner_(options.dbus_task_runner), 40 dbus_task_runner_(options.dbus_task_runner),
41 on_all_methods_exported_(false, false), 41 on_name_obtained_(false, false),
42 num_exported_methods_(0) { 42 num_exported_methods_(0) {
43 } 43 }
44 44
45 TestService::~TestService() { 45 TestService::~TestService() {
46 Stop(); 46 Stop();
47 } 47 }
48 48
49 bool TestService::StartService() { 49 bool TestService::StartService() {
50 base::Thread::Options thread_options; 50 base::Thread::Options thread_options;
51 thread_options.message_loop_type = base::MessageLoop::TYPE_IO; 51 thread_options.message_loop_type = base::MessageLoop::TYPE_IO;
52 return StartWithOptions(thread_options); 52 return StartWithOptions(thread_options);
53 } 53 }
54 54
55 bool TestService::WaitUntilServiceIsStarted() { 55 bool TestService::WaitUntilServiceIsStarted() {
56 const base::TimeDelta timeout(TestTimeouts::action_max_timeout()); 56 const base::TimeDelta timeout(TestTimeouts::action_max_timeout());
57 // Wait until all methods are exported. 57 // Wait until the ownership of the service name is obtained.
58 return on_all_methods_exported_.TimedWait(timeout); 58 return on_name_obtained_.TimedWait(timeout);
59 } 59 }
60 60
61 void TestService::ShutdownAndBlock() { 61 void TestService::ShutdownAndBlock() {
62 message_loop()->PostTask( 62 message_loop()->PostTask(
63 FROM_HERE, 63 FROM_HERE,
64 base::Bind(&TestService::ShutdownAndBlockInternal, 64 base::Bind(&TestService::ShutdownAndBlockInternal,
65 base::Unretained(this))); 65 base::Unretained(this)));
66 } 66 }
67 67
68 bool TestService::HasDBusThread() { 68 bool TestService::HasDBusThread() {
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
131 base::Unretained(this), 131 base::Unretained(this),
132 callback)); 132 callback));
133 } 133 }
134 134
135 void TestService::OnOwnership(base::Callback<void(bool)> callback, 135 void TestService::OnOwnership(base::Callback<void(bool)> callback,
136 const std::string& service_name, 136 const std::string& service_name,
137 bool success) { 137 bool success) {
138 has_ownership_ = success; 138 has_ownership_ = success;
139 LOG_IF(ERROR, !success) << "Failed to own: " << service_name; 139 LOG_IF(ERROR, !success) << "Failed to own: " << service_name;
140 callback.Run(success); 140 callback.Run(success);
141
142 on_name_obtained_.Signal();
141 } 143 }
142 144
143 void TestService::OnExported(const std::string& interface_name, 145 void TestService::OnExported(const std::string& interface_name,
144 const std::string& method_name, 146 const std::string& method_name,
145 bool success) { 147 bool success) {
146 if (!success) { 148 if (!success) {
147 LOG(ERROR) << "Failed to export: " << interface_name << "." 149 LOG(ERROR) << "Failed to export: " << interface_name << "."
148 << method_name; 150 << method_name;
149 // Returning here will make WaitUntilServiceIsStarted() to time out 151 // Returning here will make WaitUntilServiceIsStarted() to time out
150 // and return false. 152 // and return false.
151 return; 153 return;
152 } 154 }
153 155
154 ++num_exported_methods_; 156 ++num_exported_methods_;
155 if (num_exported_methods_ == kNumMethodsToExport) 157 if (num_exported_methods_ == kNumMethodsToExport) {
156 on_all_methods_exported_.Signal(); 158 // As documented in exported_object.h, the service name should be
159 // requested after all methods are exposed.
160 bus_->RequestOwnership("org.chromium.TestService",
161 request_ownership_options_,
162 base::Bind(&TestService::OnOwnership,
163 base::Unretained(this),
164 base::Bind(&EmptyCallback)));
165 }
157 } 166 }
158 167
159 void TestService::Run(base::MessageLoop* message_loop) { 168 void TestService::Run(base::MessageLoop* message_loop) {
160 Bus::Options bus_options; 169 Bus::Options bus_options;
161 bus_options.bus_type = Bus::SESSION; 170 bus_options.bus_type = Bus::SESSION;
162 bus_options.connection_type = Bus::PRIVATE; 171 bus_options.connection_type = Bus::PRIVATE;
163 bus_options.dbus_task_runner = dbus_task_runner_; 172 bus_options.dbus_task_runner = dbus_task_runner_;
164 bus_ = new Bus(bus_options); 173 bus_ = new Bus(bus_options);
165 174
166 bus_->RequestOwnership("org.chromium.TestService",
167 request_ownership_options_,
168 base::Bind(&TestService::OnOwnership,
169 base::Unretained(this),
170 base::Bind(&EmptyCallback)));
171
172 exported_object_ = bus_->GetExportedObject( 175 exported_object_ = bus_->GetExportedObject(
173 ObjectPath("/org/chromium/TestObject")); 176 ObjectPath("/org/chromium/TestObject"));
174 177
175 int num_methods = 0; 178 int num_methods = 0;
176 exported_object_->ExportMethod( 179 exported_object_->ExportMethod(
177 "org.chromium.TestInterface", 180 "org.chromium.TestInterface",
178 "Echo", 181 "Echo",
179 base::Bind(&TestService::Echo, 182 base::Bind(&TestService::Echo,
180 base::Unretained(this)), 183 base::Unretained(this)),
181 base::Bind(&TestService::OnExported, 184 base::Bind(&TestService::OnExported,
(...skipping 425 matching lines...) Expand 10 before | Expand all | Expand 10 after
607 array_writer.OpenDictEntry(&dict_entry_writer); 610 array_writer.OpenDictEntry(&dict_entry_writer);
608 dict_entry_writer.AppendString("Name"); 611 dict_entry_writer.AppendString("Name");
609 dict_entry_writer.AppendVariantOfString(name); 612 dict_entry_writer.AppendVariantOfString(name);
610 array_writer.CloseContainer(&dict_entry_writer); 613 array_writer.CloseContainer(&dict_entry_writer);
611 writer.CloseContainer(&array_writer); 614 writer.CloseContainer(&array_writer);
612 615
613 exported_object_->SendSignal(&signal); 616 exported_object_->SendSignal(&signal);
614 } 617 }
615 618
616 } // namespace dbus 619 } // namespace dbus
OLDNEW
« no previous file with comments | « dbus/test_service.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698