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_
11 #include "base/callback.h"
12 #include "base/lazy_instance.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/memory/ref_counted.h"
15 #include "base/threading/non_thread_safe.h"
16 #include "base/threading/thread.h"
17 #include "chromeos/chromeos_export.h"
18 #include "chromeos/process_proxy/process_proxy.h"
22 typedef base::Callback
<void(pid_t
, const std::string
&, const std::string
&)>
23 ProcessOutputCallbackWithPid
;
25 // Keeps track of all created ProcessProxies. It is created lazily and should
26 // live on a single thread (where all methods must be called).
27 class CHROMEOS_EXPORT ProcessProxyRegistry
: public base::NonThreadSafe
{
29 // Info we need about a ProcessProxy instance.
30 struct ProcessProxyInfo
{
31 scoped_refptr
<ProcessProxy
> proxy
;
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 // Shuts down registry, closing all associated processed.
56 // Currently used for testing.
57 void SetOutputCallback(const ProcessOutputCallback
& callback
);
60 friend struct ::base::DefaultLazyInstanceTraits
<ProcessProxyRegistry
>;
62 ProcessProxyRegistry();
63 ~ProcessProxyRegistry();
65 // Gets called when output gets detected.
66 void OnProcessOutput(pid_t pid
,
67 ProcessOutputType type
,
68 const std::string
& data
);
70 bool EnsureWatcherThreadStarted();
72 // Map of all existing ProcessProxies.
73 std::map
<pid_t
, ProcessProxyInfo
> proxy_map_
;
75 scoped_ptr
<base::Thread
> watcher_thread_
;
77 DISALLOW_COPY_AND_ASSIGN(ProcessProxyRegistry
);
80 } // namespace chromeos
82 #endif // CHROMEOS_PROCESS_PROXY_PROCESS_PROXY_REGISTRY_H_