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 <signal.h> | 5 #include <signal.h> |
6 #include <stdlib.h> | 6 #include <stdlib.h> |
7 | 7 |
8 #include <iostream> | 8 #include <iostream> |
9 #include <string> | 9 #include <string> |
10 | 10 |
(...skipping 30 matching lines...) Expand all Loading... | |
41 // Lets the daemon fetch the exit notifier file descriptor. | 41 // Lets the daemon fetch the exit notifier file descriptor. |
42 int GetExitNotifierFD() { | 42 int GetExitNotifierFD() { |
43 DCHECK(g_notifier); | 43 DCHECK(g_notifier); |
44 return g_notifier->receiver_fd(); | 44 return g_notifier->receiver_fd(); |
45 } | 45 } |
46 | 46 |
47 class ServerDelegate : public Daemon::ServerDelegate { | 47 class ServerDelegate : public Daemon::ServerDelegate { |
48 public: | 48 public: |
49 ServerDelegate() : initialized_(false) {} | 49 ServerDelegate() : initialized_(false) {} |
50 | 50 |
51 virtual ~ServerDelegate() { | |
52 if (!controller_thread_.get()) | |
53 return; | |
54 // The DeviceController instance, if any, is constructed on the controller | |
55 // thread. Make sure that it gets deleted on that same thread. Note that | |
56 // DeleteSoon() is not used here since it would imply reading |controller_| | |
57 // from the main thread while it's set on the internal thread. | |
58 controller_thread_->message_loop_proxy()->PostTask( | |
Philippe
2013/07/22 15:16:14
FYI, I also added this in the last patch set.
| |
59 FROM_HERE, | |
60 base::Bind(&ServerDelegate::DeleteControllerOnInternalThread, | |
61 base::Unretained(this))); | |
62 } | |
63 | |
64 void DeleteControllerOnInternalThread() { | |
65 DCHECK( | |
66 controller_thread_->message_loop_proxy()->RunsTasksOnCurrentThread()); | |
67 controller_.reset(); | |
68 } | |
69 | |
51 // Daemon::ServerDelegate: | 70 // Daemon::ServerDelegate: |
52 virtual void Init() OVERRIDE { | 71 virtual void Init() OVERRIDE { |
53 DCHECK(!g_notifier); | 72 DCHECK(!g_notifier); |
54 g_notifier = new forwarder2::PipeNotifier(); | 73 g_notifier = new forwarder2::PipeNotifier(); |
55 signal(SIGTERM, KillHandler); | 74 signal(SIGTERM, KillHandler); |
56 signal(SIGINT, KillHandler); | 75 signal(SIGINT, KillHandler); |
57 controller_thread_.reset(new base::Thread("controller_thread")); | 76 controller_thread_.reset(new base::Thread("controller_thread")); |
58 controller_thread_->Start(); | 77 controller_thread_->Start(); |
59 } | 78 } |
60 | 79 |
61 virtual void OnClientConnected(scoped_ptr<Socket> client_socket) OVERRIDE { | 80 virtual void OnClientConnected(scoped_ptr<Socket> client_socket) OVERRIDE { |
62 if (initialized_) { | 81 if (initialized_) { |
63 client_socket->WriteString("OK"); | 82 client_socket->WriteString("OK"); |
64 return; | 83 return; |
65 } | 84 } |
66 controller_thread_->message_loop()->PostTask( | 85 controller_thread_->message_loop()->PostTask( |
67 FROM_HERE, | 86 FROM_HERE, |
68 base::Bind(&ServerDelegate::StartController, GetExitNotifierFD(), | 87 base::Bind(&ServerDelegate::StartController, base::Unretained(this), |
69 base::Passed(&client_socket))); | 88 GetExitNotifierFD(), base::Passed(&client_socket))); |
70 initialized_ = true; | 89 initialized_ = true; |
71 } | 90 } |
72 | 91 |
73 private: | 92 private: |
74 static void StartController(int exit_notifier_fd, | 93 void StartController(int exit_notifier_fd, scoped_ptr<Socket> client_socket) { |
75 scoped_ptr<Socket> client_socket) { | 94 DCHECK(!controller_.get()); |
76 forwarder2::DeviceController controller(exit_notifier_fd); | 95 scoped_ptr<DeviceController> controller( |
77 if (!controller.Init(kUnixDomainSocketPath)) { | 96 DeviceController::Create(kUnixDomainSocketPath, exit_notifier_fd)); |
97 if (!controller.get()) { | |
78 client_socket->WriteString( | 98 client_socket->WriteString( |
79 base::StringPrintf("ERROR: Could not initialize device controller " | 99 base::StringPrintf("ERROR: Could not initialize device controller " |
80 "with ADB socket path: %s", | 100 "with ADB socket path: %s", |
81 kUnixDomainSocketPath)); | 101 kUnixDomainSocketPath)); |
82 return; | 102 return; |
83 } | 103 } |
104 controller_.swap(controller); | |
105 controller_->Start(); | |
84 client_socket->WriteString("OK"); | 106 client_socket->WriteString("OK"); |
85 client_socket->Close(); | 107 client_socket->Close(); |
86 // Note that the following call is blocking which explains why the device | |
87 // controller has to live on a separate thread (so that the daemon command | |
88 // server is not blocked). | |
89 controller.Start(); | |
90 } | 108 } |
91 | 109 |
92 base::AtExitManager at_exit_manager_; // Used by base::Thread. | 110 scoped_ptr<DeviceController> controller_; |
93 scoped_ptr<base::Thread> controller_thread_; | 111 scoped_ptr<base::Thread> controller_thread_; |
94 bool initialized_; | 112 bool initialized_; |
95 }; | 113 }; |
96 | 114 |
97 class ClientDelegate : public Daemon::ClientDelegate { | 115 class ClientDelegate : public Daemon::ClientDelegate { |
98 public: | 116 public: |
99 ClientDelegate() : has_failed_(false) {} | 117 ClientDelegate() : has_failed_(false) {} |
100 | 118 |
101 bool has_failed() const { return has_failed_; } | 119 bool has_failed() const { return has_failed_; } |
102 | 120 |
(...skipping 18 matching lines...) Expand all Loading... | |
121 }; | 139 }; |
122 | 140 |
123 int RunDeviceForwarder(int argc, char** argv) { | 141 int RunDeviceForwarder(int argc, char** argv) { |
124 CommandLine::Init(argc, argv); // Needed by logging. | 142 CommandLine::Init(argc, argv); // Needed by logging. |
125 const bool kill_server = CommandLine::ForCurrentProcess()->HasSwitch( | 143 const bool kill_server = CommandLine::ForCurrentProcess()->HasSwitch( |
126 "kill-server"); | 144 "kill-server"); |
127 if ((kill_server && argc != 2) || (!kill_server && argc != 1)) { | 145 if ((kill_server && argc != 2) || (!kill_server && argc != 1)) { |
128 std::cerr << "Usage: device_forwarder [--kill-server]" << std::endl; | 146 std::cerr << "Usage: device_forwarder [--kill-server]" << std::endl; |
129 return 1; | 147 return 1; |
130 } | 148 } |
149 base::AtExitManager at_exit_manager; // Used by base::Thread. | |
131 ClientDelegate client_delegate; | 150 ClientDelegate client_delegate; |
132 ServerDelegate daemon_delegate; | 151 ServerDelegate daemon_delegate; |
133 const char kLogFilePath[] = ""; // Log to logcat. | 152 const char kLogFilePath[] = ""; // Log to logcat. |
134 Daemon daemon(kLogFilePath, kDaemonIdentifier, &client_delegate, | 153 Daemon daemon(kLogFilePath, kDaemonIdentifier, &client_delegate, |
135 &daemon_delegate, &GetExitNotifierFD); | 154 &daemon_delegate, &GetExitNotifierFD); |
136 | 155 |
137 if (kill_server) | 156 if (kill_server) |
138 return !daemon.Kill(); | 157 return !daemon.Kill(); |
139 | 158 |
140 if (!daemon.SpawnIfNeeded()) | 159 if (!daemon.SpawnIfNeeded()) |
141 return 1; | 160 return 1; |
142 return client_delegate.has_failed(); | 161 return client_delegate.has_failed(); |
143 } | 162 } |
144 | 163 |
145 } // namespace | 164 } // namespace |
146 } // namespace forwarder2 | 165 } // namespace forwarder2 |
147 | 166 |
148 int main(int argc, char** argv) { | 167 int main(int argc, char** argv) { |
149 return forwarder2::RunDeviceForwarder(argc, argv); | 168 return forwarder2::RunDeviceForwarder(argc, argv); |
150 } | 169 } |
OLD | NEW |