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. 4 5 #ifndef CONTENT_PUBLIC_TEST_MOCK_RENDER_PROCESS_HOST_H_ 6 #define CONTENT_PUBLIC_TEST_MOCK_RENDER_PROCESS_HOST_H_ 7 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" 14 15 class StoragePartition; 16 17 namespace content { 18 19 class MockRenderProcessHostFactory; 20 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 { 24 public: 25 explicit MockRenderProcessHost(BrowserContext* browser_context); 26 virtual ~MockRenderProcessHost(); 27 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_; } 31 32 // Provides test access to how many times a bad message has been received. 33 int bad_msg_count() const { return bad_msg_count_; } 34 35 // RenderProcessHost implementation (public portion). 36 virtual void EnableSendQueue() OVERRIDE; 37 virtual bool Init() OVERRIDE; 38 virtual int GetNextRoutingID() OVERRIDE; 39 virtual void AddRoute(int32 routing_id, IPC::Listener* listener) OVERRIDE; 40 virtual void RemoveRoute(int32 routing_id) OVERRIDE; 41 virtual void AddObserver(RenderProcessHostObserver* observer) OVERRIDE; 42 virtual void RemoveObserver(RenderProcessHostObserver* observer) OVERRIDE; 43 virtual bool WaitForBackingStoreMsg(int render_widget_id, 44 const base::TimeDelta& max_delay, 45 IPC::Message* msg) OVERRIDE; 46 virtual void ReceivedBadMessage() OVERRIDE; 47 virtual void WidgetRestored() OVERRIDE; 48 virtual void WidgetHidden() OVERRIDE; 49 virtual int VisibleWidgetCount() const OVERRIDE; 50 virtual bool IsIsolatedGuest() const OVERRIDE; 51 virtual StoragePartition* GetStoragePartition() const OVERRIDE; 52 virtual void AddWord(const base::string16& word); 53 virtual bool FastShutdownIfPossible() OVERRIDE; 54 virtual bool FastShutdownStarted() const OVERRIDE; 55 virtual void DumpHandles() OVERRIDE; 56 virtual base::ProcessHandle GetHandle() const OVERRIDE; 57 virtual int GetID() const OVERRIDE; 58 virtual bool HasConnection() const OVERRIDE; 59 virtual void SetIgnoreInputEvents(bool ignore_input_events) OVERRIDE; 60 virtual bool IgnoreInputEvents() const OVERRIDE; 61 virtual void Cleanup() OVERRIDE; 62 virtual void AddPendingView() OVERRIDE; 63 virtual void RemovePendingView() OVERRIDE; 64 virtual void SetSuddenTerminationAllowed(bool allowed) OVERRIDE; 65 virtual bool SuddenTerminationAllowed() const OVERRIDE; 66 virtual BrowserContext* GetBrowserContext() const OVERRIDE; 67 virtual bool InSameStoragePartition( 68 StoragePartition* partition) const OVERRIDE; 69 virtual IPC::ChannelProxy* GetChannel() OVERRIDE; 70 virtual void AddFilter(BrowserMessageFilter* filter) OVERRIDE; 71 virtual bool FastShutdownForPageCount(size_t count) OVERRIDE; 72 virtual base::TimeDelta GetChildProcessIdleTime() const OVERRIDE; 73 virtual void ResumeRequestsForView(int route_id) OVERRIDE; 74 virtual void FilterURL(bool empty_allowed, GURL* url) OVERRIDE; 75 #if defined(ENABLE_WEBRTC) 76 virtual void EnableAecDump(const base::FilePath& file) OVERRIDE; 77 virtual void DisableAecDump() OVERRIDE; 78 virtual void SetWebRtcLogMessageCallback( 79 base::Callback<void(const std::string&)> callback) OVERRIDE; 80 virtual WebRtcStopRtpDumpCallback StartRtpDump( 81 bool incoming, 82 bool outgoing, 83 const WebRtcRtpPacketCallback& packet_callback) OVERRIDE; 84 #endif 85 virtual void ResumeDeferredNavigation(const GlobalRequestID& request_id) 86 OVERRIDE; 87 virtual void NotifyTimezoneChange() OVERRIDE; 88 89 // IPC::Sender via RenderProcessHost. 90 virtual bool Send(IPC::Message* msg) OVERRIDE; 91 92 // IPC::Listener via RenderProcessHost. 93 virtual bool OnMessageReceived(const IPC::Message& msg) OVERRIDE; 94 virtual void OnChannelConnected(int32 peer_pid) OVERRIDE; 95 96 // Attaches the factory object so we can remove this object in its destructor 97 // and prevent MockRenderProcessHostFacotry from deleting it. 98 void SetFactory(const MockRenderProcessHostFactory* factory) { 99 factory_ = factory; 100 } 101 102 int GetActiveViewCount(); 103 104 void set_is_isolated_guest(bool is_isolated_guest) { 105 is_isolated_guest_ = is_isolated_guest; 106 } 107 108 private: 109 // Stores IPC messages that would have been sent to the renderer. 110 IPC::TestSink sink_; 111 int bad_msg_count_; 112 const MockRenderProcessHostFactory* factory_; 113 int id_; 114 BrowserContext* browser_context_; 115 ObserverList<RenderProcessHostObserver> observers_; 116 117 IDMap<RenderWidgetHost> render_widget_hosts_; 118 int prev_routing_id_; 119 IDMap<IPC::Listener> listeners_; 120 bool fast_shutdown_started_; 121 bool deletion_callback_called_; 122 bool is_isolated_guest_; 123 124 DISALLOW_COPY_AND_ASSIGN(MockRenderProcessHost); 125 }; 126 127 class MockRenderProcessHostFactory : public RenderProcessHostFactory { 128 public: 129 MockRenderProcessHostFactory(); 130 virtual ~MockRenderProcessHostFactory(); 131 132 virtual RenderProcessHost* CreateRenderProcessHost( 133 BrowserContext* browser_context, 134 SiteInstance* site_instance) const OVERRIDE; 135 136 // Removes the given MockRenderProcessHost from the MockRenderProcessHost list 137 // without deleting it. When a test deletes a MockRenderProcessHost, we need 138 // to remove it from |processes_| to prevent it from being deleted twice. 139 void Remove(MockRenderProcessHost* host) const; 140 141 private: 142 // A list of MockRenderProcessHosts created by this object. This list is used 143 // for deleting all MockRenderProcessHosts that have not deleted by a test in 144 // the destructor and prevent them from being leaked. 145 mutable ScopedVector<MockRenderProcessHost> processes_; 146 147 DISALLOW_COPY_AND_ASSIGN(MockRenderProcessHostFactory); 148 }; 149 150 } // namespace content 151 152 #endif // CONTENT_PUBLIC_TEST_MOCK_RENDER_PROCESS_HOST_H_ 153