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_factory_impl.h" |
| 6 |
| 7 #include "base/values.h" |
| 8 #include "components/framelet/browser/framelet_guest.h" |
| 9 #include "components/framelet/common/value_type_converters.h" |
| 10 #include "components/guest_view/browser/guest_view_manager.h" |
| 11 #include "content/public/browser/render_frame_host.h" |
| 12 #include "content/public/browser/web_contents.h" |
| 13 |
| 14 namespace framelet { |
| 15 |
| 16 FrameletFactoryImpl::FrameletFactoryImpl( |
| 17 content::WebContents* owner_web_contents, |
| 18 const CreateGuestViewManagerDelegate& create_delegate, |
| 19 mojo::InterfaceRequest<FrameletFactory> factory_request) |
| 20 : owner_web_contents_(owner_web_contents), |
| 21 create_delegate_(create_delegate), |
| 22 binding_(this, std::move(factory_request)), |
| 23 weak_ptr_factory_(this) {} |
| 24 |
| 25 FrameletFactoryImpl::~FrameletFactoryImpl() {} |
| 26 |
| 27 // static |
| 28 void FrameletFactoryImpl::CreateService( |
| 29 content::RenderFrameHost* render_frame_host, |
| 30 const CreateGuestViewManagerDelegate& create_delegate, |
| 31 mojo::InterfaceRequest<FrameletFactory> factory_request) { |
| 32 content::WebContents* owner_web_contents = |
| 33 content::WebContents::FromRenderFrameHost(render_frame_host); |
| 34 new FrameletFactoryImpl(owner_web_contents, create_delegate, |
| 35 std::move(factory_request)); |
| 36 } |
| 37 |
| 38 guest_view::GuestViewManager* |
| 39 FrameletFactoryImpl::GetOrCreateGuestViewManager() { |
| 40 auto manager = guest_view::GuestViewManager::FromBrowserContext( |
| 41 owner_web_contents_->GetBrowserContext()); |
| 42 if (!manager) { |
| 43 manager = guest_view::GuestViewManager::CreateWithDelegate( |
| 44 owner_web_contents_->GetBrowserContext(), |
| 45 make_scoped_ptr(create_delegate_.Run())); |
| 46 } |
| 47 return manager; |
| 48 } |
| 49 |
| 50 // static |
| 51 void FrameletFactoryImpl::FrameletCreatedCallback( |
| 52 mojo::InterfaceRequest<mojom::Framelet> framelet, |
| 53 content::WebContents* guest_web_contents) { |
| 54 if (!guest_web_contents) |
| 55 return; |
| 56 |
| 57 FrameletGuest* guest = FrameletGuest::FromWebContents(guest_web_contents); |
| 58 if (!guest) |
| 59 return; |
| 60 |
| 61 guest->Bind(std::move(framelet)); |
| 62 } |
| 63 |
| 64 void FrameletFactoryImpl::Create( |
| 65 mojo::InterfaceRequest<mojom::Framelet> framelet, |
| 66 mojo::Map<mojo::String, mojom::ValuePtr> params) { |
| 67 base::DictionaryValue create_params; |
| 68 for (auto& kv : params) |
| 69 create_params.Set(kv.GetKey(), kv.GetValue().To<base::Value*>()); |
| 70 |
| 71 guest_view::GuestViewManager::WebContentsCreatedCallback callback = |
| 72 base::Bind(&FrameletFactoryImpl::FrameletCreatedCallback, |
| 73 base::Passed(std::move(framelet))); |
| 74 auto manager = GetOrCreateGuestViewManager(); |
| 75 manager->CreateGuest(framelet::FrameletGuest::Type, owner_web_contents_, |
| 76 create_params, callback); |
| 77 } |
| 78 |
| 79 } // namespace framelet |
OLD | NEW |