Index: components/framelet/browser/framelet_factory_impl.cc |
diff --git a/components/framelet/browser/framelet_factory_impl.cc b/components/framelet/browser/framelet_factory_impl.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..63ef5208f43973ac8964e376b3f1c91955fb79b4 |
--- /dev/null |
+++ b/components/framelet/browser/framelet_factory_impl.cc |
@@ -0,0 +1,79 @@ |
+// 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_factory_impl.h" |
+ |
+#include "base/values.h" |
+#include "components/framelet/browser/framelet_guest.h" |
+#include "components/framelet/common/value_type_converters.h" |
+#include "components/guest_view/browser/guest_view_manager.h" |
+#include "content/public/browser/render_frame_host.h" |
+#include "content/public/browser/web_contents.h" |
+ |
+namespace framelet { |
+ |
+FrameletFactoryImpl::FrameletFactoryImpl( |
+ content::WebContents* owner_web_contents, |
+ const CreateGuestViewManagerDelegate& create_delegate, |
+ mojo::InterfaceRequest<FrameletFactory> factory_request) |
+ : owner_web_contents_(owner_web_contents), |
+ create_delegate_(create_delegate), |
+ binding_(this, std::move(factory_request)), |
+ weak_ptr_factory_(this) {} |
+ |
+FrameletFactoryImpl::~FrameletFactoryImpl() {} |
+ |
+// static |
+void FrameletFactoryImpl::CreateService( |
+ content::RenderFrameHost* render_frame_host, |
+ const CreateGuestViewManagerDelegate& create_delegate, |
+ mojo::InterfaceRequest<FrameletFactory> factory_request) { |
+ content::WebContents* owner_web_contents = |
+ content::WebContents::FromRenderFrameHost(render_frame_host); |
+ new FrameletFactoryImpl(owner_web_contents, create_delegate, |
+ std::move(factory_request)); |
+} |
+ |
+guest_view::GuestViewManager* |
+FrameletFactoryImpl::GetOrCreateGuestViewManager() { |
+ auto manager = guest_view::GuestViewManager::FromBrowserContext( |
+ owner_web_contents_->GetBrowserContext()); |
+ if (!manager) { |
+ manager = guest_view::GuestViewManager::CreateWithDelegate( |
+ owner_web_contents_->GetBrowserContext(), |
+ make_scoped_ptr(create_delegate_.Run())); |
+ } |
+ return manager; |
+} |
+ |
+// static |
+void FrameletFactoryImpl::FrameletCreatedCallback( |
+ mojo::InterfaceRequest<mojom::Framelet> framelet, |
+ content::WebContents* guest_web_contents) { |
+ if (!guest_web_contents) |
+ return; |
+ |
+ FrameletGuest* guest = FrameletGuest::FromWebContents(guest_web_contents); |
+ if (!guest) |
+ return; |
+ |
+ guest->Bind(std::move(framelet)); |
+} |
+ |
+void FrameletFactoryImpl::Create( |
+ mojo::InterfaceRequest<mojom::Framelet> framelet, |
+ mojo::Map<mojo::String, mojom::ValuePtr> params) { |
+ base::DictionaryValue create_params; |
+ for (auto& kv : params) |
+ create_params.Set(kv.GetKey(), kv.GetValue().To<base::Value*>()); |
+ |
+ guest_view::GuestViewManager::WebContentsCreatedCallback callback = |
+ base::Bind(&FrameletFactoryImpl::FrameletCreatedCallback, |
+ base::Passed(std::move(framelet))); |
+ auto manager = GetOrCreateGuestViewManager(); |
+ manager->CreateGuest(framelet::FrameletGuest::Type, owner_web_contents_, |
+ create_params, callback); |
+} |
+ |
+} // namespace framelet |