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

Side by Side Diff: content/browser/browser_plugin/browser_plugin_host.cc

Issue 9609008: Implemented Browser Plugin (NOT FOR REVIEW) (Closed) Base URL: http://git.chromium.org/git/chromium.git@trunk
Patch Set: Updated according to creis@'s comments Created 8 years, 7 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
(Empty)
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 #include "content/browser/browser_plugin/browser_plugin_host.h"
6
7 #include "content/browser/renderer_host/render_view_host_impl.h"
8 #include "content/browser/web_contents/web_contents_impl.h"
9 #include "content/common/browser_plugin_messages.h"
10 #include "content/public/browser/notification_details.h"
11 #include "content/public/browser/notification_source.h"
12 #include "content/public/browser/notification_types.h"
13 #include "content/public/browser/render_process_host.h"
14 #include "content/public/browser/render_widget_host_view.h"
15 #include "content/public/browser/site_instance.h"
16 #include "ppapi/proxy/ppapi_messages.h"
17
18 namespace content {
19
20 BrowserPluginHost::BrowserPluginHost(
21 WebContentsImpl* web_contents)
22 : WebContentsObserver(web_contents),
23 embedder_(NULL),
24 instance_id_(0) {
25 registrar_.Add(this,
26 NOTIFICATION_RENDER_WIDGET_VISIBILITY_CHANGED,
27 Source<RenderViewHost>(
28 web_contents->GetRenderViewHost()));
29 }
30
31 BrowserPluginHost::~BrowserPluginHost() {
32 }
33
34 BrowserPluginHost* BrowserPluginHost::GetGuestByContainerID(int container_id) {
35 ContainerInstanceMap::const_iterator it =
36 guests_by_container_id_.find(container_id);
37 if (it != guests_by_container_id_.end())
38 return it->second;
39 return NULL;
40 }
41
42 void BrowserPluginHost::RegisterContainerInstance(
43 int container_id,
44 BrowserPluginHost* observer) {
45 DCHECK(guests_by_container_id_.find(container_id) ==
46 guests_by_container_id_.end());
47 guests_by_container_id_[container_id] = observer;
48 }
49
50 bool BrowserPluginHost::OnMessageReceived(const IPC::Message& message) {
51 bool handled = true;
52 IPC_BEGIN_MESSAGE_MAP(BrowserPluginHost, message)
53 IPC_MESSAGE_HANDLER(BrowserPluginHostMsg_ConnectToChannel,
54 OnConnectToChannel)
55 IPC_MESSAGE_HANDLER(BrowserPluginHostMsg_NavigateFromEmbedder,
56 OnNavigateFromEmbedder)
57 IPC_MESSAGE_HANDLER(BrowserPluginHostMsg_NavigateFromGuest,
58 OnNavigateFromGuest)
59 IPC_MESSAGE_HANDLER(BrowserPluginHostMsg_MapInstance,
60 OnMapInstance)
61 IPC_MESSAGE_HANDLER(BrowserPluginHostMsg_ResizeGuest, OnResizeGuest)
62 IPC_MESSAGE_UNHANDLED(handled = false)
63 IPC_END_MESSAGE_MAP()
64 return handled;
65 }
66
67 void BrowserPluginHost::OnResizeGuest(int width, int height) {
68 // Tell the RenderWidgetHostView to adjust its size.
69 web_contents()->GetRenderViewHost()->GetView()->SetSize(
70 gfx::Size(width, height));
71 }
72
73 void BrowserPluginHost::OnNavigateFromEmbedder(
74 int instance_id,
75 long long frame_id,
76 const std::string& src,
77 const gfx::Size& size) {
78 BrowserPluginHost* guest_observer = GetGuestByContainerID(instance_id);
79 WebContentsImpl* guest_web_contents =
80 guest_observer ?
81 static_cast<WebContentsImpl*>(guest_observer->web_contents()): NULL;
82 if (!guest_observer) {
83 GURL url(src);
84 guest_web_contents =
85 static_cast<WebContentsImpl*>(
86 WebContents::Create(
87 web_contents()->GetBrowserContext(),
88 web_contents()->GetSiteInstance(),
89 MSG_ROUTING_NONE,
90 NULL, // base WebContents
91 NULL // session storage namespace
92 ));
93 guest_observer =
94 guest_web_contents->browser_plugin_host();
95 guest_observer->set_embedder(static_cast<WebContentsImpl*>(web_contents()));
96 guest_observer->set_instance_id(instance_id);
97 guest_observer->set_url(url);
98 guest_observer->set_initial_size(size);
99 RegisterContainerInstance(instance_id, guest_observer);
100 AddGuest(guest_web_contents, frame_id);
101 }
102
103 // TODO(fsamuel): Set the WebContentsDelegate here.
104 guest_observer->web_contents()->GetController().LoadURL(
105 guest_observer->url(),
106 Referrer(),
107 PAGE_TRANSITION_HOME_PAGE,
108 std::string());
109 }
110
111 void BrowserPluginHost::OnNavigateFromGuest(
112 PP_Instance instance,
113 const std::string& src) {
114 DCHECK(embedder());
115 GURL url(src);
116 web_contents()->GetController().LoadURL(
117 url,
118 Referrer(),
119 PAGE_TRANSITION_HOME_PAGE,
120 std::string());
121 }
122
123 void BrowserPluginHost::OnMapInstance(int container_id, PP_Instance instance) {
124 BrowserPluginHost* guest_observer = GetGuestByContainerID(container_id);
125 WebContentsImpl* guest_web_contents =
126 static_cast<WebContentsImpl*>(guest_observer->web_contents());
127 RenderViewHost* rvh = guest_web_contents->GetPendingRenderViewHost() ?
128 guest_web_contents->GetPendingRenderViewHost() :
129 guest_web_contents->GetRenderViewHost();
130 // TODO(fsamuel): This is wrong. The current size of the plugin container
131 // might not be equal to initial size on subsequent navigations.
132 // BrowserPluginHost_MapInstance should also report the current size of
133 // the container to the browser process.
134 rvh->GetView()->SetSize(guest_observer->initial_size());
135
136 rvh->Send(new BrowserPluginMsg_CompleteNavigation(
137 rvh->GetRoutingID(),
138 instance));
139 }
140
141 void BrowserPluginHost::OnConnectToChannel(
142 const IPC::ChannelHandle& channel_handle) {
143 DCHECK(embedder());
144 WebContentsImpl* embedder_web_contents =
145 static_cast<WebContentsImpl*>(web_contents());
146 RenderViewHost* rvh =
147 embedder_web_contents->GetPendingRenderViewHost() ?
148 embedder_web_contents->GetPendingRenderViewHost() :
149 embedder_web_contents->GetRenderViewHost();
150 // Tell the BrowserPlugin in the embedder that we're done and that it can
151 // begin using the guest renderer.
152 embedder()->GetRenderProcessHost()->Send(
153 new BrowserPluginMsg_LoadGuest(
154 instance_id(),
155 rvh->GetProcess()->GetID(),
156 channel_handle));
157 }
158
159 void BrowserPluginHost::AddGuest(WebContentsImpl* guest, int64 frame_id) {
160 guests_[guest] = frame_id;
161 }
162
163 void BrowserPluginHost::RemoveGuest(WebContentsImpl* guest) {
164 guests_.erase(guest);
165 }
166
167 void BrowserPluginHost::DestroyGuests() {
168 for (GuestMap::const_iterator it = guests_.begin();
169 it != guests_.end(); ++it) {
170 WebContentsImpl* web_contents = it->first;
171 delete web_contents;
172 }
173 guests_.clear();
174 guests_by_container_id_.clear();
175 }
176
177 void BrowserPluginHost::DidCommitProvisionalLoadForFrame(
178 int64 frame_id,
179 bool is_main_frame,
180 const GURL& url,
181 PageTransition transition_type) {
182 typedef std::set<WebContentsImpl*> GuestSet;
183 GuestSet guests_to_delete;
184 for (GuestMap::const_iterator it = guests_.begin();
185 it != guests_.end(); ++it) {
186 WebContentsImpl* web_contents = it->first;
187 if (it->second == frame_id) {
188 guests_to_delete.insert(web_contents);
189 }
190 }
191 for (GuestSet::const_iterator it = guests_to_delete.begin();
192 it != guests_to_delete.end(); ++it) {
193 delete *it;
194 guests_.erase(*it);
195 }
196 }
197
198 void BrowserPluginHost::RenderViewDeleted(RenderViewHost* render_view_host) {
199 DestroyGuests();
200 }
201
202 void BrowserPluginHost::RenderViewGone(base::TerminationStatus status) {
203 DestroyGuests();
204 }
205
206 void BrowserPluginHost::WebContentsDestroyed(WebContents* web_contents) {
207 DestroyGuests();
208 }
209
210 void BrowserPluginHost::Observe(
211 int type,
212 const NotificationSource& source,
213 const NotificationDetails& details) {
214 DCHECK(type == NOTIFICATION_RENDER_WIDGET_VISIBILITY_CHANGED);
215 bool visible = *Details<bool>(details).ptr();
216
217 // If the embedder is hidden we need to hide the guests as well.
218 for (GuestMap::const_iterator it = guests_.begin();
219 it != guests_.end(); ++it) {
220 WebContentsImpl* web_contents = it->first;
221 if (visible)
222 web_contents->ShowContents();
223 else
224 web_contents->HideContents();
225 }
226 }
227
228 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/browser_plugin/browser_plugin_host.h ('k') | content/browser/browser_plugin/example.html » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698