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 ShutdownForBadMessage() 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 void SetSuddenTerminationAllowed(bool allowed
) override
;
63 bool SuddenTerminationAllowed() const override
;
64 BrowserContext
* GetBrowserContext() const override
;
65 bool InSameStoragePartition(StoragePartition
* partition
) const override
;
66 IPC::ChannelProxy
* GetChannel() override
;
67 void AddFilter(BrowserMessageFilter
* filter
) override
;
68 bool FastShutdownForPageCount(size_t count
) override
;
69 base::TimeDelta
GetChildProcessIdleTime() const override
;
70 void ResumeRequestsForView(int route_id
) override
;
71 void FilterURL(bool empty_allowed
, GURL
* url
) override
;
72 #if defined(ENABLE_WEBRTC)
73 void EnableAecDump(const base::FilePath
& file
) override
;
74 void DisableAecDump() override
;
75 void SetWebRtcLogMessageCallback(
76 base::Callback
<void(const std::string
&)> callback
) override
;
77 WebRtcStopRtpDumpCallback
StartRtpDump(
80 const WebRtcRtpPacketCallback
& packet_callback
) override
;
82 void ResumeDeferredNavigation(const GlobalRequestID
& request_id
) override
;
83 void NotifyTimezoneChange() override
;
84 ServiceRegistry
* GetServiceRegistry() override
;
85 const base::TimeTicks
& GetInitTimeForNavigationMetrics() const override
;
86 bool SubscribeUniformEnabled() const override
;
87 void OnAddSubscription(unsigned int target
) override
;
88 void OnRemoveSubscription(unsigned int target
) override
;
89 void SendUpdateValueState(
90 unsigned int target
, const gpu::ValueState
& state
) override
;
91 #if defined(ENABLE_BROWSER_CDMS)
92 media::BrowserCdm
* GetBrowserCdm(int render_frame_id
,
93 int cdm_id
) const override
;
96 // IPC::Sender via RenderProcessHost.
97 bool Send(IPC::Message
* msg
) override
;
99 // IPC::Listener via RenderProcessHost.
100 bool OnMessageReceived(const IPC::Message
& msg
) override
;
101 void OnChannelConnected(int32 peer_pid
) override
;
103 // Attaches the factory object so we can remove this object in its destructor
104 // and prevent MockRenderProcessHostFacotry from deleting it.
105 void SetFactory(const MockRenderProcessHostFactory
* factory
) {
109 void set_is_isolated_guest(bool is_isolated_guest
) {
110 is_isolated_guest_
= is_isolated_guest
;
113 void SetProcessHandle(scoped_ptr
<base::ProcessHandle
> new_handle
) {
114 process_handle
= new_handle
.Pass();
117 void GetAudioOutputControllers(
118 const GetAudioOutputControllersCallback
& callback
) const override
{}
121 // Stores IPC messages that would have been sent to the renderer.
124 const MockRenderProcessHostFactory
* factory_
;
126 BrowserContext
* browser_context_
;
127 ObserverList
<RenderProcessHostObserver
> observers_
;
129 IDMap
<RenderWidgetHost
> render_widget_hosts_
;
130 int prev_routing_id_
;
131 IDMap
<IPC::Listener
> listeners_
;
132 bool fast_shutdown_started_
;
133 bool deletion_callback_called_
;
134 bool is_isolated_guest_
;
135 scoped_ptr
<base::ProcessHandle
> process_handle
;
137 DISALLOW_COPY_AND_ASSIGN(MockRenderProcessHost
);
140 class MockRenderProcessHostFactory
: public RenderProcessHostFactory
{
142 MockRenderProcessHostFactory();
143 ~MockRenderProcessHostFactory() override
;
145 RenderProcessHost
* CreateRenderProcessHost(
146 BrowserContext
* browser_context
,
147 SiteInstance
* site_instance
) const override
;
149 // Removes the given MockRenderProcessHost from the MockRenderProcessHost list
150 // without deleting it. When a test deletes a MockRenderProcessHost, we need
151 // to remove it from |processes_| to prevent it from being deleted twice.
152 void Remove(MockRenderProcessHost
* host
) const;
155 // A list of MockRenderProcessHosts created by this object. This list is used
156 // for deleting all MockRenderProcessHosts that have not deleted by a test in
157 // the destructor and prevent them from being leaked.
158 mutable ScopedVector
<MockRenderProcessHost
> processes_
;
160 DISALLOW_COPY_AND_ASSIGN(MockRenderProcessHostFactory
);
163 } // namespace content
165 #endif // CONTENT_PUBLIC_TEST_MOCK_RENDER_PROCESS_HOST_H_