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/browser/framelet_guest.h" |
| 6 |
| 7 #include "base/memory/scoped_ptr.h" |
| 8 #include "base/strings/stringprintf.h" |
| 9 #include "cc/surfaces/surface.h" |
| 10 #include "components/framelet/browser/framelet_memory_tracker.h" |
| 11 #include "components/framelet/browser/resource_usage_reporter.h" |
| 12 #include "components/framelet/common/framelet_constants.h" |
| 13 #include "components/framelet/common/framelet_messages.h" |
| 14 #include "components/framelet/common/value_type_converters.h" |
| 15 #include "components/guest_view/browser/guest_view_manager.h" |
| 16 #include "components/strings/grit/components_chromium_strings.h" |
| 17 #include "content/public/browser/render_process_host.h" |
| 18 #include "content/public/browser/site_instance.h" |
| 19 #include "content/public/common/url_constants.h" |
| 20 #include "ipc/ipc_message.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 |
| 25 namespace framelet { |
| 26 |
| 27 // static. |
| 28 const char FrameletGuest::Type[] = "framelet"; |
| 29 |
| 30 // static. |
| 31 guest_view::GuestViewBase* FrameletGuest::Create( |
| 32 content::WebContents* owner_web_contents) { |
| 33 return new FrameletGuest(owner_web_contents); |
| 34 } |
| 35 |
| 36 void FrameletGuest::Bind(mojo::InterfaceRequest<mojom::Framelet> request) { |
| 37 binding_.Bind(std::move(request)); |
| 38 } |
| 39 |
| 40 void FrameletGuest::ReportHeapSize(int heap_size) { |
| 41 resource_usage_reporter_->ReportUsage(heap_size); |
| 42 } |
| 43 |
| 44 void FrameletGuest::OnResourceUsageLevelChanged( |
| 45 const ResourceUsageLevel& usage_level) { |
| 46 // The state machine reports usage level here. |
| 47 // TODO(fsamuel): Feed this value through a ResourceUsageManager, that |
| 48 // collects all values, and only updates the embedder if the max changes. |
| 49 client_->ReportMemoryUsage( |
| 50 static_cast<mojom::ResourceUsageLevel>(usage_level)); |
| 51 } |
| 52 |
| 53 scoped_ptr<ResourceUsageRange<int>> FrameletGuest::GetRangeForUsageLevel( |
| 54 const ResourceUsageLevel& usage_level) const { |
| 55 switch (usage_level) { |
| 56 case ResourceUsageLevel::LOW: |
| 57 return make_scoped_ptr(new ResourceUsageRange<int>( |
| 58 ResourceUsageRangeType::LEFT_UNBOUNDED, kMemoryUsageLimitLow)); |
| 59 case ResourceUsageLevel::MEDIUM: |
| 60 return make_scoped_ptr(new ResourceUsageRange<int>( |
| 61 kMemoryUsageLimitLow - kMemoryUsageRangeOverlap, |
| 62 kMemoryUsageLimitMedium)); |
| 63 case ResourceUsageLevel::HIGH: |
| 64 return make_scoped_ptr(new ResourceUsageRange<int>( |
| 65 kMemoryUsageLimitMedium - kMemoryUsageRangeOverlap, |
| 66 kMemoryUsageLimitHigh)); |
| 67 case ResourceUsageLevel::CRITICAL: |
| 68 return make_scoped_ptr(new ResourceUsageRange<int>( |
| 69 ResourceUsageRangeType::RIGHT_UNBOUNDED, kMemoryUsageLimitHigh)); |
| 70 default: |
| 71 CHECK(false); |
| 72 return nullptr; |
| 73 } |
| 74 } |
| 75 |
| 76 bool FrameletGuest::SetChildFrameSurface(const cc::SurfaceId& surface_id, |
| 77 const gfx::Size& frame_size, |
| 78 float scale_factor, |
| 79 const cc::SurfaceSequence& sequence) { |
| 80 client_->SetChildFrameSurface(mus::mojom::SurfaceId::From(surface_id), |
| 81 mojo::Size::From(frame_size), scale_factor); |
| 82 return true; |
| 83 } |
| 84 |
| 85 FrameletGuest::FrameletGuest(content::WebContents* owner_web_contents) |
| 86 : GuestView<FrameletGuest>(owner_web_contents), |
| 87 monitor_resources_(true), |
| 88 binding_(this) {} |
| 89 |
| 90 FrameletGuest::~FrameletGuest() {} |
| 91 |
| 92 // Framelet implementation |
| 93 |
| 94 void FrameletGuest::Attach(mojom::FrameletClientPtr client, |
| 95 mojo::Map<mojo::String, mojom::ValuePtr> params) { |
| 96 static int element_instance_id = 1 << 16; |
| 97 client_ = std::move(client); |
| 98 content::GuestAttachParams attach_params; |
| 99 attach_params.visible = params[kVisible].To<bool>(); |
| 100 attach_params.focused = params[kFocused].To<bool>(); |
| 101 int element_width = params[guest_view::kElementWidth].To<int>(); |
| 102 int element_height = params[guest_view::kElementHeight].To<int>(); |
| 103 attach_params.view_rect = gfx::Rect(element_width, element_height); |
| 104 guest_view::GuestViewManager* manager = |
| 105 guest_view::GuestViewManager::FromBrowserContext(browser_context()); |
| 106 manager->AttachGuest(owner_web_contents()->GetRenderProcessHost()->GetID(), |
| 107 element_instance_id, guest_instance_id(), |
| 108 base::DictionaryValue()); |
| 109 host()->Attach(element_instance_id, owner_web_contents(), attach_params); |
| 110 } |
| 111 |
| 112 void FrameletGuest::Destroy() { |
| 113 GuestViewBase::Destroy(); |
| 114 } |
| 115 |
| 116 void FrameletGuest::ForwardInput(mojo::Array<uint8_t> event) { |
| 117 // TODO(fsamuel): This is not safe. This needs to be validated. |
| 118 const blink::WebInputEvent* input_event = |
| 119 reinterpret_cast<blink::WebInputEvent*>(&event.front()); |
| 120 host()->ForwardInputEvent(input_event); |
| 121 } |
| 122 |
| 123 void FrameletGuest::Resize(mojo::SizePtr new_size) { |
| 124 guest_view::SetSizeParams size_params; |
| 125 size_params.normal_size.reset(new gfx::Size(new_size.To<gfx::Size>())); |
| 126 SetSize(size_params); |
| 127 } |
| 128 |
| 129 void FrameletGuest::SetFocus(bool focused, mojom::FocusType focus_type) { |
| 130 host()->SetFocus(focused, static_cast<blink::WebFocusType>(focus_type)); |
| 131 } |
| 132 |
| 133 void FrameletGuest::SetVisible(bool visible) { |
| 134 host()->SetContainerVisible(visible); |
| 135 } |
| 136 |
| 137 // content::WebContentsDelegate implementation. |
| 138 bool FrameletGuest::HandleContextMenu( |
| 139 const content::ContextMenuParams& params) { |
| 140 return false; |
| 141 } |
| 142 |
| 143 bool FrameletGuest::CanRunInDetachedState() const { |
| 144 return true; |
| 145 } |
| 146 |
| 147 void FrameletGuest::CreateWebContents( |
| 148 const base::DictionaryValue& create_params, |
| 149 const guest_view::GuestViewBase::WebContentsCreatedCallback& callback) { |
| 150 // For each embedder process, there should exist a framelet process. |
| 151 GURL guest_site(base::StringPrintf( |
| 152 "%s://framelet-%d", content::kGuestScheme, |
| 153 owner_web_contents()->GetRenderProcessHost()->GetID())); |
| 154 |
| 155 guest_view::GuestViewManager* guest_view_manager = |
| 156 guest_view::GuestViewManager::FromBrowserContext( |
| 157 owner_web_contents()->GetBrowserContext()); |
| 158 content::SiteInstance* guest_site_instance = |
| 159 guest_view_manager->GetGuestSiteInstance(guest_site); |
| 160 if (!guest_site_instance) { |
| 161 guest_site_instance = content::SiteInstance::CreateForURL( |
| 162 owner_web_contents()->GetBrowserContext(), guest_site); |
| 163 } |
| 164 content::WebContents::CreateParams params( |
| 165 owner_web_contents()->GetBrowserContext(), guest_site_instance); |
| 166 params.guest_delegate = this; |
| 167 callback.Run(content::WebContents::Create(params)); |
| 168 } |
| 169 |
| 170 const char* FrameletGuest::GetAPINamespace() const { |
| 171 return framelet::kEmbedderAPINamespace; |
| 172 } |
| 173 |
| 174 int FrameletGuest::GetTaskPrefix() const { |
| 175 return IDS_TASK_MANAGER_FRAMELET_TAG_PREFIX; |
| 176 } |
| 177 |
| 178 void FrameletGuest::DidAttachToEmbedder() {} |
| 179 |
| 180 void FrameletGuest::DidInitialize(const base::DictionaryValue& create_params) { |
| 181 resource_usage_reporter_.reset(new ResourceUsageReporter<int>(this)); |
| 182 std::string url; |
| 183 create_params.GetString(guest_view::kUrl, &url); |
| 184 web_contents()->GetController().LoadURL(GURL(url), content::Referrer(), |
| 185 ui::PAGE_TRANSITION_AUTO_TOPLEVEL, |
| 186 std::string()); |
| 187 |
| 188 create_params.GetBoolean(framelet::kMonitorResources, &monitor_resources_); |
| 189 |
| 190 if (monitor_resources_) { |
| 191 FrameletMemoryTracker::ClientID client_id( |
| 192 web_contents()->GetRenderProcessHost()->GetID(), |
| 193 web_contents()->GetRoutingID()); |
| 194 FrameletMemoryTracker::FromBrowserContext(browser_context()) |
| 195 ->AddClient(client_id, this); |
| 196 } |
| 197 } |
| 198 |
| 199 void FrameletGuest::WillDestroy() { |
| 200 if (monitor_resources_) { |
| 201 // TODO(fsamuel): Verify whether this is actually safe. The WebContents |
| 202 // should still exist so we might be fine. |
| 203 FrameletMemoryTracker::ClientID client_id( |
| 204 web_contents()->GetRenderProcessHost()->GetID(), |
| 205 web_contents()->GetRoutingID()); |
| 206 FrameletMemoryTracker::FromBrowserContext(browser_context()) |
| 207 ->RemoveClient(client_id); |
| 208 } |
| 209 } |
| 210 |
| 211 } // namespace framelet |
OLD | NEW |