OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 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 "content/browser/web_contents/web_contents_interface_registry_impl.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/memory/ptr_util.h" |
| 9 #include "content/public/browser/render_frame_host.h" |
| 10 #include "content/public/browser/web_contents.h" |
| 11 #include "content/public/common/associated_interface_registry.h" |
| 12 |
| 13 namespace content { |
| 14 |
| 15 WebContentsInterfaceRegistryImpl::WebContentsInterfaceRegistryImpl( |
| 16 WebContents* web_contents) |
| 17 : WebContentsObserver(web_contents), weak_factory_(this) { |
| 18 } |
| 19 |
| 20 WebContentsInterfaceRegistryImpl::~WebContentsInterfaceRegistryImpl() { |
| 21 } |
| 22 |
| 23 RenderFrameHost* WebContentsInterfaceRegistryImpl::GetCurrentTargetFrame() { |
| 24 DCHECK(current_target_frame_host_); |
| 25 return current_target_frame_host_; |
| 26 } |
| 27 |
| 28 base::WeakPtr<WebContentsInterfaceRegistry> |
| 29 WebContentsInterfaceRegistryImpl::GetWeakPtr() { |
| 30 return weak_factory_.GetWeakPtr(); |
| 31 } |
| 32 |
| 33 void WebContentsInterfaceRegistryImpl::SetCurrentTargetFrameForTesting( |
| 34 RenderFrameHost* render_frame_host) { |
| 35 current_target_frame_host_ = render_frame_host; |
| 36 } |
| 37 |
| 38 void WebContentsInterfaceRegistryImpl::RenderFrameCreated( |
| 39 RenderFrameHost* render_frame_host) { |
| 40 for (auto& entry : frame_interfaces_) |
| 41 AddInterfaceToFrame(entry.first, entry.second.get(), render_frame_host); |
| 42 |
| 43 auto result = frames_.insert(render_frame_host); |
| 44 DCHECK(result.second); |
| 45 } |
| 46 |
| 47 void WebContentsInterfaceRegistryImpl::RenderFrameDeleted( |
| 48 RenderFrameHost* render_frame_host) { |
| 49 auto it = frames_.find(render_frame_host); |
| 50 if (it == frames_.end()) |
| 51 return; |
| 52 |
| 53 frames_.erase(it); |
| 54 for (auto& entry : frame_interfaces_) { |
| 55 render_frame_host->GetAssociatedInterfaceRegistry()->RemoveInterface( |
| 56 entry.first); |
| 57 } |
| 58 } |
| 59 |
| 60 void WebContentsInterfaceRegistryImpl::WebContentsDestroyed() { |
| 61 frames_.clear(); |
| 62 } |
| 63 |
| 64 void WebContentsInterfaceRegistryImpl::AddFrameInterface( |
| 65 const std::string& name, |
| 66 std::unique_ptr<FrameInterfaceBase> iface) { |
| 67 for (auto* frame : frames_) |
| 68 AddInterfaceToFrame(name, iface.get(), frame); |
| 69 |
| 70 auto result = |
| 71 frame_interfaces_.insert(std::make_pair(name, std::move(iface))); |
| 72 DCHECK(result.second); |
| 73 } |
| 74 |
| 75 void WebContentsInterfaceRegistryImpl::RemoveFrameInterface( |
| 76 const std::string& name) { |
| 77 for (auto* frame : frames_) |
| 78 frame->GetAssociatedInterfaceRegistry()->RemoveInterface(name); |
| 79 |
| 80 auto it = frame_interfaces_.find(name); |
| 81 DCHECK(it != frame_interfaces_.end()); |
| 82 frame_interfaces_.erase(it); |
| 83 } |
| 84 |
| 85 void WebContentsInterfaceRegistryImpl::WillDispatchForFrame( |
| 86 RenderFrameHost* render_frame_host) { |
| 87 current_target_frame_host_ = render_frame_host; |
| 88 } |
| 89 |
| 90 // static |
| 91 void WebContentsInterfaceRegistryImpl::AddInterfaceToFrame( |
| 92 const std::string& name, |
| 93 FrameInterfaceBase* iface, |
| 94 RenderFrameHost* render_frame_host) { |
| 95 render_frame_host->GetAssociatedInterfaceRegistry()->AddInterface( |
| 96 name, base::Bind(&FrameInterfaceBase::AddBinding, |
| 97 base::Unretained(iface), render_frame_host)); |
| 98 } |
| 99 |
| 100 } // namespace content |
OLD | NEW |