Home | History | Annotate | Download | only in process_proxy
      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.
      4 
      5 #ifndef CHROMEOS_PROCESS_PROXY_PROCESS_PROXY_REGISTRY_H_
      6 #define CHROMEOS_PROCESS_PROXY_PROCESS_PROXY_REGISTRY_H_
      7 
      8 #include <map>
      9 
     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"
     18 
     19 namespace chromeos {
     20 
     21 typedef base::Callback<void(pid_t, const std::string&, const std::string&)>
     22       ProcessOutputCallbackWithPid;
     23 
     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 {
     27  public:
     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;
     33     pid_t process_id;
     34 
     35     ProcessProxyInfo();
     36     // This is to make map::insert happy, we don't init anything.
     37     ProcessProxyInfo(const ProcessProxyInfo& other);
     38     ~ProcessProxyInfo();
     39   };
     40 
     41   static ProcessProxyRegistry* Get();
     42 
     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);
     52 
     53   // Currently used for testing.
     54   void SetOutputCallback(const ProcessOutputCallback& callback);
     55 
     56  private:
     57   friend struct ::base::DefaultLazyInstanceTraits<ProcessProxyRegistry>;
     58 
     59   ProcessProxyRegistry();
     60   ~ProcessProxyRegistry();
     61 
     62   // Gets called when output gets detected.
     63   void OnProcessOutput(pid_t pid,
     64                        ProcessOutputType type,
     65                        const std::string& data);
     66 
     67   // Map of all existing ProcessProxies.
     68   std::map<pid_t, ProcessProxyInfo> proxy_map_;
     69 
     70   DISALLOW_COPY_AND_ASSIGN(ProcessProxyRegistry);
     71 };
     72 
     73 }  // namespace chromeos
     74 
     75 #endif  // CHROMEOS_PROCESS_PROXY_PROCESS_PROXY_REGISTRY_H_
     76