[refactor] More post-NSS WebCrypto cleanups (utility functions).
[chromium-blink-merge.git] / content / browser / frame_host / render_widget_host_view_child_frame.cc
blob44f83df62433b765975b92ebbfecc48491dbc7e1
1 // Copyright 2014 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/frame_host/render_widget_host_view_child_frame.h"
7 #include "cc/surfaces/surface.h"
8 #include "cc/surfaces/surface_factory.h"
9 #include "cc/surfaces/surface_manager.h"
10 #include "cc/surfaces/surface_sequence.h"
11 #include "content/browser/accessibility/browser_accessibility_manager.h"
12 #include "content/browser/browser_plugin/browser_plugin_guest.h"
13 #include "content/browser/compositor/surface_utils.h"
14 #include "content/browser/frame_host/cross_process_frame_connector.h"
15 #include "content/browser/gpu/compositor_util.h"
16 #include "content/browser/renderer_host/render_view_host_impl.h"
17 #include "content/browser/renderer_host/render_widget_host_delegate.h"
18 #include "content/browser/renderer_host/render_widget_host_impl.h"
19 #include "content/browser/renderer_host/render_widget_host_input_event_router.h"
20 #include "content/common/gpu/gpu_messages.h"
21 #include "content/common/view_messages.h"
22 #include "content/public/browser/render_process_host.h"
23 #include "content/public/common/browser_plugin_guest_mode.h"
24 #include "ui/gfx/geometry/size_conversions.h"
25 #include "ui/gfx/geometry/size_f.h"
27 namespace content {
29 RenderWidgetHostViewChildFrame::RenderWidgetHostViewChildFrame(
30 RenderWidgetHost* widget_host)
31 : host_(RenderWidgetHostImpl::From(widget_host)),
32 use_surfaces_(UseSurfacesEnabled()),
33 next_surface_sequence_(1u),
34 last_output_surface_id_(0),
35 current_surface_scale_factor_(1.f),
36 ack_pending_count_(0),
37 frame_connector_(nullptr),
38 weak_factory_(this) {
39 if (use_surfaces_) {
40 id_allocator_ = CreateSurfaceIdAllocator();
41 if (host_->delegate() && host_->delegate()->GetInputEventRouter()) {
42 host_->delegate()->GetInputEventRouter()->AddSurfaceIdNamespaceOwner(
43 GetSurfaceIdNamespace(), this);
47 host_->SetView(this);
50 RenderWidgetHostViewChildFrame::~RenderWidgetHostViewChildFrame() {
51 if (!surface_id_.is_null())
52 surface_factory_->Destroy(surface_id_);
55 void RenderWidgetHostViewChildFrame::InitAsChild(
56 gfx::NativeView parent_view) {
57 NOTREACHED();
60 RenderWidgetHost* RenderWidgetHostViewChildFrame::GetRenderWidgetHost() const {
61 return host_;
64 void RenderWidgetHostViewChildFrame::SetSize(const gfx::Size& size) {
65 host_->WasResized();
68 void RenderWidgetHostViewChildFrame::SetBounds(const gfx::Rect& rect) {
69 SetSize(rect.size());
72 void RenderWidgetHostViewChildFrame::Focus() {
75 bool RenderWidgetHostViewChildFrame::HasFocus() const {
76 return false;
79 bool RenderWidgetHostViewChildFrame::IsSurfaceAvailableForCopy() const {
80 NOTIMPLEMENTED();
81 return false;
84 void RenderWidgetHostViewChildFrame::Show() {
85 if (!host_->is_hidden())
86 return;
87 host_->WasShown(ui::LatencyInfo());
90 void RenderWidgetHostViewChildFrame::Hide() {
91 if (host_->is_hidden())
92 return;
93 host_->WasHidden();
96 bool RenderWidgetHostViewChildFrame::IsShowing() {
97 return !host_->is_hidden();
100 gfx::Rect RenderWidgetHostViewChildFrame::GetViewBounds() const {
101 gfx::Rect rect;
102 if (frame_connector_)
103 rect = frame_connector_->ChildFrameRect();
104 return rect;
107 gfx::Vector2dF RenderWidgetHostViewChildFrame::GetLastScrollOffset() const {
108 return last_scroll_offset_;
111 gfx::NativeView RenderWidgetHostViewChildFrame::GetNativeView() const {
112 NOTREACHED();
113 return NULL;
116 gfx::NativeViewId RenderWidgetHostViewChildFrame::GetNativeViewId() const {
117 NOTREACHED();
118 return 0;
121 gfx::NativeViewAccessible
122 RenderWidgetHostViewChildFrame::GetNativeViewAccessible() {
123 NOTREACHED();
124 return NULL;
127 void RenderWidgetHostViewChildFrame::SetBackgroundColor(SkColor color) {
130 gfx::Size RenderWidgetHostViewChildFrame::GetPhysicalBackingSize() const {
131 gfx::Size size;
132 if (frame_connector_) {
133 size = gfx::ToCeiledSize(
134 gfx::ScaleSize(frame_connector_->ChildFrameRect().size(),
135 frame_connector_->device_scale_factor()));
137 return size;
140 void RenderWidgetHostViewChildFrame::InitAsPopup(
141 RenderWidgetHostView* parent_host_view,
142 const gfx::Rect& bounds) {
143 NOTREACHED();
146 void RenderWidgetHostViewChildFrame::InitAsFullscreen(
147 RenderWidgetHostView* reference_host_view) {
148 NOTREACHED();
151 void RenderWidgetHostViewChildFrame::ImeCancelComposition() {
152 // TODO(kenrb): Fix OOPIF Ime.
155 void RenderWidgetHostViewChildFrame::ImeCompositionRangeChanged(
156 const gfx::Range& range,
157 const std::vector<gfx::Rect>& character_bounds) {
158 // TODO(kenrb): Fix OOPIF Ime.
161 void RenderWidgetHostViewChildFrame::MovePluginWindows(
162 const std::vector<WebPluginGeometry>& moves) {
165 void RenderWidgetHostViewChildFrame::UpdateCursor(const WebCursor& cursor) {
168 void RenderWidgetHostViewChildFrame::SetIsLoading(bool is_loading) {
169 // It is valid for an inner WebContents's SetIsLoading() to end up here.
170 // This is because an inner WebContents's main frame's RenderWidgetHostView
171 // is a RenderWidgetHostViewChildFrame. In contrast, when there is no
172 // inner/outer WebContents, only subframe's RenderWidgetHostView can be a
173 // RenderWidgetHostViewChildFrame which do not get a SetIsLoading() call.
174 if (BrowserPluginGuestMode::UseCrossProcessFramesForGuests() &&
175 BrowserPluginGuest::IsGuest(
176 static_cast<RenderViewHostImpl*>(RenderViewHost::From(host_)))) {
177 return;
180 NOTREACHED();
183 void RenderWidgetHostViewChildFrame::TextInputStateChanged(
184 const ViewHostMsg_TextInputState_Params& params) {
185 // TODO(kenrb): Implement.
188 void RenderWidgetHostViewChildFrame::RenderProcessGone(
189 base::TerminationStatus status,
190 int error_code) {
191 if (frame_connector_)
192 frame_connector_->RenderProcessGone();
193 Destroy();
196 void RenderWidgetHostViewChildFrame::Destroy() {
197 if (frame_connector_) {
198 frame_connector_->set_view(NULL);
199 frame_connector_ = NULL;
202 if (use_surfaces_ && host_->delegate() &&
203 host_->delegate()->GetInputEventRouter()) {
204 host_->delegate()->GetInputEventRouter()->RemoveSurfaceIdNamespaceOwner(
205 GetSurfaceIdNamespace());
208 host_->SetView(NULL);
209 host_ = NULL;
210 base::MessageLoop::current()->DeleteSoon(FROM_HERE, this);
213 void RenderWidgetHostViewChildFrame::SetTooltipText(
214 const base::string16& tooltip_text) {
217 void RenderWidgetHostViewChildFrame::SelectionChanged(
218 const base::string16& text,
219 size_t offset,
220 const gfx::Range& range) {
223 void RenderWidgetHostViewChildFrame::SelectionBoundsChanged(
224 const ViewHostMsg_SelectionBounds_Params& params) {
227 #if defined(OS_ANDROID)
228 void RenderWidgetHostViewChildFrame::LockCompositingSurface() {
231 void RenderWidgetHostViewChildFrame::UnlockCompositingSurface() {
233 #endif
235 void RenderWidgetHostViewChildFrame::SurfaceDrawn(uint32 output_surface_id,
236 cc::SurfaceDrawStatus drawn) {
237 cc::CompositorFrameAck ack;
238 DCHECK(ack_pending_count_ > 0);
239 if (!surface_returned_resources_.empty())
240 ack.resources.swap(surface_returned_resources_);
241 if (host_) {
242 host_->Send(new ViewMsg_SwapCompositorFrameAck(host_->GetRoutingID(),
243 output_surface_id, ack));
245 ack_pending_count_--;
248 void RenderWidgetHostViewChildFrame::OnSwapCompositorFrame(
249 uint32 output_surface_id,
250 scoped_ptr<cc::CompositorFrame> frame) {
251 last_scroll_offset_ = frame->metadata.root_scroll_offset;
253 if (!frame_connector_)
254 return;
256 // When not using surfaces, the frame just gets proxied to
257 // the embedder's renderer to be composited.
258 if (!frame->delegated_frame_data || !use_surfaces_) {
259 frame_connector_->ChildFrameCompositorFrameSwapped(
260 output_surface_id,
261 host_->GetProcess()->GetID(),
262 host_->GetRoutingID(),
263 frame.Pass());
264 return;
267 cc::RenderPass* root_pass =
268 frame->delegated_frame_data->render_pass_list.back();
270 gfx::Size frame_size = root_pass->output_rect.size();
271 float scale_factor = frame->metadata.device_scale_factor;
273 // Check whether we need to recreate the cc::Surface, which means the child
274 // frame renderer has changed its output surface, or size, or scale factor.
275 if (output_surface_id != last_output_surface_id_ && surface_factory_) {
276 surface_factory_->Destroy(surface_id_);
277 surface_factory_.reset();
279 if (output_surface_id != last_output_surface_id_ ||
280 frame_size != current_surface_size_ ||
281 scale_factor != current_surface_scale_factor_) {
282 ClearCompositorSurfaceIfNecessary();
283 last_output_surface_id_ = output_surface_id;
284 current_surface_size_ = frame_size;
285 current_surface_scale_factor_ = scale_factor;
288 if (!surface_factory_) {
289 cc::SurfaceManager* manager = GetSurfaceManager();
290 surface_factory_ = make_scoped_ptr(new cc::SurfaceFactory(manager, this));
293 if (surface_id_.is_null()) {
294 surface_id_ = id_allocator_->GenerateId();
295 surface_factory_->Create(surface_id_);
297 cc::SurfaceSequence sequence = cc::SurfaceSequence(
298 id_allocator_->id_namespace(), next_surface_sequence_++);
299 // The renderer process will satisfy this dependency when it creates a
300 // SurfaceLayer.
301 cc::SurfaceManager* manager = GetSurfaceManager();
302 manager->GetSurfaceForId(surface_id_)->AddDestructionDependency(sequence);
303 frame_connector_->SetChildFrameSurface(surface_id_, frame_size,
304 scale_factor, sequence);
307 cc::SurfaceFactory::DrawCallback ack_callback =
308 base::Bind(&RenderWidgetHostViewChildFrame::SurfaceDrawn, AsWeakPtr(),
309 output_surface_id);
310 ack_pending_count_++;
311 // If this value grows very large, something is going wrong.
312 DCHECK(ack_pending_count_ < 1000);
313 surface_factory_->SubmitCompositorFrame(surface_id_, frame.Pass(),
314 ack_callback);
317 void RenderWidgetHostViewChildFrame::GetScreenInfo(
318 blink::WebScreenInfo* results) {
319 if (!frame_connector_)
320 return;
321 frame_connector_->GetScreenInfo(results);
324 bool RenderWidgetHostViewChildFrame::GetScreenColorProfile(
325 std::vector<char>* color_profile) {
326 if (!frame_connector_)
327 return false;
328 DCHECK(color_profile->empty());
329 NOTIMPLEMENTED(); // TODO(port): Implement.
330 return false;
333 gfx::Rect RenderWidgetHostViewChildFrame::GetBoundsInRootWindow() {
334 // We do not have any root window specific parts in this view.
335 return GetViewBounds();
338 #if defined(USE_AURA)
339 void RenderWidgetHostViewChildFrame::ProcessAckedTouchEvent(
340 const TouchEventWithLatencyInfo& touch,
341 InputEventAckState ack_result) {
343 #endif // defined(USE_AURA)
345 bool RenderWidgetHostViewChildFrame::LockMouse() {
346 return false;
349 void RenderWidgetHostViewChildFrame::UnlockMouse() {
352 uint32_t RenderWidgetHostViewChildFrame::GetSurfaceIdNamespace() {
353 if (!use_surfaces_)
354 return 0;
356 return id_allocator_->id_namespace();
359 void RenderWidgetHostViewChildFrame::ProcessKeyboardEvent(
360 const NativeWebKeyboardEvent& event) {
361 host_->ForwardKeyboardEvent(event);
364 void RenderWidgetHostViewChildFrame::ProcessMouseEvent(
365 const blink::WebMouseEvent& event) {
366 host_->ForwardMouseEvent(event);
369 void RenderWidgetHostViewChildFrame::ProcessMouseWheelEvent(
370 const blink::WebMouseWheelEvent& event) {
371 if (event.deltaX != 0 || event.deltaY != 0)
372 host_->ForwardWheelEvent(event);
375 #if defined(OS_MACOSX)
376 void RenderWidgetHostViewChildFrame::SetActive(bool active) {
379 void RenderWidgetHostViewChildFrame::SetWindowVisibility(bool visible) {
382 void RenderWidgetHostViewChildFrame::WindowFrameChanged() {
385 void RenderWidgetHostViewChildFrame::ShowDefinitionForSelection() {
388 bool RenderWidgetHostViewChildFrame::SupportsSpeech() const {
389 return false;
392 void RenderWidgetHostViewChildFrame::SpeakSelection() {
395 bool RenderWidgetHostViewChildFrame::IsSpeaking() const {
396 return false;
399 void RenderWidgetHostViewChildFrame::StopSpeaking() {
402 bool RenderWidgetHostViewChildFrame::PostProcessEventForPluginIme(
403 const NativeWebKeyboardEvent& event) {
404 return false;
406 #endif // defined(OS_MACOSX)
408 void RenderWidgetHostViewChildFrame::CopyFromCompositingSurface(
409 const gfx::Rect& src_subrect,
410 const gfx::Size& /* dst_size */,
411 ReadbackRequestCallback& callback,
412 const SkColorType preferred_color_type) {
413 callback.Run(SkBitmap(), READBACK_FAILED);
416 void RenderWidgetHostViewChildFrame::CopyFromCompositingSurfaceToVideoFrame(
417 const gfx::Rect& src_subrect,
418 const scoped_refptr<media::VideoFrame>& target,
419 const base::Callback<void(bool)>& callback) {
420 NOTIMPLEMENTED();
421 callback.Run(false);
424 bool RenderWidgetHostViewChildFrame::CanCopyToVideoFrame() const {
425 return false;
428 bool RenderWidgetHostViewChildFrame::HasAcceleratedSurface(
429 const gfx::Size& desired_size) {
430 return false;
433 gfx::GLSurfaceHandle RenderWidgetHostViewChildFrame::GetCompositingSurface() {
434 return gfx::GLSurfaceHandle(gfx::kNullPluginWindow, gfx::NULL_TRANSPORT);
437 #if defined(OS_WIN)
438 void RenderWidgetHostViewChildFrame::SetParentNativeViewAccessible(
439 gfx::NativeViewAccessible accessible_parent) {
442 gfx::NativeViewId RenderWidgetHostViewChildFrame::GetParentForWindowlessPlugin()
443 const {
444 return NULL;
446 #endif // defined(OS_WIN)
448 // cc::SurfaceFactoryClient implementation.
449 void RenderWidgetHostViewChildFrame::ReturnResources(
450 const cc::ReturnedResourceArray& resources) {
451 if (resources.empty())
452 return;
454 if (!ack_pending_count_ && host_) {
455 cc::CompositorFrameAck ack;
456 std::copy(resources.begin(), resources.end(),
457 std::back_inserter(ack.resources));
458 host_->Send(new ViewMsg_ReclaimCompositorResources(
459 host_->GetRoutingID(), last_output_surface_id_, ack));
460 return;
463 std::copy(resources.begin(), resources.end(),
464 std::back_inserter(surface_returned_resources_));
467 BrowserAccessibilityManager*
468 RenderWidgetHostViewChildFrame::CreateBrowserAccessibilityManager(
469 BrowserAccessibilityDelegate* delegate) {
470 return BrowserAccessibilityManager::Create(
471 BrowserAccessibilityManager::GetEmptyDocument(), delegate);
474 void RenderWidgetHostViewChildFrame::ClearCompositorSurfaceIfNecessary() {
475 if (surface_factory_ && !surface_id_.is_null())
476 surface_factory_->Destroy(surface_id_);
477 surface_id_ = cc::SurfaceId();
480 } // namespace content