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

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

Issue 2752283004: Add the ability to override WebContentsBindingSet binders for testing (Closed)
Patch Set: . Created 3 years, 9 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
1 // Copyright 2016 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef CONTENT_PUBLIC_BROWSER_WEB_CONTENTS_BINDING_SET_H_ 5 #ifndef CONTENT_PUBLIC_BROWSER_WEB_CONTENTS_BINDING_SET_H_
6 #define CONTENT_PUBLIC_BROWSER_WEB_CONTENTS_BINDING_SET_H_ 6 #define CONTENT_PUBLIC_BROWSER_WEB_CONTENTS_BINDING_SET_H_
7 7
8 #include <memory> 8 #include <memory>
9 #include <string> 9 #include <string>
10 10
11 #include "base/callback.h" 11 #include "base/callback.h"
12 #include "base/macros.h" 12 #include "base/macros.h"
13 #include "base/memory/ptr_util.h" 13 #include "base/memory/ptr_util.h"
14 #include "content/common/content_export.h" 14 #include "content/common/content_export.h"
15 #include "content/public/browser/web_contents.h" 15 #include "content/public/browser/web_contents.h"
16 #include "mojo/public/cpp/bindings/associated_binding_set.h" 16 #include "mojo/public/cpp/bindings/associated_binding_set.h"
17 #include "mojo/public/cpp/bindings/associated_interface_request.h" 17 #include "mojo/public/cpp/bindings/associated_interface_request.h"
18 #include "mojo/public/cpp/bindings/scoped_interface_endpoint_handle.h" 18 #include "mojo/public/cpp/bindings/scoped_interface_endpoint_handle.h"
19 19
20 namespace content { 20 namespace content {
21 21
22 class RenderFrameHost; 22 class RenderFrameHost;
23 class WebContentsImpl; 23 class WebContentsImpl;
24 24
25 // Base class for something which owns a mojo::AssociatedBindingSet on behalf 25 // Base class for something which owns a mojo::AssociatedBindingSet on behalf
26 // of a WebContents. See WebContentsFrameBindingSet<T> below. 26 // of a WebContents. See WebContentsFrameBindingSet<T> below.
27 class CONTENT_EXPORT WebContentsBindingSet { 27 class CONTENT_EXPORT WebContentsBindingSet {
28 protected: 28 public:
29 class CONTENT_EXPORT Binder { 29 class CONTENT_EXPORT Binder {
30 public: 30 public:
31 virtual ~Binder() {} 31 virtual ~Binder() {}
32 32
33 virtual void OnRequestForFrame( 33 virtual void OnRequestForFrame(
34 RenderFrameHost* render_frame_host, 34 RenderFrameHost* render_frame_host,
35 mojo::ScopedInterfaceEndpointHandle handle); 35 mojo::ScopedInterfaceEndpointHandle handle);
36 }; 36 };
37 37
38 // Helper class which test code can use with SetBinderForTesting() in order
39 // to override interface binding behavior in tests. Once this is set on a
40 // WebContentsBindingSet via SetBinderForTesting (see below), it will live
41 // until either it is replaced or the WebContents is destroyed.
42 template <typename Interface>
43 class TestBinder : public Binder {
jam 2017/03/20 17:07:32 can this be moved to content/public/test?
Ken Rockot(use gerrit already) 2017/03/20 17:50:47 Done
44 public:
45 ~TestBinder() override {}
46
47 // Call for every new incoming interface request for a frame.
48 virtual void BindRequest(
49 RenderFrameHost* render_frame_host,
50 mojo::AssociatedInterfaceRequest<Interface> request) = 0;
51
52 private:
53 // Binder:
54 void OnRequestForFrame(
55 RenderFrameHost* render_frame_host,
56 mojo::ScopedInterfaceEndpointHandle handle) override {
57 mojo::AssociatedInterfaceRequest<Interface> request;
58 request.Bind(std::move(handle));
59 BindRequest(render_frame_host, std::move(request));
60 }
61 };
62
63 template <typename Interface>
64 void SetBinderForTesting(std::unique_ptr<TestBinder<Interface>> binder) {
65 binder_for_testing_ = std::move(binder);
66 }
67
68 protected:
38 WebContentsBindingSet(WebContents* web_contents, 69 WebContentsBindingSet(WebContents* web_contents,
39 const std::string& interface_name, 70 const std::string& interface_name,
40 std::unique_ptr<Binder> binder); 71 std::unique_ptr<Binder> binder);
41 ~WebContentsBindingSet(); 72 ~WebContentsBindingSet();
42 73
43 private: 74 private:
44 friend class WebContentsImpl; 75 friend class WebContentsImpl;
45 76
46 void CloseAllBindings(); 77 void CloseAllBindings();
47 void OnRequestForFrame(RenderFrameHost* render_frame_host, 78 void OnRequestForFrame(RenderFrameHost* render_frame_host,
48 mojo::ScopedInterfaceEndpointHandle handle); 79 mojo::ScopedInterfaceEndpointHandle handle);
49 80
50 const base::Closure remove_callback_; 81 const base::Closure remove_callback_;
51 std::unique_ptr<Binder> binder_; 82 std::unique_ptr<Binder> binder_;
83 std::unique_ptr<Binder> binder_for_testing_;
52 84
53 DISALLOW_COPY_AND_ASSIGN(WebContentsBindingSet); 85 DISALLOW_COPY_AND_ASSIGN(WebContentsBindingSet);
54 }; 86 };
55 87
56 // Owns a set of Channel-associated interface bindings with frame context on 88 // Owns a set of Channel-associated interface bindings with frame context on
57 // message dispatch. 89 // message dispatch.
58 // 90 //
59 // To use this, a |mojom::Foo| implementation need only own an instance of 91 // To use this, a |mojom::Foo| implementation need only own an instance of
60 // WebContentsFrameBindingSet<mojom::Foo>. This allows remote RenderFrames to 92 // WebContentsFrameBindingSet<mojom::Foo>. This allows remote RenderFrames to
61 // acquire handles to the |mojom::Foo| interface via 93 // acquire handles to the |mojom::Foo| interface via
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after
147 } 179 }
148 180
149 RenderFrameHost* current_target_frame_ = nullptr; 181 RenderFrameHost* current_target_frame_ = nullptr;
150 182
151 DISALLOW_COPY_AND_ASSIGN(WebContentsFrameBindingSet); 183 DISALLOW_COPY_AND_ASSIGN(WebContentsFrameBindingSet);
152 }; 184 };
153 185
154 } // namespace content 186 } // namespace content
155 187
156 #endif // CONTENT_PUBLIC_BROWSER_WEB_CONTENTS_BINDING_SET_H_ 188 #endif // CONTENT_PUBLIC_BROWSER_WEB_CONTENTS_BINDING_SET_H_
OLDNEW
« no previous file with comments | « content/browser/web_contents_binding_set_browsertest.cc ('k') | content/public/browser/web_contents_binding_set.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698