1 // Copyright (c) 2012 The Chromium 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.
5 #ifndef CHROMEOS_PROCESS_PROXY_PROCESS_PROXY_REGISTRY_H_
6 #define CHROMEOS_PROCESS_PROXY_PROCESS_PROXY_REGISTRY_H_
10 #include "base/callback.h"
11 #include "base/lazy_instance.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/memory/ref_counted.h"
14 #include "base/threading/non_thread_safe.h"
15 #include "base/threading/thread.h"
16 #include "chromeos/chromeos_export.h"
17 #include "chromeos/process_proxy/process_proxy.h"
21 typedef base::Callback
<void(pid_t
, const std::string
&, const std::string
&)>
22 ProcessOutputCallbackWithPid
;
24 // Keeps track of all created ProcessProxies. It is created lazily and should
25 // live on a single thread (where all methods must be called).
26 class CHROMEOS_EXPORT ProcessProxyRegistry
: public base::NonThreadSafe
{
28 // Info we need about a ProcessProxy instance.
29 struct ProcessProxyInfo
{
30 scoped_refptr
<ProcessProxy
> proxy
;
31 scoped_ptr
<base::Thread
> watcher_thread
;
32 ProcessOutputCallbackWithPid callback
;
36 // This is to make map::insert happy, we don't init anything.
37 ProcessProxyInfo(const ProcessProxyInfo
& other
);
41 static ProcessProxyRegistry
* Get();
43 // Starts new ProcessProxy (which starts new process).
44 bool OpenProcess(const std::string
& command
, pid_t
* pid
,
45 const ProcessOutputCallbackWithPid
& callback
);
46 // Sends data to the process with id |pid|.
47 bool SendInput(pid_t pid
, const std::string
& data
);
48 // Stops the process with id |pid|.
49 bool CloseProcess(pid_t pid
);
50 // Reports terminal resize to process proxy.
51 bool OnTerminalResize(pid_t pid
, int width
, int height
);
53 // Currently used for testing.
54 void SetOutputCallback(const ProcessOutputCallback
& callback
);
57 friend struct ::base::DefaultLazyInstanceTraits
<ProcessProxyRegistry
>;
59 ProcessProxyRegistry();
60 ~ProcessProxyRegistry();
62 // Gets called when output gets detected.
63 void OnProcessOutput(pid_t pid
,
64 ProcessOutputType type
,
65 const std::string
& data
);
67 // Map of all existing ProcessProxies.
68 std::map
<pid_t
, ProcessProxyInfo
> proxy_map_
;
70 DISALLOW_COPY_AND_ASSIGN(ProcessProxyRegistry
);
73 } // namespace chromeos
75 #endif // CHROMEOS_PROCESS_PROXY_PROCESS_PROXY_REGISTRY_H_