OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2011 The Chromium OS 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. | |
4 | |
5 #ifndef _VPN_MANAGER_SERVICE_MANAGER_H_ | |
6 #define _VPN_MANAGER_SERVICE_MANAGER_H_ | |
7 | |
8 #include <string> | |
9 | |
10 #include "base/file_path.h" | |
11 #include "gtest/gtest_prod.h" // for FRIEND_TEST | |
12 | |
13 // Generic code to manage setting up and stopping a set of layered | |
14 // tunnel services. This object contains the code to manage a single | |
15 // layer. Services are meant to be started from outermost to innermost. | |
16 // Services are meant to be stopped from the innermost out. To | |
17 // stop the entire set of services, call Stop on the innermost. | |
18 // Services go from not-yet-started to started to in_running to | |
19 // was_stopped. | |
20 class ServiceManager { | |
21 public: | |
22 ServiceManager(const std::string& service_name); | |
23 virtual ~ServiceManager(); | |
24 | |
25 // Initialize directories used by services. | |
26 static void InitializeDirectories(); | |
27 | |
28 // Call to initiate this service. If starting fails immediately this | |
29 // returns false. If something fails after this returns, OnStopped | |
30 // will be called. Code outside of the service manager stack | |
31 // must only call Start on the outermost function. | |
32 virtual bool Start() = 0; | |
33 | |
34 // Callback when this service has successfully started. | |
35 virtual void OnStarted(); | |
36 | |
37 // Call to stop this service. Must not be called on a separate | |
38 // thread from Start(). Code outside of the service manager stack | |
39 // must only call Stop on the innermost service. It is ok to | |
40 // stop an already stopped service. | |
41 virtual void Stop() = 0; | |
42 | |
43 // Returns the maximum amount of time to wait before this call should be | |
44 // called again in milliseconds. | |
45 virtual int Poll() = 0; | |
46 | |
47 // Process output from child process. | |
48 virtual void ProcessOutput() = 0; | |
49 | |
50 // Returns if |pid| is a child process of this service. | |
51 virtual bool IsChild(pid_t pid) = 0; | |
52 | |
53 // Callback when this service has stopped after having started | |
54 // successfully. |was_error| indicates if an error occurred. | |
55 virtual void OnStopped(bool was_error); | |
56 | |
57 // Queries if this service is currently running. | |
58 bool is_running() { | |
59 return is_running_; | |
60 } | |
61 | |
62 // Queries if this service was once running and is now stopped. | |
63 bool was_stopped() { | |
64 return was_stopped_; | |
65 } | |
66 | |
67 // Set up layering between two service managers |outer| and |inner|. | |
68 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
| |
69 ServiceManager* inner) { | |
70 outer->inner_service_ = inner; | |
71 inner->outer_service_ = outer; | |
72 } | |
73 | |
74 const std::string& service_name() { | |
75 return service_name_; | |
76 } | |
77 | |
78 // Repeat data from the given |fd| which is assumed to be ready | |
79 // and send it out to syslog, placing |prefix| before each line | |
80 // of output. | |
81 static void WriteFdToSyslog(int fd, const std::string& prefix); | |
82 | |
83 protected: | |
84 friend class IpsecManagerTest; | |
85 friend class ServiceManagerTest; | |
86 FRIEND_TEST(ServiceManagerTest, InitializeDirectories); | |
87 FRIEND_TEST(ServiceManagerTest, OnStoppedFromFailure); | |
88 FRIEND_TEST(ServiceManagerTest, OnStoppedFromSuccess); | |
89 | |
90 // Removes temporary directory. | |
91 static void DeleteTemp(); | |
92 | |
93 // Indicates if this service is currently running. | |
94 bool is_running_; | |
95 | |
96 // Indicates if this service was running and is now stopped. | |
97 bool was_stopped_; | |
98 | |
99 // Pointer to the next layer or NULL if innermost. | |
100 ServiceManager* inner_service_; | |
101 | |
102 // Pointer to the outer layer or NULL if outermost. | |
103 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
| |
104 | |
105 // Name of this service. | |
106 std::string service_name_; | |
107 | |
108 // Path to temporary directory on cryptohome. | |
109 static FilePath* temp_path_; | |
110 }; | |
111 | |
112 #endif // _VPN_MANAGER_SERVICE_MANAGER_H_ | |
OLD | NEW |