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

Side by Side Diff: dbus/bus.cc

Issue 2808983002: Migrate to base::FileDescriptionWatcher in dbus/bus.cc. (Closed)
Patch Set: self-review Created 3 years, 8 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
« no previous file with comments | « no previous file | dbus/bus_unittest.cc » ('j') | dbus/object_proxy_unittest.cc » ('J')
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/bus.h" 5 #include "dbus/bus.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 8
9 #include <memory>
10
9 #include "base/bind.h" 11 #include "base/bind.h"
12 #include "base/files/file_descriptor_watcher_posix.h"
10 #include "base/logging.h" 13 #include "base/logging.h"
11 #include "base/message_loop/message_loop.h"
12 #include "base/stl_util.h" 14 #include "base/stl_util.h"
13 #include "base/strings/stringprintf.h" 15 #include "base/strings/stringprintf.h"
14 #include "base/threading/thread.h" 16 #include "base/threading/thread.h"
15 #include "base/threading/thread_restrictions.h" 17 #include "base/threading/thread_restrictions.h"
16 #include "base/threading/thread_task_runner_handle.h" 18 #include "base/threading/thread_task_runner_handle.h"
17 #include "base/time/time.h" 19 #include "base/time/time.h"
18 #include "dbus/exported_object.h" 20 #include "dbus/exported_object.h"
19 #include "dbus/message.h" 21 #include "dbus/message.h"
20 #include "dbus/object_manager.h" 22 #include "dbus/object_manager.h"
21 #include "dbus/object_path.h" 23 #include "dbus/object_path.h"
(...skipping 13 matching lines...) Expand all
35 const char kNameOwnerChangedSignal[] = "NameOwnerChanged"; 37 const char kNameOwnerChangedSignal[] = "NameOwnerChanged";
36 38
37 // The match rule used to filter for changes to a given service name owner. 39 // The match rule used to filter for changes to a given service name owner.
38 const char kServiceNameOwnerChangeMatchRule[] = 40 const char kServiceNameOwnerChangeMatchRule[] =
39 "type='signal',interface='org.freedesktop.DBus'," 41 "type='signal',interface='org.freedesktop.DBus',"
40 "member='NameOwnerChanged',path='/org/freedesktop/DBus'," 42 "member='NameOwnerChanged',path='/org/freedesktop/DBus',"
41 "sender='org.freedesktop.DBus',arg0='%s'"; 43 "sender='org.freedesktop.DBus',arg0='%s'";
42 44
43 // The class is used for watching the file descriptor used for D-Bus 45 // The class is used for watching the file descriptor used for D-Bus
44 // communication. 46 // communication.
45 class Watch : public base::MessagePumpLibevent::Watcher { 47 class Watch {
46 public: 48 public:
47 explicit Watch(DBusWatch* watch) 49 explicit Watch(DBusWatch* watch) : raw_watch_(watch) {
48 : raw_watch_(watch), file_descriptor_watcher_(FROM_HERE) {
49 dbus_watch_set_data(raw_watch_, this, NULL); 50 dbus_watch_set_data(raw_watch_, this, NULL);
50 } 51 }
51 52
52 ~Watch() override { dbus_watch_set_data(raw_watch_, NULL, NULL); } 53 ~Watch() { dbus_watch_set_data(raw_watch_, NULL, NULL); }
53 54
54 // Returns true if the underlying file descriptor is ready to be watched. 55 // Returns true if the underlying file descriptor is ready to be watched.
55 bool IsReadyToBeWatched() { 56 bool IsReadyToBeWatched() {
56 return dbus_watch_get_enabled(raw_watch_); 57 return dbus_watch_get_enabled(raw_watch_);
57 } 58 }
58 59
59 // Starts watching the underlying file descriptor. 60 // Starts watching the underlying file descriptor.
60 void StartWatching() { 61 void StartWatching() {
61 const int file_descriptor = dbus_watch_get_unix_fd(raw_watch_); 62 const int file_descriptor = dbus_watch_get_unix_fd(raw_watch_);
62 const int flags = dbus_watch_get_flags(raw_watch_); 63 const int flags = dbus_watch_get_flags(raw_watch_);
63 64
64 base::MessageLoopForIO::Mode mode = base::MessageLoopForIO::WATCH_READ; 65 // Using base::Unretained(this) is safe because watches are automatically
65 if ((flags & DBUS_WATCH_READABLE) && (flags & DBUS_WATCH_WRITABLE)) 66 // canceled when |read_watcher_| and |write_watcher_| are destroyed.
66 mode = base::MessageLoopForIO::WATCH_READ_WRITE; 67 if (flags & DBUS_WATCH_READABLE) {
67 else if (flags & DBUS_WATCH_READABLE) 68 read_watcher_ = base::FileDescriptorWatcher::WatchReadable(
68 mode = base::MessageLoopForIO::WATCH_READ; 69 file_descriptor, base::Bind(&Watch::OnFileCanReadWithoutBlocking,
69 else if (flags & DBUS_WATCH_WRITABLE) 70 base::Unretained(this)));
70 mode = base::MessageLoopForIO::WATCH_WRITE; 71 }
71 else 72 if (flags & DBUS_WATCH_WRITABLE) {
72 NOTREACHED(); 73 write_watcher_ = base::FileDescriptorWatcher::WatchWritable(
73 74 file_descriptor, base::Bind(&Watch::OnFileCanWriteWithoutBlocking,
74 const bool persistent = true; // Watch persistently. 75 base::Unretained(this)));
75 const bool success = base::MessageLoopForIO::current()->WatchFileDescriptor( 76 }
76 file_descriptor, persistent, mode, &file_descriptor_watcher_, this);
77 CHECK(success) << "Unable to allocate memory";
78 } 77 }
79 78
80 // Stops watching the underlying file descriptor. 79 // Stops watching the underlying file descriptor.
81 void StopWatching() { 80 void StopWatching() {
82 file_descriptor_watcher_.StopWatchingFileDescriptor(); 81 read_watcher_.reset();
82 write_watcher_.reset();
83 } 83 }
84 84
85 private: 85 private:
86 // Implement MessagePumpLibevent::Watcher. 86 void OnFileCanReadWithoutBlocking() {
Daniel Erat 2017/04/11 16:36:19 i think you could simplify this further by just ha
fdoray 2017/04/11 16:52:02 Done.
87 void OnFileCanReadWithoutBlocking(int file_descriptor) override {
88 const bool success = dbus_watch_handle(raw_watch_, DBUS_WATCH_READABLE); 87 const bool success = dbus_watch_handle(raw_watch_, DBUS_WATCH_READABLE);
89 CHECK(success) << "Unable to allocate memory"; 88 CHECK(success) << "Unable to allocate memory";
90 } 89 }
91 90
92 // Implement MessagePumpLibevent::Watcher. 91 void OnFileCanWriteWithoutBlocking() {
93 void OnFileCanWriteWithoutBlocking(int file_descriptor) override {
94 const bool success = dbus_watch_handle(raw_watch_, DBUS_WATCH_WRITABLE); 92 const bool success = dbus_watch_handle(raw_watch_, DBUS_WATCH_WRITABLE);
95 CHECK(success) << "Unable to allocate memory"; 93 CHECK(success) << "Unable to allocate memory";
96 } 94 }
97 95
98 DBusWatch* raw_watch_; 96 DBusWatch* raw_watch_;
99 base::MessagePumpLibevent::FileDescriptorWatcher file_descriptor_watcher_; 97 std::unique_ptr<base::FileDescriptorWatcher::Controller> read_watcher_;
98 std::unique_ptr<base::FileDescriptorWatcher::Controller> write_watcher_;
100 }; 99 };
101 100
102 // The class is used for monitoring the timeout used for D-Bus method 101 // The class is used for monitoring the timeout used for D-Bus method
103 // calls. 102 // calls.
104 // 103 //
105 // Unlike Watch, Timeout is a ref counted object, to ensure that |this| of 104 // Unlike Watch, Timeout is a ref counted object, to ensure that |this| of
106 // the object is is alive when HandleTimeout() is called. It's unlikely 105 // the object is is alive when HandleTimeout() is called. It's unlikely
107 // but it may be possible that HandleTimeout() is called after 106 // but it may be possible that HandleTimeout() is called after
108 // Bus::OnRemoveTimeout(). That's why we don't simply delete the object in 107 // Bus::OnRemoveTimeout(). That's why we don't simply delete the object in
109 // Bus::OnRemoveTimeout(). 108 // Bus::OnRemoveTimeout().
(...skipping 1099 matching lines...) Expand 10 before | Expand all | Expand 10 after
1209 kNameOwnerChangedSignal)) { 1208 kNameOwnerChangedSignal)) {
1210 Bus* self = static_cast<Bus*>(data); 1209 Bus* self = static_cast<Bus*>(data);
1211 self->OnServiceOwnerChanged(message); 1210 self->OnServiceOwnerChanged(message);
1212 } 1211 }
1213 // Always return unhandled to let others, e.g. ObjectProxies, handle the same 1212 // Always return unhandled to let others, e.g. ObjectProxies, handle the same
1214 // signal. 1213 // signal.
1215 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; 1214 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1216 } 1215 }
1217 1216
1218 } // namespace dbus 1217 } // namespace dbus
OLDNEW
« no previous file with comments | « no previous file | dbus/bus_unittest.cc » ('j') | dbus/object_proxy_unittest.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698