mukesh agrawal | ae30e9e | 2013-05-28 14:09:16 -0700 | [diff] [blame] | 1 | // Copyright (c) 2013 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 SHILL_EXTERNAL_TASK_H_ |
| 6 | #define SHILL_EXTERNAL_TASK_H_ |
| 7 | |
| 8 | #include <sys/types.h> |
| 9 | |
| 10 | #include <map> |
Ben Chan | cd47732 | 2014-10-17 14:19:30 -0700 | [diff] [blame] | 11 | #include <memory> |
mukesh agrawal | ae30e9e | 2013-05-28 14:09:16 -0700 | [diff] [blame] | 12 | #include <string> |
| 13 | #include <vector> |
| 14 | |
| 15 | #include <base/callback.h> |
Ben Chan | a0ddf46 | 2014-02-06 11:32:42 -0800 | [diff] [blame] | 16 | #include <base/files/file_path.h> |
mukesh agrawal | ae30e9e | 2013-05-28 14:09:16 -0700 | [diff] [blame] | 17 | #include <base/memory/weak_ptr.h> |
| 18 | #include <gtest/gtest_prod.h> // for FRIEND_TEST |
| 19 | |
mukesh agrawal | ae30e9e | 2013-05-28 14:09:16 -0700 | [diff] [blame] | 20 | #include "shill/rpc_task.h" |
| 21 | |
| 22 | namespace shill { |
| 23 | |
| 24 | class ControlInterface; |
| 25 | class Error; |
mukesh agrawal | 9da0777 | 2013-05-15 14:15:17 -0700 | [diff] [blame] | 26 | class EventDispatcher; |
Peter Qiu | a24480a | 2015-08-11 23:09:54 -0700 | [diff] [blame] | 27 | class ProcessManager; |
mukesh agrawal | ae30e9e | 2013-05-28 14:09:16 -0700 | [diff] [blame] | 28 | |
| 29 | class ExternalTask : public RPCTaskDelegate { |
| 30 | public: |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 31 | ExternalTask(ControlInterface* control, |
Peter Qiu | a24480a | 2015-08-11 23:09:54 -0700 | [diff] [blame] | 32 | ProcessManager* process_manager, |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 33 | const base::WeakPtr<RPCTaskDelegate>& task_delegate, |
| 34 | const base::Callback<void(pid_t, int)>& death_callback); |
Ben Chan | 5ea763b | 2014-08-13 11:07:54 -0700 | [diff] [blame] | 35 | ~ExternalTask() override; // But consider DestroyLater... |
mukesh agrawal | 9da0777 | 2013-05-15 14:15:17 -0700 | [diff] [blame] | 36 | |
| 37 | // Schedule later deletion of the ExternalTask. Useful when in the |
| 38 | // middle of an ExternalTask callback. Note that the caller _must_ |
| 39 | // release ownership of |this|. For example: |
| 40 | // |
| 41 | // class Foo : public SupportsWeakPtr<Foo>, public RPCTaskDelegate { |
| 42 | // public: |
| 43 | // Foo() { |
| 44 | // task_.reset(new ExternalTask(...)); |
| 45 | // } |
| 46 | // |
| 47 | // void Notify(...) { |
| 48 | // task_.release()->DestroyLater(...); // Passes ownership. |
| 49 | // } |
| 50 | // |
| 51 | // private: |
Ben Chan | cd47732 | 2014-10-17 14:19:30 -0700 | [diff] [blame] | 52 | // std::unique_ptr<ExternalTask> task_; |
mukesh agrawal | 9da0777 | 2013-05-15 14:15:17 -0700 | [diff] [blame] | 53 | // } |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 54 | void DestroyLater(EventDispatcher* dispatcher); |
mukesh agrawal | ae30e9e | 2013-05-28 14:09:16 -0700 | [diff] [blame] | 55 | |
| 56 | // Forks off a process to run |program|, with the command-line |
| 57 | // arguments |arguments|, and the environment variables specified in |
| 58 | // |environment|. |
| 59 | // |
mukesh agrawal | c4f9aa0 | 2013-08-15 19:23:13 -0700 | [diff] [blame] | 60 | // If |terminate_with_parent| is true, the child process will be |
| 61 | // configured to terminate itself if this process dies. Otherwise, |
| 62 | // the child process will retain its default behavior. |
| 63 | // |
mukesh agrawal | ae30e9e | 2013-05-28 14:09:16 -0700 | [diff] [blame] | 64 | // On success, returns true, and leaves |error| unmodified. |
| 65 | // On failure, returns false, and sets |error|. |
| 66 | // |
| 67 | // |environment| SHOULD NOT contain kRPCTaskServiceVariable or |
| 68 | // kRPCTaskPathVariable, as that may prevent the child process |
| 69 | // from communicating back to the ExternalTask. |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 70 | virtual bool Start(const base::FilePath& program, |
| 71 | const std::vector<std::string>& arguments, |
| 72 | const std::map<std::string, std::string>& environment, |
mukesh agrawal | c4f9aa0 | 2013-08-15 19:23:13 -0700 | [diff] [blame] | 73 | bool terminate_with_parent, |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 74 | Error* error); |
mukesh agrawal | ae30e9e | 2013-05-28 14:09:16 -0700 | [diff] [blame] | 75 | virtual void Stop(); |
| 76 | |
| 77 | private: |
| 78 | friend class ExternalTaskTest; |
| 79 | FRIEND_TEST(ExternalTaskTest, Destructor); |
| 80 | FRIEND_TEST(ExternalTaskTest, GetLogin); |
| 81 | FRIEND_TEST(ExternalTaskTest, Notify); |
| 82 | FRIEND_TEST(ExternalTaskTest, OnTaskDied); |
| 83 | FRIEND_TEST(ExternalTaskTest, Start); |
| 84 | FRIEND_TEST(ExternalTaskTest, Stop); |
| 85 | FRIEND_TEST(ExternalTaskTest, StopNotStarted); |
| 86 | |
| 87 | // Implements RPCTaskDelegate. |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 88 | void GetLogin(std::string* user, std::string* password) override; |
Alex Vakulenko | 016fa0e | 2014-08-11 15:59:58 -0700 | [diff] [blame] | 89 | void Notify( |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 90 | const std::string& event, |
| 91 | const std::map<std::string, std::string>& details) override; |
Peter Qiu | a24480a | 2015-08-11 23:09:54 -0700 | [diff] [blame] | 92 | |
mukesh agrawal | ae30e9e | 2013-05-28 14:09:16 -0700 | [diff] [blame] | 93 | // Called when the external process exits. |
Peter Qiu | a24480a | 2015-08-11 23:09:54 -0700 | [diff] [blame] | 94 | void OnTaskDied(int exit_status); |
mukesh agrawal | ae30e9e | 2013-05-28 14:09:16 -0700 | [diff] [blame] | 95 | |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 96 | static void Destroy(ExternalTask* task); |
mukesh agrawal | 9da0777 | 2013-05-15 14:15:17 -0700 | [diff] [blame] | 97 | |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 98 | ControlInterface* control_; |
Peter Qiu | a24480a | 2015-08-11 23:09:54 -0700 | [diff] [blame] | 99 | ProcessManager* process_manager_; |
mukesh agrawal | ae30e9e | 2013-05-28 14:09:16 -0700 | [diff] [blame] | 100 | |
Ben Chan | cd47732 | 2014-10-17 14:19:30 -0700 | [diff] [blame] | 101 | std::unique_ptr<RPCTask> rpc_task_; |
mukesh agrawal | ae30e9e | 2013-05-28 14:09:16 -0700 | [diff] [blame] | 102 | base::WeakPtr<RPCTaskDelegate> task_delegate_; |
| 103 | base::Callback<void(pid_t, int)> death_callback_; |
| 104 | |
| 105 | // The PID of the spawned process. May be 0 if no process has been |
| 106 | // spawned yet or the process has died. |
| 107 | pid_t pid_; |
| 108 | |
mukesh agrawal | ae30e9e | 2013-05-28 14:09:16 -0700 | [diff] [blame] | 109 | DISALLOW_COPY_AND_ASSIGN(ExternalTask); |
| 110 | }; |
| 111 | |
| 112 | } // namespace shill |
| 113 | |
| 114 | #endif // SHILL_EXTERNAL_TASK_H_ |