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 // Provides tests a way to simulate this render process crashing.
38 // RenderProcessHost implementation (public portion).
39 void EnableSendQueue() override
;
41 int GetNextRoutingID() override
;
42 void AddRoute(int32 routing_id
, IPC::Listener
* listener
) override
;
43 void RemoveRoute(int32 routing_id
) override
;
44 void AddObserver(RenderProcessHostObserver
* observer
) override
;
45 void RemoveObserver(RenderProcessHostObserver
* observer
) override
;
46 void ShutdownForBadMessage() override
;
47 void WidgetRestored() override
;
48 void WidgetHidden() override
;
49 int VisibleWidgetCount() const override
;
50 bool IsForGuestsOnly() const override
;
51 StoragePartition
* GetStoragePartition() const override
;
52 virtual void AddWord(const base::string16
& word
);
53 bool Shutdown(int exit_code
, bool wait
) override
;
54 bool FastShutdownIfPossible() override
;
55 bool FastShutdownStarted() const override
;
56 base::ProcessHandle
GetHandle() const override
;
57 int GetID() const override
;
58 bool HasConnection() const override
;
59 void SetIgnoreInputEvents(bool ignore_input_events
) override
;
60 bool IgnoreInputEvents() const override
;
61 void Cleanup() override
;
62 void AddPendingView() override
;
63 void RemovePendingView() override
;
64 void SetSuddenTerminationAllowed(bool allowed
) override
;
65 bool SuddenTerminationAllowed() const override
;
66 BrowserContext
* GetBrowserContext() const override
;
67 bool InSameStoragePartition(StoragePartition
* partition
) const override
;
68 IPC::ChannelProxy
* GetChannel() override
;
69 void AddFilter(BrowserMessageFilter
* filter
) override
;
70 bool FastShutdownForPageCount(size_t count
) override
;
71 base::TimeDelta
GetChildProcessIdleTime() const override
;
72 void ResumeRequestsForView(int route_id
) override
;
73 void FilterURL(bool empty_allowed
, GURL
* url
) override
;
74 #if defined(ENABLE_WEBRTC)
75 void EnableAudioDebugRecordings(const base::FilePath
& file
) override
;
76 void DisableAudioDebugRecordings() override
;
77 void SetWebRtcLogMessageCallback(
78 base::Callback
<void(const std::string
&)> callback
) override
;
79 WebRtcStopRtpDumpCallback
StartRtpDump(
82 const WebRtcRtpPacketCallback
& packet_callback
) override
;
84 void ResumeDeferredNavigation(const GlobalRequestID
& request_id
) override
;
85 void NotifyTimezoneChange(const std::string
& zone_id
) override
;
86 ServiceRegistry
* GetServiceRegistry() override
;
87 const base::TimeTicks
& GetInitTimeForNavigationMetrics() const override
;
88 bool SubscribeUniformEnabled() const override
;
89 void OnAddSubscription(unsigned int target
) override
;
90 void OnRemoveSubscription(unsigned int target
) override
;
91 void SendUpdateValueState(
92 unsigned int target
, const gpu::ValueState
& state
) override
;
93 #if defined(ENABLE_BROWSER_CDMS)
94 media::BrowserCdm
* GetBrowserCdm(int render_frame_id
,
95 int cdm_id
) const override
;
98 // IPC::Sender via RenderProcessHost.
99 bool Send(IPC::Message
* msg
) override
;
101 // IPC::Listener via RenderProcessHost.
102 bool OnMessageReceived(const IPC::Message
& msg
) override
;
103 void OnChannelConnected(int32 peer_pid
) override
;
105 // Attaches the factory object so we can remove this object in its destructor
106 // and prevent MockRenderProcessHostFacotry from deleting it.
107 void SetFactory(const MockRenderProcessHostFactory
* factory
) {
111 void set_is_for_guests_only(bool is_for_guests_only
) {
112 is_for_guests_only_
= is_for_guests_only
;
115 void SetProcessHandle(scoped_ptr
<base::ProcessHandle
> new_handle
) {
116 process_handle
= new_handle
.Pass();
119 void GetAudioOutputControllers(
120 const GetAudioOutputControllersCallback
& callback
) const override
{}
123 // Stores IPC messages that would have been sent to the renderer.
126 const MockRenderProcessHostFactory
* factory_
;
128 bool has_connection_
;
129 BrowserContext
* browser_context_
;
130 base::ObserverList
<RenderProcessHostObserver
> observers_
;
132 IDMap
<RenderWidgetHost
> render_widget_hosts_
;
133 int prev_routing_id_
;
134 IDMap
<IPC::Listener
> listeners_
;
135 bool fast_shutdown_started_
;
136 bool deletion_callback_called_
;
137 bool is_for_guests_only_
;
138 scoped_ptr
<base::ProcessHandle
> process_handle
;
140 DISALLOW_COPY_AND_ASSIGN(MockRenderProcessHost
);
143 class MockRenderProcessHostFactory
: public RenderProcessHostFactory
{
145 MockRenderProcessHostFactory();
146 ~MockRenderProcessHostFactory() override
;
148 RenderProcessHost
* CreateRenderProcessHost(
149 BrowserContext
* browser_context
,
150 SiteInstance
* site_instance
) const override
;
152 // Removes the given MockRenderProcessHost from the MockRenderProcessHost list
153 // without deleting it. When a test deletes a MockRenderProcessHost, we need
154 // to remove it from |processes_| to prevent it from being deleted twice.
155 void Remove(MockRenderProcessHost
* host
) const;
158 // A list of MockRenderProcessHosts created by this object. This list is used
159 // for deleting all MockRenderProcessHosts that have not deleted by a test in
160 // the destructor and prevent them from being leaked.
161 mutable ScopedVector
<MockRenderProcessHost
> processes_
;
163 DISALLOW_COPY_AND_ASSIGN(MockRenderProcessHostFactory
);
166 } // namespace content
168 #endif // CONTENT_PUBLIC_TEST_MOCK_RENDER_PROCESS_HOST_H_