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_INTERFACE_BINDING_H_ | |
6 #define CONTENT_PUBLIC_BROWSER_WEB_CONTENTS_FRAME_INTERFACE_BINDING_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 WebContentsFrameInterfaceBinding { | |
18 public: | |
19 explicit WebContentsFrameInterfaceBinding(Interface* impl) | |
20 : WebContentsFrameInterfaceBinding(nullptr, impl) {} | |
21 | |
22 WebContentsFrameInterfaceBinding(WebContents* web_contents, Interface* impl) | |
23 : impl_(impl) { | |
24 if (web_contents) | |
25 AddToWebContents(web_contents); | |
26 } | |
27 | |
28 ~WebContentsFrameInterfaceBinding() { | |
29 RemoveFromWebContents(); | |
30 } | |
31 | |
32 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
| |
33 DCHECK(!weak_registry_); | |
34 WebContentsInterfaceRegistry* registry = web_contents->GetInterfaces(); | |
35 registry->AddFrameInterface<Interface>(impl_); | |
36 weak_registry_ = registry->GetWeakPtr(); | |
37 } | |
38 | |
39 void RemoveFromWebContents() { | |
40 if (weak_registry_) { | |
41 weak_registry_->RemoveFrameInterface<Interface>(); | |
42 weak_registry_.reset(); | |
43 } | |
44 } | |
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
| |
45 | |
46 private: | |
47 Interface* impl_; | |
48 | |
49 // We store a WeakPtr to the registry, because some | |
50 // WebBontentsFrameInterfaceBinding owners may exist as UserData on a | |
51 // WebContents and therefore outlive the WebContents itself. | |
52 base::WeakPtr<WebContentsInterfaceRegistry> weak_registry_; | |
53 | |
54 DISALLOW_COPY_AND_ASSIGN(WebContentsFrameInterfaceBinding); | |
55 }; | |
56 | |
57 } // namespace content | |
58 | |
59 #endif // CONTENT_PUBLIC_BROWSER_WEB_CONTENTS_FRAME_INTERFACE_BINDING_H_ | |
OLD | NEW |