broker_services.h 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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 SANDBOX_WIN_SRC_BROKER_SERVICES_H_
  5. #define SANDBOX_WIN_SRC_BROKER_SERVICES_H_
  6. #include <map>
  7. #include <memory>
  8. #include <set>
  9. #include <utility>
  10. #include "base/compiler_specific.h"
  11. #include "base/containers/flat_map.h"
  12. #include "base/memory/raw_ptr.h"
  13. #include "base/memory/scoped_refptr.h"
  14. #include "base/win/scoped_handle.h"
  15. #include "sandbox/win/src/crosscall_server.h"
  16. #include "sandbox/win/src/job.h"
  17. #include "sandbox/win/src/sandbox.h"
  18. #include "sandbox/win/src/sandbox_policy_base.h"
  19. #include "sandbox/win/src/sharedmem_ipc_server.h"
  20. #include "sandbox/win/src/threadpool.h"
  21. #include "sandbox/win/src/win_utils.h"
  22. namespace sandbox {
  23. // BrokerServicesBase ---------------------------------------------------------
  24. // Broker implementation version 0
  25. //
  26. // This is an implementation of the interface BrokerServices and
  27. // of the associated TargetProcess interface. In this implementation
  28. // TargetProcess is a friend of BrokerServices where the later manages a
  29. // collection of the former.
  30. class BrokerServicesBase final : public BrokerServices,
  31. public SingletonBase<BrokerServicesBase> {
  32. public:
  33. BrokerServicesBase();
  34. BrokerServicesBase(const BrokerServicesBase&) = delete;
  35. BrokerServicesBase& operator=(const BrokerServicesBase&) = delete;
  36. ~BrokerServicesBase();
  37. // BrokerServices interface.
  38. ResultCode Init() override;
  39. std::unique_ptr<TargetPolicy> CreatePolicy() override;
  40. std::unique_ptr<TargetPolicy> CreatePolicy(base::StringPiece key) override;
  41. ResultCode SpawnTarget(const wchar_t* exe_path,
  42. const wchar_t* command_line,
  43. std::unique_ptr<TargetPolicy> policy,
  44. ResultCode* last_warning,
  45. DWORD* last_error,
  46. PROCESS_INFORMATION* target) override;
  47. ResultCode WaitForAllTargets() override;
  48. ResultCode GetPolicyDiagnostics(
  49. std::unique_ptr<PolicyDiagnosticsReceiver> receiver) override;
  50. static void FreezeTargetConfigForTesting(TargetConfig* config);
  51. private:
  52. // The completion port used by the job objects to communicate events to
  53. // the worker thread.
  54. base::win::ScopedHandle job_port_;
  55. // Handle to a manual-reset event that is signaled when the total target
  56. // process count reaches zero.
  57. base::win::ScopedHandle no_targets_;
  58. // Handle to the worker thread that reacts to job notifications.
  59. base::win::ScopedHandle job_thread_;
  60. // Provides a pool of threads that are used to wait on the IPC calls.
  61. // Owned by TargetEventsThread which is alive until our destructor.
  62. raw_ptr<ThreadPool> thread_pool_ = nullptr;
  63. // Cache of configs backing policies. Entries are retained until shutdown and
  64. // used to prime policies created by CreatePolicy() with the same `tag`.
  65. base::flat_map<std::string, std::unique_ptr<TargetConfig>> config_cache_;
  66. };
  67. } // namespace sandbox
  68. #endif // SANDBOX_WIN_SRC_BROKER_SERVICES_H_