Index: service_manager.h |
diff --git a/service_manager.h b/service_manager.h |
new file mode 100644 |
index 0000000000000000000000000000000000000000..68dad5776457198e205c2430002448b4cb747ad6 |
--- /dev/null |
+++ b/service_manager.h |
@@ -0,0 +1,112 @@ |
+// Copyright (c) 2011 The Chromium OS Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#ifndef _VPN_MANAGER_SERVICE_MANAGER_H_ |
+#define _VPN_MANAGER_SERVICE_MANAGER_H_ |
+ |
+#include <string> |
+ |
+#include "base/file_path.h" |
+#include "gtest/gtest_prod.h" // for FRIEND_TEST |
+ |
+// Generic code to manage setting up and stopping a set of layered |
+// tunnel services. This object contains the code to manage a single |
+// layer. Services are meant to be started from outermost to innermost. |
+// Services are meant to be stopped from the innermost out. To |
+// stop the entire set of services, call Stop on the innermost. |
+// Services go from not-yet-started to started to in_running to |
+// was_stopped. |
+class ServiceManager { |
+ public: |
+ ServiceManager(const std::string& service_name); |
+ virtual ~ServiceManager(); |
+ |
+ // Initialize directories used by services. |
+ static void InitializeDirectories(); |
+ |
+ // Call to initiate this service. If starting fails immediately this |
+ // returns false. If something fails after this returns, OnStopped |
+ // will be called. Code outside of the service manager stack |
+ // must only call Start on the outermost function. |
+ virtual bool Start() = 0; |
+ |
+ // Callback when this service has successfully started. |
+ virtual void OnStarted(); |
+ |
+ // Call to stop this service. Must not be called on a separate |
+ // thread from Start(). Code outside of the service manager stack |
+ // must only call Stop on the innermost service. It is ok to |
+ // stop an already stopped service. |
+ virtual void Stop() = 0; |
+ |
+ // Returns the maximum amount of time to wait before this call should be |
+ // called again in milliseconds. |
+ virtual int Poll() = 0; |
+ |
+ // Process output from child process. |
+ virtual void ProcessOutput() = 0; |
+ |
+ // Returns if |pid| is a child process of this service. |
+ virtual bool IsChild(pid_t pid) = 0; |
+ |
+ // Callback when this service has stopped after having started |
+ // successfully. |was_error| indicates if an error occurred. |
+ virtual void OnStopped(bool was_error); |
+ |
+ // Queries if this service is currently running. |
+ bool is_running() { |
+ return is_running_; |
+ } |
+ |
+ // Queries if this service was once running and is now stopped. |
+ bool was_stopped() { |
+ return was_stopped_; |
+ } |
+ |
+ // Set up layering between two service managers |outer| and |inner|. |
+ static void SetLayerOrder(ServiceManager* outer, |
Will Drewry
2011/03/05 04:06:39
So is this currently limited to two managers for a
kmixter1
2011/03/11 01:34:27
No, it's not limited to two. This would work:
Ser
|
+ ServiceManager* inner) { |
+ outer->inner_service_ = inner; |
+ inner->outer_service_ = outer; |
+ } |
+ |
+ const std::string& service_name() { |
+ return service_name_; |
+ } |
+ |
+ // Repeat data from the given |fd| which is assumed to be ready |
+ // and send it out to syslog, placing |prefix| before each line |
+ // of output. |
+ static void WriteFdToSyslog(int fd, const std::string& prefix); |
+ |
+ protected: |
+ friend class IpsecManagerTest; |
+ friend class ServiceManagerTest; |
+ FRIEND_TEST(ServiceManagerTest, InitializeDirectories); |
+ FRIEND_TEST(ServiceManagerTest, OnStoppedFromFailure); |
+ FRIEND_TEST(ServiceManagerTest, OnStoppedFromSuccess); |
+ |
+ // Removes temporary directory. |
+ static void DeleteTemp(); |
+ |
+ // Indicates if this service is currently running. |
+ bool is_running_; |
+ |
+ // Indicates if this service was running and is now stopped. |
+ bool was_stopped_; |
+ |
+ // Pointer to the next layer or NULL if innermost. |
+ ServiceManager* inner_service_; |
+ |
+ // Pointer to the outer layer or NULL if outermost. |
+ ServiceManager* outer_service_; |
Will Drewry
2011/03/05 04:06:39
nit: Is there any reason these are protected inste
kmixter1
2011/03/11 01:34:27
I don't think I've ever seen that one before. Don
|
+ |
+ // Name of this service. |
+ std::string service_name_; |
+ |
+ // Path to temporary directory on cryptohome. |
+ static FilePath* temp_path_; |
+}; |
+ |
+#endif // _VPN_MANAGER_SERVICE_MANAGER_H_ |