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 #ifndef CONTENT_PUBLIC_BROWSER_WEB_CONTENTS_FRAME_BINDING_SET_H_ |
| 6 #define CONTENT_PUBLIC_BROWSER_WEB_CONTENTS_FRAME_BINDING_SET_H_ |
| 7 |
| 8 #include "base/macros.h" |
| 9 #include "content/public/browser/web_contents.h" |
| 10 #include "content/public/browser/web_contents_interface_registry.h" |
| 11 |
| 12 namespace content { |
| 13 |
| 14 // A scoper which binds |Interface| to |impl| for every RenderFrameHost |
| 15 // belonging to a single WebContents instance. |
| 16 template <typename Interface> |
| 17 class WebContentsFrameBindingSet { |
| 18 public: |
| 19 explicit WebContentsFrameBindingSet(Interface* impl) |
| 20 : WebContentsFrameBindingSet(nullptr, impl) {} |
| 21 |
| 22 WebContentsFrameBindingSet(WebContents* web_contents, Interface* impl) |
| 23 : impl_(impl) { |
| 24 if (web_contents) |
| 25 SetWebContents(web_contents); |
| 26 } |
| 27 |
| 28 ~WebContentsFrameBindingSet() { |
| 29 ClearWebContents(); |
| 30 } |
| 31 |
| 32 void SetWebContents(WebContents* web_contents) { |
| 33 DCHECK(!weak_registry_); |
| 34 WebContentsInterfaceRegistry* registry = |
| 35 web_contents->GetWebContentsInterfaceRegistry(); |
| 36 registry->AddFrameInterface<Interface>(impl_); |
| 37 weak_registry_ = registry->GetWeakPtr(); |
| 38 } |
| 39 |
| 40 void ClearWebContents() { |
| 41 if (weak_registry_) { |
| 42 weak_registry_->RemoveFrameInterface<Interface>(); |
| 43 weak_registry_.reset(); |
| 44 } |
| 45 } |
| 46 |
| 47 // Returns the RenderFrameHost currently targeted by a message dispatch to |
| 48 // the WebContents. |
| 49 RenderFrameHost* GetCurrentTargetFrame() { |
| 50 return weak_registry_->GetCurrentTargetFrame(); |
| 51 } |
| 52 |
| 53 private: |
| 54 Interface* impl_; |
| 55 |
| 56 // We store a WeakPtr to the registry, because some |
| 57 // WebBontentsFrameInterfaceBinding owners may exist as UserData on a |
| 58 // WebContents and therefore outlive the WebContents itself. |
| 59 base::WeakPtr<WebContentsInterfaceRegistry> weak_registry_; |
| 60 |
| 61 DISALLOW_COPY_AND_ASSIGN(WebContentsFrameBindingSet); |
| 62 }; |
| 63 |
| 64 } // namespace content |
| 65 |
| 66 #endif // CONTENT_PUBLIC_BROWSER_WEB_CONTENTS_FRAME_BINDING_SET_H_ |
OLD | NEW |