Chromium Code Reviews| 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 class ScopedTempDir; | |
| 14 | |
| 15 // Generic code to manage setting up and stopping a set of layered | |
| 16 // tunnel services. This object contains the code to manage a single | |
| 17 // layer. Services are meant to be started from outermost to innermost. | |
| 18 // Services are meant to be stopped from the innermost out. To | |
| 19 // stop the entire set of services, call Stop on the innermost. | |
| 20 // Services go from not-yet-started to started to in_running to | |
| 21 // was_stopped. | |
| 22 class ServiceManager { | |
| 23 public: | |
| 24 ServiceManager(const std::string& service_name); | |
| 25 virtual ~ServiceManager(); | |
| 26 | |
| 27 // Initialize directories used by services. |scoped_temp_dir| will | |
| 28 // be set to manage an appropriate temp directory. This function | |
| 29 // uses a reference to |scoped_temp_dir| and so its lifetime must be | |
| 30 // equal to that of all objects derived from ServiceManager. | |
| 31 static void InitializeDirectories(ScopedTempDir* scoped_temp_path); | |
| 32 | |
| 33 // Call to initiate this service. If starting fails immediately this | |
| 34 // returns false. If something fails after this returns, OnStopped | |
| 35 // will be called. Code outside of the service manager stack | |
| 36 // must only call Start on the outermost function. | |
| 37 virtual bool Start() = 0; | |
| 38 | |
| 39 // Callback when this service has successfully started. | |
| 40 virtual void OnStarted(); | |
| 41 | |
| 42 // Call to stop this service. Must not be called on a separate | |
| 43 // thread from Start(). Code outside of the service manager stack | |
| 44 // must only call Stop on the innermost service. It is ok to | |
| 45 // stop an already stopped service. | |
| 46 virtual void Stop() = 0; | |
| 47 | |
| 48 // Returns the maximum amount of time to wait before this call should be | |
| 49 // called again in milliseconds. | |
| 50 virtual int Poll() = 0; | |
| 51 | |
| 52 // Process output from child process. | |
| 53 virtual void ProcessOutput() = 0; | |
| 54 | |
| 55 // Returns if |pid| is a child process of this service. | |
| 56 virtual bool IsChild(pid_t pid) = 0; | |
| 57 | |
| 58 // Callback when this service has stopped after having started | |
| 59 // successfully. |was_error| indicates if an error occurred. | |
| 60 virtual void OnStopped(bool was_error); | |
| 61 | |
| 62 // Queries if this service is currently running. | |
| 63 bool is_running() { | |
| 64 return is_running_; | |
| 65 } | |
| 66 | |
| 67 // Queries if this service was once running and is now stopped. | |
| 68 bool was_stopped() { | |
| 69 return was_stopped_; | |
| 70 } | |
| 71 | |
| 72 // Set up layering between two service managers |outer| and |inner|. | |
| 73 // This function may be called multiple times to chain servics together, | |
| 74 // for instance: | |
| 75 // ServiceManager::SetLayerOrder(&turkey, &duck); | |
| 76 // ServiceManager::SetLayerOrder(&duck, &chicken); | |
| 77 static void SetLayerOrder(ServiceManager* outer, | |
| 78 ServiceManager* inner) { | |
| 79 outer->inner_service_ = inner; | |
| 80 inner->outer_service_ = outer; | |
| 81 } | |
| 82 | |
| 83 const std::string& service_name() { | |
| 84 return service_name_; | |
| 85 } | |
| 86 | |
| 87 // Repeat data from the given |fd| which is assumed to be ready | |
| 88 // and send it out to syslog, placing |prefix| before each line | |
| 89 // of output. | |
|
petkov
2011/03/11 19:12:53
The amount of data repeat per call is undefined? D
kmixter1
2011/03/11 20:53:04
Done.
| |
| 90 static void WriteFdToSyslog(int fd, const std::string& prefix, | |
| 91 std::string* partial_line); | |
| 92 | |
| 93 protected: | |
| 94 friend class IpsecManagerTest; | |
| 95 friend class L2tpManagerTest; | |
| 96 friend class ServiceManagerTest; | |
| 97 FRIEND_TEST(L2tpManagerTest, PollNothingIfRunning); | |
| 98 FRIEND_TEST(IpsecManagerTest, PollNothingIfRunning); | |
| 99 FRIEND_TEST(ServiceManagerTest, InitializeDirectories); | |
| 100 FRIEND_TEST(ServiceManagerTest, OnStoppedFromFailure); | |
| 101 FRIEND_TEST(ServiceManagerTest, OnStoppedFromSuccess); | |
| 102 | |
| 103 ServiceManager* inner_service() { return inner_service_; } | |
| 104 | |
| 105 ServiceManager* outer_service() { return outer_service_; } | |
| 106 | |
| 107 static const FilePath* temp_path() { return temp_path_; } | |
| 108 | |
| 109 private: | |
| 110 // Indicates if this service is currently running. | |
| 111 bool is_running_; | |
| 112 | |
| 113 // Indicates if this service was running and is now stopped. | |
| 114 bool was_stopped_; | |
| 115 | |
| 116 // Pointer to the next layer or NULL if innermost. | |
| 117 ServiceManager* inner_service_; | |
| 118 | |
| 119 // Pointer to the outer layer or NULL if outermost. | |
| 120 ServiceManager* outer_service_; | |
| 121 | |
| 122 // Name of this service. | |
| 123 std::string service_name_; | |
| 124 | |
| 125 // Path to temporary directory on cryptohome. | |
| 126 static const FilePath* temp_path_; | |
| 127 | |
| 128 // Path to base directory of temporary directory on cryptohome. | |
| 129 static const char* temp_base_path_; | |
| 130 }; | |
| 131 | |
| 132 #endif // _VPN_MANAGER_SERVICE_MANAGER_H_ | |
| OLD | NEW |