Chromium Code Reviews| Index: content/public/browser/web_contents_frame_interface_binding.h |
| diff --git a/content/public/browser/web_contents_frame_interface_binding.h b/content/public/browser/web_contents_frame_interface_binding.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..4b2ffd683ee17900b7a0e1c11b306131bfcf562c |
| --- /dev/null |
| +++ b/content/public/browser/web_contents_frame_interface_binding.h |
| @@ -0,0 +1,59 @@ |
| +// 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_INTERFACE_BINDING_H_ |
| +#define CONTENT_PUBLIC_BROWSER_WEB_CONTENTS_FRAME_INTERFACE_BINDING_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 WebContentsFrameInterfaceBinding { |
| + public: |
| + explicit WebContentsFrameInterfaceBinding(Interface* impl) |
| + : WebContentsFrameInterfaceBinding(nullptr, impl) {} |
| + |
| + WebContentsFrameInterfaceBinding(WebContents* web_contents, Interface* impl) |
| + : impl_(impl) { |
| + if (web_contents) |
| + AddToWebContents(web_contents); |
| + } |
| + |
| + ~WebContentsFrameInterfaceBinding() { |
| + RemoveFromWebContents(); |
| + } |
| + |
| + void AddToWebContents(WebContents* web_contents) { |
|
Sam McNally
2016/09/09 05:33:15
This could be clearer that an instance can be adde
Ken Rockot(use gerrit already)
2016/09/09 16:01:50
Done
|
| + DCHECK(!weak_registry_); |
| + WebContentsInterfaceRegistry* registry = web_contents->GetInterfaces(); |
| + registry->AddFrameInterface<Interface>(impl_); |
| + weak_registry_ = registry->GetWeakPtr(); |
| + } |
| + |
| + void RemoveFromWebContents() { |
| + if (weak_registry_) { |
| + weak_registry_->RemoveFrameInterface<Interface>(); |
| + weak_registry_.reset(); |
| + } |
| + } |
|
Sam McNally
2016/09/09 05:33:15
Please add a current RenderFrameHost accessor. It
Ken Rockot(use gerrit already)
2016/09/09 16:01:50
Done
|
| + |
| + 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(WebContentsFrameInterfaceBinding); |
| +}; |
| + |
| +} // namespace content |
| + |
| +#endif // CONTENT_PUBLIC_BROWSER_WEB_CONTENTS_FRAME_INTERFACE_BINDING_H_ |