Index: components/framelet/browser/framelet_guest.cc |
diff --git a/components/framelet/browser/framelet_guest.cc b/components/framelet/browser/framelet_guest.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..78c6137e5964ca1306df34c161e04d47397c5bab |
--- /dev/null |
+++ b/components/framelet/browser/framelet_guest.cc |
@@ -0,0 +1,211 @@ |
+// Copyright 2015 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "components/framelet/browser/framelet_guest.h" |
+ |
+#include "base/memory/scoped_ptr.h" |
+#include "base/strings/stringprintf.h" |
+#include "cc/surfaces/surface.h" |
+#include "components/framelet/browser/framelet_memory_tracker.h" |
+#include "components/framelet/browser/resource_usage_reporter.h" |
+#include "components/framelet/common/framelet_constants.h" |
+#include "components/framelet/common/framelet_messages.h" |
+#include "components/framelet/common/value_type_converters.h" |
+#include "components/guest_view/browser/guest_view_manager.h" |
+#include "components/strings/grit/components_chromium_strings.h" |
+#include "content/public/browser/render_process_host.h" |
+#include "content/public/browser/site_instance.h" |
+#include "content/public/common/url_constants.h" |
+#include "ipc/ipc_message.h" |
+#include "mojo/converters/blink/blink_input_events_type_converters.h" |
+#include "mojo/converters/geometry/geometry_type_converters.h" |
+#include "mojo/converters/surfaces/surfaces_type_converters.h" |
+ |
+namespace framelet { |
+ |
+// static. |
+const char FrameletGuest::Type[] = "framelet"; |
+ |
+// static. |
+guest_view::GuestViewBase* FrameletGuest::Create( |
+ content::WebContents* owner_web_contents) { |
+ return new FrameletGuest(owner_web_contents); |
+} |
+ |
+void FrameletGuest::Bind(mojo::InterfaceRequest<mojom::Framelet> request) { |
+ binding_.Bind(std::move(request)); |
+} |
+ |
+void FrameletGuest::ReportHeapSize(int heap_size) { |
+ resource_usage_reporter_->ReportUsage(heap_size); |
+} |
+ |
+void FrameletGuest::OnResourceUsageLevelChanged( |
+ const ResourceUsageLevel& usage_level) { |
+ // The state machine reports usage level here. |
+ // TODO(fsamuel): Feed this value through a ResourceUsageManager, that |
+ // collects all values, and only updates the embedder if the max changes. |
+ client_->ReportMemoryUsage( |
+ static_cast<mojom::ResourceUsageLevel>(usage_level)); |
+} |
+ |
+scoped_ptr<ResourceUsageRange<int>> FrameletGuest::GetRangeForUsageLevel( |
+ const ResourceUsageLevel& usage_level) const { |
+ switch (usage_level) { |
+ case ResourceUsageLevel::LOW: |
+ return make_scoped_ptr(new ResourceUsageRange<int>( |
+ ResourceUsageRangeType::LEFT_UNBOUNDED, kMemoryUsageLimitLow)); |
+ case ResourceUsageLevel::MEDIUM: |
+ return make_scoped_ptr(new ResourceUsageRange<int>( |
+ kMemoryUsageLimitLow - kMemoryUsageRangeOverlap, |
+ kMemoryUsageLimitMedium)); |
+ case ResourceUsageLevel::HIGH: |
+ return make_scoped_ptr(new ResourceUsageRange<int>( |
+ kMemoryUsageLimitMedium - kMemoryUsageRangeOverlap, |
+ kMemoryUsageLimitHigh)); |
+ case ResourceUsageLevel::CRITICAL: |
+ return make_scoped_ptr(new ResourceUsageRange<int>( |
+ ResourceUsageRangeType::RIGHT_UNBOUNDED, kMemoryUsageLimitHigh)); |
+ default: |
+ CHECK(false); |
+ return nullptr; |
+ } |
+} |
+ |
+bool FrameletGuest::SetChildFrameSurface(const cc::SurfaceId& surface_id, |
+ const gfx::Size& frame_size, |
+ float scale_factor, |
+ const cc::SurfaceSequence& sequence) { |
+ client_->SetChildFrameSurface(mus::mojom::SurfaceId::From(surface_id), |
+ mojo::Size::From(frame_size), scale_factor); |
+ return true; |
+} |
+ |
+FrameletGuest::FrameletGuest(content::WebContents* owner_web_contents) |
+ : GuestView<FrameletGuest>(owner_web_contents), |
+ monitor_resources_(true), |
+ binding_(this) {} |
+ |
+FrameletGuest::~FrameletGuest() {} |
+ |
+// Framelet implementation |
+ |
+void FrameletGuest::Attach(mojom::FrameletClientPtr client, |
+ mojo::Map<mojo::String, mojom::ValuePtr> params) { |
+ static int element_instance_id = 1 << 16; |
+ client_ = std::move(client); |
+ content::GuestAttachParams attach_params; |
+ attach_params.visible = params[kVisible].To<bool>(); |
+ attach_params.focused = params[kFocused].To<bool>(); |
+ int element_width = params[guest_view::kElementWidth].To<int>(); |
+ int element_height = params[guest_view::kElementHeight].To<int>(); |
+ attach_params.view_rect = gfx::Rect(element_width, element_height); |
+ guest_view::GuestViewManager* manager = |
+ guest_view::GuestViewManager::FromBrowserContext(browser_context()); |
+ manager->AttachGuest(owner_web_contents()->GetRenderProcessHost()->GetID(), |
+ element_instance_id, guest_instance_id(), |
+ base::DictionaryValue()); |
+ host()->Attach(element_instance_id, owner_web_contents(), attach_params); |
+} |
+ |
+void FrameletGuest::Destroy() { |
+ GuestViewBase::Destroy(); |
+} |
+ |
+void FrameletGuest::ForwardInput(mojo::Array<uint8_t> event) { |
+ // TODO(fsamuel): This is not safe. This needs to be validated. |
+ const blink::WebInputEvent* input_event = |
+ reinterpret_cast<blink::WebInputEvent*>(&event.front()); |
+ host()->ForwardInputEvent(input_event); |
+} |
+ |
+void FrameletGuest::Resize(mojo::SizePtr new_size) { |
+ guest_view::SetSizeParams size_params; |
+ size_params.normal_size.reset(new gfx::Size(new_size.To<gfx::Size>())); |
+ SetSize(size_params); |
+} |
+ |
+void FrameletGuest::SetFocus(bool focused, mojom::FocusType focus_type) { |
+ host()->SetFocus(focused, static_cast<blink::WebFocusType>(focus_type)); |
+} |
+ |
+void FrameletGuest::SetVisible(bool visible) { |
+ host()->SetContainerVisible(visible); |
+} |
+ |
+// content::WebContentsDelegate implementation. |
+bool FrameletGuest::HandleContextMenu( |
+ const content::ContextMenuParams& params) { |
+ return false; |
+} |
+ |
+bool FrameletGuest::CanRunInDetachedState() const { |
+ return true; |
+} |
+ |
+void FrameletGuest::CreateWebContents( |
+ const base::DictionaryValue& create_params, |
+ const guest_view::GuestViewBase::WebContentsCreatedCallback& callback) { |
+ // For each embedder process, there should exist a framelet process. |
+ GURL guest_site(base::StringPrintf( |
+ "%s://framelet-%d", content::kGuestScheme, |
+ owner_web_contents()->GetRenderProcessHost()->GetID())); |
+ |
+ guest_view::GuestViewManager* guest_view_manager = |
+ guest_view::GuestViewManager::FromBrowserContext( |
+ owner_web_contents()->GetBrowserContext()); |
+ content::SiteInstance* guest_site_instance = |
+ guest_view_manager->GetGuestSiteInstance(guest_site); |
+ if (!guest_site_instance) { |
+ guest_site_instance = content::SiteInstance::CreateForURL( |
+ owner_web_contents()->GetBrowserContext(), guest_site); |
+ } |
+ content::WebContents::CreateParams params( |
+ owner_web_contents()->GetBrowserContext(), guest_site_instance); |
+ params.guest_delegate = this; |
+ callback.Run(content::WebContents::Create(params)); |
+} |
+ |
+const char* FrameletGuest::GetAPINamespace() const { |
+ return framelet::kEmbedderAPINamespace; |
+} |
+ |
+int FrameletGuest::GetTaskPrefix() const { |
+ return IDS_TASK_MANAGER_FRAMELET_TAG_PREFIX; |
+} |
+ |
+void FrameletGuest::DidAttachToEmbedder() {} |
+ |
+void FrameletGuest::DidInitialize(const base::DictionaryValue& create_params) { |
+ resource_usage_reporter_.reset(new ResourceUsageReporter<int>(this)); |
+ std::string url; |
+ create_params.GetString(guest_view::kUrl, &url); |
+ web_contents()->GetController().LoadURL(GURL(url), content::Referrer(), |
+ ui::PAGE_TRANSITION_AUTO_TOPLEVEL, |
+ std::string()); |
+ |
+ create_params.GetBoolean(framelet::kMonitorResources, &monitor_resources_); |
+ |
+ if (monitor_resources_) { |
+ FrameletMemoryTracker::ClientID client_id( |
+ web_contents()->GetRenderProcessHost()->GetID(), |
+ web_contents()->GetRoutingID()); |
+ FrameletMemoryTracker::FromBrowserContext(browser_context()) |
+ ->AddClient(client_id, this); |
+ } |
+} |
+ |
+void FrameletGuest::WillDestroy() { |
+ if (monitor_resources_) { |
+ // TODO(fsamuel): Verify whether this is actually safe. The WebContents |
+ // should still exist so we might be fine. |
+ FrameletMemoryTracker::ClientID client_id( |
+ web_contents()->GetRenderProcessHost()->GetID(), |
+ web_contents()->GetRoutingID()); |
+ FrameletMemoryTracker::FromBrowserContext(browser_context()) |
+ ->RemoveClient(client_id); |
+ } |
+} |
+ |
+} // namespace framelet |