process_proxy_registry.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. #ifndef CHROMEOS_PROCESS_PROXY_PROCESS_PROXY_REGISTRY_H_
  5. #define CHROMEOS_PROCESS_PROXY_PROCESS_PROXY_REGISTRY_H_
  6. #include <map>
  7. #include <memory>
  8. #include <string>
  9. #include "base/callback.h"
  10. #include "base/command_line.h"
  11. #include "base/component_export.h"
  12. #include "base/lazy_instance.h"
  13. #include "base/memory/ref_counted.h"
  14. #include "base/sequence_checker.h"
  15. #include "base/task/sequenced_task_runner.h"
  16. #include "base/threading/thread.h"
  17. #include "chromeos/process_proxy/process_proxy.h"
  18. namespace chromeos {
  19. // Keeps track of all created ProcessProxies. It is created lazily and should
  20. // live on a single thread (where all methods must be called).
  21. class COMPONENT_EXPORT(CHROMEOS_PROCESS_PROXY) ProcessProxyRegistry {
  22. public:
  23. using OutputCallback =
  24. base::RepeatingCallback<void(const std::string& id,
  25. const std::string& output_type,
  26. const std::string& output_data)>;
  27. // Info we need about a ProcessProxy instance.
  28. struct ProcessProxyInfo {
  29. scoped_refptr<ProcessProxy> proxy;
  30. OutputCallback callback;
  31. ProcessProxyInfo();
  32. // This is to make map::insert happy, we don't init anything.
  33. ProcessProxyInfo(const ProcessProxyInfo& other);
  34. ~ProcessProxyInfo();
  35. };
  36. static ProcessProxyRegistry* Get();
  37. ProcessProxyRegistry(const ProcessProxyRegistry&) = delete;
  38. ProcessProxyRegistry& operator=(const ProcessProxyRegistry&) = delete;
  39. // Converts the id returned by OpenProcess() to the system pid.
  40. static int ConvertToSystemPID(const std::string& id);
  41. // Returns a SequencedTaskRunner where the singleton instance of
  42. // ProcessProxyRegistry lives.
  43. static scoped_refptr<base::SequencedTaskRunner> GetTaskRunner();
  44. // Starts new ProcessProxy (which starts new process).
  45. // Returns true if the process is created successfully, false otherwise.
  46. // The unique process id is passed back via |id|.
  47. bool OpenProcess(const base::CommandLine& cmdline,
  48. const std::string& user_id_hash,
  49. const OutputCallback& callback,
  50. std::string* id);
  51. // Sends data to the process identified by |id|.
  52. void SendInput(const std::string& id,
  53. const std::string& data,
  54. base::OnceCallback<void(bool)> callback);
  55. // Stops the process identified by |id|.
  56. bool CloseProcess(const std::string& id);
  57. // Reports terminal resize to process proxy.
  58. bool OnTerminalResize(const std::string& id, int width, int height);
  59. // Notifies process proxy identified by |id| that previously reported output
  60. // has been handled.
  61. void AckOutput(const std::string& id);
  62. // Shuts down registry, closing all associated processed.
  63. void ShutDown();
  64. // Get the process for testing purposes.
  65. const base::Process* GetProcessForTesting(const std::string& id);
  66. private:
  67. friend struct ::base::LazyInstanceTraitsBase<ProcessProxyRegistry>;
  68. ProcessProxyRegistry();
  69. ~ProcessProxyRegistry();
  70. // Gets called when output gets detected.
  71. void OnProcessOutput(const std::string& id,
  72. ProcessOutputType type,
  73. const std::string& data);
  74. bool EnsureWatcherThreadStarted();
  75. // Map of all existing ProcessProxies.
  76. std::map<std::string, ProcessProxyInfo> proxy_map_;
  77. std::unique_ptr<base::Thread> watcher_thread_;
  78. SEQUENCE_CHECKER(sequence_checker_);
  79. };
  80. } // namespace chromeos
  81. #endif // CHROMEOS_PROCESS_PROXY_PROCESS_PROXY_REGISTRY_H_