| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef CHROME_BROWSER_RENDERER_HOST_RENDER_PROCESS_HOST_H_ | 5 #ifndef CHROME_BROWSER_RENDERER_HOST_RENDER_PROCESS_HOST_H_ |
| 6 #define CHROME_BROWSER_RENDERER_HOST_RENDER_PROCESS_HOST_H_ | 6 #define CHROME_BROWSER_RENDERER_HOST_RENDER_PROCESS_HOST_H_ |
| 7 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 #include <set> | 9 // TODO(jam): remove this file when all files have been converted. |
| 10 | 10 #include "content/browser/renderer_host/render_process_host.h" |
| 11 #include "app/surface/transport_dib.h" | |
| 12 #include "base/id_map.h" | |
| 13 #include "base/process.h" | |
| 14 #include "base/process_util.h" | |
| 15 #include "base/scoped_ptr.h" | |
| 16 #include "base/time.h" | |
| 17 #include "chrome/common/visitedlink_common.h" | |
| 18 #include "ipc/ipc_sync_channel.h" | |
| 19 | |
| 20 class Profile; | |
| 21 class URLRequestContextGetter; | |
| 22 struct ViewMsg_ClosePage_Params; | |
| 23 | |
| 24 namespace base { | |
| 25 class SharedMemory; | |
| 26 } | |
| 27 | |
| 28 // Virtual interface that represents the browser side of the browser <-> | |
| 29 // renderer communication channel. There will generally be one | |
| 30 // RenderProcessHost per renderer process. | |
| 31 // | |
| 32 // The concrete implementation of this class for normal use is the | |
| 33 // BrowserRenderProcessHost. It may also be implemented by a testing interface | |
| 34 // for mocking purposes. | |
| 35 class RenderProcessHost : public IPC::Channel::Sender, | |
| 36 public IPC::Channel::Listener { | |
| 37 public: | |
| 38 typedef IDMap<RenderProcessHost>::iterator iterator; | |
| 39 | |
| 40 // We classify renderers according to their highest privilege, and try | |
| 41 // to group pages into renderers with similar privileges. | |
| 42 // Note: it may be possible for a renderer to have multiple privileges, | |
| 43 // in which case we call it an "extension" renderer. | |
| 44 enum Type { | |
| 45 TYPE_NORMAL, // Normal renderer, no extra privileges. | |
| 46 TYPE_WEBUI, // Renderer with WebUI privileges, like New Tab. | |
| 47 TYPE_EXTENSION, // Renderer with extension privileges. | |
| 48 }; | |
| 49 | |
| 50 // Details for RENDERER_PROCESS_CLOSED notifications. | |
| 51 struct RendererClosedDetails { | |
| 52 RendererClosedDetails(base::TerminationStatus status, | |
| 53 int exit_code, | |
| 54 bool was_extension_renderer) { | |
| 55 this->status = status; | |
| 56 this->exit_code = exit_code; | |
| 57 this->was_extension_renderer = was_extension_renderer; | |
| 58 } | |
| 59 base::TerminationStatus status; | |
| 60 int exit_code; | |
| 61 bool was_extension_renderer; | |
| 62 }; | |
| 63 | |
| 64 explicit RenderProcessHost(Profile* profile); | |
| 65 virtual ~RenderProcessHost(); | |
| 66 | |
| 67 // Returns the user profile associated with this renderer process. | |
| 68 Profile* profile() const { return profile_; } | |
| 69 | |
| 70 // Returns the unique ID for this child process. This can be used later in | |
| 71 // a call to FromID() to get back to this object (this is used to avoid | |
| 72 // sending non-threadsafe pointers to other threads). | |
| 73 // | |
| 74 // This ID will be unique for all child processes, including workers, plugins, | |
| 75 // etc. It is generated by ChildProcessInfo. | |
| 76 int id() const { return id_; } | |
| 77 | |
| 78 // Returns true iff channel_ has been set to non-NULL. Use this for checking | |
| 79 // if there is connection or not. | |
| 80 bool HasConnection() { return channel_.get() != NULL; } | |
| 81 | |
| 82 bool sudden_termination_allowed() const { | |
| 83 return sudden_termination_allowed_; | |
| 84 } | |
| 85 void set_sudden_termination_allowed(bool enabled) { | |
| 86 sudden_termination_allowed_ = enabled; | |
| 87 } | |
| 88 | |
| 89 // Used for refcounting, each holder of this object must Attach and Release | |
| 90 // just like it would for a COM object. This object should be allocated on | |
| 91 // the heap; when no listeners own it any more, it will delete itself. | |
| 92 void Attach(IPC::Channel::Listener* listener, int routing_id); | |
| 93 | |
| 94 // See Attach() | |
| 95 void Release(int listener_id); | |
| 96 | |
| 97 // Listeners should call this when they've sent a "Close" message and | |
| 98 // they're waiting for a "Close_ACK", so that if the renderer process | |
| 99 // goes away we'll know that it was intentional rather than a crash. | |
| 100 void ReportExpectingClose(int32 listener_id); | |
| 101 | |
| 102 // Allows iteration over this RenderProcessHost's RenderViewHost listeners. | |
| 103 // Use from UI thread only. | |
| 104 typedef IDMap<IPC::Channel::Listener>::const_iterator listeners_iterator; | |
| 105 | |
| 106 listeners_iterator ListenersIterator() { | |
| 107 return listeners_iterator(&listeners_); | |
| 108 } | |
| 109 | |
| 110 IPC::Channel::Listener* GetListenerByID(int routing_id) { | |
| 111 return listeners_.Lookup(routing_id); | |
| 112 } | |
| 113 | |
| 114 // Called to inform the render process host of a new "max page id" for a | |
| 115 // render view host. The render process host computes the largest page id | |
| 116 // across all render view hosts and uses the value when it needs to | |
| 117 // initialize a new renderer in place of the current one. | |
| 118 void UpdateMaxPageID(int32 page_id); | |
| 119 | |
| 120 void set_ignore_input_events(bool ignore_input_events) { | |
| 121 ignore_input_events_ = ignore_input_events; | |
| 122 } | |
| 123 bool ignore_input_events() { | |
| 124 return ignore_input_events_; | |
| 125 } | |
| 126 | |
| 127 // Returns how long the child has been idle. The definition of idle | |
| 128 // depends on when a derived class calls mark_child_process_activity_time(). | |
| 129 // This is a rough indicator and its resolution should not be better than | |
| 130 // 10 milliseconds. | |
| 131 base::TimeDelta get_child_process_idle_time() const { | |
| 132 return base::TimeTicks::Now() - child_process_activity_time_; | |
| 133 } | |
| 134 | |
| 135 // Call this function when it is evident that the child process is actively | |
| 136 // performing some operation, for example if we just received an IPC message. | |
| 137 void mark_child_process_activity_time() { | |
| 138 child_process_activity_time_ = base::TimeTicks::Now(); | |
| 139 } | |
| 140 | |
| 141 // Try to shutdown the associated render process as fast as possible, but | |
| 142 // only if |count| matches the number of render widgets that this process | |
| 143 // controls. | |
| 144 bool FastShutdownForPageCount(size_t count); | |
| 145 | |
| 146 bool fast_shutdown_started() { | |
| 147 return fast_shutdown_started_; | |
| 148 } | |
| 149 | |
| 150 // Virtual interface --------------------------------------------------------- | |
| 151 | |
| 152 // Initialize the new renderer process, returning true on success. This must | |
| 153 // be called once before the object can be used, but can be called after | |
| 154 // that with no effect. Therefore, if the caller isn't sure about whether | |
| 155 // the process has been created, it should just call Init(). | |
| 156 virtual bool Init( | |
| 157 bool is_accessibility_enabled, bool is_extensions_process) = 0; | |
| 158 | |
| 159 // Gets the next available routing id. | |
| 160 virtual int GetNextRoutingID() = 0; | |
| 161 | |
| 162 // Called on the UI thread to cancel any outstanding resource requests for | |
| 163 // the specified render widget. | |
| 164 virtual void CancelResourceRequests(int render_widget_id) = 0; | |
| 165 | |
| 166 // Called on the UI thread to simulate a ClosePage_ACK message to the | |
| 167 // ResourceDispatcherHost. Necessary for a cross-site request, in the case | |
| 168 // that the original RenderViewHost is not live and thus cannot run an | |
| 169 // onunload handler. | |
| 170 virtual void CrossSiteClosePageACK( | |
| 171 const ViewMsg_ClosePage_Params& params) = 0; | |
| 172 | |
| 173 // Called on the UI thread to wait for the next UpdateRect message for the | |
| 174 // specified render widget. Returns true if successful, and the msg out- | |
| 175 // param will contain a copy of the received UpdateRect message. | |
| 176 virtual bool WaitForUpdateMsg(int render_widget_id, | |
| 177 const base::TimeDelta& max_delay, | |
| 178 IPC::Message* msg) = 0; | |
| 179 | |
| 180 // Called when a received message cannot be decoded. | |
| 181 virtual void ReceivedBadMessage() = 0; | |
| 182 | |
| 183 // Track the count of visible widgets. Called by listeners to register and | |
| 184 // unregister visibility. | |
| 185 virtual void WidgetRestored() = 0; | |
| 186 virtual void WidgetHidden() = 0; | |
| 187 | |
| 188 // Called when RenderView is created by a listener. | |
| 189 virtual void ViewCreated() = 0; | |
| 190 | |
| 191 // Informs the renderer about a new visited link table. | |
| 192 virtual void SendVisitedLinkTable(base::SharedMemory* table_memory) = 0; | |
| 193 | |
| 194 // Notify the renderer that a link was visited. | |
| 195 virtual void AddVisitedLinks( | |
| 196 const VisitedLinkCommon::Fingerprints& links) = 0; | |
| 197 | |
| 198 // Clear internal visited links buffer and ask the renderer to update link | |
| 199 // coloring state for all of its links. | |
| 200 virtual void ResetVisitedLinks() = 0; | |
| 201 | |
| 202 // Try to shutdown the associated renderer process as fast as possible. | |
| 203 // If this renderer has any RenderViews with unload handlers, then this | |
| 204 // function does nothing. The current implementation uses TerminateProcess. | |
| 205 // Returns True if it was able to do fast shutdown. | |
| 206 virtual bool FastShutdownIfPossible() = 0; | |
| 207 | |
| 208 // Synchronously sends the message, waiting for the specified timeout. The | |
| 209 // implementor takes ownership of the given Message regardless of whether or | |
| 210 // not this method succeeds. Returns true on success. | |
| 211 virtual bool SendWithTimeout(IPC::Message* msg, int timeout_ms) = 0; | |
| 212 | |
| 213 // Returns the process object associated with the child process. In certain | |
| 214 // tests or single-process mode, this will actually represent the current | |
| 215 // process. | |
| 216 // | |
| 217 // NOTE: this is not necessarily valid immediately after calling Init, as | |
| 218 // Init starts the process asynchronously. It's guaranteed to be valid after | |
| 219 // the first IPC arrives. | |
| 220 virtual base::ProcessHandle GetHandle() = 0; | |
| 221 | |
| 222 // Transport DIB functions --------------------------------------------------- | |
| 223 | |
| 224 // Return the TransportDIB for the given id. On Linux, this can involve | |
| 225 // mapping shared memory. On Mac, the shared memory is created in the browser | |
| 226 // process and the cached metadata is returned. On Windows, this involves | |
| 227 // duplicating the handle from the remote process. The RenderProcessHost | |
| 228 // still owns the returned DIB. | |
| 229 virtual TransportDIB* GetTransportDIB(TransportDIB::Id dib_id) = 0; | |
| 230 | |
| 231 // Static management functions ----------------------------------------------- | |
| 232 | |
| 233 // Flag to run the renderer in process. This is primarily | |
| 234 // for debugging purposes. When running "in process", the | |
| 235 // browser maintains a single RenderProcessHost which communicates | |
| 236 // to a RenderProcess which is instantiated in the same process | |
| 237 // with the Browser. All IPC between the Browser and the | |
| 238 // Renderer is the same, it's just not crossing a process boundary. | |
| 239 static bool run_renderer_in_process() { | |
| 240 return run_renderer_in_process_; | |
| 241 } | |
| 242 static void set_run_renderer_in_process(bool value) { | |
| 243 run_renderer_in_process_ = value; | |
| 244 } | |
| 245 | |
| 246 // Allows iteration over all the RenderProcessHosts in the browser. Note | |
| 247 // that each host may not be active, and therefore may have NULL channels. | |
| 248 static iterator AllHostsIterator(); | |
| 249 | |
| 250 // Returns the RenderProcessHost given its ID. Returns NULL if the ID does | |
| 251 // not correspond to a live RenderProcessHost. | |
| 252 static RenderProcessHost* FromID(int render_process_id); | |
| 253 | |
| 254 // Returns true if the caller should attempt to use an existing | |
| 255 // RenderProcessHost rather than creating a new one. | |
| 256 static bool ShouldTryToUseExistingProcessHost(); | |
| 257 | |
| 258 // Get an existing RenderProcessHost associated with the given profile, if | |
| 259 // possible. The renderer process is chosen randomly from suitable renderers | |
| 260 // that share the same profile and type. | |
| 261 // Returns NULL if no suitable renderer process is available, in which case | |
| 262 // the caller is free to create a new renderer. | |
| 263 static RenderProcessHost* GetExistingProcessHost(Profile* profile, Type type); | |
| 264 | |
| 265 // Overrides the default heuristic for limiting the max renderer process | |
| 266 // count. This is useful for unit testing process limit behaviors. | |
| 267 // A value of zero means to use the default heuristic. | |
| 268 static void SetMaxRendererProcessCount(size_t count); | |
| 269 | |
| 270 protected: | |
| 271 // A proxy for our IPC::Channel that lives on the IO thread (see | |
| 272 // browser_process.h) | |
| 273 scoped_ptr<IPC::SyncChannel> channel_; | |
| 274 | |
| 275 // The registered listeners. When this list is empty or all NULL, we should | |
| 276 // delete ourselves | |
| 277 IDMap<IPC::Channel::Listener> listeners_; | |
| 278 | |
| 279 // The maximum page ID we've ever seen from the renderer process. | |
| 280 int32 max_page_id_; | |
| 281 | |
| 282 // True if fast shutdown has been performed on this RPH. | |
| 283 bool fast_shutdown_started_; | |
| 284 | |
| 285 // True if we've posted a DeleteTask and will be deleted soon. | |
| 286 bool deleting_soon_; | |
| 287 | |
| 288 private: | |
| 289 // The globally-unique identifier for this RPH. | |
| 290 int id_; | |
| 291 | |
| 292 Profile* profile_; | |
| 293 | |
| 294 // set of listeners that expect the renderer process to close | |
| 295 std::set<int> listeners_expecting_close_; | |
| 296 | |
| 297 // True if the process can be shut down suddenly. If this is true, then we're | |
| 298 // sure that all the RenderViews in the process can be shutdown suddenly. If | |
| 299 // it's false, then specific RenderViews might still be allowed to be shutdown | |
| 300 // suddenly by checking their SuddenTerminationAllowed() flag. This can occur | |
| 301 // if one tab has an unload event listener but another tab in the same process | |
| 302 // doesn't. | |
| 303 bool sudden_termination_allowed_; | |
| 304 | |
| 305 // Set to true if we shouldn't send input events. We actually do the | |
| 306 // filtering for this at the render widget level. | |
| 307 bool ignore_input_events_; | |
| 308 | |
| 309 // See getter above. | |
| 310 static bool run_renderer_in_process_; | |
| 311 | |
| 312 // Records the last time we regarded the child process active. | |
| 313 base::TimeTicks child_process_activity_time_; | |
| 314 | |
| 315 DISALLOW_COPY_AND_ASSIGN(RenderProcessHost); | |
| 316 }; | |
| 317 | |
| 318 // Factory object for RenderProcessHosts. Using this factory allows tests to | |
| 319 // swap out a different one to use a TestRenderProcessHost. | |
| 320 class RenderProcessHostFactory { | |
| 321 public: | |
| 322 virtual ~RenderProcessHostFactory() {} | |
| 323 virtual RenderProcessHost* CreateRenderProcessHost( | |
| 324 Profile* profile) const = 0; | |
| 325 }; | |
| 326 | 11 |
| 327 #endif // CHROME_BROWSER_RENDERER_HOST_RENDER_PROCESS_HOST_H_ | 12 #endif // CHROME_BROWSER_RENDERER_HOST_RENDER_PROCESS_HOST_H_ |
| OLD | NEW |