michael@0: // Copyright (c) 2012 The Chromium Authors. All rights reserved. michael@0: // Use of this source code is governed by a BSD-style license that can be michael@0: // found in the LICENSE file. michael@0: michael@0: #ifndef SANDBOX_WIN_SRC_BROKER_SERVICES_H_ michael@0: #define SANDBOX_WIN_SRC_BROKER_SERVICES_H_ michael@0: michael@0: #include michael@0: #include michael@0: #include michael@0: #include "base/basictypes.h" michael@0: #include "base/compiler_specific.h" michael@0: #include "base/win/scoped_handle.h" michael@0: #include "sandbox/win/src/crosscall_server.h" michael@0: #include "sandbox/win/src/job.h" michael@0: #include "sandbox/win/src/sandbox.h" michael@0: #include "sandbox/win/src/sharedmem_ipc_server.h" michael@0: #include "sandbox/win/src/win2k_threadpool.h" michael@0: #include "sandbox/win/src/win_utils.h" michael@0: michael@0: namespace { michael@0: michael@0: struct JobTracker; michael@0: struct PeerTracker; michael@0: michael@0: } // namespace michael@0: michael@0: namespace sandbox { michael@0: michael@0: class PolicyBase; michael@0: michael@0: // BrokerServicesBase --------------------------------------------------------- michael@0: // Broker implementation version 0 michael@0: // michael@0: // This is an implementation of the interface BrokerServices and michael@0: // of the associated TargetProcess interface. In this implementation michael@0: // TargetProcess is a friend of BrokerServices where the later manages a michael@0: // collection of the former. michael@0: class BrokerServicesBase : public BrokerServices, michael@0: public SingletonBase { michael@0: public: michael@0: BrokerServicesBase(); michael@0: michael@0: ~BrokerServicesBase(); michael@0: michael@0: // BrokerServices interface. michael@0: virtual ResultCode Init() OVERRIDE; michael@0: virtual TargetPolicy* CreatePolicy() OVERRIDE; michael@0: virtual ResultCode SpawnTarget(const wchar_t* exe_path, michael@0: const wchar_t* command_line, michael@0: TargetPolicy* policy, michael@0: PROCESS_INFORMATION* target) OVERRIDE; michael@0: virtual ResultCode WaitForAllTargets() OVERRIDE; michael@0: virtual ResultCode AddTargetPeer(HANDLE peer_process) OVERRIDE; michael@0: virtual ResultCode InstallAppContainer(const wchar_t* sid, michael@0: const wchar_t* name) OVERRIDE; michael@0: virtual ResultCode UninstallAppContainer(const wchar_t* sid) OVERRIDE; michael@0: michael@0: // Checks if the supplied process ID matches one of the broker's active michael@0: // target processes michael@0: // Returns: michael@0: // true if there is an active target process for this ID, otherwise false. michael@0: bool IsActiveTarget(DWORD process_id); michael@0: michael@0: private: michael@0: // Releases the Job and notifies the associated Policy object to its michael@0: // resources as well. michael@0: static void FreeResources(JobTracker* tracker); michael@0: michael@0: // The routine that the worker thread executes. It is in charge of michael@0: // notifications and cleanup-related tasks. michael@0: static DWORD WINAPI TargetEventsThread(PVOID param); michael@0: michael@0: // Removes a target peer from the process list if it expires. michael@0: static VOID CALLBACK RemovePeer(PVOID parameter, BOOLEAN timeout); michael@0: michael@0: // The completion port used by the job objects to communicate events to michael@0: // the worker thread. michael@0: HANDLE job_port_; michael@0: michael@0: // Handle to a manual-reset event that is signaled when the total target michael@0: // process count reaches zero. michael@0: HANDLE no_targets_; michael@0: michael@0: // Handle to the worker thread that reacts to job notifications. michael@0: HANDLE job_thread_; michael@0: michael@0: // Lock used to protect the list of targets from being modified by 2 michael@0: // threads at the same time. michael@0: CRITICAL_SECTION lock_; michael@0: michael@0: // provides a pool of threads that are used to wait on the IPC calls. michael@0: ThreadProvider* thread_pool_; michael@0: michael@0: // List of the trackers for closing and cleanup purposes. michael@0: typedef std::list JobTrackerList; michael@0: JobTrackerList tracker_list_; michael@0: michael@0: // Maps peer process IDs to the saved handle and wait event. michael@0: // Prevents peer callbacks from accessing the broker after destruction. michael@0: typedef std::map PeerTrackerMap; michael@0: PeerTrackerMap peer_map_; michael@0: michael@0: // Provides a fast lookup to identify sandboxed processes that belong to a michael@0: // job. Consult |jobless_process_handles_| for handles of pocess without job. michael@0: std::set child_process_ids_; michael@0: michael@0: DISALLOW_COPY_AND_ASSIGN(BrokerServicesBase); michael@0: }; michael@0: michael@0: } // namespace sandbox michael@0: michael@0: michael@0: #endif // SANDBOX_WIN_SRC_BROKER_SERVICES_H_