15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved. 25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be 35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file. 45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 5868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#ifndef CONTENT_CHILD_CHILD_PROCESS_H_ 6868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#define CONTENT_CHILD_CHILD_PROCESS_H_ 75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/basictypes.h" 95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/memory/scoped_ptr.h" 105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/synchronization/waitable_event.h" 11868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/threading/thread.h" 125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/common/content_export.h" 135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace content { 155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class ChildThread; 165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Base class for child processes of the browser process (i.e. renderer and 185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// plugin host). This is a singleton object for each child process. 190529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch// 200529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch// During process shutdown the following sequence of actions happens in 210529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch// order. 220529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch// 230529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch// 1. ChildProcess::~ChildProcess() is called. 240529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch// 2. Shutdown event is fired. Background threads should stop. 250529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch// 3. ChildThread::Shutdown() is called. ChildThread is also deleted. 260529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch// 4. IO thread is stopped. 270529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch// 5. Main message loop exits. 280529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch// 6. Child process is now fully stopped. 290529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch// 300529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch// Note: IO thread outlives the ChildThread object. 315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class CONTENT_EXPORT ChildProcess { 325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public: 335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Child processes should have an object that derives from this class. 345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Normally you would immediately call set_main_thread after construction. 355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) ChildProcess(); 365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual ~ChildProcess(); 375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // May be NULL if the main thread hasn't been set explicitly. 395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) ChildThread* main_thread(); 405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Sets the object associated with the main thread of this process. 425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Takes ownership of the pointer. 435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) void set_main_thread(ChildThread* thread); 445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 45c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) base::MessageLoop* io_message_loop() { return io_thread_.message_loop(); } 465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) base::MessageLoopProxy* io_message_loop_proxy() { 477d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) return io_thread_.message_loop_proxy().get(); 485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // A global event object that is signalled when the main thread's message 515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // loop exits. This gives background threads a way to observe the main 525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // thread shutting down. This can be useful when a background thread is 535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // waiting for some information from the browser process. If the browser 545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // process goes away prematurely, the background thread can at least notice 555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // the child processes's main thread exiting to determine that it should give 565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // up waiting. 576e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles) // For example, see the renderer code used to implement GetCookies(). 585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) base::WaitableEvent* GetShutDownEvent(); 595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // These are used for ref-counting the child process. The process shuts 615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // itself down when the ref count reaches 0. 625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // For example, in the renderer process, generally each tab managed by this 635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // process will hold a reference to the process, and release when closed. 645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) void AddRefProcess(); 655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) void ReleaseProcess(); 665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 67eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch // Getter for the one ChildProcess object for this process. Can only be called 68eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch // on the main thread. 6990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) static ChildProcess* current(); 705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) static void WaitForDebugger(const std::string& label); 725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private: 735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) int ref_count_; 745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // An event that will be signalled when we shutdown. 765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) base::WaitableEvent shutdown_event_; 775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // The thread that handles IO events. 795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) base::Thread io_thread_; 805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // NOTE: make sure that main_thread_ is listed after shutdown_event_, since 825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // it depends on it (indirectly through IPC::SyncChannel). Same for 835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // io_thread_. 845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) scoped_ptr<ChildThread> main_thread_; 855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) DISALLOW_COPY_AND_ASSIGN(ChildProcess); 875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}; 885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} // namespace content 905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 91868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#endif // CONTENT_CHILD_CHILD_PROCESS_H_ 92