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 CONTENT_BROWSER_BROWSER_CHILD_PROCESS_HOST_IMPL_H_ 6 #define CONTENT_BROWSER_BROWSER_CHILD_PROCESS_HOST_IMPL_H_ 7 8 #include <list> 9 10 #include "base/compiler_specific.h" 11 #include "base/memory/scoped_ptr.h" 12 #include "base/process/process.h" 13 #include "base/synchronization/waitable_event_watcher.h" 14 #include "content/browser/child_process_launcher.h" 15 #include "content/browser/power_monitor_message_broadcaster.h" 16 #include "content/public/browser/browser_child_process_host.h" 17 #include "content/public/browser/child_process_data.h" 18 #include "content/public/common/child_process_host_delegate.h" 19 20 namespace content { 21 22 class BrowserChildProcessHostIterator; 23 class BrowserChildProcessObserver; 24 25 // Plugins/workers and other child processes that live on the IO thread use this 26 // class. RenderProcessHostImpl is the main exception that doesn't use this 27 /// class because it lives on the UI thread. 28 class CONTENT_EXPORT BrowserChildProcessHostImpl 29 : public BrowserChildProcessHost, 30 public NON_EXPORTED_BASE(ChildProcessHostDelegate), 31 public ChildProcessLauncher::Client { 32 public: 33 BrowserChildProcessHostImpl( 34 int process_type, 35 BrowserChildProcessHostDelegate* delegate); 36 virtual ~BrowserChildProcessHostImpl(); 37 38 // Terminates all child processes and deletes each BrowserChildProcessHost 39 // instance. 40 static void TerminateAll(); 41 42 // BrowserChildProcessHost implementation: 43 virtual bool Send(IPC::Message* message) OVERRIDE; 44 virtual void Launch( 45 #if defined(OS_WIN) 46 SandboxedProcessLauncherDelegate* delegate, 47 #elif defined(OS_POSIX) 48 bool use_zygote, 49 const base::EnvironmentVector& environ, 50 #endif 51 CommandLine* cmd_line) OVERRIDE; 52 virtual const ChildProcessData& GetData() const OVERRIDE; 53 virtual ChildProcessHost* GetHost() const OVERRIDE; 54 virtual base::TerminationStatus GetTerminationStatus(int* exit_code) OVERRIDE; 55 virtual void SetName(const string16& name) OVERRIDE; 56 virtual void SetHandle(base::ProcessHandle handle) OVERRIDE; 57 58 // Returns the handle of the child process. This can be called only after 59 // OnProcessLaunched is called or it will be invalid and may crash. 60 base::ProcessHandle GetHandle() const; 61 62 // Removes this host from the host list. Calls ChildProcessHost::ForceShutdown 63 void ForceShutdown(); 64 65 // Callers can reduce the BrowserChildProcess' priority. 66 void SetBackgrounded(bool backgrounded); 67 68 // Controls whether the child process should be terminated on browser 69 // shutdown. Default is to always terminate. 70 void SetTerminateChildOnShutdown(bool terminate_on_shutdown); 71 72 // Called when an instance of a particular child is created in a page. 73 static void NotifyProcessInstanceCreated(const ChildProcessData& data); 74 75 BrowserChildProcessHostDelegate* delegate() const { return delegate_; } 76 77 typedef std::list<BrowserChildProcessHostImpl*> BrowserChildProcessList; 78 private: 79 friend class BrowserChildProcessHostIterator; 80 friend class BrowserChildProcessObserver; 81 82 static BrowserChildProcessList* GetIterator(); 83 84 static void AddObserver(BrowserChildProcessObserver* observer); 85 static void RemoveObserver(BrowserChildProcessObserver* observer); 86 87 // ChildProcessHostDelegate implementation: 88 virtual bool CanShutdown() OVERRIDE; 89 virtual void OnChildDisconnected() OVERRIDE; 90 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE; 91 virtual void OnChannelConnected(int32 peer_pid) OVERRIDE; 92 virtual void OnChannelError() OVERRIDE; 93 94 // ChildProcessLauncher::Client implementation. 95 virtual void OnProcessLaunched() OVERRIDE; 96 97 #if defined(OS_WIN) 98 void DeleteProcessWaitableEvent(base::WaitableEvent* event); 99 void OnProcessExitedEarly(base::WaitableEvent* event); 100 #endif 101 102 ChildProcessData data_; 103 BrowserChildProcessHostDelegate* delegate_; 104 scoped_ptr<ChildProcessHost> child_process_host_; 105 106 scoped_ptr<ChildProcessLauncher> child_process_; 107 108 PowerMonitorMessageBroadcaster power_monitor_message_broadcaster_; 109 110 #if defined(OS_WIN) 111 // Watches to see if the child process exits before the IPC channel has 112 // been connected. Thereafter, its exit is determined by an error on the 113 // IPC channel. 114 base::WaitableEventWatcher early_exit_watcher_; 115 #endif 116 }; 117 118 } // namespace content 119 120 #endif // CONTENT_BROWSER_BROWSER_CHILD_PROCESS_HOST_IMPL_H_ 121