OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 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 "components/framelet/renderer/framelet_container.h" |
| 6 |
| 7 #include "cc/blink/web_layer_impl.h" |
| 8 #include "cc/layers/solid_color_layer.h" |
| 9 #include "cc/layers/surface_layer.h" |
| 10 #include "components/framelet/common/framelet.mojom.h" |
| 11 #include "components/framelet/common/framelet_constants.h" |
| 12 #include "components/framelet/common/value_type_converters.h" |
| 13 #include "components/guest_view/common/guest_view_constants.h" |
| 14 #include "components/guest_view/common/guest_view_messages.h" |
| 15 #include "components/guest_view/renderer/guest_view_request.h" |
| 16 #include "content/public/common/service_registry.h" |
| 17 #include "content/public/renderer/render_frame.h" |
| 18 #include "content/public/renderer/render_thread.h" |
| 19 #include "content/public/renderer/render_view.h" |
| 20 #include "grit/components_scaled_resources.h" |
| 21 #include "mojo/converters/blink/blink_input_events_type_converters.h" |
| 22 #include "mojo/converters/geometry/geometry_type_converters.h" |
| 23 #include "mojo/converters/surfaces/surfaces_type_converters.h" |
| 24 #include "mojo/public/cpp/bindings/map.h" |
| 25 #include "third_party/WebKit/public/web/WebDocument.h" |
| 26 #include "third_party/WebKit/public/web/WebFramelet.h" |
| 27 #include "third_party/WebKit/public/web/WebInputEvent.h" |
| 28 #include "third_party/WebKit/public/web/WebLocalFrame.h" |
| 29 #include "ui/base/resource/resource_bundle.h" |
| 30 |
| 31 namespace framelet { |
| 32 |
| 33 FrameletContainer::FrameletContainer(content::RenderFrame* render_frame, |
| 34 const GURL& url, |
| 35 IPC::Sender* thread_safe_sender) |
| 36 : GuestViewContainer(render_frame), |
| 37 guest_instance_id_(guest_view::kInstanceIDNone), |
| 38 focused_(false), |
| 39 visible_(false), |
| 40 killed_(false), |
| 41 url_(url), |
| 42 thread_safe_sender_(thread_safe_sender), |
| 43 framelet_(nullptr), |
| 44 solid_layer_( |
| 45 cc::SolidColorLayer::Create(cc_blink::WebLayerImpl::LayerSettings())), |
| 46 click_to_play_image_(ResourceBundle::GetSharedInstance().GetImageNamed( |
| 47 IDR_FRAMELET_CLICK_TO_PLAY)), |
| 48 click_to_play_layer_( |
| 49 cc::UIResourceLayer::Create(cc_blink::WebLayerImpl::LayerSettings())), |
| 50 binding_(this) { |
| 51 SetElementInstanceID(content::RenderThread::Get()->GenerateRoutingID()); |
| 52 } |
| 53 |
| 54 FrameletContainer::~FrameletContainer() {} |
| 55 |
| 56 void FrameletContainer::didAttach(blink::WebFramelet* framelet) { |
| 57 framelet_ = framelet; |
| 58 // Only create a Framelet if we have a non-zero size. |
| 59 CreateFrameletIfReady(ResourceMonitoring::ENABLED); |
| 60 } |
| 61 |
| 62 void FrameletContainer::didDetach() { |
| 63 if (framelet_guest_) { |
| 64 framelet_guest_->Destroy(); |
| 65 framelet_guest_.reset(); |
| 66 binding_.Close(); |
| 67 } |
| 68 } |
| 69 |
| 70 void FrameletContainer::forwardInputEvent( |
| 71 const blink::WebInputEvent* input_event) { |
| 72 if (killed_) { |
| 73 // If this guest has been killed. |
| 74 // TODO(fsamuel): We want a platform-agnostic way to detect click. |
| 75 bool click = input_event->type == blink::WebInputEvent::GestureTap || |
| 76 ((input_event->type == blink::WebInputEvent::MouseUp) && |
| 77 (static_cast<const blink::WebMouseEvent*>(input_event) |
| 78 ->clickCount == 1)); |
| 79 if (click) { |
| 80 CreateFrameletIfReady(ResourceMonitoring::DISABLED); |
| 81 return; |
| 82 } |
| 83 } |
| 84 |
| 85 if (!has_guest()) |
| 86 return; |
| 87 |
| 88 mojo::Array<uint8_t> mojo_input(input_event->size); |
| 89 memcpy(&mojo_input.front(), input_event, input_event->size); |
| 90 framelet_guest_->ForwardInput(std::move(mojo_input)); |
| 91 } |
| 92 |
| 93 void FrameletContainer::frameRectsChanged(const blink::WebRect& frame_rect) { |
| 94 element_size_ = gfx::Size(frame_rect.width, frame_rect.height); |
| 95 UpdateClickToPlayLayerPosition(); |
| 96 if (!ready_) { |
| 97 ready_ = true; |
| 98 CreateFrameletIfReady(ResourceMonitoring::ENABLED); |
| 99 return; |
| 100 } |
| 101 if (!has_guest()) |
| 102 return; |
| 103 |
| 104 framelet_guest_->Resize(mojo::Size::From(element_size_)); |
| 105 } |
| 106 |
| 107 void FrameletContainer::updateFocus(bool focused, |
| 108 blink::WebFocusType focus_type) { |
| 109 focused_ = focused; |
| 110 focus_type_ = focus_type; |
| 111 if (!has_guest()) |
| 112 return; |
| 113 framelet_guest_->SetFocus(focused, static_cast<mojom::FocusType>(focus_type)); |
| 114 } |
| 115 |
| 116 void FrameletContainer::updateVisibility(bool visible) { |
| 117 visible_ = visible; |
| 118 if (!has_guest()) |
| 119 return; |
| 120 |
| 121 framelet_guest_->SetVisible(visible); |
| 122 } |
| 123 |
| 124 mojo::Map<mojo::String, mojom::ValuePtr> FrameletContainer::GetParams( |
| 125 ResourceMonitoring resource_monitoring) const { |
| 126 mojo::Map<mojo::String, mojom::ValuePtr> params; |
| 127 params[guest_view::kElementWidth] = |
| 128 mojom::Value::From(element_size().width()); |
| 129 params[guest_view::kElementHeight] = |
| 130 mojom::Value::From(element_size().height()); |
| 131 params[guest_view::kUrl] = mojom::Value::From(url().spec()); |
| 132 params[kFocused] = mojom::Value::From(focused()); |
| 133 params[kVisible] = mojom::Value::From(visible()); |
| 134 params[kMonitorResources] = |
| 135 mojom::Value::From(resource_monitoring == ResourceMonitoring::ENABLED); |
| 136 return params; |
| 137 } |
| 138 |
| 139 void FrameletContainer::CreateFrameletIfReady( |
| 140 ResourceMonitoring resource_monitoring) { |
| 141 if (!ready_ || !framelet_ || !render_frame()) |
| 142 return; |
| 143 |
| 144 killed_ = false; |
| 145 solid_layer_->SetIsDrawable(false); |
| 146 click_to_play_layer_->SetIsDrawable(false); |
| 147 |
| 148 // Get a Framelet Factory. |
| 149 mojom::FrameletFactoryPtr factory; |
| 150 render_frame()->GetServiceRegistry()->ConnectToRemoteService( |
| 151 mojo::GetProxy(&factory)); |
| 152 |
| 153 // Create a Framelet. |
| 154 factory->Create(mojo::GetProxy(&framelet_guest_), |
| 155 GetParams(resource_monitoring)); |
| 156 |
| 157 // Attach the Framelet to this container. |
| 158 mojom::FrameletClientPtr client; |
| 159 binding_.Bind(GetProxy(&client)); |
| 160 framelet_guest_->Attach(std::move(client), GetParams(resource_monitoring)); |
| 161 } |
| 162 |
| 163 void FrameletContainer::UpdateClickToPlayLayerPosition() { |
| 164 gfx::PointF position( |
| 165 (element_size().width() - click_to_play_image_.Size().width()) / 2.f, |
| 166 (element_size().height() - click_to_play_image_.Size().height()) / 2.f); |
| 167 click_to_play_layer_->SetPosition(position); |
| 168 } |
| 169 |
| 170 void FrameletContainer::ReportMemoryUsage( |
| 171 mojom::ResourceUsageLevel usage_level) { |
| 172 // TODO(fsamuel): Do something useful with the heap size. |
| 173 switch (usage_level) { |
| 174 case mojom::ResourceUsageLevel::LOW: { |
| 175 solid_layer_->SetIsDrawable(false); |
| 176 click_to_play_layer_->SetIsDrawable(false); |
| 177 return; |
| 178 } |
| 179 case mojom::ResourceUsageLevel::MEDIUM: { |
| 180 solid_layer_->SetIsDrawable(true); |
| 181 solid_layer_->SetBackgroundColor(SkColorSetARGBInline(255, 255, 255, 0)); |
| 182 click_to_play_layer_->SetIsDrawable(false); |
| 183 return; |
| 184 } |
| 185 case mojom::ResourceUsageLevel::HIGH: { |
| 186 solid_layer_->SetIsDrawable(true); |
| 187 solid_layer_->SetBackgroundColor(SkColorSetARGBInline(255, 255, 0, 0)); |
| 188 click_to_play_layer_->SetIsDrawable(false); |
| 189 return; |
| 190 } |
| 191 case mojom::ResourceUsageLevel::CRITICAL: { |
| 192 killed_ = true; |
| 193 solid_layer_->SetIsDrawable(true); |
| 194 solid_layer_->SetBackgroundColor(SkColorSetARGBInline(255, 0, 0, 0)); |
| 195 click_to_play_layer_->SetIsDrawable(true); |
| 196 // Kill the guest. |
| 197 if (framelet_guest_) { |
| 198 framelet_guest_->Destroy(); |
| 199 framelet_guest_.reset(); |
| 200 binding_.Close(); |
| 201 } |
| 202 return; |
| 203 } |
| 204 default: |
| 205 DCHECK(false); |
| 206 } |
| 207 } |
| 208 |
| 209 void FrameletContainer::SetChildFrameSurface( |
| 210 mus::mojom::SurfaceIdPtr surface_id, |
| 211 mojo::SizePtr frame_size, |
| 212 float scale_factor) { |
| 213 cc::SurfaceLayer::SatisfyCallback satisfy_callback = |
| 214 base::Bind(&FrameletContainer::SatisfyCallbackOnCompositorThread, |
| 215 thread_safe_sender_, render_frame()->GetRoutingID(), |
| 216 element_instance_id()); |
| 217 cc::SurfaceLayer::RequireCallback require_callback = |
| 218 base::Bind(&FrameletContainer::RequireCallbackOnCompositorThread, |
| 219 thread_safe_sender_, render_frame()->GetRoutingID(), |
| 220 element_instance_id()); |
| 221 scoped_refptr<cc::SurfaceLayer> surface_layer = |
| 222 cc::SurfaceLayer::Create(cc_blink::WebLayerImpl::LayerSettings(), |
| 223 satisfy_callback, require_callback); |
| 224 surface_layer->SetSurfaceId(surface_id.To<cc::SurfaceId>(), scale_factor, |
| 225 frame_size.To<gfx::Size>()); |
| 226 gfx::Size frame_size_in_dip( |
| 227 gfx::ScaleToFlooredSize(frame_size.To<gfx::Size>(), 1.0f / scale_factor)); |
| 228 surface_layer->SetBounds(frame_size_in_dip); |
| 229 |
| 230 solid_layer_->SetMasksToBounds(true); |
| 231 solid_layer_->SetBounds(frame_size_in_dip); |
| 232 solid_layer_->SetBackgroundColor(SkColorSetARGBInline(255, 0, 0, 192)); |
| 233 solid_layer_->SetIsDrawable(false); |
| 234 solid_layer_->SetOpacity(0.5f); |
| 235 surface_layer->AddChild(solid_layer_); |
| 236 |
| 237 SkBitmap* bitmap = const_cast<SkBitmap*>(click_to_play_image_.ToSkBitmap()); |
| 238 click_to_play_layer_->SetMasksToBounds(true); |
| 239 click_to_play_layer_->SetBounds(click_to_play_image_.Size()); |
| 240 click_to_play_layer_->SetBitmap(*bitmap); |
| 241 click_to_play_layer_->SetOpacity(0.75f); |
| 242 surface_layer->AddChild(click_to_play_layer_); |
| 243 |
| 244 blink::WebLayer* layer = new cc_blink::WebLayerImpl(surface_layer); |
| 245 framelet_->setWebLayer(layer); |
| 246 web_layer_.reset(layer); |
| 247 } |
| 248 |
| 249 void FrameletContainer::SatisfyCallbackOnCompositorThread( |
| 250 IPC::Sender* sender, |
| 251 int host_routing_id, |
| 252 int element_instance_id, |
| 253 cc::SurfaceSequence sequence) {} |
| 254 |
| 255 void FrameletContainer::RequireCallbackOnCompositorThread( |
| 256 IPC::Sender* sender, |
| 257 int host_routing_id, |
| 258 int element_instance_id, |
| 259 cc::SurfaceId id, |
| 260 cc::SurfaceSequence sequence) {} |
| 261 |
| 262 } // namespace framelet |
OLD | NEW |