Index: content/public/browser/web_contents_frame_binding_set.h |
diff --git a/content/public/browser/web_contents_frame_binding_set.h b/content/public/browser/web_contents_frame_binding_set.h |
new file mode 100644 |
index 0000000000000000000000000000000000000000..f6407d0184bc17305746ad5044c38ab6c1760dd4 |
--- /dev/null |
+++ b/content/public/browser/web_contents_frame_binding_set.h |
@@ -0,0 +1,66 @@ |
+// Copyright 2016 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. |
+ |
+#ifndef CONTENT_PUBLIC_BROWSER_WEB_CONTENTS_FRAME_BINDING_SET_H_ |
+#define CONTENT_PUBLIC_BROWSER_WEB_CONTENTS_FRAME_BINDING_SET_H_ |
+ |
+#include "base/macros.h" |
+#include "content/public/browser/web_contents.h" |
+#include "content/public/browser/web_contents_interface_registry.h" |
+ |
+namespace content { |
+ |
+// A scoper which binds |Interface| to |impl| for every RenderFrameHost |
+// belonging to a single WebContents instance. |
+template <typename Interface> |
+class WebContentsFrameBindingSet { |
+ public: |
+ explicit WebContentsFrameBindingSet(Interface* impl) |
+ : WebContentsFrameBindingSet(nullptr, impl) {} |
+ |
+ WebContentsFrameBindingSet(WebContents* web_contents, Interface* impl) |
+ : impl_(impl) { |
+ if (web_contents) |
+ SetWebContents(web_contents); |
+ } |
+ |
+ ~WebContentsFrameBindingSet() { |
+ ClearWebContents(); |
+ } |
+ |
+ void SetWebContents(WebContents* web_contents) { |
+ DCHECK(!weak_registry_); |
+ WebContentsInterfaceRegistry* registry = |
+ web_contents->GetWebContentsInterfaceRegistry(); |
+ registry->AddFrameInterface<Interface>(impl_); |
+ weak_registry_ = registry->GetWeakPtr(); |
+ } |
+ |
+ void ClearWebContents() { |
+ if (weak_registry_) { |
+ weak_registry_->RemoveFrameInterface<Interface>(); |
+ weak_registry_.reset(); |
+ } |
+ } |
+ |
+ // Returns the RenderFrameHost currently targeted by a message dispatch to |
+ // the WebContents. |
+ RenderFrameHost* GetCurrentTargetFrame() { |
+ return weak_registry_->GetCurrentTargetFrame(); |
+ } |
+ |
+ private: |
+ Interface* impl_; |
+ |
+ // We store a WeakPtr to the registry, because some |
+ // WebBontentsFrameInterfaceBinding owners may exist as UserData on a |
+ // WebContents and therefore outlive the WebContents itself. |
+ base::WeakPtr<WebContentsInterfaceRegistry> weak_registry_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(WebContentsFrameBindingSet); |
+}; |
+ |
+} // namespace content |
+ |
+#endif // CONTENT_PUBLIC_BROWSER_WEB_CONTENTS_FRAME_BINDING_SET_H_ |