Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(220)

Side by Side Diff: content/browser/renderer_host/render_process_host_impl.cc

Issue 23235002: Set up content in-process main threads via factory (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: ios Created 7 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 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 // Represents the browser side of the browser <--> renderer communication 5 // Represents the browser side of the browser <--> renderer communication
6 // channel. There will be one RenderProcessHost per renderer process. 6 // channel. There will be one RenderProcessHost per renderer process.
7 7
8 #include "content/browser/renderer_host/render_process_host_impl.h" 8 #include "content/browser/renderer_host/render_process_host_impl.h"
9 9
10 #include <algorithm> 10 #include <algorithm>
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after
111 #include "content/public/browser/notification_types.h" 111 #include "content/public/browser/notification_types.h"
112 #include "content/public/browser/render_process_host_factory.h" 112 #include "content/public/browser/render_process_host_factory.h"
113 #include "content/public/browser/render_widget_host.h" 113 #include "content/public/browser/render_widget_host.h"
114 #include "content/public/browser/resource_context.h" 114 #include "content/public/browser/resource_context.h"
115 #include "content/public/browser/user_metrics.h" 115 #include "content/public/browser/user_metrics.h"
116 #include "content/public/common/content_constants.h" 116 #include "content/public/common/content_constants.h"
117 #include "content/public/common/content_switches.h" 117 #include "content/public/common/content_switches.h"
118 #include "content/public/common/process_type.h" 118 #include "content/public/common/process_type.h"
119 #include "content/public/common/result_codes.h" 119 #include "content/public/common/result_codes.h"
120 #include "content/public/common/url_constants.h" 120 #include "content/public/common/url_constants.h"
121 #include "content/renderer/render_process_impl.h"
122 #include "content/renderer/render_thread_impl.h"
123 #include "gpu/command_buffer/service/gpu_switches.h" 121 #include "gpu/command_buffer/service/gpu_switches.h"
124 #include "ipc/ipc_channel.h" 122 #include "ipc/ipc_channel.h"
125 #include "ipc/ipc_logging.h" 123 #include "ipc/ipc_logging.h"
126 #include "ipc/ipc_platform_file.h" 124 #include "ipc/ipc_platform_file.h"
127 #include "ipc/ipc_switches.h" 125 #include "ipc/ipc_switches.h"
128 #include "media/base/media_switches.h" 126 #include "media/base/media_switches.h"
129 #include "net/url_request/url_request_context_getter.h" 127 #include "net/url_request/url_request_context_getter.h"
130 #include "ppapi/shared_impl/ppapi_switches.h" 128 #include "ppapi/shared_impl/ppapi_switches.h"
131 #include "ui/base/ui_base_switches.h" 129 #include "ui/base/ui_base_switches.h"
132 #include "ui/gl/gl_switches.h" 130 #include "ui/gl/gl_switches.h"
(...skipping 17 matching lines...) Expand all
150 148
151 #include "third_party/skia/include/core/SkBitmap.h" 149 #include "third_party/skia/include/core/SkBitmap.h"
152 150
153 extern bool g_exited_main_message_loop; 151 extern bool g_exited_main_message_loop;
154 152
155 static const char* kSiteProcessMapKeyName = "content_site_process_map"; 153 static const char* kSiteProcessMapKeyName = "content_site_process_map";
156 154
157 namespace content { 155 namespace content {
158 namespace { 156 namespace {
159 157
160 base::MessageLoop* g_in_process_thread;
161
162 void CacheShaderInfo(int32 id, base::FilePath path) { 158 void CacheShaderInfo(int32 id, base::FilePath path) {
163 ShaderCacheFactory::GetInstance()->SetCacheInfo(id, path); 159 ShaderCacheFactory::GetInstance()->SetCacheInfo(id, path);
164 } 160 }
165 161
166 void RemoveShaderInfo(int32 id) { 162 void RemoveShaderInfo(int32 id) {
167 ShaderCacheFactory::GetInstance()->RemoveCacheInfo(id); 163 ShaderCacheFactory::GetInstance()->RemoveCacheInfo(id);
168 } 164 }
169 165
170 } // namespace
171
172 #if !defined(CHROME_MULTIPLE_DLL)
173
174 // This class creates the IO thread for the renderer when running in
175 // single-process mode. It's not used in multi-process mode.
176 class RendererMainThread : public base::Thread {
177 public:
178 explicit RendererMainThread(const std::string& channel_id)
179 : Thread("Chrome_InProcRendererThread"),
180 channel_id_(channel_id) {
181 }
182
183 virtual ~RendererMainThread() {
184 Stop();
185 }
186
187 protected:
188 virtual void Init() OVERRIDE {
189 render_process_.reset(new RenderProcessImpl());
190 new RenderThreadImpl(channel_id_);
191 g_in_process_thread = message_loop();
192 }
193
194 virtual void CleanUp() OVERRIDE {
195 g_in_process_thread = NULL;
196 render_process_.reset();
197
198 // It's a little lame to manually set this flag. But the single process
199 // RendererThread will receive the WM_QUIT. We don't need to assert on
200 // this thread, so just force the flag manually.
201 // If we want to avoid this, we could create the InProcRendererThread
202 // directly with _beginthreadex() rather than using the Thread class.
203 // We used to set this flag in the Init function above. However there
204 // other threads like WebThread which are created by this thread
205 // which resets this flag. Please see Thread::StartWithOptions. Setting
206 // this flag to true in Cleanup works around these problems.
207 SetThreadWasQuitProperly(true);
208 }
209
210 private:
211 std::string channel_id_;
212 scoped_ptr<RenderProcess> render_process_;
213
214 DISALLOW_COPY_AND_ASSIGN(RendererMainThread);
215 };
216
217 #endif
218
219 namespace {
220
221 // Helper class that we pass to ResourceMessageFilter so that it can find the 166 // Helper class that we pass to ResourceMessageFilter so that it can find the
222 // right net::URLRequestContext for a request. 167 // right net::URLRequestContext for a request.
223 class RendererURLRequestContextSelector 168 class RendererURLRequestContextSelector
224 : public ResourceMessageFilter::URLRequestContextSelector { 169 : public ResourceMessageFilter::URLRequestContextSelector {
225 public: 170 public:
226 RendererURLRequestContextSelector(BrowserContext* browser_context, 171 RendererURLRequestContextSelector(BrowserContext* browser_context,
227 int render_child_id) 172 int render_child_id)
228 : request_context_(browser_context->GetRequestContextForRenderProcess( 173 : request_context_(browser_context->GetRequestContextForRenderProcess(
229 render_child_id)), 174 render_child_id)),
230 media_request_context_( 175 media_request_context_(
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after
332 virtual void PreSpawnTarget(sandbox::TargetPolicy* policy, 277 virtual void PreSpawnTarget(sandbox::TargetPolicy* policy,
333 bool* success) { 278 bool* success) {
334 AddBaseHandleClosePolicy(policy); 279 AddBaseHandleClosePolicy(policy);
335 GetContentClient()->browser()->PreSpawnRenderer(policy, success); 280 GetContentClient()->browser()->PreSpawnRenderer(policy, success);
336 } 281 }
337 }; 282 };
338 #endif // OS_WIN 283 #endif // OS_WIN
339 284
340 } // namespace 285 } // namespace
341 286
287 RendererMainThreadFactoryFunction g_renderer_main_thread_factory = NULL;
288
289 void RenderProcessHost::RegisterRendererMainThreadFactory(
290 RendererMainThreadFactoryFunction create) {
291 g_renderer_main_thread_factory = create;
292 }
293
294 base::MessageLoop* g_in_process_thread;
295
296 base::MessageLoop*
297 RenderProcessHostImpl::GetInProcessRendererThreadForTesting() {
298 return g_in_process_thread;
299 }
300
342 // Stores the maximum number of renderer processes the content module can 301 // Stores the maximum number of renderer processes the content module can
343 // create. 302 // create.
344 static size_t g_max_renderer_count_override = 0; 303 static size_t g_max_renderer_count_override = 0;
345 304
346 // static 305 // static
347 size_t RenderProcessHost::GetMaxRendererProcessCount() { 306 size_t RenderProcessHost::GetMaxRendererProcessCount() {
348 if (g_max_renderer_count_override) 307 if (g_max_renderer_count_override)
349 return g_max_renderer_count_override; 308 return g_max_renderer_count_override;
350 309
351 // Defines the maximum number of renderer processes according to the 310 // Defines the maximum number of renderer processes according to the
(...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after
504 this, 463 this,
505 BrowserThread::GetMessageLoopProxyForThread( 464 BrowserThread::GetMessageLoopProxyForThread(
506 BrowserThread::IO).get())); 465 BrowserThread::IO).get()));
507 466
508 // Call the embedder first so that their IPC filters have priority. 467 // Call the embedder first so that their IPC filters have priority.
509 GetContentClient()->browser()->RenderProcessHostCreated(this); 468 GetContentClient()->browser()->RenderProcessHostCreated(this);
510 469
511 CreateMessageFilters(); 470 CreateMessageFilters();
512 471
513 // Single-process mode not supported in multiple-dll mode currently. 472 // Single-process mode not supported in multiple-dll mode currently.
514 #if !defined(CHROME_MULTIPLE_DLL) 473 if (run_renderer_in_process() && g_renderer_main_thread_factory) {
515 if (run_renderer_in_process()) {
516 // Crank up a thread and run the initialization there. With the way that 474 // Crank up a thread and run the initialization there. With the way that
517 // messages flow between the browser and renderer, this thread is required 475 // messages flow between the browser and renderer, this thread is required
518 // to prevent a deadlock in single-process mode. Since the primordial 476 // to prevent a deadlock in single-process mode. Since the primordial
519 // thread in the renderer process runs the WebKit code and can sometimes 477 // thread in the renderer process runs the WebKit code and can sometimes
520 // make blocking calls to the UI thread (i.e. this thread), they need to run 478 // make blocking calls to the UI thread (i.e. this thread), they need to run
521 // on separate threads. 479 // on separate threads.
522 in_process_renderer_.reset(new RendererMainThread(channel_id)); 480 in_process_renderer_.reset(g_renderer_main_thread_factory(channel_id));
523 481
524 base::Thread::Options options; 482 base::Thread::Options options;
525 #if defined(OS_WIN) && !defined(OS_MACOSX) 483 #if defined(OS_WIN) && !defined(OS_MACOSX)
526 // In-process plugins require this to be a UI message loop. 484 // In-process plugins require this to be a UI message loop.
527 options.message_loop_type = base::MessageLoop::TYPE_UI; 485 options.message_loop_type = base::MessageLoop::TYPE_UI;
528 #else 486 #else
529 // We can't have multiple UI loops on Linux and Android, so we don't support 487 // We can't have multiple UI loops on Linux and Android, so we don't support
530 // in-process plugins. 488 // in-process plugins.
531 options.message_loop_type = base::MessageLoop::TYPE_DEFAULT; 489 options.message_loop_type = base::MessageLoop::TYPE_DEFAULT;
532 #endif 490 #endif
533 in_process_renderer_->StartWithOptions(options); 491 in_process_renderer_->StartWithOptions(options);
534 492
493 g_in_process_thread = in_process_renderer_->message_loop();
494
535 OnProcessLaunched(); // Fake a callback that the process is ready. 495 OnProcessLaunched(); // Fake a callback that the process is ready.
536 } else 496 } else {
537 #endif // !CHROME_MULTIPLE_DLL
538 {
539 // Build command line for renderer. We call AppendRendererCommandLine() 497 // Build command line for renderer. We call AppendRendererCommandLine()
540 // first so the process type argument will appear first. 498 // first so the process type argument will appear first.
541 CommandLine* cmd_line = new CommandLine(renderer_path); 499 CommandLine* cmd_line = new CommandLine(renderer_path);
542 if (!renderer_prefix.empty()) 500 if (!renderer_prefix.empty())
543 cmd_line->PrependWrapper(renderer_prefix); 501 cmd_line->PrependWrapper(renderer_prefix);
544 AppendRendererCommandLine(cmd_line); 502 AppendRendererCommandLine(cmd_line);
545 cmd_line->AppendSwitchASCII(switches::kProcessChannelID, channel_id); 503 cmd_line->AppendSwitchASCII(switches::kProcessChannelID, channel_id);
546 504
547 // Spawn the child process asynchronously to avoid blocking the UI thread. 505 // Spawn the child process asynchronously to avoid blocking the UI thread.
548 // As long as there's no renderer prefix, we can use the zygote process 506 // As long as there's no renderer prefix, we can use the zygote process
(...skipping 1058 matching lines...) Expand 10 before | Expand all | Expand 10 after
1607 1565
1608 // Only register valid, non-empty sites. Empty or invalid sites will not 1566 // Only register valid, non-empty sites. Empty or invalid sites will not
1609 // use process-per-site mode. We cannot check whether the process has 1567 // use process-per-site mode. We cannot check whether the process has
1610 // appropriate bindings here, because the bindings have not yet been granted. 1568 // appropriate bindings here, because the bindings have not yet been granted.
1611 std::string site = SiteInstance::GetSiteForURL(browser_context, url) 1569 std::string site = SiteInstance::GetSiteForURL(browser_context, url)
1612 .possibly_invalid_spec(); 1570 .possibly_invalid_spec();
1613 if (!site.empty()) 1571 if (!site.empty())
1614 map->RegisterProcess(site, process); 1572 map->RegisterProcess(site, process);
1615 } 1573 }
1616 1574
1617 base::MessageLoop*
1618 RenderProcessHostImpl::GetInProcessRendererThreadForTesting() {
1619 return g_in_process_thread;
1620 }
1621
1622 void RenderProcessHostImpl::ProcessDied(bool already_dead) { 1575 void RenderProcessHostImpl::ProcessDied(bool already_dead) {
1623 // Our child process has died. If we didn't expect it, it's a crash. 1576 // Our child process has died. If we didn't expect it, it's a crash.
1624 // In any case, we need to let everyone know it's gone. 1577 // In any case, we need to let everyone know it's gone.
1625 // The OnChannelError notification can fire multiple times due to nested sync 1578 // The OnChannelError notification can fire multiple times due to nested sync
1626 // calls to a renderer. If we don't have a valid channel here it means we 1579 // calls to a renderer. If we don't have a valid channel here it means we
1627 // already handled the error. 1580 // already handled the error.
1628 1581
1629 // child_process_launcher_ can be NULL in single process mode or if fast 1582 // child_process_launcher_ can be NULL in single process mode or if fast
1630 // termination happened. 1583 // termination happened.
1631 int exit_code = 0; 1584 int exit_code = 0;
(...skipping 175 matching lines...) Expand 10 before | Expand all | Expand 10 after
1807 // Skip widgets in other processes. 1760 // Skip widgets in other processes.
1808 if (widgets[i]->GetProcess()->GetID() != GetID()) 1761 if (widgets[i]->GetProcess()->GetID() != GetID())
1809 continue; 1762 continue;
1810 1763
1811 RenderViewHost* rvh = RenderViewHost::From(widgets[i]); 1764 RenderViewHost* rvh = RenderViewHost::From(widgets[i]);
1812 rvh->UpdateWebkitPreferences(rvh->GetWebkitPreferences()); 1765 rvh->UpdateWebkitPreferences(rvh->GetWebkitPreferences());
1813 } 1766 }
1814 } 1767 }
1815 1768
1816 } // namespace content 1769 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698