1 // Copyright (c) 2012 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.
5 #ifndef CONTENT_PUBLIC_TEST_MOCK_RENDER_PROCESS_HOST_H_
6 #define CONTENT_PUBLIC_TEST_MOCK_RENDER_PROCESS_HOST_H_
8 #include "base/basictypes.h"
9 #include "base/memory/scoped_vector.h"
10 #include "base/observer_list.h"
11 #include "content/public/browser/render_process_host.h"
12 #include "content/public/browser/render_process_host_factory.h"
13 #include "ipc/ipc_test_sink.h"
15 class StoragePartition
;
19 class MockRenderProcessHostFactory
;
21 // A mock render process host that has no corresponding renderer process. All
22 // IPC messages are sent into the message sink for inspection by tests.
23 class MockRenderProcessHost
: public RenderProcessHost
{
25 explicit MockRenderProcessHost(BrowserContext
* browser_context
);
26 ~MockRenderProcessHost() override
;
28 // Provides access to all IPC messages that would have been sent to the
29 // renderer via this RenderProcessHost.
30 IPC::TestSink
& sink() { return sink_
; }
32 // Provides test access to how many times a bad message has been received.
33 int bad_msg_count() const { return bad_msg_count_
; }
35 // RenderProcessHost implementation (public portion).
36 void EnableSendQueue() override
;
38 int GetNextRoutingID() override
;
39 void AddRoute(int32 routing_id
, IPC::Listener
* listener
) override
;
40 void RemoveRoute(int32 routing_id
) override
;
41 void AddObserver(RenderProcessHostObserver
* observer
) override
;
42 void RemoveObserver(RenderProcessHostObserver
* observer
) override
;
43 void ReceivedBadMessage() override
;
44 void WidgetRestored() override
;
45 void WidgetHidden() override
;
46 int VisibleWidgetCount() const override
;
47 bool IsIsolatedGuest() const override
;
48 StoragePartition
* GetStoragePartition() const override
;
49 virtual void AddWord(const base::string16
& word
);
50 bool Shutdown(int exit_code
, bool wait
) override
;
51 bool FastShutdownIfPossible() override
;
52 bool FastShutdownStarted() const override
;
53 void DumpHandles() override
;
54 base::ProcessHandle
GetHandle() const override
;
55 int GetID() const override
;
56 bool HasConnection() const override
;
57 void SetIgnoreInputEvents(bool ignore_input_events
) override
;
58 bool IgnoreInputEvents() const override
;
59 void Cleanup() override
;
60 void AddPendingView() override
;
61 void RemovePendingView() override
;
62 bool SuddenTerminationAllowed() const override
;
63 BrowserContext
* GetBrowserContext() const override
;
64 bool InSameStoragePartition(StoragePartition
* partition
) const override
;
65 IPC::ChannelProxy
* GetChannel() override
;
66 void AddFilter(BrowserMessageFilter
* filter
) override
;
67 bool FastShutdownForPageCount(size_t count
) override
;
68 base::TimeDelta
GetChildProcessIdleTime() const override
;
69 void ResumeRequestsForView(int route_id
) override
;
70 void FilterURL(bool empty_allowed
, GURL
* url
) override
;
71 #if defined(ENABLE_WEBRTC)
72 void EnableAecDump(const base::FilePath
& file
) override
;
73 void DisableAecDump() override
;
74 void SetWebRtcLogMessageCallback(
75 base::Callback
<void(const std::string
&)> callback
) override
;
76 WebRtcStopRtpDumpCallback
StartRtpDump(
79 const WebRtcRtpPacketCallback
& packet_callback
) override
;
81 void ResumeDeferredNavigation(const GlobalRequestID
& request_id
) override
;
82 void NotifyTimezoneChange() override
;
83 ServiceRegistry
* GetServiceRegistry() override
;
84 const base::TimeTicks
& GetInitTimeForNavigationMetrics() const override
;
85 bool SubscribeUniformEnabled() const override
;
86 void OnAddSubscription(unsigned int target
) override
;
87 void OnRemoveSubscription(unsigned int target
) override
;
88 void SendUpdateValueState(
89 unsigned int target
, const gpu::ValueState
& state
) override
;
90 #if defined(ENABLE_BROWSER_CDMS)
91 media::BrowserCdm
* GetBrowserCdm(int render_frame_id
,
92 int cdm_id
) const override
;
95 // IPC::Sender via RenderProcessHost.
96 bool Send(IPC::Message
* msg
) override
;
98 // IPC::Listener via RenderProcessHost.
99 bool OnMessageReceived(const IPC::Message
& msg
) override
;
100 void OnChannelConnected(int32 peer_pid
) override
;
102 // Attaches the factory object so we can remove this object in its destructor
103 // and prevent MockRenderProcessHostFacotry from deleting it.
104 void SetFactory(const MockRenderProcessHostFactory
* factory
) {
108 void set_is_isolated_guest(bool is_isolated_guest
) {
109 is_isolated_guest_
= is_isolated_guest
;
112 void SetProcessHandle(scoped_ptr
<base::ProcessHandle
> new_handle
) {
113 process_handle
= new_handle
.Pass();
116 void GetAudioOutputControllers(
117 const GetAudioOutputControllersCallback
& callback
) const override
{}
120 // Stores IPC messages that would have been sent to the renderer.
123 const MockRenderProcessHostFactory
* factory_
;
125 BrowserContext
* browser_context_
;
126 ObserverList
<RenderProcessHostObserver
> observers_
;
128 IDMap
<RenderWidgetHost
> render_widget_hosts_
;
129 int prev_routing_id_
;
130 IDMap
<IPC::Listener
> listeners_
;
131 bool fast_shutdown_started_
;
132 bool deletion_callback_called_
;
133 bool is_isolated_guest_
;
134 scoped_ptr
<base::ProcessHandle
> process_handle
;
136 DISALLOW_COPY_AND_ASSIGN(MockRenderProcessHost
);
139 class MockRenderProcessHostFactory
: public RenderProcessHostFactory
{
141 MockRenderProcessHostFactory();
142 ~MockRenderProcessHostFactory() override
;
144 RenderProcessHost
* CreateRenderProcessHost(
145 BrowserContext
* browser_context
,
146 SiteInstance
* site_instance
) const override
;
148 // Removes the given MockRenderProcessHost from the MockRenderProcessHost list
149 // without deleting it. When a test deletes a MockRenderProcessHost, we need
150 // to remove it from |processes_| to prevent it from being deleted twice.
151 void Remove(MockRenderProcessHost
* host
) const;
154 // A list of MockRenderProcessHosts created by this object. This list is used
155 // for deleting all MockRenderProcessHosts that have not deleted by a test in
156 // the destructor and prevent them from being leaked.
157 mutable ScopedVector
<MockRenderProcessHost
> processes_
;
159 DISALLOW_COPY_AND_ASSIGN(MockRenderProcessHostFactory
);
162 } // namespace content
164 #endif // CONTENT_PUBLIC_TEST_MOCK_RENDER_PROCESS_HOST_H_