• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 // Copyright (C) 2013 The Android Open Source Project
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //      http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16 
17 #ifndef SHILL_EXTERNAL_TASK_H_
18 #define SHILL_EXTERNAL_TASK_H_
19 
20 #include <sys/types.h>
21 
22 #include <map>
23 #include <memory>
24 #include <string>
25 #include <vector>
26 
27 #include <base/callback.h>
28 #include <base/files/file_path.h>
29 #include <base/memory/weak_ptr.h>
30 #include <gtest/gtest_prod.h>  // for FRIEND_TEST
31 
32 #include "shill/rpc_task.h"
33 
34 namespace shill {
35 
36 class ControlInterface;
37 class Error;
38 class EventDispatcher;
39 class ProcessManager;
40 
41 class ExternalTask : public RPCTaskDelegate {
42  public:
43   ExternalTask(ControlInterface* control,
44                ProcessManager* process_manager,
45                const base::WeakPtr<RPCTaskDelegate>& task_delegate,
46                const base::Callback<void(pid_t, int)>& death_callback);
47   ~ExternalTask() override;  // But consider DestroyLater...
48 
49   // Schedule later deletion of the ExternalTask. Useful when in the
50   // middle of an ExternalTask callback. Note that the caller _must_
51   // release ownership of |this|. For example:
52   //
53   //   class Foo : public SupportsWeakPtr<Foo>, public RPCTaskDelegate {
54   //    public:
55   //      Foo() {
56   //        task_.reset(new ExternalTask(...));
57   //      }
58   //
59   //      void Notify(...) {
60   //        task_.release()->DestroyLater(...);  // Passes ownership.
61   //      }
62   //
63   //    private:
64   //      std::unique_ptr<ExternalTask> task_;
65   //   }
66   void DestroyLater(EventDispatcher* dispatcher);
67 
68   // Forks off a process to run |program|, with the command-line
69   // arguments |arguments|, and the environment variables specified in
70   // |environment|.
71   //
72   // If |terminate_with_parent| is true, the child process will be
73   // configured to terminate itself if this process dies. Otherwise,
74   // the child process will retain its default behavior.
75   //
76   // On success, returns true, and leaves |error| unmodified.
77   // On failure, returns false, and sets |error|.
78   //
79   // |environment| SHOULD NOT contain kRPCTaskServiceVariable or
80   // kRPCTaskPathVariable, as that may prevent the child process
81   // from communicating back to the ExternalTask.
82   virtual bool Start(const base::FilePath& program,
83                      const std::vector<std::string>& arguments,
84                      const std::map<std::string, std::string>& environment,
85                      bool terminate_with_parent,
86                      Error* error);
87   virtual void Stop();
88 
89  private:
90   friend class ExternalTaskTest;
91   FRIEND_TEST(ExternalTaskTest, Destructor);
92   FRIEND_TEST(ExternalTaskTest, GetLogin);
93   FRIEND_TEST(ExternalTaskTest, Notify);
94   FRIEND_TEST(ExternalTaskTest, OnTaskDied);
95   FRIEND_TEST(ExternalTaskTest, Start);
96   FRIEND_TEST(ExternalTaskTest, Stop);
97   FRIEND_TEST(ExternalTaskTest, StopNotStarted);
98 
99   // Implements RPCTaskDelegate.
100   void GetLogin(std::string* user, std::string* password) override;
101   void Notify(
102       const std::string& event,
103       const std::map<std::string, std::string>& details) override;
104 
105   // Called when the external process exits.
106   void OnTaskDied(int exit_status);
107 
108   static void Destroy(ExternalTask* task);
109 
110   ControlInterface* control_;
111   ProcessManager* process_manager_;
112 
113   std::unique_ptr<RPCTask> rpc_task_;
114   base::WeakPtr<RPCTaskDelegate> task_delegate_;
115   base::Callback<void(pid_t, int)> death_callback_;
116 
117   // The PID of the spawned process. May be 0 if no process has been
118   // spawned yet or the process has died.
119   pid_t pid_;
120 
121   DISALLOW_COPY_AND_ASSIGN(ExternalTask);
122 };
123 
124 }  // namespace shill
125 
126 #endif  // SHILL_EXTERNAL_TASK_H_
127