Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / content / browser / renderer_host / render_view_host_factory.cc
blobbfa5c98fff9dfe665189c61fba585c48a5dc65ea
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.
5 #include "content/browser/renderer_host/render_view_host_factory.h"
7 #include "base/logging.h"
8 #include "content/browser/gpu/gpu_surface_tracker.h"
9 #include "content/browser/renderer_host/render_view_host_impl.h"
11 namespace content {
13 // static
14 RenderViewHostFactory* RenderViewHostFactory::factory_ = NULL;
16 // static
17 RenderViewHost* RenderViewHostFactory::Create(
18 SiteInstance* instance,
19 RenderViewHostDelegate* delegate,
20 RenderWidgetHostDelegate* widget_delegate,
21 int32 routing_id,
22 int32 main_frame_routing_id,
23 bool swapped_out,
24 bool hidden) {
25 int32 surface_id;
26 // RenderViewHost creation can be either browser-driven (by the user opening a
27 // new tab) or renderer-driven (by script calling window.open, etc).
29 // In the browser-driven case, the routing ID of the view is lazily assigned:
30 // this is signified by passing MSG_ROUTING_NONE for |routing_id|.
31 if (routing_id == MSG_ROUTING_NONE) {
32 routing_id = instance->GetProcess()->GetNextRoutingID();
33 // No surface has yet been created for the RVH.
34 surface_id = GpuSurfaceTracker::Get()->AddSurfaceForRenderer(
35 instance->GetProcess()->GetID(), routing_id);
36 } else {
37 // Otherwise, in the renderer-driven case, the routing ID of the view is
38 // already set. This is due to the fact that a sync render->browser IPC is
39 // involved. In order to quickly reply to the sync IPC, the routing IDs are
40 // assigned as early as possible. The IO thread immediately sends a reply to
41 // the sync IPC, while deferring the creation of the actual Host objects to
42 // the UI thread. In this case, a surface already exists for the RVH.
43 surface_id = GpuSurfaceTracker::Get()->LookupSurfaceForRenderer(
44 instance->GetProcess()->GetID(), routing_id);
46 if (factory_) {
47 return factory_->CreateRenderViewHost(instance, delegate, widget_delegate,
48 routing_id, surface_id,
49 main_frame_routing_id, swapped_out);
51 return new RenderViewHostImpl(instance, delegate, widget_delegate, routing_id,
52 surface_id, main_frame_routing_id, swapped_out,
53 hidden, true /* has_initialized_audio_host */);
56 // static
57 void RenderViewHostFactory::RegisterFactory(RenderViewHostFactory* factory) {
58 DCHECK(!factory_) << "Can't register two factories at once.";
59 factory_ = factory;
62 // static
63 void RenderViewHostFactory::UnregisterFactory() {
64 DCHECK(factory_) << "No factory to unregister.";
65 factory_ = NULL;
68 } // namespace content