Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(995)

Side by Side Diff: content/public/browser/web_contents_interface_registry.h

Issue 2310563002: Adds routed interface support between RenderFrameHost and RenderFrame (Closed)
Patch Set: Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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_INTERFACE_REGISTRY_H_
6 #define CONTENT_PUBLIC_BROWSER_WEB_CONTENTS_INTERFACE_REGISTRY_H_
7
8 #include <map>
9 #include <string>
10
11 #include "base/macros.h"
12 #include "content/common/content_export.h"
13 #include "mojo/public/cpp/bindings/associated_binding_set.h"
14 #include "mojo/public/cpp/bindings/associated_interface_request.h"
15 #include "mojo/public/cpp/bindings/scoped_interface_endpoint_handle.h"
16
17 namespace content {
18
19 class RenderFrameHost;
20 class WebContents;
21
22 // A helper class to hold per-frame associated interface bindings for every
23 // frame belonging to a single WebContents.
24 class CONTENT_EXPORT WebContentsInterfaceRegistry {
25 public:
26 virtual ~WebContentsInterfaceRegistry() {}
27
28 // Creates a new WebContentsInterfaceRegistry to manage per-frame associated
29 // interface bindings on all frames belonging to |web_contents|.
30 // |web_contents| is not owned and must outlive the returned
31 // WebContentsInterfaceRegistry instance.
32 static std::unique_ptr<WebContentsInterfaceRegistry> Create(
33 WebContents* web_contents);
34
35 // Returns the RenderFrameHost targeted by the current binding dispatch. See
36 // AddFrameInterface() below for details.
37 virtual RenderFrameHost* GetCurrentTargetFrame() = 0;
38
39 // Allows test code to control the target frame currently seen by calls to
40 // GetCurrentTargetFrame(). Useful when simulating incoming message calls.
41 virtual void SetCurrentTargetFrameForTesting(
42 RenderFrameHost* render_frame_host) = 0;
43
44 // Adds a new interface to the registry. A separate Interface binding is
45 // (lazily) established for every RenderFrameHost belonging to every
46 // WebContents the registry knows about.
47 //
48 // Incoming messages on Interface are routed to |impl|, and during the extent
49 // of any message dispatch (i.e. any Interface call on |impl|), |impl| may
50 // call GetCurrentTargetFrame() above to get the RenderFrameHost whose
51 // specific binding was targeted by the message.
52 template <typename Interface>
53 void AddFrameInterface(Interface* iface) {
54 AddFrameInterface(Interface::Name_,
55 base::MakeUnique<FrameInterface<Interface>>(iface, this));
56 }
57
58 protected:
59 class FrameInterfaceBase {
60 public:
61 virtual ~FrameInterfaceBase() {}
62
63 virtual void AddBinding(RenderFrameHost* render_frame_host,
64 mojo::ScopedInterfaceEndpointHandle handle) = 0;
65 virtual void RemoveBinding(RenderFrameHost* render_frame_host) = 0;
66 virtual RenderFrameHost* GetCurrentTargetFrame() = 0;
67 };
68
69 // Generic helper for the implementation to provide. See the templated
70 // AddFrameInterface above.
71 virtual void AddFrameInterface(const std::string& name,
72 std::unique_ptr<FrameInterfaceBase> iface) = 0;
73
74 // Called immediately before any message or error is dispatched on behalf of
75 // any owned bindings.
76 virtual void WillDispatchForFrame(RenderFrameHost* render_frame_host) = 0;
77
78 template <typename Interface>
79 class FrameInterface : public FrameInterfaceBase {
80 public:
81 FrameInterface(Interface* impl, WebContentsInterfaceRegistry* registry)
82 : impl_(impl), bindings_(mojo::BindingSetDispatchMode::WITH_CONTEXT) {
83 bindings_.set_pre_dispatch_handler(
84 base::Bind(&WebContentsInterfaceRegistry::WillDispatchForContext,
85 base::Unretained(registry)));
86 }
87
88 ~FrameInterface() override {}
89
90 void AddBinding(RenderFrameHost* render_frame_host,
91 mojo::ScopedInterfaceEndpointHandle handle) override {
92 mojo::AssociatedInterfaceRequest<Interface> request;
93 request.Bind(std::move(handle));
94 frame_binding_map_[render_frame_host].push_back(bindings_.AddBinding(
95 impl_, std::move(request), static_cast<void*>(render_frame_host)));
96 }
97
98 void RemoveBinding(RenderFrameHost* render_frame_host) override {
99 auto it = frame_binding_map_.find(render_frame_host);
100 DCHECK(it != frame_binding_map_.end());
101 for (auto id : it->second)
102 bindings_.RemoveBinding(id);
103 frame_binding_map_.erase(it);
104 }
105
106 RenderFrameHost* GetCurrentTargetFrame() override {
107 return static_cast<RenderFrameHost*>(bindings_.dispatch_context());
108 }
109
110 private:
111 Interface* const impl_;
112 mojo::AssociatedBindingSet<Interface> bindings_;
113 std::map<RenderFrameHost*, std::vector<mojo::BindingId>> frame_binding_map_;
114
115 DISALLOW_COPY_AND_ASSIGN(FrameInterface);
116 };
117
118 private:
119 void WillDispatchForContext(void* context) {
120 WillDispatchForFrame(reinterpret_cast<RenderFrameHost*>(context));
121 }
122 };
123
124 } // namespace content
125
126 #endif // CONTENT_PUBLIC_BROWSER_WEB_CONTENTS_INTERFACE_REGISTRY_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698