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/strings/stringprintf.h" |
| 8 #include "cc/surfaces/surface.h" |
| 9 #include "components/framelet/browser/framelet_memory_tracker.h" |
| 10 #include "components/framelet/browser/resource_usage_reporter.h" |
| 11 #include "components/framelet/common/framelet_constants.h" |
| 12 #include "components/framelet/common/framelet_messages.h" |
| 13 #include "components/guest_view/browser/guest_view_manager.h" |
| 14 #include "components/strings/grit/components_chromium_strings.h" |
| 15 #include "content/public/browser/render_process_host.h" |
| 16 #include "content/public/browser/site_instance.h" |
| 17 #include "content/public/common/url_constants.h" |
| 18 #include "ipc/ipc_message.h" |
| 19 |
| 20 namespace framelet { |
| 21 |
| 22 // static. |
| 23 const char FrameletGuest::Type[] = "framelet"; |
| 24 |
| 25 // static. |
| 26 guest_view::GuestViewBase* FrameletGuest::Create( |
| 27 content::WebContents* owner_web_contents) { |
| 28 return new FrameletGuest(owner_web_contents); |
| 29 } |
| 30 |
| 31 void FrameletGuest::AttachToEmbedderFrame(int element_instance_id, |
| 32 int embedder_local_render_frame_id, |
| 33 const base::DictionaryValue& params) { |
| 34 element_instance_id_ = element_instance_id; |
| 35 // We should only update params if there's a change between creation and |
| 36 // attachment. |
| 37 content::GuestAttachParams attach_params; |
| 38 params.GetBoolean(framelet::kVisible, &attach_params.visible); |
| 39 params.GetBoolean(framelet::kFocused, &attach_params.focused); |
| 40 int element_width = 300; |
| 41 int element_height = 300; |
| 42 params.GetInteger(guest_view::kElementWidth, &element_width); |
| 43 params.GetInteger(guest_view::kElementHeight, &element_height); |
| 44 // TODO(fsamuel): This should probably be a size rather than a rect. |
| 45 attach_params.view_rect = gfx::Rect(element_width, element_height); |
| 46 host()->Attach(element_instance_id, owner_web_contents(), attach_params); |
| 47 } |
| 48 |
| 49 void FrameletGuest::ReportHeapSize(int heap_size) { |
| 50 resource_usage_reporter_->ReportUsage(heap_size); |
| 51 } |
| 52 |
| 53 void FrameletGuest::OnResourceUsageLevelChanged( |
| 54 const ResourceUsageLevel& usage_level) { |
| 55 // The state machine reports usage level here. |
| 56 // TODO(fsamuel): Feed this value through a ResourceUsageManager, that |
| 57 // collects all values, and only updates the embedder if the max changes. |
| 58 owner_web_contents()->Send(new ChromeGuestViewMsg_ReportMemoryUsage( |
| 59 element_instance_id(), usage_level)); |
| 60 } |
| 61 |
| 62 scoped_ptr<ResourceUsageRange<int>> FrameletGuest::GetRangeForUsageLevel( |
| 63 const ResourceUsageLevel& usage_level) const { |
| 64 switch (usage_level) { |
| 65 case ResourceUsageLevel::LOW: |
| 66 return make_scoped_ptr(new ResourceUsageRange<int>( |
| 67 ResourceUsageRangeType::LEFT_UNBOUNDED, kMemoryUsageLimitLow)); |
| 68 case ResourceUsageLevel::MEDIUM: |
| 69 return make_scoped_ptr(new ResourceUsageRange<int>( |
| 70 kMemoryUsageLimitLow - kMemoryUsageRangeOverlap, |
| 71 kMemoryUsageLimitMedium)); |
| 72 case ResourceUsageLevel::HIGH: |
| 73 return make_scoped_ptr(new ResourceUsageRange<int>( |
| 74 kMemoryUsageLimitMedium - kMemoryUsageRangeOverlap, |
| 75 kMemoryUsageLimitHigh)); |
| 76 case ResourceUsageLevel::CRITICAL: |
| 77 return make_scoped_ptr(new ResourceUsageRange<int>( |
| 78 ResourceUsageRangeType::RIGHT_UNBOUNDED, kMemoryUsageLimitHigh)); |
| 79 default: |
| 80 CHECK(false); |
| 81 return nullptr; |
| 82 } |
| 83 } |
| 84 |
| 85 bool FrameletGuest::SetChildFrameSurface(const cc::SurfaceId& surface_id, |
| 86 const gfx::Size& frame_size, |
| 87 float scale_factor, |
| 88 const cc::SurfaceSequence& sequence) { |
| 89 owner_web_contents()->Send(new ChromeGuestViewMsg_SetChildFrameSurface( |
| 90 element_instance_id(), surface_id, frame_size, scale_factor, sequence)); |
| 91 return true; |
| 92 } |
| 93 |
| 94 FrameletGuest::FrameletGuest(content::WebContents* owner_web_contents) |
| 95 : GuestView<FrameletGuest>(owner_web_contents), monitor_resources_(true) {} |
| 96 |
| 97 FrameletGuest::~FrameletGuest() {} |
| 98 |
| 99 // content::WebContentsDelegate implementation. |
| 100 bool FrameletGuest::HandleContextMenu( |
| 101 const content::ContextMenuParams& params) { |
| 102 return false; |
| 103 } |
| 104 |
| 105 const char* FrameletGuest::GetAPINamespace() const { |
| 106 return framelet::kEmbedderAPINamespace; |
| 107 } |
| 108 |
| 109 bool FrameletGuest::CanRunInDetachedState() const { |
| 110 return true; |
| 111 } |
| 112 |
| 113 int FrameletGuest::GetTaskPrefix() const { |
| 114 return IDS_TASK_MANAGER_FRAMELET_TAG_PREFIX; |
| 115 } |
| 116 |
| 117 void FrameletGuest::CreateWebContents( |
| 118 const base::DictionaryValue& create_params, |
| 119 const guest_view::GuestViewBase::WebContentsCreatedCallback& callback) { |
| 120 // For each embedder process, there should exist a framelet process. |
| 121 GURL guest_site(base::StringPrintf( |
| 122 "%s://framelet-%d", content::kGuestScheme, |
| 123 owner_web_contents()->GetRenderProcessHost()->GetID())); |
| 124 |
| 125 guest_view::GuestViewManager* guest_view_manager = |
| 126 guest_view::GuestViewManager::FromBrowserContext( |
| 127 owner_web_contents()->GetBrowserContext()); |
| 128 content::SiteInstance* guest_site_instance = |
| 129 guest_view_manager->GetGuestSiteInstance(guest_site); |
| 130 if (!guest_site_instance) { |
| 131 guest_site_instance = content::SiteInstance::CreateForURL( |
| 132 owner_web_contents()->GetBrowserContext(), guest_site); |
| 133 } |
| 134 content::WebContents::CreateParams params( |
| 135 owner_web_contents()->GetBrowserContext(), guest_site_instance); |
| 136 params.guest_delegate = this; |
| 137 callback.Run(content::WebContents::Create(params)); |
| 138 } |
| 139 |
| 140 void FrameletGuest::DidAttachToEmbedder() {} |
| 141 |
| 142 void FrameletGuest::DidInitialize(const base::DictionaryValue& create_params) { |
| 143 resource_usage_reporter_.reset(new ResourceUsageReporter<int>(this)); |
| 144 std::string url; |
| 145 create_params.GetString(guest_view::kUrl, &url); |
| 146 web_contents()->GetController().LoadURL(GURL(url), content::Referrer(), |
| 147 ui::PAGE_TRANSITION_AUTO_TOPLEVEL, |
| 148 std::string()); |
| 149 |
| 150 create_params.GetBoolean(framelet::kMonitorResources, &monitor_resources_); |
| 151 |
| 152 if (monitor_resources_) { |
| 153 FrameletMemoryTracker::ClientID client_id( |
| 154 web_contents()->GetRenderProcessHost()->GetID(), |
| 155 web_contents()->GetRoutingID()); |
| 156 FrameletMemoryTracker::FromBrowserContext(browser_context()) |
| 157 ->AddClient(client_id, this); |
| 158 } |
| 159 } |
| 160 |
| 161 bool FrameletGuest::OnMessageReceivedFromEmbedder(const IPC::Message& message) { |
| 162 bool handled = true; |
| 163 IPC_BEGIN_MESSAGE_MAP(FrameletGuest, message) |
| 164 IPC_MESSAGE_HANDLER(ChromeGuestViewHostMsg_DestroyFramelet, |
| 165 OnDestroyFramelet) |
| 166 IPC_MESSAGE_HANDLER(ChromeGuestViewHostMsg_ForwardInputEvent, |
| 167 OnForwardInputEvent) |
| 168 IPC_MESSAGE_HANDLER(ChromeGuestViewHostMsg_ResizeFramelet, OnResizeFramelet) |
| 169 IPC_MESSAGE_HANDLER(ChromeGuestViewHostMsg_SetContainerVisible, |
| 170 OnSetContainerVisible) |
| 171 IPC_MESSAGE_HANDLER(ChromeGuestViewHostMsg_SetFocus, OnSetFocus) |
| 172 IPC_MESSAGE_UNHANDLED(handled = false) |
| 173 IPC_END_MESSAGE_MAP(); |
| 174 return handled; |
| 175 } |
| 176 |
| 177 void FrameletGuest::WillDestroy() { |
| 178 if (monitor_resources_) { |
| 179 // TODO(fsamuel): Verify whether this is actually safe. The WebContents |
| 180 // should still exist so we might be fine. |
| 181 FrameletMemoryTracker::ClientID client_id( |
| 182 web_contents()->GetRenderProcessHost()->GetID(), |
| 183 web_contents()->GetRoutingID()); |
| 184 FrameletMemoryTracker::FromBrowserContext(browser_context()) |
| 185 ->RemoveClient(client_id); |
| 186 } |
| 187 } |
| 188 |
| 189 void FrameletGuest::OnDestroyFramelet(int element_instance_id) { |
| 190 // Since FrameletGuest is only managed on one thread, any subsequent IPC sent |
| 191 // to the browser will arrive after the guest has been destroyed. |
| 192 owner_web_contents()->Send( |
| 193 new ChromeGuestViewMsg_DestroyFramelet_ACK(element_instance_id)); |
| 194 Destroy(); |
| 195 // WARNING: We cannot access member variables beyond this point because |this| |
| 196 // has been destroyed. |
| 197 } |
| 198 |
| 199 void FrameletGuest::OnForwardInputEvent(int element_instance_id, |
| 200 const blink::WebInputEvent* event) { |
| 201 host()->ForwardInputEvent(event); |
| 202 } |
| 203 |
| 204 void FrameletGuest::OnResizeFramelet(int element_instance_id, |
| 205 const gfx::Size& new_size) { |
| 206 guest_view::SetSizeParams size_params; |
| 207 size_params.normal_size.reset(new gfx::Size(new_size)); |
| 208 SetSize(size_params); |
| 209 } |
| 210 |
| 211 void FrameletGuest::OnSetContainerVisible(int element_instance_id, |
| 212 bool visible) { |
| 213 host()->SetContainerVisible(visible); |
| 214 } |
| 215 |
| 216 void FrameletGuest::OnSetFocus(int element_instance_id, |
| 217 bool focused, |
| 218 blink::WebFocusType focus_type) { |
| 219 host()->SetFocus(focused, focus_type); |
| 220 } |
| 221 |
| 222 } // namespace framelet |
OLD | NEW |