OLD | NEW |
| (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_embedder.h" | |
6 | |
7 #include <set> | |
8 | |
9 #include "base/logging.h" | |
10 #include "base/stl_util.h" | |
11 #include "base/time.h" | |
12 #include "content/browser/browser_plugin/browser_plugin_embedder_helper.h" | |
13 #include "content/browser/browser_plugin/browser_plugin_guest.h" | |
14 #include "content/browser/browser_plugin/browser_plugin_host_factory.h" | |
15 #include "content/browser/renderer_host/render_view_host_impl.h" | |
16 #include "content/browser/web_contents/web_contents_impl.h" | |
17 #include "content/public/browser/notification_details.h" | |
18 #include "content/public/browser/notification_service.h" | |
19 #include "content/public/browser/notification_source.h" | |
20 #include "content/public/browser/notification_types.h" | |
21 #include "content/public/browser/web_contents_view.h" | |
22 #include "content/public/common/url_constants.h" | |
23 #include "third_party/WebKit/Source/WebKit/chromium/public/WebInputEvent.h" | |
24 #include "ui/gfx/size.h" | |
25 #include "ui/surface/transport_dib.h" | |
26 | |
27 namespace content { | |
28 | |
29 // static | |
30 BrowserPluginHostFactory* BrowserPluginEmbedder::factory_ = NULL; | |
31 | |
32 BrowserPluginEmbedder::BrowserPluginEmbedder( | |
33 WebContentsImpl* web_contents, | |
34 RenderViewHost* render_view_host) | |
35 : WebContentsObserver(web_contents), | |
36 render_view_host_(render_view_host) { | |
37 // Listen to visibility changes so that an embedder hides its guests | |
38 // as well. | |
39 registrar_.Add(this, | |
40 NOTIFICATION_WEB_CONTENTS_VISIBILITY_CHANGED, | |
41 Source<WebContents>(web_contents)); | |
42 | |
43 // |render_view_host| manages the ownership of this BrowserPluginGuestHelper. | |
44 new BrowserPluginEmbedderHelper(this, render_view_host); | |
45 } | |
46 | |
47 BrowserPluginEmbedder::~BrowserPluginEmbedder() { | |
48 // Destroy guests that are managed by the current embedder. | |
49 DestroyGuests(); | |
50 } | |
51 | |
52 // static | |
53 BrowserPluginEmbedder* BrowserPluginEmbedder::Create( | |
54 WebContentsImpl* web_contents, | |
55 content::RenderViewHost* render_view_host) { | |
56 if (factory_) { | |
57 return factory_->CreateBrowserPluginEmbedder(web_contents, | |
58 render_view_host); | |
59 } | |
60 return new BrowserPluginEmbedder(web_contents, render_view_host); | |
61 } | |
62 | |
63 BrowserPluginGuest* BrowserPluginEmbedder::GetGuestByInstanceID( | |
64 int instance_id) const { | |
65 ContainerInstanceMap::const_iterator it = | |
66 guest_web_contents_by_instance_id_.find(instance_id); | |
67 if (it != guest_web_contents_by_instance_id_.end()) | |
68 return static_cast<WebContentsImpl*>(it->second)->GetBrowserPluginGuest(); | |
69 return NULL; | |
70 } | |
71 | |
72 void BrowserPluginEmbedder::AddGuest(int instance_id, | |
73 WebContents* guest_web_contents, | |
74 int64 frame_id) { | |
75 DCHECK(guest_web_contents_by_instance_id_.find(instance_id) == | |
76 guest_web_contents_by_instance_id_.end()); | |
77 guest_web_contents_by_instance_id_[instance_id] = guest_web_contents; | |
78 } | |
79 | |
80 void BrowserPluginEmbedder::NavigateGuest(RenderViewHost* render_view_host, | |
81 int instance_id, | |
82 int64 frame_id, | |
83 const std::string& src, | |
84 const gfx::Size& size) { | |
85 BrowserPluginGuest* guest = GetGuestByInstanceID(instance_id); | |
86 WebContentsImpl* guest_web_contents = NULL; | |
87 GURL url(src); | |
88 if (!guest) { | |
89 const std::string& host = | |
90 render_view_host->GetSiteInstance()->GetSite().host(); | |
91 guest_web_contents = WebContentsImpl::CreateGuest( | |
92 web_contents()->GetBrowserContext(), | |
93 host, | |
94 instance_id); | |
95 | |
96 guest = guest_web_contents->GetBrowserPluginGuest(); | |
97 guest->set_embedder_render_process_host( | |
98 render_view_host->GetProcess()); | |
99 | |
100 guest_web_contents->GetMutableRendererPrefs()-> | |
101 throttle_input_events = false; | |
102 AddGuest(instance_id, guest_web_contents, frame_id); | |
103 guest_web_contents->SetDelegate(guest); | |
104 } else { | |
105 guest_web_contents = static_cast<WebContentsImpl*>(guest->web_contents()); | |
106 } | |
107 | |
108 // We ignore loading empty urls in web_contents. | |
109 // If a guest sets empty src attribute after it has navigated to some | |
110 // non-empty page, the action is considered no-op. | |
111 // TODO(lazyboy): The js shim for browser-plugin might need to reflect empty | |
112 // src ignoring in the shadow DOM element: http://crbug.com/149001. | |
113 if (!src.empty()) { | |
114 guest_web_contents->GetController().LoadURL(url, | |
115 Referrer(), | |
116 PAGE_TRANSITION_AUTO_SUBFRAME, | |
117 std::string()); | |
118 } | |
119 | |
120 if (!size.IsEmpty()) | |
121 guest_web_contents->GetView()->SizeContents(size); | |
122 } | |
123 | |
124 void BrowserPluginEmbedder::UpdateRectACK(int instance_id, | |
125 int message_id, | |
126 const gfx::Size& size) { | |
127 BrowserPluginGuest* guest = GetGuestByInstanceID(instance_id); | |
128 if (guest) | |
129 guest->UpdateRectACK(message_id, size); | |
130 } | |
131 | |
132 void BrowserPluginEmbedder::ResizeGuest(int instance_id, | |
133 TransportDIB* damage_buffer, | |
134 #if defined(OS_WIN) | |
135 int damage_buffer_size, | |
136 #endif | |
137 int width, | |
138 int height, | |
139 bool resize_pending, | |
140 float scale_factor) { | |
141 BrowserPluginGuest* guest = GetGuestByInstanceID(instance_id); | |
142 if (!guest) | |
143 return; | |
144 WebContentsImpl* guest_web_contents = | |
145 static_cast<WebContentsImpl*>(guest->web_contents()); | |
146 guest->SetDamageBuffer(damage_buffer, | |
147 #if defined(OS_WIN) | |
148 damage_buffer_size, | |
149 #endif | |
150 gfx::Size(width, height), | |
151 scale_factor); | |
152 if (!resize_pending) | |
153 guest_web_contents->GetView()->SizeContents(gfx::Size(width, height)); | |
154 } | |
155 | |
156 void BrowserPluginEmbedder::SetFocus(int instance_id, | |
157 bool focused) { | |
158 BrowserPluginGuest* guest = GetGuestByInstanceID(instance_id); | |
159 if (guest) | |
160 guest->SetFocus(focused); | |
161 } | |
162 | |
163 void BrowserPluginEmbedder::DestroyGuests() { | |
164 STLDeleteContainerPairSecondPointers( | |
165 guest_web_contents_by_instance_id_.begin(), | |
166 guest_web_contents_by_instance_id_.end()); | |
167 guest_web_contents_by_instance_id_.clear(); | |
168 } | |
169 | |
170 void BrowserPluginEmbedder::HandleInputEvent(int instance_id, | |
171 RenderViewHost* render_view_host, | |
172 const gfx::Rect& guest_rect, | |
173 const WebKit::WebInputEvent& event, | |
174 IPC::Message* reply_message) { | |
175 BrowserPluginGuest* guest = GetGuestByInstanceID(instance_id); | |
176 if (guest) | |
177 guest->HandleInputEvent(render_view_host, guest_rect, event, reply_message); | |
178 } | |
179 | |
180 void BrowserPluginEmbedder::DestroyGuestByInstanceID(int instance_id) { | |
181 BrowserPluginGuest* guest = GetGuestByInstanceID(instance_id); | |
182 if (guest) { | |
183 WebContents* guest_web_contents = guest->web_contents(); | |
184 | |
185 // Destroy the guest's web_contents. | |
186 delete guest_web_contents; | |
187 guest_web_contents_by_instance_id_.erase(instance_id); | |
188 } | |
189 } | |
190 | |
191 void BrowserPluginEmbedder::RenderViewDeleted( | |
192 RenderViewHost* render_view_host) { | |
193 DestroyGuests(); | |
194 } | |
195 | |
196 void BrowserPluginEmbedder::RenderViewGone(base::TerminationStatus status) { | |
197 DestroyGuests(); | |
198 } | |
199 | |
200 void BrowserPluginEmbedder::WebContentsVisibilityChanged(bool visible) { | |
201 // If the embedder is hidden we need to hide the guests as well. | |
202 for (ContainerInstanceMap::const_iterator it = | |
203 guest_web_contents_by_instance_id_.begin(); | |
204 it != guest_web_contents_by_instance_id_.end(); ++it) { | |
205 WebContents* web_contents = it->second; | |
206 if (visible) | |
207 web_contents->WasShown(); | |
208 else | |
209 web_contents->WasHidden(); | |
210 } | |
211 } | |
212 | |
213 void BrowserPluginEmbedder::PluginDestroyed(int instance_id) { | |
214 DestroyGuestByInstanceID(instance_id); | |
215 } | |
216 | |
217 void BrowserPluginEmbedder::Observe(int type, | |
218 const NotificationSource& source, | |
219 const NotificationDetails& details) { | |
220 switch (type) { | |
221 case NOTIFICATION_WEB_CONTENTS_VISIBILITY_CHANGED: { | |
222 bool visible = *Details<bool>(details).ptr(); | |
223 WebContentsVisibilityChanged(visible); | |
224 break; | |
225 } | |
226 default: | |
227 NOTREACHED() << "Unexpected notification type: " << type; | |
228 } | |
229 } | |
230 | |
231 } // namespace content | |
OLD | NEW |