Home | History | Annotate | Download | only in browser
      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_PUBLIC_BROWSER_RENDER_PROCESS_HOST_H_
      6 #define CONTENT_PUBLIC_BROWSER_RENDER_PROCESS_HOST_H_
      7 
      8 #include "base/basictypes.h"
      9 #include "base/id_map.h"
     10 #include "base/process/kill.h"
     11 #include "base/process/process_handle.h"
     12 #include "base/supports_user_data.h"
     13 #include "content/common/content_export.h"
     14 #include "ipc/ipc_channel_proxy.h"
     15 #include "ipc/ipc_sender.h"
     16 #include "ui/gfx/native_widget_types.h"
     17 
     18 class GURL;
     19 struct ViewMsg_SwapOut_Params;
     20 
     21 namespace base {
     22 class TimeDelta;
     23 }
     24 
     25 namespace content {
     26 class BrowserContext;
     27 class BrowserMessageFilter;
     28 class RenderProcessHostObserver;
     29 class RenderWidgetHost;
     30 class ServiceRegistry;
     31 class StoragePartition;
     32 struct GlobalRequestID;
     33 
     34 // Interface that represents the browser side of the browser <-> renderer
     35 // communication channel. There will generally be one RenderProcessHost per
     36 // renderer process.
     37 class CONTENT_EXPORT RenderProcessHost : public IPC::Sender,
     38                                          public IPC::Listener,
     39                                          public base::SupportsUserData {
     40  public:
     41   typedef IDMap<RenderProcessHost>::iterator iterator;
     42 
     43   // Details for RENDERER_PROCESS_CLOSED notifications.
     44   struct RendererClosedDetails {
     45     RendererClosedDetails(base::ProcessHandle handle,
     46                           base::TerminationStatus status,
     47                           int exit_code) {
     48       this->handle = handle;
     49       this->status = status;
     50       this->exit_code = exit_code;
     51     }
     52     base::ProcessHandle handle;
     53     base::TerminationStatus status;
     54     int exit_code;
     55   };
     56 
     57   // General functions ---------------------------------------------------------
     58 
     59   virtual ~RenderProcessHost() {}
     60 
     61   // Initialize the new renderer process, returning true on success. This must
     62   // be called once before the object can be used, but can be called after
     63   // that with no effect. Therefore, if the caller isn't sure about whether
     64   // the process has been created, it should just call Init().
     65   virtual bool Init() = 0;
     66 
     67   // Gets the next available routing id.
     68   virtual int GetNextRoutingID() = 0;
     69 
     70   // These methods add or remove listener for a specific message routing ID.
     71   // Used for refcounting, each holder of this object must AddRoute and
     72   // RemoveRoute. This object should be allocated on the heap; when no
     73   // listeners own it any more, it will delete itself.
     74   virtual void AddRoute(int32 routing_id, IPC::Listener* listener) = 0;
     75   virtual void RemoveRoute(int32 routing_id) = 0;
     76 
     77   // Add and remove observers for lifecycle events. The order in which
     78   // notifications are sent to observers is undefined. Observers must be sure to
     79   // remove the observer before they go away.
     80   virtual void AddObserver(RenderProcessHostObserver* observer) = 0;
     81   virtual void RemoveObserver(RenderProcessHostObserver* observer) = 0;
     82 
     83   // Called when a received message cannot be decoded.
     84   virtual void ReceivedBadMessage() = 0;
     85 
     86   // Track the count of visible widgets. Called by listeners to register and
     87   // unregister visibility.
     88   virtual void WidgetRestored() = 0;
     89   virtual void WidgetHidden() = 0;
     90   virtual int VisibleWidgetCount() const = 0;
     91 
     92   // Indicates whether the current RenderProcessHost is associated with an
     93   // isolated guest renderer process. Not all guest renderers are created equal.
     94   // A guest, as indicated by BrowserPluginGuest::IsGuest, may coexist with
     95   // other non-guest renderers in the same process if IsIsolatedGuest is false.
     96   virtual bool IsIsolatedGuest() const = 0;
     97 
     98   // Returns the storage partition associated with this process.
     99   //
    100   // TODO(nasko): Remove this function from the public API once
    101   // URLRequestContextGetter's creation is moved into StoragePartition.
    102   // http://crbug.com/158595
    103   virtual StoragePartition* GetStoragePartition() const = 0;
    104 
    105   // Try to shutdown the associated renderer process as fast as possible.
    106   // If this renderer has any RenderViews with unload handlers, then this
    107   // function does nothing.  The current implementation uses TerminateProcess.
    108   // Returns True if it was able to do fast shutdown.
    109   virtual bool FastShutdownIfPossible() = 0;
    110 
    111   // Returns true if fast shutdown was started for the renderer.
    112   virtual bool FastShutdownStarted() const = 0;
    113 
    114   // Dump the child process' handle table before shutting down.
    115   virtual void DumpHandles() = 0;
    116 
    117   // Returns the process object associated with the child process.  In certain
    118   // tests or single-process mode, this will actually represent the current
    119   // process.
    120   //
    121   // NOTE: this is not necessarily valid immediately after calling Init, as
    122   // Init starts the process asynchronously.  It's guaranteed to be valid after
    123   // the first IPC arrives.
    124   virtual base::ProcessHandle GetHandle() const = 0;
    125 
    126   // Returns the user browser context associated with this renderer process.
    127   virtual content::BrowserContext* GetBrowserContext() const = 0;
    128 
    129   // Returns whether this process is using the same StoragePartition as
    130   // |partition|.
    131   virtual bool InSameStoragePartition(StoragePartition* partition) const = 0;
    132 
    133   // Returns the unique ID for this child process host. This can be used later
    134   // in a call to FromID() to get back to this object (this is used to avoid
    135   // sending non-threadsafe pointers to other threads).
    136   //
    137   // This ID will be unique across all child process hosts, including workers,
    138   // plugins, etc.
    139   //
    140   // This will never return ChildProcessHost::kInvalidUniqueID.
    141   virtual int GetID() const = 0;
    142 
    143   // Returns true iff channel_ has been set to non-NULL. Use this for checking
    144   // if there is connection or not. Virtual for mocking out for tests.
    145   virtual bool HasConnection() const = 0;
    146 
    147   // Call this to allow queueing of IPC messages that are sent before the
    148   // process is launched.
    149   virtual void EnableSendQueue() = 0;
    150 
    151   // Returns the renderer channel.
    152   virtual IPC::ChannelProxy* GetChannel() = 0;
    153 
    154   // Adds a message filter to the IPC channel.
    155   virtual void AddFilter(BrowserMessageFilter* filter) = 0;
    156 
    157   // Try to shutdown the associated render process as fast as possible
    158   virtual bool FastShutdownForPageCount(size_t count) = 0;
    159 
    160   // TODO(ananta)
    161   // Revisit whether the virtual functions declared from here on need to be
    162   // part of the interface.
    163   virtual void SetIgnoreInputEvents(bool ignore_input_events) = 0;
    164   virtual bool IgnoreInputEvents() const = 0;
    165 
    166   // Schedules the host for deletion and removes it from the all_hosts list.
    167   virtual void Cleanup() = 0;
    168 
    169   // Track the count of pending views that are being swapped back in.  Called
    170   // by listeners to register and unregister pending views to prevent the
    171   // process from exiting.
    172   virtual void AddPendingView() = 0;
    173   virtual void RemovePendingView() = 0;
    174 
    175   // Sets a flag indicating that the process can be abnormally terminated.
    176   virtual void SetSuddenTerminationAllowed(bool allowed) = 0;
    177   // Returns true if the process can be abnormally terminated.
    178   virtual bool SuddenTerminationAllowed() const = 0;
    179 
    180   // Returns how long the child has been idle. The definition of idle
    181   // depends on when a derived class calls mark_child_process_activity_time().
    182   // This is a rough indicator and its resolution should not be better than
    183   // 10 milliseconds.
    184   virtual base::TimeDelta GetChildProcessIdleTime() const = 0;
    185 
    186   // Called to resume the requests for a view created through window.open that
    187   // were initially blocked.
    188   virtual void ResumeRequestsForView(int route_id) = 0;
    189 
    190   // Checks that the given renderer can request |url|, if not it sets it to
    191   // about:blank.
    192   // |empty_allowed| must be set to false for navigations for security reasons.
    193   virtual void FilterURL(bool empty_allowed, GURL* url) = 0;
    194 
    195 #if defined(ENABLE_WEBRTC)
    196   virtual void EnableAecDump(const base::FilePath& file) = 0;
    197   virtual void DisableAecDump() = 0;
    198 
    199   // When set, |callback| receives log messages regarding, for example, media
    200   // devices (webcams, mics, etc) that were initially requested in the render
    201   // process associated with this RenderProcessHost.
    202   virtual void SetWebRtcLogMessageCallback(
    203       base::Callback<void(const std::string&)> callback) = 0;
    204 
    205   typedef base::Callback<void(scoped_ptr<uint8[]> packet_header,
    206                               size_t header_length,
    207                               size_t packet_length,
    208                               bool incoming)> WebRtcRtpPacketCallback;
    209 
    210   typedef base::Callback<void(bool incoming, bool outgoing)>
    211       WebRtcStopRtpDumpCallback;
    212 
    213   // Starts passing RTP packets to |packet_callback| and returns the callback
    214   // used to stop dumping.
    215   virtual WebRtcStopRtpDumpCallback StartRtpDump(
    216       bool incoming,
    217       bool outgoing,
    218       const WebRtcRtpPacketCallback& packet_callback) = 0;
    219 #endif
    220 
    221   // Tells the ResourceDispatcherHost to resume a deferred navigation without
    222   // transferring it to a new renderer process.
    223   virtual void ResumeDeferredNavigation(const GlobalRequestID& request_id) = 0;
    224 
    225   // Notifies the renderer that the timezone configuration of the system might
    226   // have changed.
    227   virtual void NotifyTimezoneChange() = 0;
    228 
    229   // Returns the ServiceRegistry for this process.
    230   virtual ServiceRegistry* GetServiceRegistry() = 0;
    231 
    232   // Static management functions -----------------------------------------------
    233 
    234   // Flag to run the renderer in process.  This is primarily
    235   // for debugging purposes.  When running "in process", the
    236   // browser maintains a single RenderProcessHost which communicates
    237   // to a RenderProcess which is instantiated in the same process
    238   // with the Browser.  All IPC between the Browser and the
    239   // Renderer is the same, it's just not crossing a process boundary.
    240 
    241   static bool run_renderer_in_process();
    242 
    243   // This also calls out to ContentBrowserClient::GetApplicationLocale and
    244   // modifies the current process' command line.
    245   static void SetRunRendererInProcess(bool value);
    246 
    247   // Allows iteration over all the RenderProcessHosts in the browser. Note
    248   // that each host may not be active, and therefore may have NULL channels.
    249   static iterator AllHostsIterator();
    250 
    251   // Returns the RenderProcessHost given its ID.  Returns NULL if the ID does
    252   // not correspond to a live RenderProcessHost.
    253   static RenderProcessHost* FromID(int render_process_id);
    254 
    255   // Returns whether the process-per-site model is in use (globally or just for
    256   // the current site), in which case we should ensure there is only one
    257   // RenderProcessHost per site for the entire browser context.
    258   static bool ShouldUseProcessPerSite(content::BrowserContext* browser_context,
    259                                       const GURL& url);
    260 
    261   // Returns true if the caller should attempt to use an existing
    262   // RenderProcessHost rather than creating a new one.
    263   static bool ShouldTryToUseExistingProcessHost(
    264       content::BrowserContext* browser_context, const GURL& site_url);
    265 
    266   // Get an existing RenderProcessHost associated with the given browser
    267   // context, if possible.  The renderer process is chosen randomly from
    268   // suitable renderers that share the same context and type (determined by the
    269   // site url).
    270   // Returns NULL if no suitable renderer process is available, in which case
    271   // the caller is free to create a new renderer.
    272   static RenderProcessHost* GetExistingProcessHost(
    273       content::BrowserContext* browser_context, const GURL& site_url);
    274 
    275   // Overrides the default heuristic for limiting the max renderer process
    276   // count.  This is useful for unit testing process limit behaviors.  It is
    277   // also used to allow a command line parameter to configure the max number of
    278   // renderer processes and should only be called once during startup.
    279   // A value of zero means to use the default heuristic.
    280   static void SetMaxRendererProcessCount(size_t count);
    281 
    282   // Returns the current maximum number of renderer process hosts kept by the
    283   // content module.
    284   static size_t GetMaxRendererProcessCount();
    285 };
    286 
    287 }  // namespace content.
    288 
    289 #endif  // CONTENT_PUBLIC_BROWSER_RENDER_PROCESS_HOST_H_
    290