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 // A BrowserPluginEmbedder has a list of guests it manages.
6 // In the beginning when a renderer sees one or more guests (BrowserPlugin
7 // instance(s)) and there is a request to navigate to them, the WebContents for
8 // that renderer creates a BrowserPluginEmbedder for itself. The
9 // BrowserPluginEmbedder, in turn, manages a set of BrowserPluginGuests -- one
10 // BrowserPluginGuest for each guest in the embedding WebContents. Note that
11 // each of these BrowserPluginGuest objects has its own WebContents.
12 // BrowserPluginEmbedder routes any messages directed to a guest from the
13 // renderer (BrowserPlugin) to the appropriate guest (identified by the guest's
16 // BrowserPluginEmbedder is responsible for cleaning up the guests when the
17 // embedder frame navigates away to a different page or deletes the guests from
20 #ifndef CONTENT_BROWSER_BROWSER_PLUGIN_BROWSER_PLUGIN_EMBEDDER_H_
21 #define CONTENT_BROWSER_BROWSER_PLUGIN_BROWSER_PLUGIN_EMBEDDER_H_
25 #include "content/public/browser/notification_observer.h"
26 #include "content/public/browser/notification_registrar.h"
27 #include "content/public/browser/web_contents.h"
28 #include "content/public/browser/web_contents_observer.h"
30 struct BrowserPluginHostMsg_CreateGuest_Params
;
31 struct BrowserPluginHostMsg_ResizeGuest_Params
;
39 class BrowserPluginGuest
;
40 class BrowserPluginHostFactory
;
41 class WebContentsImpl
;
43 // A browser plugin embedder provides functionality for WebContents to operate
44 // in the 'embedder' role. It manages list of guests inside the embedder.
46 // The embedder's WebContents manages the lifetime of the embedder. They are
47 // created when a renderer asks WebContents to navigate (for the first time) to
48 // some guest. It gets destroyed when either the WebContents goes away or there
49 // is a RenderViewHost swap in WebContents.
50 class CONTENT_EXPORT BrowserPluginEmbedder
: public WebContentsObserver
,
51 public NotificationObserver
{
53 typedef std::map
<int, WebContents
*> ContainerInstanceMap
;
55 virtual ~BrowserPluginEmbedder();
57 static BrowserPluginEmbedder
* Create(WebContentsImpl
* web_contents
,
58 RenderViewHost
* render_view_host
);
60 // Create a guest WebContents with the provided |instance_id| and |params| and
61 // add it to this BrowserPluginEmbedder. Optionally, the new guest may be
62 // attached to a |guest_opener|, and may be attached to a pre-selected
64 void CreateGuest(int instance_id
,
66 BrowserPluginGuest
* guest_opener
,
67 const BrowserPluginHostMsg_CreateGuest_Params
& params
);
69 // Returns a guest browser plugin delegate by its container ID specified
71 BrowserPluginGuest
* GetGuestByInstanceID(int instance_id
) const;
73 // Destroy the guest with the provided |instance_id|. Remove references to the
74 // guest in this BrowserPluginEmbedder.
75 void DestroyGuestByInstanceID(int instance_id
);
77 // Overrides factory for testing. Default (NULL) value indicates regular
78 // (non-test) environment.
79 static void set_factory_for_testing(BrowserPluginHostFactory
* factory
) {
83 bool visible() const { return visible_
; }
85 // Returns the RenderViewHost at a point (|x|, |y|) asynchronously via
86 // |callback|. We need a roundtrip to renderer process to get this
88 void GetRenderViewHostAtPosition(
91 const WebContents::GetRenderViewHostCallback
& callback
);
93 // WebContentsObserver implementation.
94 virtual void RenderViewDeleted(RenderViewHost
* render_view_host
) OVERRIDE
;
95 virtual void RenderViewGone(base::TerminationStatus status
) OVERRIDE
;
96 virtual bool OnMessageReceived(const IPC::Message
& message
) OVERRIDE
;
98 // NotificationObserver method override.
99 virtual void Observe(int type
,
100 const NotificationSource
& source
,
101 const NotificationDetails
& details
) OVERRIDE
;
104 friend class TestBrowserPluginEmbedder
;
106 BrowserPluginEmbedder(WebContentsImpl
* web_contents
,
107 RenderViewHost
* render_view_host
);
109 // Adds a new guest web_contents to the embedder (overridable in test).
110 virtual void AddGuest(int instance_id
, WebContents
* guest_web_contents
);
113 // Called when visiblity of web_contents changes, so the embedder will
114 // show/hide its guest.
115 void WebContentsVisibilityChanged(bool visible
);
117 static bool ShouldForwardToBrowserPluginGuest(const IPC::Message
& message
);
121 void OnCreateGuest(int instance_id
,
122 const BrowserPluginHostMsg_CreateGuest_Params
& params
);
123 void OnPluginAtPositionResponse(int instance_id
,
125 const gfx::Point
& position
);
126 void OnPluginDestroyed(int instance_id
);
127 void OnSwapBuffersACK(int route_id
,
129 const std::string
& mailbox_name
,
132 // Static factory instance (always NULL for non-test).
133 static BrowserPluginHostFactory
* factory_
;
135 // A scoped container for notification registries.
136 NotificationRegistrar registrar_
;
138 // Contains guests' WebContents, mapping from their instance ids.
139 ContainerInstanceMap guest_web_contents_by_instance_id_
;
140 RenderViewHost
* render_view_host_
;
141 // Tracks the visibility state of the embedder.
143 // Map that contains outstanding queries to |GetBrowserPluginAt|.
144 // We need a roundtrip to renderer process to know the answer, therefore
145 // storing these callbacks is required.
146 typedef std::map
<int, WebContents::GetRenderViewHostCallback
>
147 GetRenderViewHostCallbackMap
;
148 GetRenderViewHostCallbackMap pending_get_render_view_callbacks_
;
149 // Next request id for BrowserPluginMsg_PluginAtPositionRequest query.
150 int next_get_render_view_request_id_
;
152 DISALLOW_COPY_AND_ASSIGN(BrowserPluginEmbedder
);
155 } // namespace content
157 #endif // CONTENT_BROWSER_BROWSER_PLUGIN_BROWSER_PLUGIN_EMBEDDER_H_